Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
3DVector.inl
1#pragma once
2
3BEGIN_MOOTOOLS_NAMESPACE
4
5template<class TYPE> inline C3DTVector<TYPE>::C3DTVector()
6{
7 x = (TYPE)0.0f;
8 y = (TYPE)0.0f;
9 z = (TYPE)0.0f;
10}
11
12template <class TYPE> inline C3DTVector<TYPE>::C3DTVector(TYPE *val)
13{
14 x = val[0];
15 y = val[1];
16 z = val[2];
17}
18
19template<class TYPE> inline C3DTVector<TYPE>::C3DTVector(float refx, float refy, float refz)
20{
21 x = (TYPE)refx;
22 y = (TYPE)refy;
23 z = (TYPE)refz;
24}
25
26template<class TYPE> inline C3DTVector<TYPE>::C3DTVector(double refx, double refy, double refz)
27{
28 x = (TYPE)refx;
29 y = (TYPE)refy;
30 z = (TYPE)refz;
31}
32
33template<class TYPE> inline C3DTVector<TYPE>::C3DTVector(const C3DTPoint<TYPE>& point1, const C3DTPoint<TYPE>& point2)
34{
35 x = point2.x - point1.x;
36 y = point2.y - point1.y;
37 z = point2.z - point1.z;
38}
39
40template<class TYPE> inline C3DTVector<TYPE>::C3DTVector(const C3DTVector& vector)
41{
42 x = vector.x;
43 y = vector.y;
44 z = vector.z;
45}
46
47template<class TYPE> inline C3DTVector<TYPE>::C3DTVector(const C3DTPoint<TYPE>& point)
48{
49 x = point.x;
50 y = point.y;
51 z = point.z;
53
54template<class TYPE> inline void C3DTVector<TYPE>::Init(float refx, float refy, float refz)
56 x = (TYPE)refx;
57 y = (TYPE)refy;
58 z = (TYPE)refz;
61template<class TYPE> inline void C3DTVector<TYPE>::Init(double refx, double refy, double refz)
63 x = (TYPE)refx;
64 y = (TYPE)refy;
65 z = (TYPE)refz;
68template<class TYPE> inline void C3DTVector<TYPE>::InitFromPoint(const C3DTPoint<TYPE>& point)
70 x = point.x;
71 y = point.y;
72 z = point.z;
73};
74
75template <class TYPE>
76template <class TYPE2> inline void C3DTVector<TYPE>::SetValues(const TYPE2 *val)
77{
78 x = (TYPE)val[0];
79 y = (TYPE)val[1];
80 z = (TYPE)val[2];
81}
82
83template<class TYPE> inline void C3DTVector<TYPE>::InitFromPoints(const C3DTPoint<TYPE>& point1, const C3DTPoint<TYPE>& point2) // return vector point1to2 which is point2-point1
84{
85 x = point2.x - point1.x;
86 y = point2.y - point1.y;
87 z = point2.z - point1.z;
88};
89
90template<class TYPE> inline void C3DTVector<TYPE>::SelfNegate()
91{
92 x = -x;
93 y = -y;
94 z = -z;
95};
96
97template<class TYPE> inline void C3DTVector<TYPE>::SelfInvert()
99 x = (TYPE)(1.0 / x);
100 y = (TYPE)(1.0 / y);
101 z = (TYPE)(1.0 / z);
104template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::Negate() const
105{
106 return C3DTVector(-x, -y, -z);
108
110{
112 neg.x = (x == -0.0) ? 0.0 : -x;
113 neg.y = (y == -0.0) ? 0.0 : -y;
114 neg.z = (z == -0.0) ? 0.0 : -z;
115 return neg;
116}
117
118template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::Invert() const
119{
120 return C3DTVector(1.0 / x, 1.0 / y, 1.0 / z);
121}
122
124{
126 inv.x = (TYPE)((x == -0.0) ? HUGE_VAL : 1.0 / x);
127 inv.y = (TYPE)((y == -0.0) ? HUGE_VAL : 1.0 / y);
128 inv.z = (TYPE)((z == -0.0) ? HUGE_VAL : 1.0 / z);
129 return inv;
130}
131
132template<class TYPE> inline C3DTVector<TYPE>& C3DTVector<TYPE>::operator=(const C3DTVector& vect)
133{
134 CPt::operator=(vect);
135 x = vect.x;
136 y = vect.y;
137 z = vect.z;
138 return *this;
139};
140
142{
143 x = vect.x;
144 y = vect.y;
145 z = vect.z;
146 return *this;
147}
148
149template<class TYPE> inline bool C3DTVector<TYPE>::operator==(const C3DTVector& src) const
150{
151 if (x != src.x || y != src.y || z != src.z)
152 return false;
153
154 return true;
155}
156
157template<class TYPE> inline bool C3DTVector<TYPE>::operator!=(const C3DTVector& src) const
158{
159 if (x != src.x || y != src.y || z != src.z)
160 return true;
161
162 return false;
163}
164
165template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::operator^(const C3DTVector& vect) const
166{
167 return C3DTVector(double(y)*vect.z - double(z)*vect.y,
168 double(z)*vect.x - double(x)*vect.z,
169 double(x)*vect.y - double(y)*vect.x);
170};
171
172template<class TYPE> inline void C3DTVector<TYPE>::operator-=(const C3DTVector& vect)
173{
174 x -= vect.x;
175 y -= vect.y;
176 z -= vect.z;
177};
178
179template <class TYPE> inline void C3DTVector<TYPE>::SubstractTo(const C3DTVector<TYPE>& vect)
180{
181 x = vect.x - x;
182 y = vect.y - y;
183 z = vect.z - z;
184}
185
186template<class TYPE> inline void C3DTVector<TYPE>::operator+=(const C3DTVector& vect)
187{
188 x += vect.x;
189 y += vect.y;
190 z += vect.z;
191};
192
193template<class TYPE> inline void C3DTVector<TYPE>::operator-=(const C3DTPoint<TYPE>& pt)
194{
195 x -= pt.x;
196 y -= pt.y;
197 z -= pt.z;
198};
199
200template<class TYPE> inline void C3DTVector<TYPE>::operator+=(const C3DTPoint<TYPE>& pt)
201{
202 x += pt.x;
203 y += pt.y;
204 z += pt.z;
205};
206
207template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::operator+(const C3DTVector& vect) const
208{
209 return C3DTVector(x+vect.x, y+vect.y, z+vect.z);
210};
211
212template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::operator-(const C3DTVector& vect) const
213{
214 return C3DTVector(x-vect.x, y-vect.y, z-vect.z);
215};
216
217template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::operator-() const
218{
219 return C3DTVector(-x, -y, -z);
220};
221
222template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::operator*(double weight) const
223{
224 return C3DTVector(double(x)*weight, double(y)*weight, double(z)*weight);
225}
226
227template<class TYPE> inline double C3DTVector<TYPE>::operator*(const C3DTVector& vect) const
228{
229 return (double(x)*vect.x + double(y)*vect.y + double(z)*vect.z);
230}
231
232template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::operator/(double weight) const
233{
234 return C3DTVector(x/weight, y/weight, z/weight);
235}
236
237template<class TYPE> inline C3DTVector<TYPE>& C3DTVector<TYPE>::operator*=(double weight)
238{
239 x = (TYPE)(x * weight);
240 y = (TYPE)(y * weight);
241 z = (TYPE)(z * weight);
242
243 return *this;
244}
245
246template<class TYPE> inline C3DTVector<TYPE>& C3DTVector<TYPE>::operator/=(double weight)
247{
248 x = (TYPE)(x / weight);
249 y = (TYPE)(y / weight);
250 z = (TYPE)(z / weight);
251
252 return *this;
253}
254
255
256template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::Mult(const C3DTVector& vect) const
257{
258 return (double(x) * vect.x + double(y) * vect.y + double(z) * vect.z);
259}
260
261template<class TYPE> inline C3DTVector<TYPE> C3DTVector<TYPE>::Div(const C3DTVector& vect, bool checkZero) const
262{
263 if (checkZero)
264 {
266 if (vect.x != 0.0)
267 div.x = (TYPE)(x / vect.x);
268 else
269 div.x = x;
270 if (vect.y != 0.0)
271 div.y = (TYPE)(y / vect.y);
272 else
273 div.y = y;
274 if (vect.y != 0.0)
275 div.y = (TYPE)(y / vect.y);
276 else
277 div.z = z;
278 return div;
279 }
280
281 return C3DTVector<TYPE>(x / vect.x, y / vect.y, z / vect.z);
282}
283
284template<class TYPE> inline C3DTVector<TYPE>& C3DTVector<TYPE>::SelfMult(const C3DTVector& vect)
285{
286 x = (TYPE)(x * vect.x);
287 y = (TYPE)(y * vect.y);
288 z = (TYPE)(z * vect.z);
289
290 return *this;
291}
292
293template<class TYPE> inline C3DTVector<TYPE>& C3DTVector<TYPE>::SelfDiv(const C3DTVector& vect, bool checkZero)
294{
295 if (checkZero)
296 {
297 if (vect.x != 0.0)
298 x = (TYPE)(x / vect.x);
299 if (vect.y != 0.0)
300 y = (TYPE)(y / vect.y);
301 if (vect.z != 0.0)
302 z = (TYPE)(z / vect.z);
303 return *this;
304 }
305
306 x = (TYPE)(x / vect.x);
307 y = (TYPE)(y / vect.y);
308 z = (TYPE)(z / vect.z);
309 return *this;
310}
311
312template <class TYPE> inline C3DVectorI C3DTVector<TYPE>::Floor() const
313{
314 return C3DVectorI(floor(x), floor(y), floor(z));
315}
316
317template <class TYPE> inline C3DVectorI C3DTVector<TYPE>::Ceil() const
318{
319 return C3DVectorI(ceil(x), ceil(y), ceil(z));
320}
321
322template <class TYPE> inline void C3DTVector<TYPE>::Offset(TYPE value)
323{
324 x += value;
325 y += value;
326 z += value;
327}
328
329template <class TYPE> inline bool C3DTVector<TYPE>::IsAnormal() const
330{
331 // Do not check fpclassify, and subnormal values
332 if (IsInfinite() || const_cast<C3DTPoint<TYPE> *>(this)->IsNan())
333 return true;
334
335 return false;
336}
337
338template <class TYPE> inline bool C3DTVector<TYPE>::IsInfinite() const
339{
340 if (_finite(x) && _finite(y) && _finite(z))
341 return false;
342
343 return true;
344}
345
346template <class TYPE> inline bool C3DTVector<TYPE>::IsNan(bool resetToZero)
347{
348 if (resetToZero)
349 {
350 bool nan = false;
351 if (_isnan(x))
352 {
353 x = 0.0f;
354 nan = true;
355 }
356
357 if (_isnan(y))
358 {
359 y = 0.0f;
360 nan = true;
361 }
362
363 if (_isnan(z))
364 {
365 z = 0.0f;
366 nan = true;
367 }
368
369 return nan;
370 }
371
372 if (_isnan(x) || _isnan(y) || _isnan(z))
373 return true;
374
375 return false;
376}
377
378// AB^AC = (AB.y*AC.z-AB.z-AC.y,
379// AB.z*AC.x-AB.x-AC.z,
380// AB.x*AC.y-AB.y*AC.x);
381// = |AB| * |AC| * sin(AB,AC) * N; (N est la normale unitee)
382// = 2 * surface * N;
383template<class TYPE> inline double Area(const C3DTVector<TYPE>& vect1, const C3DTVector<TYPE>& vect2)
384{
385 return ((vect1^vect2).Length()/2.0);
386}
387
388
389template<class TYPE> inline double C3DTVector<TYPE>::Length() const
390{
391 return sqrt(double(x)*x + double(y)*y + double(z)*z);
392}
393
394template<class TYPE> inline double C3DTVector<TYPE>::Length2() const
395{
396 return (double(x)*x + double(y)*y + double(z)*z);
397}
398
399// A.B = |A||B|cos(angle)
400template<class TYPE> inline double Cos(const C3DTVector<TYPE>& vect1, const C3DTVector<TYPE>& vect2)
401{
402 double prodlength = sqrt(vect1.Length2() * vect2.Length2());
403 if (prodlength < PRECISION_LIMIT)
404 {
405#ifdef _DEBUG
406 XTRACE(trace3DModule, 1, "Invalid Cosinus\n");
407#endif
408 return 0.0;
409 }
410
411 double result = (vect1*vect2) / prodlength;
412 if (result > 1.0)
413 result = 1.0;
414 else if (result < -1.0)
415 result = -1.0;
416
417 return result;
418}
419
420// Normalize a vector
421template<class TYPE> inline bool C3DTVector<TYPE>::Normalize()
422{
423 double length = Length();
424 if (length > PRECISION_LIMIT)
425 {
426 x = (TYPE)(x/length);
427 y = (TYPE)(y/length);
428 z = (TYPE)(z/length);
429
430 return true;
431 }
432#ifdef _DEBUG
433 else
434 MOOTOOLS_PRIVATE_TRACE("Warning null vector in 3DVector.Cpp\n");
435#endif
436
437 return false;
438}
439template<class TYPE> inline bool C3DTVector<TYPE>::IsNull(double precision) const
440{
441 if (fabs(x) < precision && fabs(y) < precision && fabs(z) < precision)
442 return true;
443
444 return false;
445}
446
447template<class TYPE> inline bool C3DTVector<TYPE>::IsNormalized(double precision) const
448{
449 if (fabs(Length2()-1.0) < precision)
450 return true;
451
452 return false;
453}
454
455template<class TYPE> inline bool C3DTVector<TYPE>::IsUnit(double precision) const
456{
457 // Scale might be negative or < 1.0
458 if (fabs(fabs(x) - 1.0) > precision)
459 return false;
460 if (fabs(fabs(y) - 1.0) > precision)
461 return false;
462 if (fabs(fabs(z) - 1.0) > precision)
463 return false;
464
465 return true;
466}
467
468END_MOOTOOLS_NAMESPACE
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
bool IsInfinite() const
Check if one of the coordinate has inf value.
Definition 3DPoint.inl:186
bool IsNan(bool resetToZero=false)
Check if one of the coordinate has nan value. resetToZero nan = true, reset coordinates to zero if an...
Definition 3DPoint.inl:194
Definition 3DVector.h:20
bool IsInfinite() const
Check if one of the coordinate has inf value.
Definition 3DVector.inl:338
C3DTVector Negate() const
/ operator - : return negate of each component (-x, -y, -z)
Definition 3DVector.inl:104
bool Normalize()
Return false if vector length is null.
Definition 3DVector.inl:421
bool IsNull(double precision=PRECISION_LIMIT) const
Compare vector to (0.0, 0.0, 0.0). Return true if equal, false otherwise.
Definition 3DVector.inl:439
C3DTVector Mult(const C3DTVector &vector) const
Multiply each component (x, y, z) with vect(x, y, z). This is not the scalar product !
Definition 3DVector.inl:256
bool IsNan(bool resetToZero=false)
Check if one of the coordinate has nan value. resetToZero nan = true, reset coordinates to zero if an...
Definition 3DVector.inl:346
C3DTVector Div(const C3DTVector &vector, bool checkZero=true) const
Divide each component (x, y, z) with vect(x, y, z). This is not the scalar product !
Definition 3DVector.inl:261
C3DTVector & SelfDiv(const C3DTVector &vector, bool checkZero=true)
Self divide each component (x, y, z) with vect(x, y, z). This is not the scalar product !
Definition 3DVector.inl:293
void Offset(TYPE value)
Add value to x, y, z.
Definition 3DVector.inl:322
C3DTVector Invert() const
/ operator / : return invert each component (1/x, 1/y, 1/z)
Definition 3DVector.inl:118
void SetValues(const TYPE2 *values3t)
Set vector from 3 TYPE.
Definition 3DVector.inl:76
void SelfInvert()
/ operator / : invert each component (1/x, 1/y, 1/z)
Definition 3DVector.inl:97
void SelfNegate()
/ operator - : negate each component (-x, -y, -z)
Definition 3DVector.inl:90
C3DTVector NegateCheckZeroSign() const
/ operator - : return negate of each component (-x, -y, -z). If a component is 0, it return a +0,...
Definition 3DVector.inl:109
bool IsNormalized(double precision=FLOAT_EPSILON) const
Check if vector normalized. Return true if normalized, false otherwise.
Definition 3DVector.inl:447
bool IsAnormal() const
either infinite or nan. Zero and denormalized value are allowed.
Definition 3DVector.inl:329
C3DTVector & SelfMult(const C3DTVector &vector)
Self multiply each component (x, y, z) with vect(x, y, z). This is not the scalar product !...
Definition 3DVector.inl:284
bool IsUnit(double precision=PRECISION_LIMIT) const
Compare vector to (1.0, 1.0, 1.0). Return true if equal, false otherwise.
Definition 3DVector.inl:455
C3DTVector InvertCheckZeroSign() const
/ operator / : return invert each component (1/x, 1/y, 1/z). If a component is -0,...
Definition 3DVector.inl:123
void SubstractTo(const C3DTVector &vect)
return this = vect-this, instead this = this-vect. This can be used for fast computation (using the s...
Definition 3DVector.inl:179