template <typename T> T clip(const T& n, const T& lower, const T& upper) { return std::max(lower, std::min(n, upper)); } // Credit: https://stackoverflow.com/questions/9323903/most-efficient-elegant-way-to-clip-a-number