Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
4DVector.inl
1#pragma once
2
3BEGIN_MOOTOOLS_NAMESPACE
4
5//////////////////////////////////////////////////////////////////////
6// Construction/Destruction
7//////////////////////////////////////////////////////////////////////
8
9template <class TYPE> inline C4DTVector<TYPE>::C4DTVector()
10{
11 t = 1;
12}
13
14template <class TYPE> inline C4DTVector<TYPE>::C4DTVector(const C4DTVector& plane)
15{
16 dir = plane.dir;
17 t = plane.t;
18}
19
20template <class TYPE> inline C4DTVector<TYPE>::C4DTVector(const C3DTPoint<TYPE>& pt1, const C3DTPoint<TYPE>& pt2, const C3DTPoint<TYPE>& pt3, bool normalize)
21{
22 InitPlane(pt1, pt2, pt3, normalize);
23}
24
25template <class TYPE> inline void C4DTVector<TYPE>::Serialize(CXArchive& ar)
26{
27 if (ar.IsStoring())
28 {
29 ar << dir.x;
30 ar << dir.y;
31 ar << dir.z;
32 ar << t;
33 }
34 else
35 {
36 ar >> dir.x;
37 ar >> dir.y;
38 ar >> dir.z;
39 ar >> t;
40 }
41}
42
43template <class TYPE> inline bool C4DTVector<TYPE>::InitPlane(const C3DTPoint<TYPE>& pt1, const C3DTPoint<TYPE>& pt2, const C3DTPoint<TYPE>& pt3, bool normalize)
44{
45 // Compute the normal of the three first point
48
49 // If point are colinear normal is 0 0 0
50 // normal coordinate are coef a, b, c
51 dir.x = (TYPE)(vect1.y * vect2.z - vect1.z * vect2.y);
52 dir.y = (TYPE)(vect1.z * vect2.x - vect1.x * vect2.z);
53 dir.z = (TYPE)(vect1.x * vect2.y - vect1.y * vect2.x);
54
55 if (normalize)
56 {
57 double length = dir.Length();
58 if (length < PRECISION_LIMIT)
59 {
60 XTRACE(trace3DModule, 0, "template <class TYPE> C4DTVector<TYPE>::InitPlane: Warning null vector\n");
61 dir.Init(1.0, 0, 0);
62 t = 1;
63
64 return false;
65 }
66
67 dir /= length;
68 }
69
70 // plan equation is ax+by+cz+t = 0 : compute t for point[0]
71 t = -pt1.x*dir.x - pt1.y*dir.y - pt1.z*dir.z;
72
73 return true;
74}
75
76template <class TYPE> inline double C4DTVector<TYPE>::GetSignedDistance(const C3DTPoint<TYPE>& pt) const // plane direction need to be normalized
77{
78 return pt.x*dir.x + pt.y*dir.y + pt.z*dir.z + t;
79}
80
81template <class TYPE> inline double C4DTVector<TYPE>::GetDistance(const C3DTPoint<TYPE>& pt) const
82{
83 return fabs(pt.x*dir.x + pt.y*dir.y + pt.z*dir.z + t);
84}
85
86template <class TYPE> inline C3DTPoint<TYPE> C4DTVector<TYPE>::GetProjection(const C3DTPoint<TYPE>& pt) const
87{
88 double d = GetSignedDistance(pt);
89 return C3DPoint(pt.x - dir.x*d, pt.y - dir.y*d, pt.z - dir.z*d);
90}
91
92//////////////////////
93// Mix type conversion (C3DTPoint<float> to C3DTPoint<double> for example)
94template <class TYPE>
95template <class TYPE2> C4DTVector<TYPE>::C4DTVector(const C4DTVector<TYPE2>& plane)
96{
97 dir.x = (TYPE)plane.dir.x;
98 dir.y = (TYPE)plane.dir.y;
99 dir.z = (TYPE)plane.dir.z;
100 t = (TYPE)plane.t;
101}
102
103template <class TYPE>
105{
106 dir.x = (TYPE)plane.dir.x;
107 dir.y = (TYPE)plane.dir.y;
108 dir.z = (TYPE)plane.dir.z;
109 t = (TYPE)plane.t;
110
111 return *this;
112}
113END_MOOTOOLS_NAMESPACE
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Definition 4DVector.h:15
Definition XArchive.h:17