Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
3DPoint.h
Go to the documentation of this file.
1//! @file 3DPoint.h
2//! @brief C3DTPoint template class definition for handling x, y, z 3D point coordinate
3//
4//////////////////////////////////////////////////////////////////////
5
6#if !defined(AFX_3DPOINT_H__CA6BC29E_FAC2_11D1_A0DE_000000000000__INCLUDED_)
7#define AFX_3DPOINT_H__CA6BC29E_FAC2_11D1_A0DE_000000000000__INCLUDED_
8
9#ifdef _MSC_VER
10#pragma once
11#endif // _MSC_VER
12
13#ifdef _DEBUG
14#include "StringTools.h"
15#endif
16
17#include "Point.h"
18
19BEGIN_MOOTOOLS_NAMESPACE
20
21//! @class C3DTPoint
22//! @brief The class defines an x, y, z 3D point which can use int, float or double
23//!
24//! @tparam TYPE
25//! can be int (C3DPointI), float (C3DPoint or C3DPointF) or double (C3DPointD)
26template<class TYPE> class C3DTPoint : public CPt
27{
28 friend class C3DPointList;
29
30 public:
31 TYPE x, y, z;
32
33 public:
34 C3DTPoint();
35 C3DTPoint(TYPE *val);
36 C3DTPoint(int refx, int refy, int refz);
37 C3DTPoint(float refx, float refy, float refz);
38 C3DTPoint(double refx, double refy, double refz);
39 C3DTPoint(const C3DTPoint& pt);
40 C3DTPoint(const C3DTPoint& pt, const C3DTVector<TYPE>& vect, bool add); //!< if add = true, the point is point = pt+vect, if add = false, point = pt-vect
41
42 //! @name Access functions
43 //! Get access to the 3D point x, y, z values
44 //!
45 //! @{
46 TYPE& operator[](int i);
47 const TYPE& operator[](int i) const;
48 TYPE* ValPtr();
49 const TYPE* ValPtr() const;
50
51 //! @}
52
53 unsigned int SizeOf() const;
54
55 //! @name Operators
56 //!
57 //! @{
58 double operator *(const C4DTVector<TYPE>& vect) const;
59 C3DTPoint operator *(double value) const;
60 //template <class TYPE> friend C3DTPoint operator *(double value, const C3DTPoint& pt);
61 double Mult(const double& xx, const double& yy, const double& zz) const;
62 C3DTPoint& operator=(const C3DTPoint& src);
63 C3DTPoint& operator=(const C3DTVector<TYPE>& vect);
64 C3DTPoint operator-(const C3DTVector<TYPE>& vect1) const;
65 C3DTPoint operator+(const C3DTVector<TYPE>& vect1) const;
66 C3DTPoint operator +(const C3DTPoint& pt1) const;
67 C3DTPoint operator -(const C3DTPoint& pt1) const;
69 C3DTPoint operator /(double weight) const;
74 C3DTPoint& operator *=(double weight);
75 C3DTPoint& operator /=(double weight);
76 bool operator==(const C3DTPoint& src) const;
77 bool operator!=(const C3DTPoint& src) const;
78
79 //! @}
80
81 C3DPointI Floor() const;
82 C3DPointI Ceil() const;
83
84 //////////////////////
85 //! @name Mix type conversion (C3DTPoint<float> to C3DTPoint<double> for example)
86 //! @{
87 template <class TYPE2> C3DTPoint(const C3DTPoint<TYPE2>& pt);
88 template <class TYPE2> C3DTPoint(const C3DTVector<TYPE2>& pt);
89 template <class TYPE2> C3DTPoint& operator=(const C3DTPoint<TYPE2>& pt);
90
91 //! @}
92
93 void Init(TYPE x = (TYPE)0.0, TYPE y = (TYPE)0, TYPE z = (TYPE)0);
94 void Swap(unsigned int swapMode);
95 void SubstractTo(const C3DTPoint& pt); //!< return this = pt-this, instead this = this-pt. This can be used for fast computation (using the standard operator return a C3DPointT)
96 void Offset(TYPE value); //!< Add value to x, y, z
97
98 // Check value
99 bool IsAnormal() const; //!< either infinite or nan. Zero and denormalized value are allowed.
100 bool IsInfinite() const; //!< Check if one of the coordinate has inf value.
101 bool IsNan(bool resetToZero = false); //!< Check if one of the coordinate has nan value. resetToZero nan = true, reset coordinates to zero if any.
102
103 virtual void To4DPoint(C4DPoint& pt) const;
104 void SetValues(const TYPE *values3t); //!< Set point from 3 TYPE
105 void GetFloat(float *values3f) const; //!< Get point and fill 3 floats
106 void SetFloat(const float *values3f); //!< Set point from 3 floats
107 void GetDouble(double *values3d) const; //!< Get point and fill 3 doubles
108 void SetDouble(const double *values3d); //!< Set point from 3 doubles
109
110 virtual CPt& operator=(const C4DPoint& pt);
111
112 virtual void Serialize(CXArchive& ar);
113
114 virtual CPt& operator=(const CPt& refpoint);
115
116 #ifdef _DEBUG
117 virtual void Dump() const;
118
119 virtual bool IsKindOf(unsigned int classid) const;
120 virtual unsigned int ClassID() const;
121 #endif
122};
123
124//////////////////////
125// Mix type conversion (C3DTPoint<float> to C3DTPoint<double> for example)
126template <class TYPE>
127template <class TYPE2> C3DTPoint<TYPE>::C3DTPoint(const C3DTPoint<TYPE2>& pt)
128{
129 XASSERT(ClassID() != pt.ClassID()); // (the default operator should be called if same type)
130
131 x = (TYPE)pt.x;
132 y = (TYPE)pt.y;
133 z = (TYPE)pt.z;
134}
135
136template <class TYPE>
137template <class TYPE2> C3DTPoint<TYPE>::C3DTPoint(const C3DTVector<TYPE2>& vect)
138{
139 x = (TYPE)vect.x;
140 y = (TYPE)vect.y;
141 z = (TYPE)vect.z;
142}
143
144template <class TYPE>
146{
147 XASSERT(ClassID() != pt.ClassID()); // (the default operator should be called if same type)
148
149 x = (TYPE)pt.x;
150 y = (TYPE)pt.y;
151 z = (TYPE)pt.z;
152
153 CPt::operator=(pt);
154
155 return *this;
156}
157
158template <typename TYPE> class CHashMethods<C3DTPoint<TYPE> > // Keep space between > > for Linux C98
159{
160public:
161 static inline bool HashCompare(const C3DTPoint<TYPE>& pt1, const C3DTPoint<TYPE>& pt2)
162 {
163 if (pt1.x != pt2.x || pt1.y != pt2.y || pt1.z != pt2.z)
164 return false;
165
166 return true;
167 }
168
169 static inline unsigned int HashValue(const C3DTPoint<TYPE>& hash)
170 {
171 return CHashMethods<TYPE>::HashValue(hash.x + hash.y + hash.z);
172 }
173};
174
175template <> inline unsigned int CHashMethods<C3DTPoint<int> >::HashValue(const C3DTPoint<int>& hash) // Keep space between > > for Linux C98
176{
177 return HashMixValue(hash.x + HashMixValue(hash.y)) + hash.z;
178}
179
180DLL_3DFUNCTION bool GetXYZSwap(unsigned int swapMode, int& coord1, int& coord2, int& invert);
181DLL_3DFUNCTION bool IsInBox(const C3DPoint& refpt, const C3DPoint& pt1, const C3DPoint& pt2);
182DLL_3DFUNCTION C3DPoint NoisyWaveFunc(const C3DPoint& refpt, const C3DPoint& upperleft, const C3DVector& length, double amplitude, double wavelength, double phase);
183DLL_3DFUNCTION double GetCompacityFactor2(const C3DPoint& pt1, const C3DPoint& pt2, const C3DPoint& pt3);
184DLL_3DFUNCTION double GetCompacityFactor(const C3DPoint& pt1, const C3DPoint& pt2, const C3DPoint& pt3);
185
186END_MOOTOOLS_NAMESPACE
187
188#include "3DVector.h"
189#include "4DVector.h"
190#include "3DPoint.inl"
191
192BEGIN_MOOTOOLS_NAMESPACE
193
194/////////////////////////////////////////////////////////////////////
195// Specialization
196//////////////////////////////////////////////////////////////////////
197#ifdef _DEBUG
198template<> inline unsigned int C3DTPoint<int>::ClassID() const
199{
200 return MAKE_CUSTOM_ID('3', 'D', 'P', 'I');
201}
202
203template<> inline unsigned int C3DTPoint<float>::ClassID() const
204{
205 return MAKE_CUSTOM_ID('3', 'D', 'P', 'F');
206}
207
208template<> inline unsigned int C3DTPoint<double>::ClassID() const
209{
210 return MAKE_CUSTOM_ID('3', 'D', 'P', 'D');
211}
212#endif
213
214//////////////////////////////////////////////////////////////////////
215// Implementation
216//////////////////////////////////////////////////////////////////////
217
218#ifdef _DEBUG
219template <class TYPE> void C3DTPoint<TYPE>::Dump() const
220{
221 XTRACE(_T("%.5f %.5f %.5f\n"), x, y, z);
222 CPt::Dump();
223}
224
225template <> inline void C3DTPoint<int>::Dump() const
226{
227 XTRACE(_T("%d %d %d\n"), x, y, z);
228 CPt::Dump();
229}
230#endif //_DEBUG
231
232template <class TYPE> void C3DTPoint<TYPE>::Init(TYPE x, TYPE y, TYPE z)
233{
234 this->x = (TYPE)x;
235 this->y = (TYPE)y;
236 this->z = (TYPE)z;
237}
238
239template <class TYPE> void C3DTPoint<TYPE>::Serialize(CXArchive& ar)
240{
241 CPt::Serialize(ar);
242
243 if (ar.IsStoring())
244 {
245 ar << x;
246 ar << y;
247 ar << z;
248 }
249 else
250 {
251 ar >> x;
252 ar >> y;
253 ar >> z;
254 }
255}
256
257template <class TYPE> void C3DTPoint<TYPE>::To4DPoint(C4DPoint& pt) const
258{
259 pt.size = 3;
260 pt.value[0] = x;
261 pt.value[1] = y;
262 pt.value[2] = z;
263}
264
265template <class TYPE> CPt& C3DTPoint<TYPE>::operator=(const C4DPoint& pt)
266{
267 XASSERT(pt.size == 3);
268 x = (TYPE)pt.value[0];
269 y = (TYPE)pt.value[1];
270 z = (TYPE)pt.value[2];
271 return *this;
272}
273
274template<class TYPE> void C3DTPoint<TYPE>::Swap(unsigned int swapMode)
275{
276 // The order of inversion is important (swap before invert)
277 if (swapMode & SWAP_AXIS_FLAGS)
278 {
279 TYPE tmp;
280 switch (swapMode & SWAP_AXIS_FLAGS)
281 {
282 default:
283 XASSERT(0);
284 break;
285
286 case SWAP_XY:
287 SWAP_VALUES(x, y, tmp);
288 break;
289 case SWAP_XZ:
290 SWAP_VALUES(x, z, tmp);
291 break;
292 case SWAP_YZ:
293 SWAP_VALUES(y, z, tmp);
294 break;
295 }
296 }
297
298 if (swapMode & SWAP_INVERT_FLAGS)
299 {
300 switch (swapMode & SWAP_INVERT_FLAGS)
301 {
302 default:
303 XASSERT(0);
304 break;
305
306 case SWAP_INVERT_X:
307 x = -x;
308 break;
309 case SWAP_INVERT_Y:
310 y = -y;
311 break;
312 case SWAP_INVERT_Z:
313 z = -z;
314 break;
315 }
316 }
317}
318
319
320template <class TYPE> inline void C3DTPoint<TYPE>::SetFloat(const float *values)
321{
322 x = (TYPE)values[0];
323 y = (TYPE)values[1];
324 z = (TYPE)values[2];
325}
326
327template <class TYPE> void C3DTPoint<TYPE>::GetFloat(float *values) const
328{
329 values[0] = x;
330 values[1] = y;
331 values[2] = z;
332}
333
334template <class TYPE> void C3DTPoint<TYPE>::GetDouble(double *values) const
335{
336 values[0] = x;
337 values[1] = y;
338 values[2] = z;
339}
340
341template <class TYPE> void C3DTPoint<TYPE>::SetDouble(const double *values)
342{
343 x = (TYPE)values[0];
344 y = (TYPE)values[1];
345 z = (TYPE)values[2];
346}
347
348template <class TYPE> CPt& C3DTPoint<TYPE>::operator=(const CPt& refpoint)
349{
350 XASSERT(refpoint.IsKindOf(C3DTPoint<TYPE>::ClassID()));
351
352 x = ((C3DTPoint<TYPE>&)refpoint).x;
353 y = ((C3DTPoint<TYPE>&)refpoint).y;
354 z = ((C3DTPoint<TYPE>&)refpoint).z;
355
356 return CPt::operator=(refpoint);
357}
358
359#ifdef _DEBUG
360template<class TYPE>
361bool C3DTPoint<TYPE>::IsKindOf(unsigned int classid) const
362{
363 if (C3DTPoint::ClassID() == classid)
364 return true;
365
366 return CPt::IsKindOf(classid);
367}
368
369template<class TYPE>
370unsigned int C3DTPoint<TYPE>::ClassID() const
371{
372 XASSERT(0);
373 return MAKE_CUSTOM_ID('3', 'D', 'P', 'T');
374}
375#endif
376
377END_MOOTOOLS_NAMESPACE
378
379#endif // !defined(AFX_3DPOINT_H__CA6BC29E_FAC2_11D1_A0DE_000000000000__INCLUDED_)
CPt class is the base class for different class of points (C3DPoint, CUVWPoint...)
Definition 3DPointList.h:267
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
void SetValues(const TYPE *values3t)
Set point from 3 TYPE.
Definition 3DPoint.inl:88
void SetFloat(const float *values3f)
Set point from 3 floats.
Definition 3DPoint.h:320
bool IsAnormal() const
either infinite or nan. Zero and denormalized value are allowed.
Definition 3DPoint.inl:177
void SubstractTo(const C3DTPoint &pt)
return this = pt-this, instead this = this-pt. This can be used for fast computation (using the stand...
Definition 3DPoint.inl:153
void GetDouble(double *values3d) const
Get point and fill 3 doubles.
Definition 3DPoint.h:334
void SetDouble(const double *values3d)
Set point from 3 doubles.
Definition 3DPoint.h:341
bool IsInfinite() const
Check if one of the coordinate has inf value.
Definition 3DPoint.inl:186
void GetFloat(float *values3f) const
Get point and fill 3 floats.
Definition 3DPoint.h:327
void Offset(TYPE value)
Add value to x, y, z.
Definition 3DPoint.inl:169
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 4DPoint.h:17
Definition Hash.h:146
CPt base only contains some flags that are used by the derived class.
Definition Point.h:60
Definition XArchive.h:17