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_PXVEC3_H |
31 | #define PXFOUNDATION_PXVEC3_H |
32 | |
33 | /** \addtogroup foundation |
34 | @{ |
35 | */ |
36 | |
37 | #include "foundation/PxMath.h" |
38 | |
39 | #if !PX_DOXYGEN |
40 | namespace physx |
41 | { |
42 | #endif |
43 | |
44 | /** |
45 | \brief 3 Element vector class. |
46 | |
47 | This is a 3-dimensional vector class with public data members. |
48 | */ |
49 | class PxVec3 |
50 | { |
51 | public: |
52 | /** |
53 | \brief default constructor leaves data uninitialized. |
54 | */ |
55 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3() |
56 | { |
57 | } |
58 | |
59 | /** |
60 | \brief zero constructor. |
61 | */ |
62 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(PxZERO r) : x(0.0f), y(0.0f), z(0.0f) |
63 | { |
64 | PX_UNUSED(r); |
65 | } |
66 | |
67 | /** |
68 | \brief Assigns scalar parameter to all elements. |
69 | |
70 | Useful to initialize to zero or one. |
71 | |
72 | \param[in] a Value to assign to elements. |
73 | */ |
74 | explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float a) : x(a), y(a), z(a) |
75 | { |
76 | } |
77 | |
78 | /** |
79 | \brief Initializes from 3 scalar parameters. |
80 | |
81 | \param[in] nx Value to initialize X component. |
82 | \param[in] ny Value to initialize Y component. |
83 | \param[in] nz Value to initialize Z component. |
84 | */ |
85 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(float nx, float ny, float nz) : x(nx), y(ny), z(nz) |
86 | { |
87 | } |
88 | |
89 | /** |
90 | \brief Copy ctor. |
91 | */ |
92 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3(const PxVec3& v) : x(v.x), y(v.y), z(v.z) |
93 | { |
94 | } |
95 | |
96 | // Operators |
97 | |
98 | /** |
99 | \brief Assignment operator |
100 | */ |
101 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator=(const PxVec3& p) |
102 | { |
103 | x = p.x; |
104 | y = p.y; |
105 | z = p.z; |
106 | return *this; |
107 | } |
108 | |
109 | /** |
110 | \brief element access |
111 | */ |
112 | PX_CUDA_CALLABLE PX_FORCE_INLINE float& operator[](unsigned int index) |
113 | { |
114 | PX_SHARED_ASSERT(index <= 2); |
115 | |
116 | return reinterpret_cast<float*>(this)[index]; |
117 | } |
118 | |
119 | /** |
120 | \brief element access |
121 | */ |
122 | PX_CUDA_CALLABLE PX_FORCE_INLINE const float& operator[](unsigned int index) const |
123 | { |
124 | PX_SHARED_ASSERT(index <= 2); |
125 | |
126 | return reinterpret_cast<const float*>(this)[index]; |
127 | } |
128 | |
129 | /** |
130 | \brief returns true if the two vectors are exactly equal. |
131 | */ |
132 | PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec3& v) const |
133 | { |
134 | return x == v.x && y == v.y && z == v.z; |
135 | } |
136 | |
137 | /** |
138 | \brief returns true if the two vectors are not exactly equal. |
139 | */ |
140 | PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec3& v) const |
141 | { |
142 | return x != v.x || y != v.y || z != v.z; |
143 | } |
144 | |
145 | /** |
146 | \brief tests for exact zero vector |
147 | */ |
148 | PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const |
149 | { |
150 | return x == 0.0f && y == 0.0f && z == 0.0f; |
151 | } |
152 | |
153 | /** |
154 | \brief returns true if all 3 elems of the vector are finite (not NAN or INF, etc.) |
155 | */ |
156 | PX_CUDA_CALLABLE PX_INLINE bool isFinite() const |
157 | { |
158 | return PxIsFinite(f: x) && PxIsFinite(f: y) && PxIsFinite(f: z); |
159 | } |
160 | |
161 | /** |
162 | \brief is normalized - used by API parameter validation |
163 | */ |
164 | PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const |
165 | { |
166 | const float unitTolerance = 1e-4f; |
167 | return isFinite() && PxAbs(a: magnitude() - 1) < unitTolerance; |
168 | } |
169 | |
170 | /** |
171 | \brief returns the squared magnitude |
172 | |
173 | Avoids calling PxSqrt()! |
174 | */ |
175 | PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitudeSquared() const |
176 | { |
177 | return x * x + y * y + z * z; |
178 | } |
179 | |
180 | /** |
181 | \brief returns the magnitude |
182 | */ |
183 | PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const |
184 | { |
185 | return PxSqrt(a: magnitudeSquared()); |
186 | } |
187 | |
188 | /** |
189 | \brief negation |
190 | */ |
191 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-() const |
192 | { |
193 | return PxVec3(-x, -y, -z); |
194 | } |
195 | |
196 | /** |
197 | \brief vector addition |
198 | */ |
199 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator+(const PxVec3& v) const |
200 | { |
201 | return PxVec3(x + v.x, y + v.y, z + v.z); |
202 | } |
203 | |
204 | /** |
205 | \brief vector difference |
206 | */ |
207 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator-(const PxVec3& v) const |
208 | { |
209 | return PxVec3(x - v.x, y - v.y, z - v.z); |
210 | } |
211 | |
212 | /** |
213 | \brief scalar post-multiplication |
214 | */ |
215 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator*(float f) const |
216 | { |
217 | return PxVec3(x * f, y * f, z * f); |
218 | } |
219 | |
220 | /** |
221 | \brief scalar division |
222 | */ |
223 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 operator/(float f) const |
224 | { |
225 | f = 1.0f / f; |
226 | return PxVec3(x * f, y * f, z * f); |
227 | } |
228 | |
229 | /** |
230 | \brief vector addition |
231 | */ |
232 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator+=(const PxVec3& v) |
233 | { |
234 | x += v.x; |
235 | y += v.y; |
236 | z += v.z; |
237 | return *this; |
238 | } |
239 | |
240 | /** |
241 | \brief vector difference |
242 | */ |
243 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator-=(const PxVec3& v) |
244 | { |
245 | x -= v.x; |
246 | y -= v.y; |
247 | z -= v.z; |
248 | return *this; |
249 | } |
250 | |
251 | /** |
252 | \brief scalar multiplication |
253 | */ |
254 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator*=(float f) |
255 | { |
256 | x *= f; |
257 | y *= f; |
258 | z *= f; |
259 | return *this; |
260 | } |
261 | /** |
262 | \brief scalar division |
263 | */ |
264 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3& operator/=(float f) |
265 | { |
266 | f = 1.0f / f; |
267 | x *= f; |
268 | y *= f; |
269 | z *= f; |
270 | return *this; |
271 | } |
272 | |
273 | /** |
274 | \brief returns the scalar product of this and other. |
275 | */ |
276 | PX_CUDA_CALLABLE PX_FORCE_INLINE float dot(const PxVec3& v) const |
277 | { |
278 | return x * v.x + y * v.y + z * v.z; |
279 | } |
280 | |
281 | /** |
282 | \brief cross product |
283 | */ |
284 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 cross(const PxVec3& v) const |
285 | { |
286 | return PxVec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); |
287 | } |
288 | |
289 | /** return a unit vector */ |
290 | |
291 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 getNormalized() const |
292 | { |
293 | const float m = magnitudeSquared(); |
294 | return m > 0.0f ? *this * PxRecipSqrt(a: m) : PxVec3(0, 0, 0); |
295 | } |
296 | |
297 | /** |
298 | \brief normalizes the vector in place |
299 | */ |
300 | PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize() |
301 | { |
302 | const float m = magnitude(); |
303 | if(m > 0.0f) |
304 | *this /= m; |
305 | return m; |
306 | } |
307 | |
308 | /** |
309 | \brief normalizes the vector in place. Does nothing if vector magnitude is under PX_NORMALIZATION_EPSILON. |
310 | Returns vector magnitude if >= PX_NORMALIZATION_EPSILON and 0.0f otherwise. |
311 | */ |
312 | PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeSafe() |
313 | { |
314 | const float mag = magnitude(); |
315 | if(mag < PX_NORMALIZATION_EPSILON) |
316 | return 0.0f; |
317 | *this *= 1.0f / mag; |
318 | return mag; |
319 | } |
320 | |
321 | /** |
322 | \brief normalizes the vector in place. Asserts if vector magnitude is under PX_NORMALIZATION_EPSILON. |
323 | returns vector magnitude. |
324 | */ |
325 | PX_CUDA_CALLABLE PX_FORCE_INLINE float normalizeFast() |
326 | { |
327 | const float mag = magnitude(); |
328 | PX_SHARED_ASSERT(mag >= PX_NORMALIZATION_EPSILON); |
329 | *this *= 1.0f / mag; |
330 | return mag; |
331 | } |
332 | |
333 | /** |
334 | \brief a[i] * b[i], for all i. |
335 | */ |
336 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 multiply(const PxVec3& a) const |
337 | { |
338 | return PxVec3(x * a.x, y * a.y, z * a.z); |
339 | } |
340 | |
341 | /** |
342 | \brief element-wise minimum |
343 | */ |
344 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 minimum(const PxVec3& v) const |
345 | { |
346 | return PxVec3(PxMin(a: x, b: v.x), PxMin(a: y, b: v.y), PxMin(a: z, b: v.z)); |
347 | } |
348 | |
349 | /** |
350 | \brief returns MIN(x, y, z); |
351 | */ |
352 | PX_CUDA_CALLABLE PX_FORCE_INLINE float minElement() const |
353 | { |
354 | return PxMin(a: x, b: PxMin(a: y, b: z)); |
355 | } |
356 | |
357 | /** |
358 | \brief element-wise maximum |
359 | */ |
360 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 maximum(const PxVec3& v) const |
361 | { |
362 | return PxVec3(PxMax(a: x, b: v.x), PxMax(a: y, b: v.y), PxMax(a: z, b: v.z)); |
363 | } |
364 | |
365 | /** |
366 | \brief returns MAX(x, y, z); |
367 | */ |
368 | PX_CUDA_CALLABLE PX_FORCE_INLINE float maxElement() const |
369 | { |
370 | return PxMax(a: x, b: PxMax(a: y, b: z)); |
371 | } |
372 | |
373 | /** |
374 | \brief returns absolute values of components; |
375 | */ |
376 | PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 abs() const |
377 | { |
378 | return PxVec3(PxAbs(a: x), PxAbs(a: y), PxAbs(a: z)); |
379 | } |
380 | |
381 | float x, y, z; |
382 | }; |
383 | |
384 | PX_CUDA_CALLABLE static PX_FORCE_INLINE PxVec3 operator*(float f, const PxVec3& v) |
385 | { |
386 | return PxVec3(f * v.x, f * v.y, f * v.z); |
387 | } |
388 | |
389 | #if !PX_DOXYGEN |
390 | } // namespace physx |
391 | #endif |
392 | |
393 | /** @} */ |
394 | #endif // #ifndef PXFOUNDATION_PXVEC3_H |
395 | |