SPH
Box.h
Go to the documentation of this file.
1 #pragma once
2 
7 
11 
13 
17 class Box {
18 private:
19  Vector minBound = Vector(LARGE);
20  Vector maxBound = Vector(-LARGE);
21 
22 public:
29  INLINE Box() = default;
30 
34  INLINE Box(const Vector& minBound, const Vector& maxBound)
35  : minBound(minBound)
36  , maxBound(maxBound) {
37  SPH_ASSERT(isValid());
38  }
39 
41  static Box EMPTY() {
42  return Box();
43  }
44 
49  INLINE void extend(const Vector& v) {
50  maxBound = max(maxBound, v);
51  minBound = min(minBound, v);
52  }
53 
58  INLINE void extend(const Box& other) {
59  maxBound = max(maxBound, other.maxBound);
60  minBound = min(minBound, other.minBound);
61  }
62 
66  INLINE bool contains(const Vector& v) const {
67  for (int i = 0; i < 3; ++i) {
68  if (v[i] < minBound[i] || v[i] > maxBound[i]) {
69  return false;
70  }
71  }
72  return true;
73  }
74 
76  INLINE Vector clamp(const Vector& v) const {
77  SPH_ASSERT(isValid());
78  return Sph::clamp(v, minBound, maxBound);
79  }
80 
82  INLINE const Vector& lower() const {
83  SPH_ASSERT(isValid());
84  return minBound;
85  }
86 
89  SPH_ASSERT(isValid());
90  return minBound;
91  }
92 
94  INLINE const Vector& upper() const {
95  SPH_ASSERT(isValid());
96  return maxBound;
97  }
98 
101  SPH_ASSERT(isValid());
102  return maxBound;
103  }
104 
106  INLINE Vector size() const {
107  SPH_ASSERT(isValid());
108  return maxBound - minBound;
109  }
110 
112  INLINE Vector center() const {
113  SPH_ASSERT(isValid());
114  return 0.5_f * (minBound + maxBound);
115  }
116 
118  INLINE Float volume() const {
119  const Vector s = size();
120  return s[X] * s[Y] * s[Z];
121  }
122 
124  INLINE bool operator==(const Box& other) const {
125  return minBound == other.minBound && maxBound == other.maxBound;
126  }
127 
129  INLINE bool operator!=(const Box& other) const {
130  return minBound != other.minBound || maxBound != other.maxBound;
131  }
132 
134  INLINE Box translate(const Vector& offset) const {
135  return Box(minBound + offset, maxBound + offset);
136  }
137 
144  INLINE Pair<Box> split(const Size dim, const Float x) const {
145  SPH_ASSERT(isValid());
146  SPH_ASSERT(dim < 3);
147  SPH_ASSERT(x >= minBound[dim] && x <= maxBound[dim]);
148  Box b1 = *this, b2 = *this;
149  b1.maxBound[dim] = x;
150  b2.minBound[dim] = x;
151  return { b1, b2 };
152  }
153 
155  INLINE Box intersect(const Box& other) const {
156  Box is;
157  is.minBound = max(minBound, other.minBound);
158  is.maxBound = min(maxBound, other.maxBound);
159  if (is.isValid()) {
160  return is;
161  } else {
162  return Box::EMPTY();
163  }
164  }
165 
167  template <typename TFunctor>
168  void iterate(const Vector& step, TFunctor&& functor) const {
169  SPH_ASSERT(isValid());
170  for (Float x = minBound[X]; x <= maxBound[X]; x += step[X]) {
171  for (Float y = minBound[Y]; y <= maxBound[Y]; y += step[Y]) {
172  for (Float z = minBound[Z]; z <= maxBound[Z]; z += step[Z]) {
173  functor(Vector(x, y, z));
174  }
175  }
176  }
177  }
178 
181  template <typename TFunctor>
182  void iterateWithIndices(const Vector& step, TFunctor&& functor) const {
183  SPH_ASSERT(isValid());
184  Size i = 0, j = 0, k = 0;
185  for (Float z = minBound[Z]; z <= maxBound[Z]; z += step[Z], k++) {
186  i = 0;
187  j = 0;
188  for (Float y = minBound[Y]; y <= maxBound[Y]; y += step[Y], j++) {
189  i = 0;
190  for (Float x = minBound[X]; x <= maxBound[X]; x += step[X], i++) {
191  functor(Indices(i, j, k), Vector(x, y, z));
192  }
193  }
194  }
195  }
196 
200  friend std::ostream& operator<<(std::ostream& stream, const Box& box) {
201  if (box == Box::EMPTY()) {
202  stream << "EMPTY";
203  } else {
204  stream << box.lower() << box.upper();
205  }
206  return stream;
207  }
208 
209 private:
210  bool isValid() const {
211  return minElement(maxBound - minBound) >= 0._f;
212  }
213 };
214 
215 
INLINE Float minElement(const AntisymmetricTensor &t)
Returns the minimal element of the tensor.
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
Vectorized computations with integral numbers.
constexpr INLINE T max(const T &f1, const T &f2)
Definition: MathBasic.h:20
constexpr INLINE T clamp(const T &f, const T &f1, const T &f2)
Definition: MathBasic.h:35
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Definition: MathBasic.h:15
constexpr Float LARGE
Definition: MathUtils.h:34
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Basic vector algebra. Computations are accelerated using SIMD.
BasicVector< Float > Vector
Definition: Vector.h:539
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
Helper object defining three-dimensional interval (box).
Definition: Box.h:17
INLINE Box translate(const Vector &offset) const
Returns a box with specified offset.
Definition: Box.h:134
friend std::ostream & operator<<(std::ostream &stream, const Box &box)
Prints the bounds of the box into the stream.
Definition: Box.h:200
INLINE void extend(const Vector &v)
Enlarges the box to contain the vector.
Definition: Box.h:49
INLINE const Vector & lower() const
Returns lower bounds of the box.
Definition: Box.h:82
INLINE Box()=default
Constructs empty box with negative dimensions.
INLINE const Vector & upper() const
Returns upper bounds of the box.
Definition: Box.h:94
INLINE Vector center() const
Returns the center of the box.
Definition: Box.h:112
INLINE bool contains(const Vector &v) const
Checks if the vector lies inside the box.
Definition: Box.h:66
INLINE Vector size() const
Returns box dimensions.
Definition: Box.h:106
INLINE Vector & lower()
Returns lower bounds of the box.
Definition: Box.h:88
void iterateWithIndices(const Vector &step, TFunctor &&functor) const
Execute functor for all possible values of vector (with constant stepping), passing auxiliary indices...
Definition: Box.h:182
INLINE Box intersect(const Box &other) const
Computes the intersection of this box with another one.
Definition: Box.h:155
INLINE Float volume() const
Returns the volume of the box.
Definition: Box.h:118
INLINE Box(const Vector &minBound, const Vector &maxBound)
Constructs a box given its 'corners'.
Definition: Box.h:34
INLINE Pair< Box > split(const Size dim, const Float x) const
Splits the box along given coordinate.
Definition: Box.h:144
INLINE Vector clamp(const Vector &v) const
Clamps all components of the vector to fit within the box.
Definition: Box.h:76
static Box EMPTY()
Syntactic sugar, returns a default-constructed (empty) box.
Definition: Box.h:41
INLINE Vector & upper()
Returns upper bounds of the box.
Definition: Box.h:100
void iterate(const Vector &step, TFunctor &&functor) const
Execute functor for all possible values of vector (with constant stepping)
Definition: Box.h:168
INLINE bool operator!=(const Box &other) const
Checks for inequality of boxes.
Definition: Box.h:129
INLINE void extend(const Box &other)
Enlarges the box to contain another box.
Definition: Box.h:58
INLINE bool operator==(const Box &other) const
Compares two boxes, return true if box lower and upper bounds are equal.
Definition: Box.h:124
Helper object for storing three (possibly four) int or bool values.
Definition: Indices.h:16
Array with fixed number of allocated elements.
Definition: StaticArray.h:19