diff --git a/include/openspace/util/timerange.h b/include/openspace/util/timerange.h new file mode 100644 index 0000000000..059a0d8670 --- /dev/null +++ b/include/openspace/util/timerange.h @@ -0,0 +1,123 @@ +/***************************************************************************************** +* * +* OpenSpace * +* * +* Copyright (c) 2014-2016 * +* * +* 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 +#include // ephemerisTimeFromDate + +#ifndef __TIMERANGE_H__ +#define __TIMERANGE_H__ + +namespace { + const std::string KEY_START = "Start"; + const std::string KEY_END = "End"; +} + +namespace openspace { + +struct TimeRange { + + /** + * Default constructor initializes an empty time range. + */ + TimeRange() : start(DBL_MAX), end(-DBL_MAX) { }; + + /** + * Initializes a TimeRange with both start and end time. Initializing empty timeranges + * is OK. + */ + TimeRange(double startTime, double endTime) : start(startTime) , end(endTime) { }; + + /** + * Throws exception if unable to parse the provided \class ghoul::Dictionary + */ + TimeRange(const ghoul::Dictionary& dict) { + if (!initializeFromDictionary(dict, *this)) { + throw std::runtime_error("Unable to read TimeRange from dictionary"); + } + } + + /** + * \returns true if timeRange could be initialized from the dictionary, false otherwise. + */ + static bool initializeFromDictionary(const ghoul::Dictionary& dict, TimeRange& timeRange) { + std::string startTimeStr; + std::string endTimeStr; + + bool success = true; + success &= dict.getValue(KEY_START, startTimeStr); + success &= dict.getValue(KEY_END, endTimeStr); + if (success) { + // Parse to date. + // @TODO converting string to time stamp should not rely on Spice + timeRange.start = SpiceManager::ref().ephemerisTimeFromDate(startTimeStr); + timeRange.end = SpiceManager::ref().ephemerisTimeFromDate(endTimeStr); + return true; + } + else { + // Could not read TimeRange from Dict + return false; + } + } + + void include(double val){ + if (start > val) start = val; + if (end < val) end = val; + }; + + void include(const TimeRange& other) { + if (other.start < start) start = other.start; + if (other.end > end) end = other.end; + } + + double duration() const { + return end - start; + } + + bool isDefined() const { + return start <= end; + } + + bool isEmpty() const { + return !isDefined(); + } + + bool inRange(double min, double max){ + return (min >= start && max <= end); + } + + bool includes(double val) const { + return (start <= val && val <= end); + } + + bool includes(const TimeRange& o) const { + return start <= o.start && o.end <= end; + } + + double start; + double end; +}; + +} // namespace openspace + +#endif //__TIMERANGE_H__