SPH
Interval.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Globals.h"
9 #include "math/MathUtils.h"
11 
13 
17 class Interval {
18 private:
19  Float minBound;
20  Float maxBound;
21 
22 public:
28  : minBound(INFTY)
29  , maxBound(-INFTY) {}
30 
35  : minBound(lower)
36  , maxBound(upper) {}
37 
41  INLINE void extend(const Float& value) {
42  minBound = min(minBound, value);
43  maxBound = max(maxBound, value);
44  }
45 
49  INLINE void extend(const Interval& other) {
50  minBound = min(minBound, other.minBound);
51  maxBound = max(maxBound, other.maxBound);
52  }
53 
55  INLINE bool contains(const Float& value) const {
56  return minBound <= value && value <= maxBound;
57  }
58 
60  INLINE Interval intersect(const Interval& other) const {
61  Interval is;
62  is.minBound = max(minBound, other.minBound);
63  is.maxBound = min(maxBound, other.maxBound);
64  return is;
65  }
66 
68  INLINE Float clamp(const Float& value) const {
69  SPH_ASSERT(minBound <= maxBound);
70  return max(minBound, min(value, maxBound));
71  }
72 
74  INLINE Float lower() const {
75  return minBound;
76  }
77 
79  INLINE Float upper() const {
80  return maxBound;
81  }
82 
84  INLINE Float center() const {
85  return 0.5_f * (minBound + maxBound);
86  }
87 
89  INLINE Float size() const {
90  return maxBound - minBound;
91  }
92 
94  INLINE bool operator==(const Interval& other) const {
95  return minBound == other.minBound && maxBound == other.maxBound;
96  }
97 
99  INLINE bool operator!=(const Interval& other) const {
100  return !(*this == other);
101  }
102 
104  INLINE bool empty() const {
105  return minBound > maxBound;
106  }
107 
109  static Interval unbounded() {
110  return Interval(-INFTY, INFTY);
111  }
112 
113  friend std::ostream& operator<<(std::ostream& stream, const Interval& range);
114 };
115 
116 
119 template <typename T>
120 INLINE T clamp(const T& v, const Interval& range) {
121  return T(range.clamp(v));
122 }
123 
128 template <typename T>
129 INLINE Pair<T> clampWithDerivative(const T&, const T&, const Interval&) {
131 }
132 
133 template <>
134 INLINE Pair<Float> clampWithDerivative<Float>(const Float& v, const Float& dv, const Interval& range) {
135  const bool zeroDeriv = (v >= range.upper() && dv > 0._f) || (v <= range.lower() && dv < 0._f);
136  return { clamp(v, range), zeroDeriv ? 0._f : dv };
137 }
138 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Global parameters of the code.
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
INLINE T clamp(const T &v, const Interval &range)
Definition: Interval.h:120
INLINE Pair< Float > clampWithDerivative< Float >(const Float &v, const Float &dv, const Interval &range)
Definition: Interval.h:134
INLINE Pair< T > clampWithDerivative(const T &, const T &, const Interval &)
Returns clamped values and the value of derivative.
Definition: Interval.h:129
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
Additional math routines (with more includes).
constexpr Float INFTY
Definition: MathUtils.h:38
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
INLINE void extend(const Interval &other)
Extends the interval to contain another interval.
Definition: Interval.h:49
INLINE Float lower() const
Returns lower bound of the interval.
Definition: Interval.h:74
INLINE Interval(const Float &lower, const Float &upper)
Constructs the interval given its lower and upper bound.
Definition: Interval.h:34
INLINE bool operator==(const Interval &other) const
Comparison operator; true if and only if both bounds are equal.
Definition: Interval.h:94
INLINE Interval()
Default construction of an empty interval.
Definition: Interval.h:27
INLINE void extend(const Float &value)
Extends the interval to contain given value.
Definition: Interval.h:41
INLINE Float upper() const
Returns upper bound of the interval.
Definition: Interval.h:79
friend std::ostream & operator<<(std::ostream &stream, const Interval &range)
Definition: Interval.cpp:23
INLINE bool operator!=(const Interval &other) const
Negation of comparison operator.
Definition: Interval.h:99
static Interval unbounded()
Returns an unbounded (infinite) interval.
Definition: Interval.h:109
INLINE Float center() const
Returns the center of the interval.
Definition: Interval.h:84
INLINE Float size() const
Returns the size of the interval.
Definition: Interval.h:89
INLINE bool contains(const Float &value) const
Checks whether value is inside the interval.
Definition: Interval.h:55
INLINE bool empty() const
Returns true if the interval is empty (default constructed).
Definition: Interval.h:104
INLINE Float clamp(const Float &value) const
Clamps the given value by the interval.
Definition: Interval.h:68
INLINE Interval intersect(const Interval &other) const
Computes the intersection with another interval.
Definition: Interval.h:60
Array with fixed number of allocated elements.
Definition: StaticArray.h:19