AGX Dynamics  2.38.0.2
Bound.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_BOUND_H
18 #define AGX_BOUND_H
19 
20 #include <agx/Real.h>
21 #include <agx/Vec2.h>
22 #include <agx/Vec3.h>
23 #include <agx/Vec4.h>
24 #include <agx/Integer.h>
25 #include <agxData/Type.h>
26 
27 namespace agx
28 {
29 
35  template <typename T>
36  class BoundT
37  {
38  public:
39  typedef T Type;
40 
41  public:
42 
46  BoundT();
47 
48 
53  BoundT(const T& min, const T& max);
54 
55  /*
56  Default copy constructor
57  */
58  BoundT(const BoundT& other);
59 
63  template <typename T2>
64  operator BoundT<T2>() const;
65 
67  const T& min() const;
69  const T& max() const;
70 
72  T& min();
74  T& max();
75 
77  T size() const;
78 
80  T mid() const;
81 
83  void set(const T& min, const T& max);
84 
89  template <typename T2>
90  BoundT& operator*= (T2 scale);
91 
96  template <typename T2>
97  BoundT operator* (T2 scale);
98 
100  bool contains(const T& value) const;
101 
103  BoundT calculateOverlap(const BoundT& other) const;
104 
106  bool hasOverlap(const BoundT& other) const;
107 
109  BoundT translate(const T& offset) const;
110 
111  bool operator== (const BoundT& other) const;
112  bool operator!= (const BoundT& other) const;
113  BoundT& operator= (const BoundT& other);
114 
116  bool isValid() const;
117 
118  protected:
119  T m_min;
120  T m_max;
121 
122  };
123 
124 
134 
135 
145 
146 
150 
154 
158 
159 
160 
161  /* Implementation */
162 
163  #define AGX_BOUND_VEC2_OVERRIDE(VecType) \
164  template <> \
165  AGX_FORCE_INLINE bool BoundT<VecType>::contains(const VecType& value) const \
166  { \
167  return value[0] >= m_min[0] && value[1] >= m_min[1] && \
168  value[0] <= m_max[0] && value[1] <= m_max[1]; \
169  } \
170  template <> \
171  AGX_FORCE_INLINE bool BoundT<VecType>::hasOverlap(const BoundT<VecType>& other) const \
172  { \
173  const bool overlaps = \
174  (m_max[0] >= other.min()[0]) && (other.max()[0] >= m_min[0]) && \
175  (m_max[1] >= other.min()[1]) && (other.max()[1] >= m_min[1]); \
176  return overlaps; \
177  } \
178  template <> \
179  AGX_FORCE_INLINE bool BoundT<VecType>::isValid() const \
180  { \
181  return (m_min[0] <= m_max[0] && m_min[1] <= m_max[1]); \
182  } \
183  template <> \
184  AGX_FORCE_INLINE BoundT<VecType> BoundT<VecType>::calculateOverlap(const BoundT<VecType>& other) const \
185  { \
186  return BoundT<VecType>( \
187  VecType(std::max(m_min[0], other.min()[0]), std::max(m_min[1], other.min()[1])), \
188  VecType(std::min(m_max[0], other.max()[0]), std::min(m_max[1], other.max()[1]))); \
189  }
190 
191  #define AGX_BOUND_VEC3_OVERRIDE(VecType) \
192  template <> \
193  AGX_FORCE_INLINE bool BoundT<VecType>::contains(const VecType& value) const \
194  { \
195  return value[0] >= m_min[0] && value[1] >= m_min[1] && value[2] >= m_min[2] && \
196  value[0] <= m_max[0] && value[1] <= m_max[1] && value[2] <= m_max[2]; \
197  } \
198  template <> \
199  AGX_FORCE_INLINE bool BoundT<VecType>::hasOverlap(const BoundT<VecType>& other) const \
200  { \
201  const bool overlaps = \
202  (m_max[0] >= other.min()[0]) && (other.max()[0] >= m_min[0]) && \
203  (m_max[1] >= other.min()[1]) && (other.max()[1] >= m_min[1]) && \
204  (m_max[2] >= other.min()[2]) && (other.max()[2] >= m_min[2]); \
205  return overlaps; \
206  } \
207  template <> \
208  AGX_FORCE_INLINE bool BoundT<VecType>::isValid() const \
209  { \
210  return (m_min[0] <= m_max[0] && m_min[1] <= m_max[1] && m_min[2] <= m_max[2]); \
211  } \
212  template <> \
213  AGX_FORCE_INLINE BoundT<VecType> BoundT<VecType>::calculateOverlap(const BoundT<VecType>& other) const \
214  { \
215  return BoundT<VecType>( \
216  VecType(std::max(m_min[0], other.m_min[0]), std::max(m_min[1], other.m_min[1]), std::max(m_min[2], other.m_min[2])), \
217  VecType(std::min(m_max[0], other.m_max[0]), std::max(m_min[1], other.m_min[1]), std::max(m_min[2], other.m_min[2]))); \
218  }
219 
222 
225 
228 
231 
234 
237 
238 
239  template <typename T> template <typename T2>
240  AGX_FORCE_INLINE BoundT<T>::operator BoundT<T2>() const { return BoundT<T2>(static_cast<T2>(m_min), static_cast<T2>(m_max)); }
241 
242  template <typename T>
244 
245  template <typename T>
246  AGX_FORCE_INLINE BoundT<T>::BoundT(const T& min, const T& max) : m_min(min), m_max(max) {}
247 
248  template <typename T>
249  AGX_FORCE_INLINE BoundT<T>::BoundT(const BoundT& other) : m_min(other.m_min), m_max(other.m_max) {}
250 
251  template <typename T>
252  AGX_FORCE_INLINE const T& BoundT<T>::min() const { return m_min; }
253 
254  template <typename T>
255  AGX_FORCE_INLINE const T& BoundT<T>::max() const { return m_max; }
256 
257  template <typename T>
258  AGX_FORCE_INLINE T& BoundT<T>::min() { return m_min; }
259 
260  template <typename T>
261  AGX_FORCE_INLINE T& BoundT<T>::max() { return m_max; }
262 
263  template <typename T>
264  AGX_FORCE_INLINE void BoundT<T>::set(const T& min, const T& max) { m_min = min; m_max = max; }
265 
266  template <typename T>
267  AGX_FORCE_INLINE T BoundT<T>::size() const { return m_max - m_min; }
268 
269  template <typename T>
270  AGX_FORCE_INLINE T BoundT<T>::mid() const { return (m_min + m_max) * 0.5; }
271 
272  template <typename T>
273  AGX_FORCE_INLINE bool BoundT<T>::isValid() const { return m_min <= m_max; }
274 
275 
276  template <typename T> template <typename T2>
278  {
279  T mid = this->mid();
280  T halfSize = this->size() * scale * 0.5;
281  m_min = mid - halfSize;
282  m_max = mid + halfSize;
283  return *this;
284  }
285 
286  template <typename T> template <typename T2>
288  {
289  BoundT bound(m_min, m_max);
290  bound *= scale;
291  return bound;
292  }
293 
294  template <typename T>
295  AGX_FORCE_INLINE bool BoundT<T>::contains(const T& value) const { return value >= m_min && value <= m_max; }
296 
297  template <typename T>
299  {
300  return BoundT(std::max(m_min, other.m_min), std::min(m_max, other.m_max));
301  }
302 
303  template <typename T>
305  {
306  agxAbort1("Type specific implementation missing");
307  return false;
308  }
309 
310  template <typename T>
312  {
313  return BoundT(m_min + offset, m_max + offset);
314  }
315 
316  template <typename T>
318  {
319  return m_min == other.m_min && m_max == other.m_max;
320  }
321 
322 
323  template <typename T>
325  {
326  m_min = other.m_min;
327  m_max = other.m_max;
328  return *this;
329  }
330 
331 
332  template <typename T>
334  {
335  return m_min != other.m_min || m_max != other.m_max;
336  }
337 
338  template<typename T>
339  AGX_FORCE_INLINE std::ostream& operator << ( std::ostream& output, const BoundT<T>& bound)
340  {
341  output << bound.min() << " : " << bound.max();
342  return output;
343  }
344 
345 }
346 
353 
360 
367 
368 
369 #endif /* _AGX_BOUND_H_ */
AGX_BOUND_VEC3_OVERRIDE
#define AGX_BOUND_VEC3_OVERRIDE(VecType)
Definition: Bound.h:191
agx::Bound2u64
BoundT< Vec2u64 > Bound2u64
Definition: Bound.h:144
agx::Bound2i32
BoundT< Vec2i32 > Bound2i32
Definition: Bound.h:140
agx::Vec2u64
Vec2T< UInt64 > Vec2u64
Definition: Vec2.h:51
agx::Vec2u32
Vec2T< UInt32 > Vec2u32
Definition: Vec2.h:50
agx::BoundT::calculateOverlap
BoundT calculateOverlap(const BoundT &other) const
Definition: Bound.h:298
agx::Bound2
BoundT< Vec2 > Bound2
Definition: Bound.h:136
agx::BoundT::mid
T mid() const
Definition: Bound.h:270
agx::Bound2i
BoundT< Vec2i > Bound2i
Definition: Bound.h:139
agx::Vec3d
Vec3T< Real64 > Vec3d
Definition: Vec3.h:40
agx::Bound1
BoundT< Real > Bound1
Definition: Bound.h:125
agx::BoundT::operator*
BoundT operator*(T2 scale)
Create a new bound that has the same midpoint as the current bound, but with a size that is scaled ac...
agx::BoundT::operator*=
BoundT & operator*=(T2 scale)
Scale the bound so that is size is changed according to the given scale, but the midpoint remains unc...
agx::Bound3f
BoundT< Vec3f > Bound3f
Definition: Bound.h:148
agx::operator*
Vec3T< T > operator*(const Vec3T< T > &v, const AffineMatrix4x4T< T > &m)
Definition: AffineMatrix4x4.h:870
agx::Vec2i64
Vec2T< Int64 > Vec2i64
Definition: Vec2.h:44
agx::Bound1u32
BoundT< UInt32 > Bound1u32
Definition: Bound.h:132
agx::Vec2f
Vec2T< Real32 > Vec2f
Definition: Vec2.h:36
AGX_BOUND_VEC2_OVERRIDE
#define AGX_BOUND_VEC2_OVERRIDE(VecType)
Definition: Bound.h:163
agx::Bound3d
BoundT< Vec3d > Bound3d
Definition: Bound.h:149
agx::BoundT::Type
T Type
Definition: Bound.h:39
agx::Bound3u64
BoundT< Vec3u64 > Bound3u64
Definition: Bound.h:157
agx::BoundT::m_max
T m_max
Definition: Bound.h:120
agx::Bound1i64
BoundT< Int64 > Bound1i64
Definition: Bound.h:130
agx::BoundT::size
T size() const
Definition: Bound.h:267
agx::Bound3
BoundT< Vec3 > Bound3
Definition: Bound.h:147
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agx::BoundT
A BoundT represents a range defined by a minimum and a maximum value.
Definition: Bound.h:36
Vec4.h
agx::Bound2u32
BoundT< Vec2u32 > Bound2u32
Definition: Bound.h:143
Vec2.h
agx::Bound1i32
BoundT< Int32 > Bound1i32
Definition: Bound.h:129
Vec3.h
Type.h
agx::Bound1d
BoundT< Real64 > Bound1d
Definition: Bound.h:127
agx::Bound2i64
BoundT< Vec2i64 > Bound2i64
Definition: Bound.h:141
agx::Bound3i32
BoundT< Vec3i32 > Bound3i32
Definition: Bound.h:152
agx::Bound2f
BoundT< Vec2f > Bound2f
Definition: Bound.h:137
agx::Bound1u64
BoundT< UInt64 > Bound1u64
Definition: Bound.h:133
Integer.h
agx::BoundT::operator!=
bool operator!=(const BoundT &other) const
Definition: Bound.h:333
agx::min
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:861
agx::Bound2d
BoundT< Vec2d > Bound2d
Definition: Bound.h:138
agx::BoundT::m_min
T m_min
Definition: Bound.h:119
agx::Bound1f
BoundT< Real32 > Bound1f
Definition: Bound.h:126
agx::BoundT::max
const T & max() const
Definition: Bound.h:255
agx::Bound3u32
BoundT< Vec3u32 > Bound3u32
Definition: Bound.h:156
agx::Vec3i64
Vec3T< Int64 > Vec3i64
Definition: Vec3.h:52
agx::BoundT::operator=
BoundT & operator=(const BoundT &other)
Definition: Bound.h:324
agx::BoundT::set
void set(const T &min, const T &max)
Relocate the bound to the new location.
Definition: Bound.h:264
agx::Bound2u
BoundT< Vec2u > Bound2u
Definition: Bound.h:142
agx::BoundT::min
const T & min() const
Definition: Bound.h:252
agx::operator!=
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:207
agx::BoundT::isValid
bool isValid() const
Definition: Bound.h:273
agx::Vec3u32
Vec3T< UInt32 > Vec3u32
Definition: Vec3.h:51
agx::Bound3i64
BoundT< Vec3i64 > Bound3i64
Definition: Bound.h:153
agx::BoundT::hasOverlap
bool hasOverlap(const BoundT &other) const
Definition: Bound.h:304
agx::BoundT::contains
bool contains(const T &value) const
Definition: Bound.h:295
agx::BoundT::translate
BoundT translate(const T &offset) const
Move the bound's midpoint without altering its size.
Definition: Bound.h:311
agx::Bound1i
BoundT< Int > Bound1i
Definition: Bound.h:128
agx::Vec2d
Vec2T< Real64 > Vec2d
Definition: Vec2.h:37
agx::Bound3u
BoundT< Vec3u > Bound3u
Definition: Bound.h:155
agx::operator==
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:201
AGX_TYPE_BINDING
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
agx::Vec3u64
Vec3T< UInt64 > Vec3u64
Definition: Vec3.h:53
agx::Vec2i32
Vec2T< Int32 > Vec2i32
Definition: Vec2.h:43
operator<<
std::ostream & operator<<(std::ostream &o, const agx::Vec6 &v)
Definition: Vec6.h:230
agxAbort1
#define agxAbort1(msg)
Definition: debug.h:165
agx::BoundT::BoundT
BoundT()
Create a bound initialized to the empty range located at the type's default value.
Definition: Bound.h:243
agx::Vec3f
Vec3T< Real32 > Vec3f
Definition: Vec3.h:39
Real.h
agx::max
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:855
agx::Bound1u
BoundT< UInt > Bound1u
Definition: Bound.h:131
agx::Vec3i32
Vec3T< Int32 > Vec3i32
Definition: Vec3.h:50
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agx::Bound3i
BoundT< Vec3i > Bound3i
Definition: Bound.h:151
agx::BoundT::operator==
bool operator==(const BoundT &other) const
Definition: Bound.h:317