Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cPlane.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cPlane_H
7 #define _INC_cPlane_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../Math/cVector.h"
13 #include "../Math/cMatrix.h"
14 
15 namespace GrayLib
16 {
18  {
25 
26  typedef cVector4f SUPER_t;
27 
28  public:
29  enum REL_TYPE // like COMPARE_TYPE
30  {
35  REL_BACK = -1,
36  REL_PLANAR = 0,
37  REL_FRONT = 1,
38  };
39 
40  cPlanef() : SUPER_t(0, 1, 0)
41  {
42  RecalculateD(cVector3f(0, 0, 0));
43  }
44  cPlanef(const SUPER_t& v) noexcept : SUPER_t(v)
45  {}
46  cPlanef(const DVALUE_t* pVals) noexcept : SUPER_t(pVals)
47  {}
48  cPlanef(const cVector3f& vNormal, DVALUE_t fDistFromOrigin) : SUPER_t(vNormal, fDistFromOrigin)
49  {
51  }
52  cPlanef(DVALUE_t a, DVALUE_t b, DVALUE_t c, DVALUE_t d) noexcept : SUPER_t(a, b, c, d)
53  {}
54 
55  cPlanef(const cVector3f& v1, const cVector3f& v2, const cVector3f& v3)
56  {
58  InitPlanePoints(v1, v2, v3);
59  }
60  cPlanef(const cVector3f& vPoint, const cVector3f& vNormal)
61  {
62  InitPointNormal(vPoint, vNormal);
63  }
64 
65 #if defined(USE_DXM) || defined(USE_DX) // ASSUME DVALUE_t = float !!!
66  // Cast directly to the DirectX equivalent type.
67  cPlanef(const XMFLOAT4& v) : SUPER_t(v)
68  {}
69 #endif
70 
71  // Init
72 
73  void InitPlane(const cVector3f& vNormal, float fDistFromOrigin)
74  {
75  ref_Normal() = vNormal;
76  m_a[3] = fDistFromOrigin;
77  }
78  void InitPointNormal(const cVector3f& vPoint, const cVector3f& vNormal)
79  {
80  // D3DXPlaneFromPointNormal or XMPlaneFromPointNormal
81 #ifdef USE_DXM
82  * this = ::XMPlaneFromPointNormal((XMVECTOR)vPoint, (XMVECTOR)vNormal);
83 #else
84  ref_Normal() = vNormal;
85  RecalculateD(vPoint);
86 #endif
87  }
88  void InitPointVector(const cVector3f& vPoint, const cVector3f& vVector)
89  {
90  InitPointNormal(vPoint, vVector.get_Normalized());
91  }
92 
93  void InitPlanePoints(const cVector3f& v1, const cVector3f& v2, const cVector3f& v3)
94  {
96 #ifdef USE_DXM
97  ::D3DXPlaneFromPoints((D3DXPLANE*)this, (XMFLOAT3*)&v1, (XMFLOAT3*)&v2, (XMFLOAT3*)&v3);
98 #else
99  cVector3f vEdge1 = v2 - v1;
100  cVector3f vEdge2 = v3 - v1;
101  cVector3f vNormal1 = vEdge1.GetCross(vEdge2);
102  cVector3f vNormalN = vNormal1.get_Normalized();
103  InitPointNormal(v1, vNormalN);
104 #endif
105  }
106  void InitTransformPlane(const cPlanef& plane, const cMatrix4x4f& m1)
107  {
110 #ifdef USE_DXM
111  ::D3DXPlaneTransform((D3DXPLANE*)this, (D3DXPLANE*)&plane, (const XMFLOAT4X4*)&m1);
112 #else
113  this->m_x = m1.m[0][0] * plane.m_x + m1.m[1][0] * plane.m_y + m1.m[2][0] * plane.m_z + m1.m[3][0] * plane.w;
114  this->m_y = m1.m[0][1] * plane.m_x + m1.m[1][1] * plane.m_y + m1.m[2][1] * plane.m_z + m1.m[3][1] * plane.w;
115  this->m_z = m1.m[0][2] * plane.m_x + m1.m[1][2] * plane.m_y + m1.m[2][2] * plane.m_z + m1.m[3][2] * plane.w;
116  this->m_w = m1.m[0][3] * plane.m_x + m1.m[1][3] * plane.m_y + m1.m[2][3] * plane.m_z + m1.m[3][3] * plane.w;
117 #endif
118  }
119  void InitNormalized(const cPlanef& plane)
120  {
124 #ifdef USE_DXM
125  ::D3DXPlaneNormalize((D3DXPLANE*)this, (const D3DXPLANE*)&plane);
126 #else
127  float nNorm = plane.get_Normal().get_Magnitude(); // [3] ??
128  if (nNorm != 0)
129  {
130  Set(plane.GetScaled(1 / nNorm));
131  }
132  else
133  {
134  SetZero();
135  }
136 #endif
137  }
138 
140  const cVector3f& get_Normal() const noexcept
141  {
142  return (const cVector3f&)get_V3();
143  }
144  DVALUE_t get_DistFromOrigin() const noexcept
145  {
146  return m_a[3];
147  }
148  cVector3f get_MemberPoint() const noexcept
149  {
151  return get_Normal() * -get_DistFromOrigin();
152  }
153 
154  inline DVALUE_t GetDotCoord(const cVector3f& v) const
155  {
158 #ifdef USE_DXM
159  return ::D3DXPlaneDotCoord((D3DXPLANE*)this, (XMFLOAT3*)&v);
160 #else
161  return get_Normal().GetDot(v) + m_a[3] ;
162 #endif
163  }
164  inline cPlanef GetTransformPlane(const cMatrix4x4f& m1) const
165  {
166  cPlanef planeRet;
167  planeRet.InitTransformPlane(*this, m1);
168  return planeRet;
169  }
171  {
172  cPlanef planeRet;
173  planeRet.InitNormalized(*this);
174  return planeRet;
175  }
176 
177  bool IsFrontFacing(const cVector3f& vLookDir) const
178  {
186  return(get_Normal().GetDot(vLookDir) <= 0.0f);
187  }
188 
189  float GetDistanceTo(const cVector3f& vPoint) const
190  {
193  return vPoint.GetDot(get_Normal()) + get_DistFromOrigin();
194  }
195 
196  REL_TYPE GetPointRelation(const cVector3f& vPoint) const
197  {
204 
205  float d = get_Normal().GetDot(vPoint) + get_DistFromOrigin();
206  if (d < -k_FLT_MIN2)
207  return REL_FRONT;
208  if (d > k_FLT_MIN2)
209  return REL_BACK;
210  return REL_PLANAR;
211  }
212 
213  // Modify
214 
216  {
217  return (cVector3f&)ref_V3();
218  }
219  void RecalculateD(const cVector3f& vMember)
220  {
223  m_a[3] = -vMember.GetDot(get_Normal());
224  }
226  {
227  InitNormalized(*this);
228  }
230  {
232  ref_Normal() = get_Normal().GetTransNorm(m);
233  // leave a.w;
234  }
235 
236 #if 0
237  bool GetIntersectionWithPlane(const cPlanef& rPlane2, cVector3f& outLinePoint, cVector3f& outLineVect) const
238  {
241 
242  float fn00 = m_vNormal.get_Magnitude();
243  float fn01 = m_vNormal.GetDot(rPlane2.m_vNormal);
244  float fn11 = rPlane2.m_vNormal.get_Magnitude();
245  float det = fn00 * fn11 - fn01 * fn01;
246 
247  if (CalcI::Abs(det) < 1e-08f)
248  return false;
249 
250  det = 1.0f / det;
251  float fc0 = (fn11 * -m_D + fn01 * rPlane2.m_D) * det;
252  float fc1 = (fn00 * -rPlane2.m_D + fn01 * m_D) * det;
253 
254  outLineVect = m_vNormal.GetCross(rPlane2.m_vNormal);
255  outLinePoint = m_vNormal * fc0 + rPlane2.m_vNormal * fc1;
256  return true;
257  }
258 #endif
259 
261  };
262 }
263 #endif // _INC_cPlanef_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
Definition: cMatrix.h:194
TYPE m[_ROWS][_COLS]
Definition: cMatrix.h:38
Definition: cPlane.h:18
REL_TYPE GetPointRelation(const cVector3f &vPoint) const
Definition: cPlane.h:196
cVector3f get_MemberPoint() const noexcept
Definition: cPlane.h:148
cPlanef(const DVALUE_t *pVals) noexcept
Definition: cPlane.h:46
void InitPlanePoints(const cVector3f &v1, const cVector3f &v2, const cVector3f &v3)
Definition: cPlane.h:93
REL_TYPE
Definition: cPlane.h:30
void InitPointNormal(const cVector3f &vPoint, const cVector3f &vNormal)
Definition: cPlane.h:78
void InitPointVector(const cVector3f &vPoint, const cVector3f &vVector)
Definition: cPlane.h:88
cPlanef(const cVector3f &v1, const cVector3f &v2, const cVector3f &v3)
Definition: cPlane.h:55
void InitNormalized(const cPlanef &plane)
Definition: cPlane.h:119
cPlanef(const cVector3f &vNormal, DVALUE_t fDistFromOrigin)
Definition: cPlane.h:48
cPlanef(DVALUE_t a, DVALUE_t b, DVALUE_t c, DVALUE_t d) noexcept
Definition: cPlane.h:52
UNITTEST_FRIEND(cPlane)
void RecalculateD(const cVector3f &vMember)
Definition: cPlane.h:219
cPlanef(const cVector3f &vPoint, const cVector3f &vNormal)
Definition: cPlane.h:60
void SetNormalized()
Definition: cPlane.h:225
cPlanef GetTransformPlane(const cMatrix4x4f &m1) const
Definition: cPlane.h:164
float GetDistanceTo(const cVector3f &vPoint) const
Definition: cPlane.h:189
void InitPlane(const cVector3f &vNormal, float fDistFromOrigin)
Definition: cPlane.h:73
cPlanef()
Definition: cPlane.h:40
void InitTransformPlane(const cPlanef &plane, const cMatrix4x4f &m1)
Definition: cPlane.h:106
void TransformNormals(const cMatrix4x4f &m)
Definition: cPlane.h:229
DVALUE_t get_DistFromOrigin() const noexcept
Definition: cPlane.h:144
const cVector3f & get_Normal() const noexcept
Getters.
Definition: cPlane.h:140
DVALUE_t GetDotCoord(const cVector3f &v) const
Definition: cPlane.h:154
bool IsFrontFacing(const cVector3f &vLookDir) const
Definition: cPlane.h:177
cPlanef(const SUPER_t &v) noexcept
Definition: cPlane.h:44
cVector3f & ref_Normal()
Definition: cPlane.h:215
cPlanef get_Normalized() const
Definition: cPlane.h:170
THIS_t GetCross(const THIS_t &rB) const
Definition: cVecT.h:621
TYPE m_z
Definition: cVecT.h:675
TYPE m_x
Definition: cVecT.h:675
TYPE w
Definition: cVecT.h:676
TYPE m_y
Definition: cVecT.h:675
TYPE get_Magnitude() const noexcept
Definition: cVecT.h:129
float DVALUE_t
Dimension value type.
Definition: cVecT.h:48
_TYPE_C GetScaled(TYPE nScale) const
Definition: cVecT.h:184
_TYPE_C get_Normalized() const
Definition: cVecT.h:283
TYPE GetDot(const THIS_t &v2) const
Definition: cVecT.h:255
Definition: cVector.h:94
Definition: cVector.h:261
Definition: cMesh.h:22
static TYPE Abs(TYPE a) noexcept
similar to ABS(n) macro. Does nothing for unsigned types.
Definition: MathDX.h:58
Definition: MathDX.h:138
Definition: MathDX.h:106