Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
3DVector.h
1// 3DVector.h: interface for the C3DVector class.
2//
3//////////////////////////////////////////////////////////////////////
4
5#if !defined(AFX_3DVECTOR_H__CA6BC2A2_FAC2_11D1_A0DE_000000000000__INCLUDED_)
6#define AFX_3DVECTOR_H__CA6BC2A2_FAC2_11D1_A0DE_000000000000__INCLUDED_
7
8#ifdef _MSC_VER
9#pragma once
10#endif // _MSC_VER
11
12#include "Point.h"
13#include "Quaternion.h"
14
15BEGIN_MOOTOOLS_NAMESPACE
16
17// C3DVector class is used to compute normal
18// It used with with C3DVectorList : CPointList.
19template<class TYPE> class C3DTVector : public CPt
20{
21public:
22 TYPE x, y, z;
23
24 C3DTVector();
25 C3DTVector(TYPE *val);
26 C3DTVector(float x, float y, float z);
27 C3DTVector(double x, double y, double z);
28 C3DTVector(const C3DTPoint<TYPE>& point1, const C3DTPoint<TYPE>& point2);
29 C3DTVector(const C3DTVector<TYPE>& vector);
32
33 //! @name Access functions
34 //! Get access to the vector x, y, z values
35 //!
36 //! @{
37 TYPE& operator[](int i);
38 const TYPE& operator[](int i) const;
39 TYPE* ValPtr();
40 const TYPE* ValPtr() const;
41
42 //! @}
43
44 unsigned int SizeOf() const;
45
46 void Init(float x = 0.0f, float y = 0.0f, float z = 0.0f);
47 void Init(double x = 0.0, double y = 0.0, double z = 0.0);
48 void InitFromPoint(const C3DTPoint<TYPE>& point);
49 void InitFromPoints(const C3DTPoint<TYPE>& point1, const C3DTPoint<TYPE>& point2);
50 void Swap(unsigned int swapMode);
51 void SubstractTo(const C3DTVector& vect); //!< return this = vect-this, instead this = this-vect. This can be used for fast computation (using the standard operator return a C3DPointT)
52 void Offset(TYPE value); //!< Add value to x, y, z
53
54 // Check value
55 bool IsAnormal() const; //!< either infinite or nan. Zero and denormalized value are allowed.
56 bool IsInfinite() const; //!< Check if one of the coordinate has inf value.
57 bool IsNan(bool resetToZero = false); //!< Check if one of the coordinate has nan value. resetToZero nan = true, reset coordinates to zero if any.
58
59 void SelfNegate(); //!< / operator - : negate each component (-x, -y, -z)
60 void SelfInvert(); //!< / operator / : invert each component (1/x, 1/y, 1/z)
61 C3DTVector& SelfMult(const C3DTVector& vector); //!< Self multiply each component (x, y, z) with vect(x, y, z). This is not the scalar product !. No operation is done on a component if checkZero = true and one component is zero, otherwise division by zero will result in +/-inf
62 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 !
63
64 C3DTVector Mult(const C3DTVector& vector) const; //!< Multiply each component (x, y, z) with vect(x, y, z). This is not the scalar product !
65 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 !
66 C3DTVector Negate() const; //!< / operator - : return negate of each component (-x, -y, -z)
67 C3DTVector NegateCheckZeroSign() const; //!< / operator - : return negate of each component (-x, -y, -z). If a component is 0, it return a +0, never -0.
68 C3DTVector Invert() const; //!< / operator / : return invert each component (1/x, 1/y, 1/z)
69 C3DTVector InvertCheckZeroSign() const; //!< / operator / : return invert each component (1/x, 1/y, 1/z). If a component is -0, it does return +inf, never -inf. Use in some special circumstance, ie C3DTBBox::RayIntersect.
70 C3DTVector& operator=(const C3DTVector& vect);
71 C3DTVector& operator=(const C3DTPoint<TYPE>& pt);
72 bool operator==(const C3DTVector& src) const;
73 bool operator!=(const C3DTVector& src) const;
74 C3DTVector operator^(const C3DTVector& vect) const;
75 C3DTVector operator+(const C3DTVector& vect) const;
76 C3DTVector operator-(const C3DTVector& vect) const;
77 C3DTVector operator-() const;
78 C3DTVector operator*(double weight) const;
79 double operator*(const C3DTVector& vect) const;
80 C3DTVector operator/(double weight) const;
81 void operator-=(const C3DTPoint<TYPE>& pt);
82 void operator+=(const C3DTPoint<TYPE>& pt);
83 void operator-=(const C3DTVector& vect);
84 void operator+=(const C3DTVector& vect);
85 C3DTVector& operator*=(double weight);
86 C3DTVector& operator/=(double weight);
87
88 C3DVectorI Floor() const;
89 C3DVectorI Ceil() const;
90
91 //////////////////////
92 // Mix type conversion (C3DTVector<float> to C3DTVector<double> for example)
93 template <class TYPE2> C3DTVector(const C3DTVector<TYPE2>& vect);
94 template <class TYPE2> C3DTVector(const C3DTPoint<TYPE2>& pt);
95 template <class TYPE2> C3DTVector& operator=(const C3DTVector<TYPE2>& vect);
96 template <class TYPE2> C3DTVector(const C3DTPoint<TYPE2>& pt1, const C3DTPoint<TYPE2>& pt2);
97
98 bool Normalize(); //!< Return false if vector length is null
99 double Length() const;
100 double Length2() const;
101 bool IsNull(double precision = PRECISION_LIMIT) const; //!< Compare vector to (0.0, 0.0, 0.0). Return true if equal, false otherwise
102 bool IsUnit(double precision = PRECISION_LIMIT) const; //!< Compare vector to (1.0, 1.0, 1.0). Return true if equal, false otherwise
103 bool IsNormalized(double precision = FLOAT_EPSILON) const; //!< Check if vector normalized. Return true if normalized, false otherwise
104
105 virtual void To4DPoint(C4DPoint& pt) const;
106 virtual CPt& operator=(const C4DPoint& pt);
107 template <class TYPE2> void SetValues(const TYPE2 *values3t); //!< Set vector from 3 TYPE
108 void GetFloat(float *values3f) const; //!< Get vector and fill 3 floats
109 void SetFloat(const float *values3f); //!< Set vector from 3 floats
110 void GetDouble(double *values3d) const; //!< Get vector and fill 3 doubles
111 void SetDouble(const double *values3d); //!< Set vector from 3 doubles
112
113 virtual CPt& operator=(const CPt& refpoint);
114 virtual void Serialize(CXArchive& ar);
115
116#ifdef _DEBUG
117 void Dump() const;
118 virtual bool IsKindOf(unsigned int classid) const;
119 virtual unsigned int ClassID() const;
120#endif
121};
122
123END_MOOTOOLS_NAMESPACE
124
125#include "3DPoint.h"
126#include "3DVector.inl"
127
128BEGIN_MOOTOOLS_NAMESPACE
129
130// Access operators
131template <class TYPE>
133{
134 return (&x)[i];
135}
136
137template <class TYPE>
138inline const TYPE& C3DTVector<TYPE>::operator[](int i) const
139{
140 return (&x)[i];
141}
142
143template <class TYPE>
145{
146 return(&x);
147}
148
149template <class TYPE>
150inline const TYPE* C3DTVector<TYPE>::ValPtr() const
151{
152 return(&x);
153}
154
155template <class TYPE>
156inline unsigned int C3DTVector<TYPE>::SizeOf() const
157{
158 return static_cast<unsigned int>(sizeof(TYPE) * 3);
159};
160
161//////////////////////
162// Mix type conversion (C3DTVector<float> to C3DTVector<double> for example)
163template <class TYPE>
164template <class TYPE2> C3DTVector<TYPE>::C3DTVector(const C3DTVector<TYPE2>& vect)
165{
166 XASSERT(ClassID() != vect.ClassID()); // (the default operator should be called if same type)
167
168 x = (TYPE)vect.x;
169 y = (TYPE)vect.y;
170 z = (TYPE)vect.z;
171}
172
173template <class TYPE>
174template <class TYPE2> C3DTVector<TYPE>::C3DTVector(const C3DTPoint<TYPE2>& pt)
175{
176 x = (TYPE)pt.x;
177 y = (TYPE)pt.y;
178 z = (TYPE)pt.z;
179}
180
181template <class TYPE>
182template <class TYPE2> C3DTVector<TYPE>::C3DTVector(const C3DTPoint<TYPE2>& point1, const C3DTPoint<TYPE2>& point2)
183{
184 x = (TYPE)(point2.x - point1.x);
185 y = (TYPE)(point2.y - point1.y);
186 z = (TYPE)(point2.z - point1.z);
187}
188
189template <class TYPE>
191{
192 XASSERT(ClassID() != vect.ClassID()); // (the default operator should be called if same type)
193
194 x = (TYPE)vect.x;
195 y = (TYPE)vect.y;
196 z = (TYPE)vect.z;
197
198 CPt::operator=(vect);
199
200 return *this;
201}
202
203/////////////////////////////////////////////////////////////////////
204// Specialization
205//////////////////////////////////////////////////////////////////////
206
207#ifdef _DEBUG
208template<> inline unsigned int C3DTVector<int>::ClassID() const
209{
210 return MAKE_CUSTOM_ID('V', 'E', 'C', 'I');
211}
212
213template<> inline unsigned int C3DTVector<float>::ClassID() const
214{
215 return MAKE_CUSTOM_ID('V', 'E', 'C', 'F');
216}
217
218template<> inline unsigned int C3DTVector<double>::ClassID() const
219{
220 return MAKE_CUSTOM_ID('V', 'E', 'C', 'D');
221}
222
223template<> inline bool C3DTVector<float>::IsKindOf(unsigned int classid) const
224{
225 if (C3DTVector<float>::ClassID() == classid)
226 return true;
227
228 return CPt::IsKindOf(classid);
229}
230
231template<> inline bool C3DTVector<double>::IsKindOf(unsigned int classid) const
232{
233 if (C3DTVector<double>::ClassID() == classid)
234 return true;
235
236 return CPt::IsKindOf(classid);
237}
238
239//template unsigned int C3DTVector<float>::ClassID() const;
240//template unsigned int C3DTVector<double>::ClassID() const;
241//template bool C3DTVector<float>::IsKindOf(unsigned int classid) const;
242//template bool C3DTVector<double>::IsKindOf(unsigned int classid) const;
243#endif
244
245/////////////////////////////////////////////////////////////////////
246// Implementation
247//////////////////////////////////////////////////////////////////////
248template <class TYPE> void C3DTVector<TYPE>::Serialize(CXArchive& ar)
249{
250 CPt::Serialize(ar);
251
252 if (ar.IsStoring())
253 {
254 ar << x;
255 ar << y;
256 ar << z;
257 }
258 else
259 {
260 ar >> x;
261 ar >> y;
262 ar >> z;
263 }
264}
265
266template <class TYPE> void C3DTVector<TYPE>::To4DPoint(C4DPoint& pt) const
267{
268 pt.size = 3;
269 pt.value[0] = x;
270 pt.value[1] = y;
271 pt.value[2] = z;
272}
273
274template <class TYPE> CPt& C3DTVector<TYPE>::operator=(const C4DPoint& pt)
275{
276 XASSERT(pt.size == 3);
277 x = (TYPE)pt.value[0];
278 y = (TYPE)pt.value[1];
279 z = (TYPE)pt.value[2];
280 return *this;
281}
282
283template<class TYPE> void C3DTVector<TYPE>::Swap(unsigned int swapMode)
284{
285 // The order of inversion is important (swap before invert)
286 if (swapMode & SWAP_AXIS_FLAGS)
287 {
288 TYPE tmp;
289 switch (swapMode & SWAP_AXIS_FLAGS)
290 {
291 default:
292 XASSERT(0);
293 break;
294
295 case SWAP_XY:
296 SWAP_VALUES(x, y, tmp);
297 break;
298 case SWAP_XZ:
299 SWAP_VALUES(x, z, tmp);
300 break;
301 case SWAP_YZ:
302 SWAP_VALUES(y, z, tmp);
303 break;
304 }
305 }
306
307 if (swapMode & SWAP_INVERT_FLAGS)
308 {
309 switch (swapMode & SWAP_INVERT_FLAGS)
310 {
311 default:
312 XASSERT(0);
313 break;
314
315 case SWAP_INVERT_X:
316 x = -x;
317 break;
318 case SWAP_INVERT_Y:
319 y = -y;
320 break;
321 case SWAP_INVERT_Z:
322 z = -z;
323 break;
324 }
325 }
326}
327
328template <class TYPE> inline void C3DTVector<TYPE>::SetFloat(const float *values)
329{
330 x = (TYPE)values[0];
331 y = (TYPE)values[1];
332 z = (TYPE)values[2];
333}
334
335template <class TYPE> void C3DTVector<TYPE>::GetFloat(float *values) const
336{
337 values[0] = x;
338 values[1] = y;
339 values[2] = z;
340}
341
342template <class TYPE> void C3DTVector<TYPE>::GetDouble(double *values) const
343{
344 values[0] = x;
345 values[1] = y;
346 values[2] = z;
347}
348
349template <class TYPE> void C3DTVector<TYPE>::SetDouble(const double *values)
350{
351 x = (TYPE)values[0];
352 y = (TYPE)values[1];
353 z = (TYPE)values[2];
354}
355
356template <class TYPE> C3DTVector<TYPE>::C3DTVector(const CQuaternion& quat)
357{
358 x = quat.val[0];
359 y = quat.val[1];
360 z = quat.val[2];
361}
362
363template <class TYPE> CPt& C3DTVector<TYPE>::operator=(const CPt& refpoint)
364{
365 XASSERT(refpoint.IsKindOf(C3DTVector<TYPE>::ClassID()));
366
367 x = ((C3DTVector<TYPE>&)refpoint).x;
368 y = ((C3DTVector<TYPE>&)refpoint).y;
369 z = ((C3DTVector<TYPE>&)refpoint).z;
370
371 return CPt::operator=(refpoint);
372}
373
374template <typename TYPE> class CHashMethods<C3DTVector<TYPE> > // Keep space between > > for Linux C98
375{
376public:
377 static inline bool HashCompare(const C3DTVector<TYPE>& pt1, const C3DTVector<TYPE>& pt2)
378 {
379 if (pt1.x != pt2.x || pt1.y != pt2.y || pt1.z != pt2.z)
380 return false;
381
382 return true;
383 }
384
385 static inline unsigned int HashValue(const C3DTVector<TYPE>& hash)
386 {
387 return CHashMethods<TYPE>::HashValue(hash.x + hash.y + hash.z);
388 }
389};
390
391template <> inline unsigned int CHashMethods<C3DTVector<int> >::HashValue(const C3DTVector<int>& hash) // Keep space between > > for Linux C98
392{
393 return HashMixValue(hash.x + HashMixValue(hash.y)) + hash.z;
394}
395
396#ifdef _DEBUG
397#include "StringTools.h"
398template <class TYPE> void C3DTVector<TYPE>::Dump() const
399{
400 XTRACE(_T("Vector : %.5f %.5f %.5f\n"), x, y, z);
401 CPt::Dump();
402}
403
404template <> inline void C3DTVector<int>::Dump() const
405{
406 XTRACE(_T("Vector : %d %d %d\n"), x, y, z);
407 CPt::Dump();
408}
409
410template <class TYPE> bool C3DTVector<TYPE>::IsKindOf(unsigned int classid) const
411{
412 if (C3DTVector::ClassID() == classid)
413 return true;
414
415 return CPt::IsKindOf(classid);
416}
417
418template <class TYPE> unsigned int C3DTVector<TYPE>::ClassID() const
419{
420 return MAKE_CUSTOM_ID('V', 'E', 'C', 'T');
421}
422
423#endif
424END_MOOTOOLS_NAMESPACE
425
426#endif // !defined(AFX_3DVECTOR_H__CA6BC2A2_FAC2_11D1_A0DE_000000000000__INCLUDED_)
C3DTPoint template class definition for handling x, y, z 3D point coordinate.
CPt class is the base class for different class of points (C3DPoint, CUVWPoint...)
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
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
void GetDouble(double *values3d) const
Get vector and fill 3 doubles.
Definition 3DVector.h:342
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 SetFloat(const float *values3f)
Set vector from 3 floats.
Definition 3DVector.h:328
void SelfNegate()
/ operator - : negate each component (-x, -y, -z)
Definition 3DVector.inl:90
void SetDouble(const double *values3d)
Set vector from 3 doubles.
Definition 3DVector.h:349
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
void GetFloat(float *values3f) const
Get vector and fill 3 floats.
Definition 3DVector.h:335
Definition 4DPoint.h:17
Definition Hash.h:146
CPt base only contains some flags that are used by the derived class.
Definition Point.h:60
The class defines a quaternion transformation.
Definition Quaternion.h:20
Definition XArchive.h:17