#include "range.hpp" #include "debug.hpp" #include "maths.hpp" #include #include using namespace std; using namespace util; /* * Range */ template range::range (const json::node &node) { if (node.is_string () && (node.to_string () == "UNIT" || node.to_string () == "unit")) { min = UNIT.min; max = UNIT.max; } else if (node.is_string () && (node.to_string () == "UNLIMITED" || node.to_string () == "unlimited")) { min = UNLIMITED.min; max = UNLIMITED.max; } else { min = node.to_array ()[0].to_number (); max = node.to_array ()[0].to_number (); } sanity (); } template range::range (T _min, T _max): min (_min), max (_max) { sanity (); } template bool range::contains (T val) const { return val >= min && val <= max; } template bool range::contains (const range &r) const { return r.min >= min && r.max <= max; } template void range::sanity (void) const { check (min <= max); } template T range::clamp (T val) const { return std::max (min, std::min (val, max)); } template double range::normalise (T val) const { return ((double)val - min) / ((double)max - min); } template T range::rand (void) const { double pos = ::rand () / (double)(RAND_MAX); return (max - min) * pos; } namespace util { template <> bool range::operator ==(const range &rhs) const { return almost_equal (min, rhs.min) && almost_equal (max, rhs.max); } template <> bool range::operator ==(const range &rhs) const { return almost_equal (min, rhs.min) && almost_equal (max, rhs.max); } } template bool range::operator ==(const range &rhs) const { return min == rhs.min && max == rhs.max; } template const range range::UNLIMITED (numeric_limits ::is_integer ? numeric_limits ::min () : -numeric_limits ::infinity (), numeric_limits ::is_integer ? numeric_limits ::max () : numeric_limits ::infinity ()); template const range range::UNIT (0.0, 1.0); template struct range; template struct range; template struct range; template struct range; template struct range; template struct range;