RavEngine
Loading...
Searching...
No Matches
ExtVec3.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26
27#ifndef EXT_VEC3_H
28#define EXT_VEC3_H
29
30#include "foundation/PxMath.h"
31#include "foundation/PxVec3.h"
32
33namespace physx
34{
35 namespace Ext
36 {
37 //3D vector class with double precision
38 class Vec3
39 {
40 public:
41 PxF64 x;
42 PxF64 y;
43 PxF64 z;
44
45 PX_FORCE_INLINE Vec3() : x(0), y(0), z(0) {}
46
47 PX_FORCE_INLINE Vec3(const Vec3 &other) { x = other.x; y = other.y; z = other.z; }
48
49 PX_FORCE_INLINE Vec3(PxF64 x_, PxF64 y_, PxF64 z_) : x(x_), y(y_), z(z_)
50 {}
51
52 PX_FORCE_INLINE Vec3(PxF32 x_, PxF32 y_, PxF32 z_) : x(PxF64(x_)), y(PxF64(y_)), z(PxF64(z_))
53 {}
54
55 PX_FORCE_INLINE void set(PxF64 xCoord, PxF64 yCoord, PxF64 zCoord)
56 {
57 x = xCoord;
58 y = yCoord;
59 z = zCoord;
60 }
61
62 PX_FORCE_INLINE PxVec3 toFloat() const
63 {
64 return PxVec3(PxF32(x), PxF32(y), PxF32(z));
65 }
66
67 PX_FORCE_INLINE Vec3 max(const Vec3& lhs) const
68 {
69 return Vec3(PxMax(x, lhs.x), PxMax(y, lhs.y), PxMax(z, lhs.z));
70 }
71
72 PX_FORCE_INLINE Vec3 min(const Vec3& lhs) const
73 {
74 return Vec3(PxMin(x, lhs.x), PxMin(y, lhs.y), PxMin(z, lhs.z));
75 }
76
77 PX_FORCE_INLINE PxF64 magnitudeSquared() const
78 {
79 return x * x + y * y + z * z;
80 }
81
82 PX_FORCE_INLINE PxF64 magnitude() const
83 {
84 return PxSqrt(x * x + y * y + z * z);
85 }
86
87 PX_FORCE_INLINE Vec3 getNormalized() const
88 {
89 PxF64 s = magnitudeSquared();
90 if (s == 0)
91 return *this;
92 s = 1.0 / PxSqrt(s);
93 return Vec3(s * x, s * y, s * z);
94 }
95
96 PX_FORCE_INLINE PxF64 dot(const Vec3& rhs) const
97 {
98 return x * rhs.x + y * rhs.y + z * rhs.z;
99 }
100
101 PX_FORCE_INLINE Vec3 cross(const Vec3& v) const
102 {
103 return Vec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
104 }
105
106 PX_FORCE_INLINE Vec3 operator+(const Vec3& v) const
107 {
108 return Vec3(x + v.x, y + v.y, z + v.z);
109 }
110
111 PX_FORCE_INLINE Vec3 operator-(const Vec3& v) const
112 {
113 return Vec3(x - v.x, y - v.y, z - v.z);
114 }
115
116 PX_FORCE_INLINE Vec3 operator*(const PxF64& v) const
117 {
118 return Vec3(x * v, y * v, z * v);
119 }
120
121 PX_FORCE_INLINE Vec3 operator/(const PxF64& v) const
122 {
123 PxF64 inv = 1.0 / v;
124 return Vec3(x * inv, y * inv, z * inv);
125 }
126
127 PX_FORCE_INLINE Vec3& operator+=(const Vec3& v)
128 {
129 x += v.x;
130 y += v.y;
131 z += v.z;
132 return *this;
133 }
134
135 PX_FORCE_INLINE Vec3& operator-=(const Vec3& v)
136 {
137 x -= v.x;
138 y -= v.y;
139 z -= v.z;
140 return *this;
141 }
142
143 PX_FORCE_INLINE Vec3& operator*=(PxF64 f)
144 {
145 x *= f;
146 y *= f;
147 z *= f;
148 return *this;
149 }
150
151 PX_FORCE_INLINE Vec3& operator/=(PxF64 f)
152 {
153 f = 1.0 / f;
154 x *= f;
155 y *= f;
156 z *= f;
157 return *this;
158 }
159
160 PX_FORCE_INLINE PxF64& operator[](PxU32 index)
161 {
162 PX_ASSERT(index <= 2);
163
164 return reinterpret_cast<PxF64*>(this)[index];
165 }
166
167 PX_FORCE_INLINE const PxF64& operator[](PxU32 index) const
168 {
169 PX_ASSERT(index <= 2);
170
171 return reinterpret_cast<const PxF64*>(this)[index];
172 }
173 };
174
175 // ---------------------------------------------------------------------------------
176 struct Bounds3 {
177 Bounds3() {}
178 Bounds3(const Vec3 &min, const Vec3 &max) : minimum(min), maximum(max) {}
179
180 void setEmpty() {
181 minimum = Vec3(PX_MAX_F64, PX_MAX_F64, PX_MAX_F64);
182 maximum = Vec3(-PX_MAX_F64, -PX_MAX_F64, -PX_MAX_F64);
183 }
184
185 Vec3 getDimensions() const {
186 return maximum - minimum;
187 }
188
189 void include(const Vec3 &p) {
190 minimum = minimum.min(p);
191 maximum = maximum.max(p);
192 }
193
194 void include(const Bounds3 &b) {
195 minimum = minimum.min(b.minimum);
196 maximum = maximum.max(b.maximum);
197 }
198 void expand(double d) {
199 minimum -= Vec3(d, d, d);
200 maximum += Vec3(d, d, d);
201 }
202 Vec3 minimum, maximum;
203 };
204 }
205}
206
207#endif
208
Definition ExtVec3.h:39
3 Element vector class.
Definition PxVec3.h:50
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSqrt(float a)
Square root.
Definition PxMath.h:146
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:72
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:88
Definition ExtVec3.h:176