NW4F G3d
Loading...
Searching...
No Matches
g3d_Vector4-inl.h
Go to the documentation of this file.
1#include <nw/g3d/math/g3d_MathCommon.h>
2
3namespace nw { namespace g3d { namespace math {
4
6Vec4 Vec4::Make(float x, float y, float z, float w)
7{
8 return Vec4().Set(x, y, z, w);
9}
10
12Vec4* Vec4::Cast(float* a)
13{
14 return reinterpret_cast<Vec4*>(a);
15}
16
18const Vec4* Vec4::Cast(const float* a)
19{
20 return reinterpret_cast<const Vec4*>(a);
21}
22
24Vec4& Vec4::Set(float x, float y, float z, float w)
25{
26 this->x = x;
27 this->y = y;
28 this->z = z;
29 this->w = w;
30 return *this;
31}
32
34Vec4& Vec4::Set(const float* a)
35{
37 for (int i = 0; i < DIM; ++i)
38 {
39 this->a[i] = a[i];
40 }
41 return *this;
42}
43
45Vec4& Vec4::Set(const Vec4& v)
46{
47 for (int i = 0; i < DIM; ++i)
48 {
49 a[i] = v.a[i];
50 }
51 return *this;
52}
53
56{
57 for (int i = 0; i < DIM; ++i)
58 {
59 a[i] = 0.0f;
60 }
61 return *this;
62}
63
65Vec4& Vec4::Neg(const Vec4& v)
66{
67#if defined( __ghs__ )
68 ps[0] = __PS_NEG(v.ps[0]);
69 ps[1] = __PS_NEG(v.ps[1]);
70#else
71 for (int i = 0; i < DIM; ++i)
72 {
73 a[i] = -v.a[i];
74 }
75#endif
76 return *this;
77}
78
80Vec4& Vec4::Rcp(const Vec4& v)
81{
82#if defined( __ghs__ )
83 ps[0] = Math::Rcp(v.ps[0]);
84 ps[1] = Math::Rcp(v.ps[1]);
85#else
86 for (int i = 0; i < DIM; ++i)
87 {
88 a[i] = Math::Rcp(v.a[i]);
89 }
90#endif
91 return *this;
92}
93
94//--------------------------------------------------------------------------------------------------
95
97Vec4& Vec4::Add(const Vec4& lhs, const Vec4& rhs)
98{
99#if defined( __ghs__ )
100 ps[0] = __PS_ADD(lhs.ps[0], rhs.ps[0]);
101 ps[1] = __PS_ADD(lhs.ps[1], rhs.ps[1]);
102#else
103 for (int i = 0; i < DIM; ++i)
104 {
105 a[i] = lhs.a[i] + rhs.a[i];
106 }
107#endif
108 return *this;
109}
110
112Vec4& Vec4::Sub(const Vec4& lhs, const Vec4& rhs)
113{
114#if defined( __ghs__ )
115 ps[0] = __PS_SUB(lhs.ps[0], rhs.ps[0]);
116 ps[1] = __PS_SUB(lhs.ps[1], rhs.ps[1]);
117#else
118 for (int i = 0; i < DIM; ++i)
119 {
120 a[i] = lhs.a[i] - rhs.a[i];
121 }
122#endif
123 return *this;
124}
125
127Vec4& Vec4::Mul(const Vec4& lhs, const Vec4& rhs)
128{
129#if defined( __ghs__ )
130 ps[0] = __PS_MUL(lhs.ps[0], rhs.ps[0]);
131 ps[1] = __PS_MUL(lhs.ps[1], rhs.ps[1]);
132#else
133 for (int i = 0; i < DIM; ++i)
134 {
135 a[i] = lhs.a[i] * rhs.a[i];
136 }
137#endif
138 return *this;
139}
140
142Vec4& Vec4::Div(const Vec4& lhs, const Vec4& rhs)
143{
144 Vec4 rcp;
145 rcp.Rcp(rhs);
146 Mul(lhs, rhs);
147 return *this;
148}
149
151Vec4& Vec4::Mul(const Vec4& lhs, float rhs)
152{
153#if defined( __ghs__ )
154 ps[0] = __PS_MULS0F(lhs.ps[0], rhs);
155 ps[1] = __PS_MULS0F(lhs.ps[1], rhs);
156#else
157 for (int i = 0; i < DIM; ++i)
158 {
159 a[i] = lhs.a[i] * rhs;
160 }
161#endif
162 return *this;
163}
164
166Vec4& Vec4::Div(const Vec4& lhs, float rhs)
167{
168 float rcp = Math::Rcp(rhs);
169 this->Mul(lhs, rcp);
170 return *this;
171}
172
173//--------------------------------------------------------------------------------------------------
174
176float Vec4::Length(const Vec4& v)
177{
178 return Math::Sqrt(LengthSq(v));
179}
180
182float Vec4::LengthSq(const Vec4& v)
183{
184 return Dot(v, v);
185}
186
188float Vec4::Distance(const Vec4& lhs, const Vec4& rhs)
189{
190 Vec4 vec;
191 vec.Sub(lhs, rhs);
192 return Length(vec);
193}
194
196float Vec4::DistanceSq(const Vec4& lhs, const Vec4& rhs)
197{
198 Vec4 vec;
199 vec.Sub(lhs, rhs);
200 return LengthSq(vec);
201}
202
204float Vec4::Dot(const Vec4& lhs, const Vec4& rhs)
205{
206#if defined( __ghs__ )
207 f32x2 ps = __PS_MUL(lhs.ps[0], rhs.ps[0]);
208 ps = __PS_MADD(lhs.ps[1], rhs.ps[1], ps);
209 ps = __PS_SUM0(ps, ps, ps);
210 return ps[0];
211#else
212 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
213#endif
214}
215
217float Vec4::Normalize(const Vec4& v)
218{
219 float lengthSq = v.LengthSq(v);
220 if (lengthSq > 0.0f)
221 {
222 float rcp = Math::RSqrt(lengthSq);
223 Mul(v, rcp);
224 return lengthSq * rcp;
225 }
226 return 0.0f;
227}
228
229} } } // namespace nw::g3d::math
#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