AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
Vec2Template.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. 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
18
19#ifndef AGX_VEC2_TEMPLATE_H
20#define AGX_VEC2_TEMPLATE_H
21
22#include <iosfwd>
23
24#include <agx/agx.h>
25#include <agx/Math.h>
26
27namespace agx
28{
29 template <typename T>
30 class Vec2T
31 {
32 public:
33 typedef T Type;
34
35 public:
36
38 Vec2T(const Vec2T& copy ) = default;
39
41 template<typename T2>
42 AGX_FORCE_INLINE explicit Vec2T(const Vec2T<T2>& copy )
43 {
44 //memcpy(&m_data, copy.m_data, sizeof(agx::Real)*3);
45 m_data[ 0 ] = (T)copy[ 0 ];
46 m_data[ 1 ] = (T)copy[ 1 ];
47 }
48
53 m_data[0] = T();
54 m_data[1] = T();
55 }
56
57 explicit AGX_FORCE_INLINE Vec2T( T r ) {
58 m_data[0] = m_data[1] = r;
59 }
60
62 m_data[0] = x;
63 m_data[1] = y;
64 }
65
66 explicit AGX_FORCE_INLINE Vec2T(const T v[2] ) {
67 m_data[0] = v[0];
68 m_data[1] = v[1];
69 }
70
72 static AGX_FORCE_INLINE Vec2T random(const Vec2T& min, const Vec2T& max);
73
77 AGX_FORCE_INLINE bool operator == ( const Vec2T& v ) const {
78 return m_data[0] == v.m_data[0] && m_data[1] == v.m_data[1];
79 }
80
84 AGX_FORCE_INLINE bool operator != ( const Vec2T& v ) const {
85 return m_data[0] != v.m_data[0] || m_data[1] != v.m_data[1];
86 }
87
91 AGX_FORCE_INLINE static Vec2T componentMin(const Vec2T& v1, const Vec2T& v2) {
92 return Vec2T(std::min(v1[0], v2[0]), std::min(v1[1], v2[1]));
93 }
94
98 AGX_FORCE_INLINE static Vec2T componentMax(const Vec2T& v1, const Vec2T& v2) {
99 return Vec2T(std::max(v1[0], v2[0]), std::max(v1[1], v2[1]));
100 }
101
106 {
107 return std::min(m_data[0], m_data[1]);
108 }
109
114 {
115 return std::max(m_data[0], m_data[1]);
116 }
117
122 {
123 T m = std::numeric_limits<T>::infinity();
124 size_t idx = 0;
125 for(size_t i=0;i<2;i++)
126 {
127 T a = agx::absolute(m_data[i]);
128 if ( a < m) {
129 idx=i;
130 m = a;
131 }
132 }
133 return idx;
134 }
139 {
140 T m = 0;
141 size_t idx = 0;
142 for(size_t i=0;i<2;i++)
143 {
144 T a = agx::absolute(m_data[i]);
145 if ( a > m) {
146 idx=i;
147 m = a;
148 }
149 }
150 return idx;
151 }
152
157 {
158 m_data[0] = agx::clamp( m_data[0], min.m_data[0], max.m_data[0] );
159 m_data[1] = agx::clamp( m_data[1], min.m_data[1], max.m_data[1] );
160 }
161
162
167 {
168 return (agx::equalsZero(m_data[0]) && agx::equalsZero(m_data[1]));
169 }
170
171
176 return m_data;
177 }
178
182 AGX_FORCE_INLINE const T* ptr() const {
183 return m_data;
184 }
185
186 AGX_FORCE_INLINE void set( T x, T y ) {
187 m_data[0] = x;
188 m_data[1] = y;
189 }
190
191 AGX_FORCE_INLINE void set( T value) {
192 m_data[0] = m_data[1] = value;
193 }
194
195 AGX_FORCE_INLINE void set( const Vec2T& rhs ) {
196 m_data[0] = rhs.m_data[0];
197 m_data[1] = rhs.m_data[1];
198 }
199
200
202 return m_data[i];
203 }
204 AGX_FORCE_INLINE const T& operator [] ( size_t i ) const {
205 return m_data[i];
206 }
207
209 return m_data[0];
210 }
212 return m_data[1];
213 }
214
215 AGX_FORCE_INLINE T x() const {
216 return m_data[0];
217 }
218 AGX_FORCE_INLINE T y() const {
219 return m_data[1];
220 }
221
223 return !isNaN();
224 }
225
226 AGX_FORCE_INLINE bool isNaN() const {
227 return agx::isNaN( m_data[0] ) || agx::isNaN( m_data[1] );
228 }
229
231 return agx::isFinite( m_data[0] ) && agx::isFinite( m_data[1] );
232 }
233
234
236 AGX_FORCE_INLINE T operator * ( const Vec2T& rhs ) const {
237 return m_data[0]*rhs.m_data[0] + m_data[1]*rhs.m_data[1];
238 }
239
241 #if 0
242 AGX_FORCE_INLINE const Vec2T operator ^ ( const Vec2T& rhs ) const {
243 return Vec2T( m_data[1]*rhs.m_data[2] - m_data[2]*rhs.m_data[1],
244 m_data[2]*rhs.m_data[0] - m_data[0]*rhs.m_data[2] );
245 m_data[0]*rhs.m_data[1] - m_data[1]*rhs.m_data[0] );
246 }
247 #endif
248
250 AGX_FORCE_INLINE const Vec2T operator | ( const Vec2T& rhs ) const {
251 return Vec2T( m_data[0] * rhs.m_data[0],
252 m_data[1] * rhs.m_data[1] );
253 }
254
256 AGX_FORCE_INLINE const Vec2T operator * ( T rhs ) const {
257 return Vec2T( m_data[0]*rhs, m_data[1]*rhs );
258 }
259
260
263 m_data[0] = m_data[0]*rhs;
264 m_data[1] = m_data[1] *rhs;
265 return *this;
266 }
267
269 AGX_FORCE_INLINE const Vec2T operator / ( T rhs ) const {
270 return Vec2T( m_data[0] / rhs, m_data[1] / rhs );
271 }
272
275 m_data[0] = m_data[0] / rhs;
276 m_data[1] = m_data[1] / rhs;
277 return *this;
278 }
279
280
282 AGX_FORCE_INLINE static Vec2T mul( const Vec2T& lhs, const Vec2T& rhs )
283 {
284 return Vec2T( lhs[0] * rhs[0],
285 lhs[1] * rhs[1]);
286 }
287
288 AGX_FORCE_INLINE static Vec2T div( const Vec2T& lhs, const Vec2T& rhs )
289 {
290 return Vec2T( lhs[0] / rhs[0],
291 lhs[1] / rhs[1]);
292 }
293
295 AGX_FORCE_INLINE const Vec2T operator + ( const Vec2T& rhs ) const {
296 return Vec2T( m_data[0] + rhs.m_data[0], m_data[1] + rhs.m_data[1] );
297 }
298
303 m_data[0] += rhs.m_data[0];
304 m_data[1] += rhs.m_data[1];
305 return *this;
306 }
307
309 AGX_FORCE_INLINE const Vec2T operator - ( const Vec2T& rhs ) const {
310 return Vec2T( m_data[0] - rhs.m_data[0], m_data[1] - rhs.m_data[1] );
311 }
312
315 m_data[0] -= rhs.m_data[0];
316 m_data[1] -= rhs.m_data[1];
317 return *this;
318 }
319
320
322 AGX_FORCE_INLINE const Vec2T operator + ( const T& rhs ) const {
323 return Vec2T( m_data[0] + rhs, m_data[1] + rhs );
324 }
325
330 m_data[0] += rhs;
331 m_data[1] += rhs;
332 return *this;
333 }
334
336 AGX_FORCE_INLINE const Vec2T operator - ( const T& rhs ) const {
337 return Vec2T( m_data[0] - rhs, m_data[1] - rhs );
338 }
339
342 m_data[0] -= rhs;
343 m_data[1] -= rhs;
344 return *this;
345 }
346
347
350 return Vec2T ( -m_data[0], -m_data[1] );
351 }
352
355 return std::sqrt( m_data[0]*m_data[0] + m_data[1]*m_data[1] );
356 }
357
360 return Real(m_data[0]*m_data[0] + m_data[1]*m_data[1]);
361 }
362
365
368 return std::sqrt(distance2(v2));
369 }
370
371
372
377 Real norm = length();
378 if ( norm > 0.0 ) {
379 Real inv = Real(1.0) / norm;
380 m_data[0] = T(m_data[0]*inv);
381 m_data[1] = T(m_data[1]*inv);
382 }
383 return( norm );
384 }
385
386 private:
387 T m_data[2];
388 }; // end of class Vec2T_template
389
390 template <typename T>
392 {
394 }
395
396 template <typename T>
398 {
399 return Vec2T(agx::random(min[0], max[0]), agx::random(min[1], max[1]));
400 }
401
402
403 template<typename T>
405 {
406 return agx::equivalent(a[0], b[0], epsilon) &&
407 agx::equivalent(a[1], b[1], epsilon);
408 }
409
410 template<typename T>
412 {
413 return Vec2T<T>(
414 std::abs(a[0]),
415 std::abs(a[1]));
416 }
417
418 template<typename T>
419 AGX_FORCE_INLINE std::ostream& operator << ( std::ostream& output, const Vec2T<T>& v )
420 {
421 output << v[0] << " " << v[1];
422 return output;
423 }
424
425
426 template<typename T>
428 {
429 Vec2T diff(m_data[0]-v2.m_data[0], m_data[1]-v2.m_data[1]);
430 return diff.length2();
431 }
432
433
434 template<>
436 {
437 // Template specialization to avoid underflow in unsigned types.
438 Vec2T diff( (UInt8) (std::max( m_data[0], v2.m_data[0] ) - std::min( m_data[0], v2.m_data[0] )),
439 (UInt8) (std::max( m_data[1], v2.m_data[1] ) - std::min( m_data[1], v2.m_data[1] )) );
440 return diff.length2();
441 }
442
443
444 template<>
446 {
447 // Template specialization to avoid underflow in unsigned types.
448 Vec2T diff( (UInt16) (std::max( m_data[0], v2.m_data[0] ) - std::min( m_data[0], v2.m_data[0] )),
449 (UInt16) (std::max( m_data[1], v2.m_data[1] ) - std::min( m_data[1], v2.m_data[1] )) );
450 return diff.length2();
451 }
452
453 template<>
455 {
456 // Template specialization to avoid underflow in unsigned types.
457 Vec2T diff( std::max( m_data[0], v2.m_data[0] ) - std::min( m_data[0], v2.m_data[0] ),
458 std::max( m_data[1], v2.m_data[1] ) - std::min( m_data[1], v2.m_data[1] ) );
459 return diff.length2();
460 }
461
462
463 template<>
465 {
466 // Template specialization to avoid underflow in unsigned types.
467 Vec2T diff( std::max( m_data[0], v2.m_data[0] ) - std::min( m_data[0], v2.m_data[0] ),
468 std::max( m_data[1], v2.m_data[1] ) - std::min( m_data[1], v2.m_data[1] ) );
469 return diff.length2();
470 }
471
472
473 template <typename T>
474 AGX_FORCE_INLINE Vec2T<T> clamp( const Vec2T<T>& vec, const Vec2T<T>& minimum, const Vec2T<T>& maximum )
475 {
476 Vec2T<T> result = vec;
477 result.clamp(minimum, maximum);
478 return result;
479 }
480
481} // end of namespace agx
482
483
484#endif
size_t maxElement() const
Definition: Vec2Template.h:138
Real length() const
Length of the vector = sqrt( vec .
Definition: Vec2Template.h:354
bool operator!=(const Vec2T &v) const
In-equality test.
Definition: Vec2Template.h:84
Vec2T(const Vec2T &copy)=default
Copy constructor.
bool equalsZero() const
Definition: Vec2Template.h:166
static Vec2T random(T min, T max)
Definition: Vec2Template.h:391
T * ptr()
Return a pointer to the data vector.
Definition: Vec2Template.h:175
T maxComponent() const
Definition: Vec2Template.h:113
const Vec2T operator/(T rhs) const
Divide by scalar.
Definition: Vec2Template.h:269
Vec2T & operator-=(const Vec2T &rhs)
Unary vector subtract.
Definition: Vec2Template.h:314
void set(const Vec2T &rhs)
Definition: Vec2Template.h:195
size_t minElement() const
Definition: Vec2Template.h:121
static Vec2T random(const Vec2T &min, const Vec2T &max)
Definition: Vec2Template.h:397
T operator*(const Vec2T &rhs) const
Dot product.
Definition: Vec2Template.h:236
const Vec2T operator+(const Vec2T &rhs) const
Binary vector add.
Definition: Vec2Template.h:295
void set(T x, T y)
Definition: Vec2Template.h:186
Real distance(const Vec2T &v2) const
Distance to another vector.
Definition: Vec2Template.h:367
Vec2T & operator*=(T rhs)
Unary multiply by scalar.
Definition: Vec2Template.h:262
const Vec2T operator|(const Vec2T &rhs) const
Cross product.
Definition: Vec2Template.h:250
const T * ptr() const
Return a const pointer to the data vector.
Definition: Vec2Template.h:182
T y() const
Definition: Vec2Template.h:218
T x() const
Definition: Vec2Template.h:215
Vec2T(const Vec2T< T2 > &copy)
Copy constructor for other types.
Definition: Vec2Template.h:42
T & operator[](size_t i)
Definition: Vec2Template.h:201
void clamp(const Vec2T &min, const Vec2T &max)
Clamp a vector between a lower and upper bound (per component).
Definition: Vec2Template.h:156
Vec2T(T x, T y)
Definition: Vec2Template.h:61
const Vec2T operator-() const
Negation operator.
Definition: Vec2Template.h:349
static Vec2T mul(const Vec2T &lhs, const Vec2T &rhs)
Element-wise-multiplication.
Definition: Vec2Template.h:282
Vec2T & operator+=(const Vec2T &rhs)
Unary vector add.
Definition: Vec2Template.h:302
Vec2T(const T v[2])
Definition: Vec2Template.h:66
Real distance2(const Vec2T &v2) const
Squared distance to another vector.
Definition: Vec2Template.h:427
bool isValid() const
Definition: Vec2Template.h:222
static Vec2T componentMax(const Vec2T &v1, const Vec2T &v2)
Creates a new vector where each component is the maximum of this and the other vector.
Definition: Vec2Template.h:98
Real normalize()
Normalize the vector so that it has length unity.
Definition: Vec2Template.h:376
void set(T value)
Definition: Vec2Template.h:191
static Vec2T componentMin(const Vec2T &v1, const Vec2T &v2)
Creates a new vector where each component is the minimum of this and the other vector.
Definition: Vec2Template.h:91
static Vec2T div(const Vec2T &lhs, const Vec2T &rhs)
Definition: Vec2Template.h:288
bool isFinite() const
Definition: Vec2Template.h:230
bool isNaN() const
Definition: Vec2Template.h:226
bool operator==(const Vec2T &v) const
Equality test.
Definition: Vec2Template.h:77
Real length2() const
Length squared of the vector = vec .
Definition: Vec2Template.h:359
Vec2T()
Default constructor.
Definition: Vec2Template.h:52
Vec2T & operator/=(T rhs)
Unary divide by scalar.
Definition: Vec2Template.h:274
T minComponent() const
Definition: Vec2Template.h:105
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint16_t UInt16
Definition: Integer.h:31
T absolute(T v)
return the absolute value.
Definition: Math.h:291
bool isNaN(T v)
Definition: Math.h:303
T1 clamp(T1 v, T2 minimum, T3 maximum)
Definition: Math.h:318
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:861
AGXPHYSICS_EXPORT agx::Bool equalsZero(const agx::AddedMassInteraction::Matrix6x6 &matrix, agx::Real eps=agx::RealEpsilon)
std::ostream & operator<<(std::ostream &os, const agx::AddedMassInteraction::Matrix6x6 &m)
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:855
T random(T min, T max)
Definition: Math.h:555
double Real
Definition: Real.h:41
uint8_t UInt8
Definition: Integer.h:30
static constexpr Real AGX_EQUIVALENT_EPSILON
Definition: Math.h:57
bool isFinite(T v)
Definition: Math.h:309
AGXPHYSICS_EXPORT agx::Bool equivalent(const agx::AddedMassInteraction::Matrix6x6 &lhs, const agx::AddedMassInteraction::Matrix6x6 &rhs, agx::Real eps=agx::RealEpsilon)