NW4F G3d
Loading...
Searching...
No Matches
g3d_Vector3-inl.h
Go to the documentation of this file.
1#include <nw/g3d/math/g3d_MathCommon.h>
2#include <nw/g3d/math/g3d_Matrix34.h>
3#include <nw/g3d/math/g3d_Matrix44.h>
4#include <nw/g3d/math/g3d_Quaternion.h>
5
6namespace nw { namespace g3d { namespace math {
7
9Vec3 Vec3::Make(float x, float y, float z)
10{
11 return Vec3().Set(x, y, z);
12}
13
15Vec3* Vec3::Cast(float* a)
16{
17 return reinterpret_cast<Vec3*>(a);
18}
19
21const Vec3* Vec3::Cast(const float* a)
22{
23 return reinterpret_cast<const Vec3*>(a);
24}
25
27Vec3& Vec3::Set(float x, float y, float z)
28{
29 this->x = x;
30 this->y = y;
31 this->z = z;
32 return *this;
33}
34
36Vec3& Vec3::Set(const float* a)
37{
39 for (int i = 0; i < DIM; ++i)
40 {
41 this->a[i] = a[i];
42 }
43 return *this;
44}
45
47Vec3& Vec3::Set(const Vec3& v)
48{
49 for (int i = 0; i < DIM; ++i)
50 {
51 a[i] = v.a[i];
52 }
53 return *this;
54}
55
58{
59 for (int i = 0; i < DIM; ++i)
60 {
61 a[i] = 0.0f;
62 }
63 return *this;
64}
65
67Vec3& Vec3::Neg(const Vec3& v)
68{
69#if defined( __ghs__ )
70 ps[0] = __PS_NEG(v.ps[0]);
71 a[2] = -v.a[2];
72#else
73 for (int i = 0; i < DIM; ++i)
74 {
75 a[i] = -v.a[i];
76 }
77#endif
78 return *this;
79}
80
82Vec3& Vec3::Rcp(const Vec3& v)
83{
84#if defined( __ghs__ )
85 ps[0] = Math::Rcp(v.ps[0]);
86 a[2] = Math::Rcp(v.a[2]);
87#else
88 for (int i = 0; i < DIM; ++i)
89 {
90 a[i] = Math::Rcp(v.a[i]);
91 }
92#endif
93 return *this;
94}
95
96
97//--------------------------------------------------------------------------------------------------
98
100Vec3& Vec3::Add(const Vec3& lhs, const Vec3& rhs)
101{
102#if defined( __ghs__ )
103 ps[0] = __PS_ADD(lhs.ps[0], rhs.ps[0]);
104 z = lhs.z + rhs.z;
105#else
106 for (int i = 0; i < DIM; ++i)
107 {
108 a[i] = lhs.a[i] + rhs.a[i];
109 }
110#endif
111 return *this;
112}
113
115Vec3& Vec3::Sub(const Vec3& lhs, const Vec3& rhs)
116{
117#if defined( __ghs__ )
118 ps[0] = __PS_SUB(lhs.ps[0], rhs.ps[0]);
119 z = lhs.z - rhs.z;
120#else
121 for (int i = 0; i < DIM; ++i)
122 {
123 a[i] = lhs.a[i] - rhs.a[i];
124 }
125#endif
126 return *this;
127}
128
130Vec3& Vec3::Mul(const Vec3& lhs, const Vec3& rhs)
131{
132#if defined( __ghs__ )
133 ps[0] = __PS_MUL(lhs.ps[0], rhs.ps[0]);
134 z = lhs.z * rhs.z;
135#else
136 for (int i = 0; i < DIM; ++i)
137 {
138 a[i] = lhs.a[i] * rhs.a[i];
139 }
140#endif
141 return *this;
142}
143
145Vec3& Vec3::Div(const Vec3& lhs, const Vec3& rhs)
146{
147 Vec3 rcp;
148 rcp.Rcp(rhs);
149 Mul(lhs, rcp);
150 return *this;
151}
152
154Vec3& Vec3::Mul(const Vec3& lhs, float rhs)
155{
156#if defined( __ghs__ )
157 ps[0] = __PS_MULS0F(lhs.ps[0], rhs);
158 z = lhs.z * rhs;
159#else
160 for (int i = 0; i < DIM; ++i)
161 {
162 a[i] = lhs.a[i] * rhs;
163 }
164#endif
165 return *this;
166}
167
169Vec3& Vec3::Mad(const Vec3& lhsMul, float rhsMul, const Vec3& add)
170{
171#if defined( __ghs__ )
172 ps[0] = __PS_MADDS0F(lhsMul.ps[0], rhsMul, add.ps[0]);
173 z = lhsMul.z * rhsMul + add.z;
174#else
175 for (int i = 0; i < DIM; ++i)
176 {
177 a[i] = lhsMul.a[i] * rhsMul + add.a[i];
178 }
179#endif
180 return *this;
181}
182
184Vec3& Vec3::Div(const Vec3& lhs, float rhs)
185{
186 float rcp = Math::Rcp(rhs);
187 this->Mul(lhs, rcp);
188 return *this;
189}
190
192Vec3& Vec3::Mul(const Mtx34& lhs, const Vec3& rhs)
193{
194#if defined( __ghs__ )
195 const f32x2 m00m01 = __PSQ_LX(&lhs, 0, 0, 0);
196 const f32x2 m02m03 = __PSQ_LX(&lhs, 8, 0, 0);
197 const f32x2 m10m11 = __PSQ_LX(&lhs, 16, 0, 0);
198 const f32x2 m12m13 = __PSQ_LX(&lhs, 24, 0, 0);
199 const f32x2 m20m21 = __PSQ_LX(&lhs, 32, 0, 0);
200 const f32x2 m22m23 = __PSQ_LX(&lhs, 40, 0, 0);
201
202 const f32x2 xy = __PSQ_LX(&rhs, 0, 0, 0);
203 const f32x2 zw = __PSQ_LX(&rhs, 8, 1, 0);
204
205 f32x2 fp0, fp1, fp2;
206
207 fp0 = __PS_MUL(m00m01, xy);
208 fp0 = __PS_MADD(m02m03, zw, fp0);
209 fp0 = __PS_SUM0(fp0, fp0, fp0);
210
211 fp1 = __PS_MUL(m10m11, xy);
212 fp1 = __PS_MADD(m12m13, zw, fp1);
213 fp1 = __PS_SUM1(fp1, fp0, fp1);
214
215 fp2 = __PS_MUL(m20m21, xy);
216 fp2 = __PS_MADD(m22m23, zw, fp2);
217 fp2 = __PS_SUM0(fp2, fp2, fp2);
218
219 ps[0] = fp1;
220 z = fp2[0];
221
222 return *this;
223#else
224 Vec3 out;
225 out.x = lhs.m00 * rhs.x + lhs.m01 * rhs.y + lhs.m02 * rhs.z + lhs.m03;
226 out.y = lhs.m10 * rhs.x + lhs.m11 * rhs.y + lhs.m12 * rhs.z + lhs.m13;
227 out.z = lhs.m20 * rhs.x + lhs.m21 * rhs.y + lhs.m22 * rhs.z + lhs.m23;
228 return *this = out;
229#endif
230}
231
232//--------------------------------------------------------------------------------------------------
233
235float Vec3::Length(const Vec3& v)
236{
238}
239
241float Vec3::LengthSq(const Vec3& v)
242{
243 return Dot(v, v);
244}
245
247float Vec3::Distance(const Vec3& lhs, const Vec3& rhs)
248{
249 Vec3 vec;
250 vec.Sub(lhs, rhs);
251 return Length(vec);
252}
253
255float Vec3::DistanceSq(const Vec3& lhs, const Vec3& rhs)
256{
257 Vec3 vec;
258 vec.Sub(lhs, rhs);
259 return LengthSq(vec);
260}
261
263float Vec3::Dot(const Vec3& lhs, const Vec3& rhs)
264{
265#if defined( __ghs__ )
266 f32x2 ps;
267 ps = __PS_MUL(lhs.ps[0], rhs.ps[0]);
268 ps = __PS_SUM0(ps, ps, ps);
269 return __FMADDS(lhs.z, rhs.z, ps[0]);
270#else
271 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
272#endif
273}
274
276Vec3& Vec3::Cross(const Vec3& lhs, const Vec3& rhs)
277{
278 Vec3 out;
279 out.x = lhs.y * rhs.z - lhs.z * rhs.y;
280 out.y = lhs.z * rhs.x - lhs.x * rhs.z;
281 out.z = lhs.x * rhs.y - lhs.y * rhs.x;
282 return *this = out;
283}
284
286float Vec3::Normalize(const Vec3& v)
287{
288 float lengthSq = LengthSq(v);
289 if (lengthSq > 0.0f)
290 {
291 float rcp = Math::RSqrt(lengthSq);
292 Mul(v, rcp);
293 return lengthSq * rcp;
294 }
295 return 0.0f;
296}
297
298//--------------------------------------------------------------------------------------------------
299
301Vec3& Vec3::Transform(const Mtx34& m, const Vec3& v)
302{
303 return Mul(m, v);
304}
305
307Vec3& Vec3::Rotate(const Mtx34& m, const Vec3& v)
308{
309#if defined( __ghs__ )
310 const f32x2 m00m01 = __PSQ_LX(&m, 0, 0, 0);
311 const f32x2 m02m03 = __PSQ_LX(&m, 8, 1, 0);
312 const f32x2 m10m11 = __PSQ_LX(&m, 16, 0, 0);
313 const f32x2 m12m13 = __PSQ_LX(&m, 24, 1, 0);
314 const f32x2 m20m21 = __PSQ_LX(&m, 32, 0, 0);
315 const f32x2 m22m23 = __PSQ_LX(&m, 40, 1, 0);
316
317 const f32x2 xy = __PSQ_LX(&v, 0, 0, 0);
318 const f32x2 zw = __PSQ_LX(&v, 8, 1, 0);
319
320 f32x2 fp0, fp1, fp2;
321
322 fp0 = __PS_MUL(m00m01, xy);
323 fp0 = __PS_SUM0(fp0, fp0, fp0);
324 fp0 = __PS_MADD(m02m03, zw, fp0);
325
326 x = fp0[0];
327
328 fp1 = __PS_MUL(m10m11, xy);
329 fp1 = __PS_SUM0(fp1, fp1, fp1);
330 fp1 = __PS_MADD(m12m13, zw, fp1);
331
332 y = fp1[0];
333
334 fp2 = __PS_MUL(m20m21, xy);
335 fp2 = __PS_SUM0(fp2, fp2, fp2);
336 fp2 = __PS_MADD(m22m23, zw, fp2);
337
338 z = fp2[0];
339
340 return *this;
341#else
342 Vec3 out;
343 out.x = m.m00 * v.x + m.m01 * v.y + m.m02 * v.z;
344 out.y = m.m10 * v.x + m.m11 * v.y + m.m12 * v.z;
345 out.z = m.m20 * v.x + m.m21 * v.y + m.m22 * v.z;
346 return *this = out;
347#endif
348}
349
351Vec3& Vec3::Rotate(const Quat& q, const Vec3& v)
352{
353 Quat r;
354 r.x = v.z * q.y + q.w * v.x -v.y * q.z;
355 r.y = v.x * q.z + q.w * v.y -v.z * q.x;
356 r.z = v.y * q.x + q.w * v.z -v.x * q.y;
357 r.w = v.x * q.x + v.y * q.y + v.z * q.z;
358
359 this->x = q.y * r.z - q.z * r.y + r.w * q.x + q.w * r.x;
360 this->y = q.z * r.x - q.x * r.z + r.w * q.y + q.w * r.y;
361 this->z = q.x * r.y - q.y * r.x + r.w * q.z + q.w * r.z;
362 return *this;
363}
364
366Vec3& Vec3::Project(const Mtx44& lhs, const Vec3& rhs)
367{
368#if defined( __ghs__ )
369 const f32x2 m00m01 = __PSQ_LX(&lhs, 0, 0, 0);
370 const f32x2 m02m03 = __PSQ_LX(&lhs, 8, 0, 0);
371 const f32x2 m10m11 = __PSQ_LX(&lhs, 16, 0, 0);
372 const f32x2 m12m13 = __PSQ_LX(&lhs, 24, 0, 0);
373 const f32x2 m20m21 = __PSQ_LX(&lhs, 32, 0, 0);
374 const f32x2 m22m23 = __PSQ_LX(&lhs, 40, 0, 0);
375 const f32x2 m30m31 = __PSQ_LX(&lhs, 48, 0, 0);
376 const f32x2 m32m33 = __PSQ_LX(&lhs, 56, 0, 0);
377
378 const f32x2 xy = __PSQ_LX(&rhs, 0, 0, 0);
379 const f32x2 zw = __PSQ_LX(&rhs, 8, 1, 0);
380
381 f32x2 fp0, fp1, fp2, fp3;
382
383 fp0 = __PS_MUL(m00m01, xy);
384 fp0 = __PS_MADD(m02m03, zw, fp0);
385 fp0 = __PS_SUM0(fp0, fp0, fp0);
386
387 fp1 = __PS_MUL(m10m11, xy);
388 fp1 = __PS_MADD(m12m13, zw, fp1);
389 fp1 = __PS_SUM1(fp1, fp0, fp1);
390
391 fp2 = __PS_MUL(m20m21, xy);
392 fp2 = __PS_MADD(m22m23, zw, fp2);
393 fp2 = __PS_SUM0(fp2, fp2, fp2);
394
395 fp3 = __PS_MUL(m30m31, xy);
396 fp3 = __PS_MADD(m32m33, zw, fp3);
397 fp3 = __PS_SUM0(fp3, fp3, fp3);
398 fp3[1] = fp3[0] = Math::Rcp(fp3[0]);
399
400 ps[0] = __PS_MUL(fp1, fp3);
401 z = fp2[0] * fp3[0];
402
403 return *this;
404#else
405 Vec3 out;
406 float r = Math::Rcp(lhs.m30 * rhs.x + lhs.m31 * rhs.y + lhs.m32 * rhs.z + lhs.m33);
407 out.x = r * ( lhs.m00 * rhs.x + lhs.m01 * rhs.y + lhs.m02 * rhs.z + lhs.m03 );
408 out.y = r * ( lhs.m10 * rhs.x + lhs.m11 * rhs.y + lhs.m12 * rhs.z + lhs.m13 );
409 out.z = r * ( lhs.m20 * rhs.x + lhs.m21 * rhs.y + lhs.m22 * rhs.z + lhs.m23 );
410
411 return *this = out;
412#endif
413}
414
415} } } // namespace nw::g3d::math
Definition g3d_MathCommon.h:9
static float Sqrt(float x)
Definition g3d_MathCommon-inl.h:163
static float RSqrt(float x)
Definition g3d_MathCommon-inl.h:150
static float Rcp(float x)
Definition g3d_MathCommon-inl.h:139
Definition g3d_Matrix34.h:34
Definition g3d_Matrix44.h:37
Definition g3d_Quaternion.h:28
Definition g3d_Vector3.h:30
Vec3 & Mad(const Vec3 &lhsMul, float rhsMul, const Vec3 &add)
Definition g3d_Vector3-inl.h:169
Vec3 & Add(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:100
Vec3 & Set(const Vec3 &v)
Definition g3d_Vector3-inl.h:47
Vec3 & Set(float x, float y, float z)
Definition g3d_Vector3-inl.h:27
static float DistanceSq(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:255
Vec3 & Div(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:145
static Vec3 * Cast(float *a)
Definition g3d_Vector3-inl.h:15
Vec3 & Rcp(const Vec3 &v)
Definition g3d_Vector3-inl.h:82
Vec3 & Cross(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:276
Vec3 & Rotate(const Mtx34 &m, const Vec3 &v)
Definition g3d_Vector3-inl.h:307
static float Length(const Vec3 &v)
Definition g3d_Vector3-inl.h:235
Vec3 & Mul(const Mtx34 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:192
Vec3 & Project(const Mtx44 &m, const Vec3 &v)
Definition g3d_Vector3-inl.h:366
float Normalize(const Vec3 &v)
Definition g3d_Vector3-inl.h:286
Vec3 & Mul(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:130
Vec3 & Sub(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:115
Vec3 & Set(const float *a)
Definition g3d_Vector3-inl.h:36
static Vec3 Make(float x, float y, float z)
Definition g3d_Vector3-inl.h:9
Vec3 & Div(const Vec3 &lhs, float rhs)
Definition g3d_Vector3-inl.h:184
static float Dot(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:263
Vec3 & Mul(const Vec3 &lhs, float rhs)
Definition g3d_Vector3-inl.h:154
Vec3 & Zero()
Definition g3d_Vector3-inl.h:57
static const Vec3 * Cast(const float *a)
Definition g3d_Vector3-inl.h:21
Vec3 & Transform(const Mtx34 &m, const Vec3 &v)
Definition g3d_Vector3-inl.h:301
static float LengthSq(const Vec3 &v)
Definition g3d_Vector3-inl.h:241
Vec3 & Neg(const Vec3 &v)
Definition g3d_Vector3-inl.h:67
Vec3 & Rotate(const Quat &q, const Vec3 &v)
Definition g3d_Vector3-inl.h:351
static float Distance(const Vec3 &lhs, const Vec3 &rhs)
Definition g3d_Vector3-inl.h:247
#define NW_G3D_ASSERT_NOT_NULL(exp)
Definition g3d_assert.h:20
#define NW_G3D_MATH_INLINE
Definition g3d_defs.h:69
Definition g3d_MathCommon.h:6
Definition g3d_GfxManage.cpp:10
float a[DIM]
Definition g3d_Vector3.h:22
@ DIM
Definition g3d_Vector3.h:14