AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Bound.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
27namespace agx
28{
29
35 template <typename T>
36 class BoundT
37 {
38 public:
39 typedef T Type;
40
41 public:
42
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:
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
348AGX_TYPE_BINDING(agx::Bound1d, "Bound1")
349AGX_TYPE_BINDING(agx::Bound2f, "Bound2")
350AGX_TYPE_BINDING(agx::Bound2d, "Bound2")
351AGX_TYPE_BINDING(agx::Bound3f, "Bound3")
352AGX_TYPE_BINDING(agx::Bound3d, "Bound3")
353
354AGX_TYPE_BINDING(agx::Bound1i32, "Bound1i")
355AGX_TYPE_BINDING(agx::Bound1i64, "Bound1i")
356AGX_TYPE_BINDING(agx::Bound2i32, "Bound2i")
357AGX_TYPE_BINDING(agx::Bound2i64, "Bound2i")
358AGX_TYPE_BINDING(agx::Bound3i32, "Bound3i")
359AGX_TYPE_BINDING(agx::Bound3i64, "Bound3i")
360
361AGX_TYPE_BINDING(agx::Bound1u32, "Bound1u")
362AGX_TYPE_BINDING(agx::Bound1u64, "Bound1u")
363AGX_TYPE_BINDING(agx::Bound2u32, "Bound2u")
364AGX_TYPE_BINDING(agx::Bound2u64, "Bound2u")
365AGX_TYPE_BINDING(agx::Bound3u32, "Bound3u")
366AGX_TYPE_BINDING(agx::Bound3u64, "Bound3u")
367
368
369#endif /* _AGX_BOUND_H_ */
#define AGX_BOUND_VEC3_OVERRIDE(VecType)
Definition Bound.h:191
#define AGX_BOUND_VEC2_OVERRIDE(VecType)
Definition Bound.h:163
#define AGX_TYPE_BINDING(_Type, _Name)
Definition Type.h:179
A BoundT represents a range defined by a minimum and a maximum value.
Definition Bound.h:37
T & max()
Definition Bound.h:261
BoundT calculateOverlap(const BoundT &other) const
Definition Bound.h:298
T size() const
Definition Bound.h:267
bool contains(const T &value) const
Definition Bound.h:295
T & min()
Definition Bound.h:258
BoundT & operator=(const BoundT &other)
Definition Bound.h:324
bool hasOverlap(const BoundT &other) const
Definition Bound.h:304
BoundT()
Create a bound initialized to the empty range located at the type's default value.
Definition Bound.h:243
bool isValid() const
Definition Bound.h:273
T mid() const
Definition Bound.h:270
BoundT & operator*=(T2 scale)
Scale the bound so that is size is changed according to the given scale, but the midpoint remains unc...
bool operator==(const BoundT &other) const
Definition Bound.h:317
BoundT translate(const T &offset) const
Move the bound's midpoint without altering its size.
Definition Bound.h:311
BoundT(const BoundT &other)
Definition Bound.h:249
void set(const T &min, const T &max)
Relocate the bound to the new location.
Definition Bound.h:264
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...
const T & min() const
Definition Bound.h:252
BoundT(const T &min, const T &max)
Definition Bound.h:246
const T & max() const
Definition Bound.h:255
bool operator!=(const BoundT &other) const
Definition Bound.h:333
#define agxAbort1(msg)
Definition debug.h:165
#define AGX_FORCE_INLINE
Definition macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
BoundT< Vec2 > Bound2
Definition Bound.h:136
Vec3T< UInt32 > Vec3u32
Definition agx/Vec3.h:51
BoundT< Vec2d > Bound2d
Definition Bound.h:138
BoundT< Vec3i64 > Bound3i64
Definition Bound.h:153
Vec2T< Real32 > Vec2f
Definition Vec2.h:36
BoundT< Vec2f > Bound2f
Definition Bound.h:137
BoundT< Vec2i32 > Bound2i32
Definition Bound.h:140
BoundT< Real > Bound1
Definition Bound.h:125
BoundT< Vec3f > Bound3f
Definition Bound.h:148
Vec3T< Real64 > Vec3d
Definition agx/Vec3.h:40
Vec3T< UInt64 > Vec3u64
Definition agx/Vec3.h:53
Vec2T< Real64 > Vec2d
Definition Vec2.h:37
Vec3T< Int32 > Vec3i32
Definition agx/Vec3.h:50
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
BoundT< Vec2u64 > Bound2u64
Definition Bound.h:144
BoundT< UInt > Bound1u
Definition Bound.h:131
BoundT< Int32 > Bound1i32
Definition Bound.h:129
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)
BoundT< Vec3u64 > Bound3u64
Definition Bound.h:157
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
BoundT< Real32 > Bound1f
Definition Bound.h:126
BoundT< Vec2i64 > Bound2i64
Definition Bound.h:141
BoundT< Vec2u > Bound2u
Definition Bound.h:142
BoundT< Vec3i32 > Bound3i32
Definition Bound.h:152
BoundT< Vec2u32 > Bound2u32
Definition Bound.h:143
BoundT< Vec2i > Bound2i
Definition Bound.h:139
Vec3T< Int64 > Vec3i64
Definition agx/Vec3.h:52
BoundT< Int64 > Bound1i64
Definition Bound.h:130
BoundT< Int > Bound1i
Definition Bound.h:128
BoundT< UInt32 > Bound1u32
Definition Bound.h:132
Vec2T< Int64 > Vec2i64
Definition Vec2.h:44
BoundT< Vec3u > Bound3u
Definition Bound.h:155
BoundT< Vec3i > Bound3i
Definition Bound.h:151
BoundT< Vec3d > Bound3d
Definition Bound.h:149
Vec2T< UInt64 > Vec2u64
Definition Vec2.h:51
Vec2T< UInt32 > Vec2u32
Definition Vec2.h:50
Vec3T< Real32 > Vec3f
Definition agx/Vec3.h:39
BoundT< Real64 > Bound1d
Definition Bound.h:127
Vec2T< Int32 > Vec2i32
Definition Vec2.h:43
BoundT< Vec3 > Bound3
Definition Bound.h:147
BoundT< Vec3u32 > Bound3u32
Definition Bound.h:156
BoundT< UInt64 > Bound1u64
Definition Bound.h:133