PxVec2.h
Go to the documentation of this file.
1 //
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions
4 // are met:
5 // * Redistributions of source code must retain the above copyright
6 // notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright
8 // notice, this list of conditions and the following disclaimer in the
9 // documentation and/or other materials provided with the distribution.
10 // * Neither the name of NVIDIA CORPORATION nor the names of its
11 // contributors may be used to endorse or promote products derived
12 // from this software without specific prior written permission.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
28 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
29 
30 #ifndef PXFOUNDATION_PXVEC2_H
31 #define PXFOUNDATION_PXVEC2_H
32 
37 #include "foundation/PxMath.h"
38 
39 #if !PX_DOXYGEN
40 namespace physx
41 {
42 #endif
43 
49 class PxVec2
50 {
51  public:
56  {
57  }
58 
63  {
64  PX_UNUSED(r);
65  }
66 
74  explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(float a) : x(a), y(a)
75  {
76  }
77 
84  PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(float nx, float ny) : x(nx), y(ny)
85  {
86  }
87 
91  PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(const PxVec2& v) : x(v.x), y(v.y)
92  {
93  }
94 
95  // Operators
96 
101  {
102  x = p.x;
103  y = p.y;
104  return *this;
105  }
106 
111  {
112  PX_SHARED_ASSERT(index >= 0 && index <= 1);
113 
114  return reinterpret_cast<float*>(this)[index];
115  }
116 
120  PX_CUDA_CALLABLE PX_FORCE_INLINE const float& operator[](int index) const
121  {
122  PX_SHARED_ASSERT(index >= 0 && index <= 1);
123 
124  return reinterpret_cast<const float*>(this)[index];
125  }
126 
131  {
132  return x == v.x && y == v.y;
133  }
134 
139  {
140  return x != v.x || y != v.y;
141  }
142 
147  {
148  return x == 0.0f && y == 0.0f;
149  }
150 
155  {
156  return PxIsFinite(x) && PxIsFinite(y);
157  }
158 
163  {
164  const float unitTolerance = 1e-4f;
165  return isFinite() && PxAbs(magnitude() - 1) < unitTolerance;
166  }
167 
174  {
175  return x * x + y * y;
176  }
177 
182  {
183  return PxSqrt(magnitudeSquared());
184  }
185 
190  {
191  return PxVec2(-x, -y);
192  }
193 
198  {
199  return PxVec2(x + v.x, y + v.y);
200  }
201 
206  {
207  return PxVec2(x - v.x, y - v.y);
208  }
209 
214  {
215  return PxVec2(x * f, y * f);
216  }
217 
222  {
223  f = 1.0f / f; // PT: inconsistent notation with operator /=
224  return PxVec2(x * f, y * f);
225  }
226 
231  {
232  x += v.x;
233  y += v.y;
234  return *this;
235  }
236 
241  {
242  x -= v.x;
243  y -= v.y;
244  return *this;
245  }
246 
251  {
252  x *= f;
253  y *= f;
254  return *this;
255  }
260  {
261  f = 1.0f / f; // PT: inconsistent notation with operator /
262  x *= f;
263  y *= f;
264  return *this;
265  }
266 
271  {
272  return x * v.x + y * v.y;
273  }
274 
278  {
279  const float m = magnitudeSquared();
280  return m > 0.0f ? *this * PxRecipSqrt(m) : PxVec2(0, 0);
281  }
282 
287  {
288  const float m = magnitude();
289  if(m > 0.0f)
290  *this /= m;
291  return m;
292  }
293 
298  {
299  return PxVec2(x * a.x, y * a.y);
300  }
301 
306  {
307  return PxVec2(PxMin(x, v.x), PxMin(y, v.y));
308  }
309 
314  {
315  return PxMin(x, y);
316  }
317 
322  {
323  return PxVec2(PxMax(x, v.x), PxMax(y, v.y));
324  }
325 
330  {
331  return PxMax(x, y);
332  }
333 
334  float x, y;
335 };
336 
338 {
339  return PxVec2(f * v.x, f * v.y);
340 }
341 
342 #if !PX_DOXYGEN
343 } // namespace physx
344 #endif
345 
347 #endif // #ifndef PXFOUNDATION_PXVEC2_H
Definition: GuContactBuffer.h:37
PX_CUDA_CALLABLE PX_FORCE_INLINE float minElement() const
returns MIN(x, y);
Definition: PxVec2.h:313
PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
returns true if all 2 elems of the vector are finite (not NAN or INF, etc.)
Definition: PxVec2.h:154
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 getNormalized() const
Definition: PxVec2.h:277
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 & operator/=(float f)
scalar division
Definition: PxVec2.h:259
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
returns the magnitude
Definition: PxVec2.h:181
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator/(float f) const
scalar division
Definition: PxVec2.h:221
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 minimum(const PxVec2 &v) const
element-wise minimum
Definition: PxVec2.h:305
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 & operator=(const PxVec2 &p)
Assignment operator.
Definition: PxVec2.h:100
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec2 &v) const
returns true if the two vectors are not exactly equal.
Definition: PxVec2.h:138
#define PX_FORCE_INLINE
Definition: PxPreprocessor.h:351
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const
tests for exact zero vector
Definition: PxVec2.h:146
PX_CUDA_CALLABLE PX_FORCE_INLINE float maxElement() const
returns MAX(x, y);
Definition: PxVec2.h:329
PxZERO
Definition: Px.h:76
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
is normalized - used by API parameter validation
Definition: PxVec2.h:162
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(float nx, float ny)
Initializes from 2 scalar parameters.
Definition: PxVec2.h:84
#define PX_SHARED_ASSERT(exp)
Definition: PxSharedAssert.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 multiply(const PxVec2 &a) const
a[i] * b[i], for all i.
Definition: PxVec2.h:297
PX_CUDA_CALLABLE PX_FORCE_INLINE bool isFinite(float a)
platform-specific finiteness check (not INF or NAN)
Definition: PxUnixIntrinsics.h:127
PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec2 &v) const
returns true if the two vectors are exactly equal.
Definition: PxVec2.h:130
static PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator*(float f, const PxVec2 &v)
Definition: PxVec2.h:337
PX_CUDA_CALLABLE PX_FORCE_INLINE float & operator[](int index)
element access
Definition: PxVec2.h:110
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition: PxMath.h:144
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2()
default constructor leaves data uninitialized.
Definition: PxVec2.h:55
float y
Definition: PxVec2.h:334
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 & operator+=(const PxVec2 &v)
vector addition
Definition: PxVec2.h:230
PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec2 &v) const
returns the scalar product of this and other.
Definition: PxVec2.h:270
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMin(T a, T b)
The return value is the lesser of the two specified values.
Definition: PxMath.h:86
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 & operator*=(float f)
scalar multiplication
Definition: PxVec2.h:250
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator-() const
negation
Definition: PxVec2.h:189
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(PxZERO r)
zero constructor.
Definition: PxVec2.h:62
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxRecipSqrt(float a)
reciprocal square root.
Definition: PxMath.h:156
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(float a)
Assigns scalar parameter to all elements.
Definition: PxVec2.h:74
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator*(float f) const
scalar post-multiplication
Definition: PxVec2.h:213
float x
Definition: PxVec2.h:334
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxAbs(float a)
abs returns the absolute value of its argument.
Definition: PxMath.h:107
PX_CUDA_CALLABLE PX_FORCE_INLINE T PxMax(T a, T b)
The return value is the greater of the two specified values.
Definition: PxMath.h:70
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator+(const PxVec2 &v) const
vector addition
Definition: PxVec2.h:197
PX_CUDA_CALLABLE PX_INLINE void PX_UNUSED(T const &)
Definition: PxPreprocessor.h:466
2 Element vector class.
Definition: PxVec2.h:49
PX_CUDA_CALLABLE PX_FORCE_INLINE bool PxIsFinite(float f)
returns true if the passed number is a finite floating point number as opposed to INF...
Definition: PxMath.h:292
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 & operator-=(const PxVec2 &v)
vector difference
Definition: PxVec2.h:240
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(const PxVec2 &v)
Copy ctor.
Definition: PxVec2.h:91
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitudeSquared() const
returns the squared magnitude
Definition: PxVec2.h:173
PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
normalizes the vector in place
Definition: PxVec2.h:286
#define PX_INLINE
Definition: PxPreprocessor.h:336
#define PX_CUDA_CALLABLE
Definition: PxPreprocessor.h:460
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator-(const PxVec2 &v) const
vector difference
Definition: PxVec2.h:205
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 maximum(const PxVec2 &v) const
element-wise maximum
Definition: PxVec2.h:321
PX_CUDA_CALLABLE PX_FORCE_INLINE const float & operator[](int index) const
element access
Definition: PxVec2.h:120