Code_TYMPAN  4.4.0
Industrial site acoustic simulation
NxVec3.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #include "NxVec3.h"
17 NxVec3::NxVec3(NxReal v) : x(v), y(v), z(v) {}
18 
19 NxVec3::NxVec3(NxReal _x, NxReal _y, NxReal _z) : x(_x), y(_y), z(_z) {}
20 
21 NxVec3::NxVec3(const NxReal v[]) : x(v[0]), y(v[1]), z(v[2]) {}
22 NxVec3::NxVec3(const NxReal v[], bool normalized) : x(v[0]), y(v[1]), z(v[2])
23 {
24  if (normalized)
25  {
26  this->normalize();
27  }
28 }
29 
30 NxVec3::NxVec3(const NxVec3& v) : x(v.x), y(v.y), z(v.z) {}
31 NxVec3::NxVec3(const NxVec3& v, bool normalized) : x(v.x), y(v.y), z(v.z)
32 {
33  if (normalized)
34  {
35  this->normalize();
36  }
37 }
38 
40 
42 {
43  x = v.x;
44  y = v.y;
45  z = v.z;
46  return *this;
47 }
48 
49 const NxReal* NxVec3::get() const
50 {
51  return &x;
52 }
53 
55 {
56  return &x;
57 }
58 
59 void NxVec3::setx(const NxReal& d)
60 {
61  x = d;
62 }
63 
64 void NxVec3::sety(const NxReal& d)
65 {
66  y = d;
67 }
68 
69 void NxVec3::setz(const NxReal& d)
70 {
71  z = d;
72 }
73 
74 bool NxVec3::operator==(const NxVec3& v) const
75 {
76  return ((x == v.x) && (y == v.y) && (z == v.z));
77 }
78 
79 bool NxVec3::operator!=(const NxVec3& v) const
80 {
81  return ((x != v.x) || (y != v.y) || (z != v.z));
82 }
83 
84 void NxVec3::set(const NxVec3& v)
85 {
86  x = v.x;
87  y = v.y;
88  z = v.z;
89 }
90 
92 {
93  x = -v.x;
94  y = -v.y;
95  z = -v.z;
96 }
97 
99 {
100  x = -x;
101  y = -y;
102  z = -z;
103 }
104 
106 {
107  this->x = x;
108  this->y = y;
109  this->z = z;
110 }
111 
113 {
114  x = v;
115  y = v;
116  z = v;
117 }
118 
120 {
121  x = y = z = 0.0;
122 }
123 
124 void NxVec3::add(const NxVec3& a, const NxVec3& b)
125 {
126  x = a.x + b.x;
127  y = a.y + b.y;
128  z = a.z + b.z;
129 }
130 
131 void NxVec3::subtract(const NxVec3& a, const NxVec3& b)
132 {
133  x = a.x - b.x;
134  y = a.y - b.y;
135  z = a.z - b.z;
136 }
137 
138 void NxVec3::arrayMultiply(const NxVec3& a, const NxVec3& b)
139 {
140  x = a.x * b.x;
141  y = a.y * b.y;
142  z = a.z * b.z;
143 }
144 
146 {
147  x = a.x * s;
148  y = a.y * s;
149  z = a.z * s;
150 }
151 
152 void NxVec3::multiplyAdd(NxReal s, const NxVec3& a, const NxVec3& b)
153 {
154  x = s * a.x + b.x;
155  y = s * a.y + b.y;
156  z = s * a.z + b.z;
157 }
158 
160 {
161  NxReal m = magnitude();
162  if (m)
163  {
164  const NxReal il = NxReal(1.0) / m;
165  x *= il;
166  y *= il;
167  z *= il;
168  }
169  return m;
170 }
171 
173 {
174  NxReal m = magnitude();
175  if (m)
176  {
177  NxReal newLength = length / m;
178  x *= newLength;
179  y *= newLength;
180  z *= newLength;
181  }
182 }
183 
184 NxReal NxVec3::dot(const NxVec3& v) const
185 {
186  return x * v.x + y * v.y + z * v.z;
187 }
188 
189 bool NxVec3::sameDirection(const NxVec3& v) const
190 {
191  return x * v.x + y * v.y + z * v.z >= 0.0f;
192 }
193 
195 {
196  return sqrt(x * x + y * y + z * z);
197 }
198 
200 {
201  NxReal dx = x - v.x;
202  NxReal dy = y - v.y;
203  NxReal dz = z - v.z;
204  return sqrt(dx * dx + dy * dy + dz * dz);
205 }
206 
207 void NxVec3::cross(const NxVec3& left, const NxVec3& right) // prefered version, w/o temp object.
208 {
209  NxReal a = (left.y * right.z) - (left.z * right.y);
210  NxReal b = (left.z * right.x) - (left.x * right.z);
211  NxReal c = (left.x * right.y) - (left.y * right.x);
212 
213  x = a;
214  y = b;
215  z = c;
216 }
217 
218 void NxVec3::cross(const NxVec3& left, const NxVec3& right,
219  bool normalized) // prefered version, w/o temp object.
220 {
221  NxReal a = (left.y * right.z) - (left.z * right.y);
222  NxReal b = (left.z * right.x) - (left.x * right.z);
223  NxReal c = (left.x * right.y) - (left.y * right.x);
224 
225  x = a;
226  y = b;
227  z = c;
228 
229  if (normalized)
230  {
231  this->normalize();
232  }
233 }
234 
236 {
237  return NxVec3(-x, -y, -z);
238 }
239 
241 {
242  return NxVec3(x + v.x, y + v.y, z + v.z);
243 }
244 
246 {
247  return NxVec3(x - v.x, y - v.y, z - v.z);
248 }
249 
251 {
252  return NxVec3(x * f, y * f, z * f);
253 }
254 
256 {
257  f = NxReal(1.0) / f;
258  return NxVec3(x * f, y * f, z * f);
259 }
260 
262 {
263  x += v.x;
264  y += v.y;
265  z += v.z;
266  return *this;
267 }
268 
270 {
271  x -= v.x;
272  y -= v.y;
273  z -= v.z;
274  return *this;
275 }
276 
278 {
279  x *= f;
280  y *= f;
281  z *= f;
282  return *this;
283 }
284 
286 {
287  f = 1.0f / f;
288  x *= f;
289  y *= f;
290  z *= f;
291  return *this;
292 }
293 
294 NxVec3 NxVec3::cross(const NxVec3& v) const
295 {
296  NxVec3 temp;
297  temp.cross(*this, v);
298  return temp;
299 }
300 
302 {
303  NxVec3 temp;
304  temp.cross(*this, v);
305  return temp;
306 }
307 
309 {
310  return x * v.x + y * v.y + z * v.z;
311 }
312 
314 {
315  return NxVec3(f * v.x, f * v.y, f * v.z);
316 }
317 NxReal s, c, xy, ys, xz, yz, xs, zs, un_c;
318 void NxVec3::rotate(NxReal angle, const NxVec3& axe)
319 {
320  s = sin(angle);
321  c = cos(angle);
322  xy = axe.x * axe.y;
323  ys = axe.y * s;
324  xz = axe.x * axe.z;
325  yz = axe.y * axe.z;
326  xs = axe.x * s;
327  zs = axe.z * s;
328  un_c = 1 - c;
329  NxReal dst_x = (axe.x * axe.x * un_c + c) * x + (xy * un_c - zs) * y + (xz * un_c + ys) * z,
330  dst_y = (xy * un_c + zs) * x + (axe.y * axe.y * un_c + c) * y + (yz * un_c - xs) * z,
331  dst_z = (xz * un_c - ys) * x + (yz * un_c + xs) * y + (axe.z * axe.z * un_c + c) * z;
332  x = dst_x;
333  y = dst_y;
334  z = dst_z;
335 
336  /*
337  from http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/rotate.html
338  ( xx(1-c)+c xy(1-c)-zs xz(1-c)+ys)
339  | yx(1-c)+zs yy(1-c)+c yz(1-c)-xs|
340  | xz(1-c)-ys yz(1-c)+xs zz(1-c)+c |
341  Where c = cos(angle), s = sine(angle), and ||( x,y,z )|| = 1
342  */
343 }
344 
345 void NxVec3::rotate(NxReal sin_angle, NxReal cos_angle, const NxVec3& axe)
346 {
347  s = sin_angle;
348  c = cos_angle;
349  xy = axe.x * axe.y;
350  ys = axe.y * s;
351  xz = axe.x * axe.z;
352  yz = axe.y * axe.z;
353  xs = axe.x * s;
354  zs = axe.z * s;
355  un_c = 1 - c;
356  NxReal dst_x = (axe.x * axe.x * un_c + c) * x + (xy * un_c - zs) * y + (xz * un_c + ys) * z,
357  dst_y = (xy * un_c + zs) * x + (axe.y * axe.y * un_c + c) * y + (yz * un_c - xs) * z,
358  dst_z = (xz * un_c - ys) * x + (yz * un_c + xs) * y + (axe.z * axe.z * un_c + c) * z;
359  x = dst_x;
360  y = dst_y;
361  z = dst_z;
362 
363  /*
364  from http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/rotate.html
365  ( xx(1-c)+c xy(1-c)-zs xz(1-c)+ys)
366  | yx(1-c)+zs yy(1-c)+c yz(1-c)-xs|
367  | xz(1-c)-ys yz(1-c)+xs zz(1-c)+c |
368  Where c = cos(angle), s = sine(angle), and ||( x,y,z )|| = 1
369  */
370 }
371 
372 void NxVec3::rotate(const NxVec3& axe)
373 {
374  NxReal dst_x = (axe.x * axe.x * un_c + c) * x + (xy * un_c - zs) * y + (xz * un_c + ys) * z;
375  NxReal dst_y = (xy * un_c + zs) * x + (axe.y * axe.y * un_c + c) * y + (yz * un_c - xs) * z;
376  NxReal dst_z = (xz * un_c - ys) * x + (yz * un_c + xs) * y + (axe.z * axe.z * un_c + c) * z;
377  x = dst_x;
378  y = dst_y;
379  z = dst_z;
380 
381  /*
382  from http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/rotate.html
383  ( xx(1-c)+c xy(1-c)-zs xz(1-c)+ys)
384  | yx(1-c)+zs yy(1-c)+c yz(1-c)-xs|
385  | xz(1-c)-ys yz(1-c)+xs zz(1-c)+c |
386  Where c = cos(angle), s = sine(angle), and ||( x,y,z )|| = 1
387  */
388 }
NxReal zs
Definition: NxVec3.cpp:317
NxReal ys
Definition: NxVec3.cpp:317
NxReal xs
Definition: NxVec3.cpp:317
NxReal xy
Definition: NxVec3.cpp:317
NxReal yz
Definition: NxVec3.cpp:317
NxReal xz
Definition: NxVec3.cpp:317
NxReal s
Definition: NxVec3.cpp:317
NxReal un_c
Definition: NxVec3.cpp:317
NxVec3 operator*(NxReal f, const NxVec3 &v)
Definition: NxVec3.cpp:313
NxReal c
Definition: NxVec3.cpp:317
#define NxReal
Definition: NxVec3.h:20
Definition: NxVec3.h:23
NxReal operator|(const NxVec3 &v) const
Definition: NxVec3.cpp:308
NxVec3 & operator+=(const NxVec3 &v)
Definition: NxVec3.cpp:261
void subtract(const NxVec3 &a, const NxVec3 &b)
Definition: NxVec3.cpp:131
const NxVec3 & operator=(const NxVec3 &)
Definition: NxVec3.cpp:41
NxVec3 operator+(const NxVec3 &v) const
Definition: NxVec3.cpp:240
void setMagnitude(NxReal)
Definition: NxVec3.cpp:172
bool sameDirection(const NxVec3 &) const
Definition: NxVec3.cpp:189
NxVec3 operator/(NxReal f) const
Definition: NxVec3.cpp:255
NxVec3()
Definition: NxVec3.cpp:39
void setx(const NxReal &d)
Definition: NxVec3.cpp:59
NxReal z
Definition: NxVec3.h:80
NxReal magnitude() const
Definition: NxVec3.cpp:194
void cross(const NxVec3 &left, const NxVec3 &right)
Definition: NxVec3.cpp:207
NxVec3 & operator*=(NxReal f)
Definition: NxVec3.cpp:277
void sety(const NxReal &d)
Definition: NxVec3.cpp:64
bool operator!=(const NxVec3 &) const
Definition: NxVec3.cpp:79
NxVec3 & operator-=(const NxVec3 &v)
Definition: NxVec3.cpp:269
NxVec3 operator^(const NxVec3 &v) const
Definition: NxVec3.cpp:301
void multiplyAdd(NxReal s, const NxVec3 &a, const NxVec3 &b)
Definition: NxVec3.cpp:152
NxVec3 & operator/=(NxReal f)
Definition: NxVec3.cpp:285
NxVec3 operator*(NxReal f) const
Definition: NxVec3.cpp:250
void rotate(NxReal angle, const NxVec3 &axe)
Definition: NxVec3.cpp:318
void add(const NxVec3 &a, const NxVec3 &b)
Definition: NxVec3.cpp:124
bool operator==(const NxVec3 &) const
Definition: NxVec3.cpp:74
void setNegative()
Definition: NxVec3.cpp:98
void setz(const NxReal &d)
Definition: NxVec3.cpp:69
NxReal normalize()
Definition: NxVec3.cpp:159
NxReal y
Definition: NxVec3.h:80
void zero()
Definition: NxVec3.cpp:119
void arrayMultiply(const NxVec3 &a, const NxVec3 &b)
Definition: NxVec3.cpp:138
const NxReal * get() const
Definition: NxVec3.cpp:49
NxReal distance(const NxVec3 &) const
Definition: NxVec3.cpp:199
void multiply(NxReal s, const NxVec3 &a)
Definition: NxVec3.cpp:145
NxReal dot(const NxVec3 &other) const
Definition: NxVec3.cpp:184
NxVec3 operator-() const
Definition: NxVec3.cpp:235
NxReal x
Definition: NxVec3.h:80
void set(const NxVec3 &)
Definition: NxVec3.cpp:84