1/*
2 * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 */
10// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12
13
14#ifndef PX_FOUNDATION_PX_VEC2_H
15#define PX_FOUNDATION_PX_VEC2_H
16
17/** \addtogroup foundation
18@{
19*/
20
21#include "foundation/PxMath.h"
22
23#ifndef PX_DOXYGEN
24namespace physx
25{
26#endif
27
28
29/**
30\brief 2 Element vector class.
31
32This is a 2-dimensional vector class with public data members.
33*/
34class PxVec2
35{
36public:
37
38 /**
39 \brief default constructor leaves data uninitialized.
40 */
41 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2() {}
42
43
44 /**
45 \brief zero constructor.
46 */
47 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(PxZERO r): x(0.0f), y(0.0f)
48 {
49 PX_UNUSED(r);
50 }
51
52 /**
53 \brief Assigns scalar parameter to all elements.
54
55 Useful to initialize to zero or one.
56
57 \param[in] a Value to assign to elements.
58 */
59 explicit PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(PxReal a): x(a), y(a) {}
60
61 /**
62 \brief Initializes from 2 scalar parameters.
63
64 \param[in] nx Value to initialize X component.
65 \param[in] ny Value to initialize Y component.
66 */
67 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(PxReal nx, PxReal ny): x(nx), y(ny){}
68
69 /**
70 \brief Copy ctor.
71 */
72 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2(const PxVec2& v): x(v.x), y(v.y) {}
73
74 //Operators
75
76 /**
77 \brief Assignment operator
78 */
79 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator=(const PxVec2& p) { x = p.x; y = p.y; return *this; }
80
81 /**
82 \brief element access
83 */
84 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal& operator[](int index)
85 {
86 PX_ASSERT(index>=0 && index<=1);
87
88 return reinterpret_cast<PxReal*>(this)[index];
89 }
90
91 /**
92 \brief element access
93 */
94 PX_CUDA_CALLABLE PX_FORCE_INLINE const PxReal& operator[](int index) const
95 {
96 PX_ASSERT(index>=0 && index<=1);
97
98 return reinterpret_cast<const PxReal*>(this)[index];
99 }
100
101 /**
102 \brief returns true if the two vectors are exactly equal.
103 */
104 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator==(const PxVec2&v) const { return x == v.x && y == v.y; }
105
106 /**
107 \brief returns true if the two vectors are not exactly equal.
108 */
109 PX_CUDA_CALLABLE PX_FORCE_INLINE bool operator!=(const PxVec2&v) const { return x != v.x || y != v.y; }
110
111 /**
112 \brief tests for exact zero vector
113 */
114 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isZero() const { return x==0.0f && y==0.0f; }
115
116 /**
117 \brief returns true if all 2 elems of the vector are finite (not NAN or INF, etc.)
118 */
119 PX_CUDA_CALLABLE PX_INLINE bool isFinite() const
120 {
121 return PxIsFinite(x) && PxIsFinite(y);
122 }
123
124 /**
125 \brief is normalized - used by API parameter validation
126 */
127 PX_CUDA_CALLABLE PX_FORCE_INLINE bool isNormalized() const
128 {
129 const float unitTolerance = 1e-4f;
130 return isFinite() && PxAbs(magnitude()-1)<unitTolerance;
131 }
132
133 /**
134 \brief returns the squared magnitude
135
136 Avoids calling PxSqrt()!
137 */
138 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal magnitudeSquared() const { return x * x + y * y; }
139
140 /**
141 \brief returns the magnitude
142 */
143 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal magnitude() const { return PxSqrt(magnitudeSquared()); }
144
145 /**
146 \brief negation
147 */
148 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator -() const
149 {
150 return PxVec2(-x, -y);
151 }
152
153 /**
154 \brief vector addition
155 */
156 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator +(const PxVec2& v) const { return PxVec2(x + v.x, y + v.y); }
157
158 /**
159 \brief vector difference
160 */
161 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator -(const PxVec2& v) const { return PxVec2(x - v.x, y - v.y); }
162
163 /**
164 \brief scalar post-multiplication
165 */
166 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator *(PxReal f) const { return PxVec2(x * f, y * f); }
167
168 /**
169 \brief scalar division
170 */
171 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 operator /(PxReal f) const
172 {
173 f = 1.0f / f; // PT: inconsistent notation with operator /=
174 return PxVec2(x * f, y * f);
175 }
176
177 /**
178 \brief vector addition
179 */
180 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator +=(const PxVec2& v)
181 {
182 x += v.x;
183 y += v.y;
184 return *this;
185 }
186
187 /**
188 \brief vector difference
189 */
190 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator -=(const PxVec2& v)
191 {
192 x -= v.x;
193 y -= v.y;
194 return *this;
195 }
196
197 /**
198 \brief scalar multiplication
199 */
200 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator *=(PxReal f)
201 {
202 x *= f;
203 y *= f;
204 return *this;
205 }
206 /**
207 \brief scalar division
208 */
209 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2& operator /=(PxReal f)
210 {
211 f = 1.0f/f; // PT: inconsistent notation with operator /
212 x *= f;
213 y *= f;
214 return *this;
215 }
216
217 /**
218 \brief returns the scalar product of this and other.
219 */
220 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal dot(const PxVec2& v) const
221 {
222 return x * v.x + y * v.y;
223 }
224
225 /** return a unit vector */
226
227 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 getNormalized() const
228 {
229 const PxReal m = magnitudeSquared();
230 return m>0.0f ? *this * PxRecipSqrt(m) : PxVec2(0,0);
231 }
232
233 /**
234 \brief normalizes the vector in place
235 */
236 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal normalize()
237 {
238 const PxReal m = magnitude();
239 if (m>0.0f)
240 *this /= m;
241 return m;
242 }
243
244 /**
245 \brief a[i] * b[i], for all i.
246 */
247 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 multiply(const PxVec2& a) const
248 {
249 return PxVec2(x*a.x, y*a.y);
250 }
251
252 /**
253 \brief element-wise minimum
254 */
255 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 minimum(const PxVec2& v) const
256 {
257 return PxVec2(PxMin(x, v.x), PxMin(y,v.y));
258 }
259
260 /**
261 \brief returns MIN(x, y);
262 */
263 PX_CUDA_CALLABLE PX_FORCE_INLINE float minElement() const
264 {
265 return PxMin(x, y);
266 }
267
268 /**
269 \brief element-wise maximum
270 */
271 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec2 maximum(const PxVec2& v) const
272 {
273 return PxVec2(PxMax(x, v.x), PxMax(y,v.y));
274 }
275
276 /**
277 \brief returns MAX(x, y);
278 */
279 PX_CUDA_CALLABLE PX_FORCE_INLINE float maxElement() const
280 {
281 return PxMax(x, y);
282 }
283
284 PxReal x,y;
285};
286
287PX_CUDA_CALLABLE static PX_FORCE_INLINE PxVec2 operator *(PxReal f, const PxVec2& v)
288{
289 return PxVec2(f * v.x, f * v.y);
290}
291
292#ifndef PX_DOXYGEN
293} // namespace physx
294#endif
295
296/** @} */
297#endif // PX_FOUNDATION_PX_VEC2_H
298