NW4F G3d
Loading...
Searching...
No Matches
g3d_Vector2-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
6Vec2 Vec2::Make(float x, float y)
7{
8 return Vec2().Set(x, y);
9}
10
12Vec2* Vec2::Cast(float* a)
13{
14 return reinterpret_cast<Vec2*>(a);
15}
16
18const Vec2* Vec2::Cast(const float* a)
19{
20 return reinterpret_cast<const Vec2*>(a);
21}
22
24Vec2& Vec2::Set(float x, float y)
25{
26 this->x = x;
27 this->y = y;
28 return *this;
29}
30
32Vec2& Vec2::Set(const float* a)
33{
35 for (int i = 0; i < DIM; ++i)
36 {
37 this->a[i] = a[i];
38 }
39 return *this;
40}
41
43Vec2& Vec2::Set(const Vec2& v)
44{
45 for (int i = 0; i < DIM; ++i)
46 {
47 a[i] = v.a[i];
48 }
49 return *this;
50}
51
54{
55 for (int i = 0; i < DIM; ++i)
56 {
57 a[i] = 0.0f;
58 }
59 return *this;
60}
61
63Vec2& Vec2::Neg(const Vec2& v)
64{
65#if defined( __ghs__ )
66 ps[0] = __PS_NEG(v.ps[0]);
67#else
68 for (int i = 0; i < DIM; ++i)
69 {
70 a[i] = -v.a[i];
71 }
72#endif
73 return *this;
74}
75
77Vec2& Vec2::Rcp(const Vec2& v)
78{
79#if defined( __ghs__ )
80 ps[0] = Math::Rcp(v.ps[0]);
81#else
82 for (int i = 0; i < DIM; ++i)
83 {
84 a[i] = Math::Rcp(v.a[i]);
85 }
86#endif
87 return *this;
88}
89
90
91//--------------------------------------------------------------------------------------------------
92
94Vec2& Vec2::Add(const Vec2& lhs, const Vec2& rhs)
95{
96#if defined( __ghs__ )
97 ps[0] = __PS_ADD(lhs.ps[0], rhs.ps[0]);
98#else
99 for (int i = 0; i < DIM; ++i)
100 {
101 a[i] = lhs.a[i] + rhs.a[i];
102 }
103#endif
104 return *this;
105}
106
108Vec2& Vec2::Sub(const Vec2& lhs, const Vec2& rhs)
109{
110#if defined( __ghs__ )
111 ps[0] = __PS_SUB(lhs.ps[0], rhs.ps[0]);
112#else
113 for (int i = 0; i < DIM; ++i)
114 {
115 a[i] = lhs.a[i] - rhs.a[i];
116 }
117#endif
118 return *this;
119}
120
122Vec2& Vec2::Mul(const Vec2& lhs, const Vec2& rhs)
123{
124#if defined( __ghs__ )
125 ps[0] = __PS_MUL(lhs.ps[0], rhs.ps[0]);
126#else
127 for (int i = 0; i < DIM; ++i)
128 {
129 a[i] = lhs.a[i] * rhs.a[i];
130 }
131#endif
132 return *this;
133}
134
136Vec2& Vec2::Div(const Vec2& lhs, const Vec2& rhs)
137{
138 Vec2 rcp;
139 rcp.Rcp(rhs);
140 Mul(lhs, rhs);
141 return *this;
142}
143
145Vec2& Vec2::Mul(const Vec2& lhs, float rhs)
146{
147#if defined( __ghs__ )
148 ps[0] = __PS_MULS0F(lhs.ps[0], rhs);
149#else
150 for (int i = 0; i < DIM; ++i)
151 {
152 a[i] = lhs.a[i] * rhs;
153 }
154#endif
155 return *this;
156}
157
159Vec2& Vec2::Div(const Vec2& lhs, float rhs)
160{
161 float rcp = Math::Rcp(rhs);
162 this->Mul(lhs, rcp);
163 return *this;
164}
165
166//--------------------------------------------------------------------------------------------------
167
169float Vec2::Length(const Vec2& v)
170{
171 return Math::Sqrt(LengthSq(v));
172}
173
175float Vec2::LengthSq(const Vec2& v)
176{
177 return Dot(v, v);
178}
179
181float Vec2::Distance(const Vec2& lhs, const Vec2& rhs)
182{
183 Vec2 vec;
184 vec.Sub(lhs, rhs);
185 return Length(vec);
186}
187
189float Vec2::DistanceSq(const Vec2& lhs, const Vec2& rhs)
190{
191 Vec2 vec;
192 vec.Sub(lhs, rhs);
193 return LengthSq(vec);
194}
195
197float Vec2::Dot(const Vec2& lhs, const Vec2& rhs)
198{
199#if defined( __ghs__ )
200 f32x2 ps;
201 ps = __PS_MUL(lhs.ps[0], rhs.ps[0]);
202 ps = __PS_SUM0(ps, ps, ps);
203 return ps[0];
204#else
205 return lhs.x * rhs.x + lhs.y * rhs.y;
206#endif
207}
208
210float Vec2::Normalize(const Vec2& v)
211{
212 float lengthSq = LengthSq(v);
213 if (lengthSq > 0.0f)
214 {
215 float rcp = Math::RSqrt(lengthSq);
216 Mul(v, rcp);
217 return lengthSq * rcp;
218 }
219 return 0.0f;
220}
221
222
223} } } // 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