Files
OpenSpace/modules/base/timeframe/timeframeinterval.cpp
Alexander Bock 6d821d4f91 Feature/codegen (#1480)
* Add the ability to automatically generate code to extract values out of a Dictionary (see https://github.com/openspace/codegen for more information on how to use this)
* Applied this technique to a large number of cases in the codebase
* Don't add _codegen files to the repository

Co-authored-by: Emma Broman <emma.broman@liu.se>
2021-02-09 09:12:43 +01:00

136 lines
5.2 KiB
C++

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2021 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/base/timeframe/timeframeinterval.h>
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
#include <openspace/util/spicemanager.h>
#include <openspace/util/time.h>
#include <optional>
namespace {
constexpr const openspace::properties::Property::PropertyInfo HasStartInfo = {
"HasStart",
"Has Start",
"If enabled, this TimeFrame will be inactive before the Start"
};
constexpr const openspace::properties::Property::PropertyInfo StartInfo = {
"Start",
"Start",
"Specifies the time when this TimeFrame becomes active"
};
constexpr const openspace::properties::Property::PropertyInfo HasEndInfo = {
"HasEnd",
"Has End",
"If enabled, this TimeFrame will be inactive after the End"
};
constexpr const openspace::properties::Property::PropertyInfo EndInfo = {
"End",
"End",
"Specifies the time when this TimeFrame becomes inactive"
};
struct [[codegen::Dictionary(TimeFrameInterval)]] Parameters {
// [[codegen::verbatim(StartInfo.description)]]
std::optional<std::variant<double, std::string>> start;
// [[codegen::verbatim(EndInfo.description)]]
std::optional<std::variant<double, std::string>> end;
};
#include "timeframeinterval_codegen.cpp"
} // namespace
namespace openspace {
documentation::Documentation TimeFrameInterval::Documentation() {
documentation::Documentation doc = codegen::doc<Parameters>();
doc.id = "base_time_frame_interval";
return doc;
}
bool TimeFrameInterval::isActive(const Time& time) const {
if (_hasStart && time.j2000Seconds() < _start ) {
return false;
}
if (_hasEnd && time.j2000Seconds() >= _end ) {
return false;
}
return true;
}
TimeFrameInterval::TimeFrameInterval()
: _hasStart(HasStartInfo, false)
, _start(StartInfo, 0, 0, 1E9)
, _hasEnd(HasEndInfo, false)
, _end(EndInfo, 0, 0, 1E9)
{
addProperty(_hasStart);
addProperty(_start);
addProperty(_hasEnd);
addProperty(_end);
}
TimeFrameInterval::TimeFrameInterval(const ghoul::Dictionary& dictionary)
: _hasStart(HasStartInfo, false)
, _start(StartInfo, 0, 0, 1E9)
, _hasEnd(HasEndInfo, false)
, _end(EndInfo, 0, 0, 1E9)
{
const Parameters p = codegen::bake<Parameters>(dictionary);
if (p.start.has_value()) {
if (std::holds_alternative<double>(*p.start)) {
_start = std::get<double>(*p.start);
}
else {
_start = SpiceManager::ref().ephemerisTimeFromDate(
std::get<std::string>(*p.start)
);
}
}
_hasStart = p.start.has_value();
addProperty(_hasStart);
addProperty(_start);
if (p.end.has_value()) {
if (std::holds_alternative<double>(*p.end)) {
_end = std::get<double>(*p.end);
}
else {
_end = SpiceManager::ref().ephemerisTimeFromDate(
std::get<std::string>(*p.end)
);
}
}
_hasEnd = p.end.has_value();
addProperty(_hasEnd);
addProperty(_end);
}
} // namespace openspace