Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cTriangle3.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cTriangle3_H
7 #define _INC_cTriangle3_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cBounds3.h"
13 #include "cPlane.h"
14 #include "cCylinder.h"
15 #include "../Math/cFloatEst.h"
16 
17 namespace GrayLib
18 {
19  UNITTEST2_PREDEF(cTriangle3);
20 
22  {
27 
28  public:
29  typedef float DVALUE_t;
30  cVector3f m_aPt[3];
31 
32  public:
33  cTriangle3f() noexcept
34  {
35  }
37  {
38  m_aPt[0] = p[0];
39  m_aPt[1] = p[1];
40  m_aPt[2] = p[2];
41  }
42  cTriangle3f(const cVector3f& a, const cVector3f& b, const cVector3f& c) noexcept
43  {
44  m_aPt[0] = a;
45  m_aPt[1] = b;
46  m_aPt[2] = c;
47  }
48 
49  bool operator==(const cTriangle3f& other) const noexcept
50  {
51  return other.m_aPt[0] == m_aPt[0] && other.m_aPt[1] == m_aPt[1] && other.m_aPt[2] == m_aPt[2];
52  }
53  bool operator!=(const cTriangle3f& other) const noexcept
54  {
55  return other.m_aPt[0] != m_aPt[0] || other.m_aPt[1] != m_aPt[1] || other.m_aPt[2] != m_aPt[2];
56  }
57 
58  bool isDegenerate() const noexcept
59  {
61  if (m_aPt[1] == m_aPt[0])
62  return true;
63  if (m_aPt[2] == m_aPt[0])
64  return true;
65  if (m_aPt[2] == m_aPt[1])
66  return true;
67  return false;
68  }
69 
71  {
73  return cPlanef(m_aPt[0], m_aPt[1], m_aPt[2]);
74  }
76  {
80  cVector3f v1 = m_aPt[1] - m_aPt[0];
81  cVector3f v2 = m_aPt[2] - m_aPt[0];
82  return v1.GetCross(v2);
83  }
85  {
87  cVector3f vCenter(
88  (m_aPt[0].x + m_aPt[1].x + m_aPt[2].x) / 3.0f,
89  (m_aPt[0].y + m_aPt[1].y + m_aPt[2].y) / 3.0f,
90  (m_aPt[0].z + m_aPt[1].z + m_aPt[2].z) / 3.0f
91  );
92  return vCenter;
93  }
94 
95  bool isFrontFacing(const cVector3f& vLookDir) const
96  {
102  cVector3f vNorm = get_Normal();
103  vNorm.SetNormalized();
104  return vNorm.GetDot(vLookDir) <= 0.0f;
105  }
106 
107  bool isPointInside(const cVector3f& p) const
108  {
113  return (isOnSameSide(p, m_aPt[0], m_aPt[1], m_aPt[2]) &&
114  isOnSameSide(p, m_aPt[1], m_aPt[0], m_aPt[2]) &&
115  isOnSameSide(p, m_aPt[2], m_aPt[0], m_aPt[1]));
116  }
117 
118 #ifdef USE_FLOAT_IEEE
119  bool isPointInsideFast(const cVector3f& p) const
120  {
127 
128  cVector3f f = m_aPt[1] - m_aPt[0];
129  cVector3f g = m_aPt[2] - m_aPt[0];
130 
131  DVALUE_t a = f.GetDot(f);
132  DVALUE_t b = f.GetDot(g);
133  DVALUE_t c = g.GetDot(g);
134 
135  DVALUE_t ac_bb = (a*c) - (b*b);
136  cVector3f vp = p - m_aPt[0];
137 
138  DVALUE_t d = vp.GetDot(f);
139  DVALUE_t e = vp.GetDot(g);
140  DVALUE_t x = (d*c) - (e*b);
141  DVALUE_t y = (e*a) - (d*b);
142  DVALUE_t z = x + y - ac_bb;
143 
145  }
146 #else
147  bool isPointInsideFast(const cVector3f& p) const
148  {
149  return isPointInside(p);
150  }
151 #endif
152 
153  bool isOnSameSide(const cVector3f& p1, const cVector3f& p2, const cVector3f& a, const cVector3f& b) const
154  {
155  cVector3f bminusa = b - a;
156  cVector3f vTmp1 = p1 - a;
157  cVector3f cp1;
158  cp1 = bminusa.GetCross(vTmp1);
159  cVector3f vTmp2 = p2 - a;
160  cVector3f cp2;
161  cp2 = bminusa.GetCross(vTmp2);
162  return (cp1.GetDot(cp2) > 0.0f);
163  }
164 
165  bool getIntersectionOfPlaneWithLine(const cVector3f& linePoint, const cVector3f& lineVect, cVector3f& outIntersection) const
166  {
175 
176  cVector3f normal = get_Normal();
177  DVALUE_t t2 = normal.GetDot(lineVect);
178  if (t2 == 0.0f)
179  return false;
180 
181  DVALUE_t d = m_aPt[0].GetDot(normal);
182  DVALUE_t t = -(normal.GetDot(linePoint) - d) / t2;
183  outIntersection = linePoint + (lineVect * t);
184  return true;
185  }
186 
187  bool getIntersectionWithLine(const cVector3f& linePoint, const cVector3f& lineVect, cVector3f& outIntersection) const
188  {
199 
200  if (!getIntersectionOfPlaneWithLine(linePoint, lineVect, outIntersection))
201  return false;
202  return isPointInside(outIntersection);
203  }
204 
206  {
207  cVector3f vPos;
208 #ifdef USE_DXM
209  ::D3DXVec3BaryCentric((XMFLOAT3*)&vPos, (XMFLOAT3*)&m_aPt[0], (XMFLOAT3*)&m_aPt[1], (XMFLOAT3*)&m_aPt[2], f, g);
210 #else
211  vPos.x = (1.0f - f - g) * (m_aPt[0].x) + f * (m_aPt[1].x) + g * (m_aPt[2].x);
212  vPos.y = (1.0f - f - g) * (m_aPt[0].y) + f * (m_aPt[1].y) + g * (m_aPt[2].y);
213  vPos.z = (1.0f - f - g) * (m_aPt[0].z) + f * (m_aPt[1].z) + g * (m_aPt[2].z);
214 #endif
215  return vPos;
216  }
217 
218  // Setters
219  void setTri(const cVector3f* p)
220  {
221  m_aPt[0] = p[0];
222  m_aPt[1] = p[1];
223  m_aPt[2] = p[2];
224  }
225  void setTri(const cVector3f& a, const cVector3f& b, const cVector3f& c)
226  {
227  m_aPt[0] = a;
228  m_aPt[1] = b;
229  m_aPt[2] = c;
230  }
231 
232  void TransformCoords(const cMatrix4x4f& mMatrix)
233  {
234  for (int i = 0; i < 3; i++)
235  {
236  m_aPt[i] = m_aPt[i].GetProj(mMatrix);
237  }
238  }
239  void MultV(const cVector3f vMult)
240  {
241  for (int i = 0; i < 3; i++)
242  {
243  m_aPt[i] *= vMult;
244  }
245  }
246  void DivV(const cVector3f vMult)
247  {
248  for (int i = 0; i < 3; i++)
249  {
250  m_aPt[i] /= vMult;
251  }
252  }
253 
254  UNITTEST_FRIEND(cTriangle3);
255  };
256 }
257 #endif // _INC_cTriangle3_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
Using X files without the sources and the makefile How to use you just create a debug directory e g
Definition: Readme.txt:21
Definition: cMatrix.h:194
Definition: cPlane.h:18
Definition: cTriangle3.h:22
cTriangle3f(const cVector3f &a, const cVector3f &b, const cVector3f &c) noexcept
Definition: cTriangle3.h:42
bool isOnSameSide(const cVector3f &p1, const cVector3f &p2, const cVector3f &a, const cVector3f &b) const
Definition: cTriangle3.h:153
bool isPointInsideFast(const cVector3f &p) const
Definition: cTriangle3.h:119
bool operator!=(const cTriangle3f &other) const noexcept
Definition: cTriangle3.h:53
bool getIntersectionWithLine(const cVector3f &linePoint, const cVector3f &lineVect, cVector3f &outIntersection) const
Definition: cTriangle3.h:187
UNITTEST_FRIEND(cTriangle3)
float DVALUE_t
Dimension value type.
Definition: cTriangle3.h:29
cTriangle3f() noexcept
Definition: cTriangle3.h:33
cTriangle3f(const cVector3f *p)
Definition: cTriangle3.h:36
cVector3f get_Normal() const
Definition: cTriangle3.h:75
void MultV(const cVector3f vMult)
Definition: cTriangle3.h:239
bool isFrontFacing(const cVector3f &vLookDir) const
Definition: cTriangle3.h:95
void setTri(const cVector3f &a, const cVector3f &b, const cVector3f &c)
Definition: cTriangle3.h:225
cVector3f get_Center() const
Definition: cTriangle3.h:84
cPlanef get_Plane() const
Definition: cTriangle3.h:70
bool getIntersectionOfPlaneWithLine(const cVector3f &linePoint, const cVector3f &lineVect, cVector3f &outIntersection) const
Definition: cTriangle3.h:165
bool isDegenerate() const noexcept
Definition: cTriangle3.h:58
void setTri(const cVector3f *p)
Definition: cTriangle3.h:219
void TransformCoords(const cMatrix4x4f &mMatrix)
Definition: cTriangle3.h:232
cVector3f GetBaryCentric(DVALUE_t f, DVALUE_t g) const
Definition: cTriangle3.h:205
bool operator==(const cTriangle3f &other) const noexcept
Definition: cTriangle3.h:49
bool isPointInside(const cVector3f &p) const
Definition: cTriangle3.h:107
void DivV(const cVector3f vMult)
Definition: cTriangle3.h:246
THIS_t GetCross(const THIS_t &rB) const
Definition: cVecT.h:621
TYPE y
Definition: cVecT.h:545
TYPE z
Definition: cVecT.h:545
TYPE x
Definition: cVecT.h:545
TYPE SetNormalized(void) noexcept
Definition: cVecT.h:334
TYPE GetDot(const THIS_t &v2) const
Definition: cVecT.h:255
Definition: cVector.h:94
THIS_t GetProj(const cMatrix4x4f &M) const
Definition: cVector.cpp:98
static const UINT32 k_SIGN_MASK
1 bit = value sign (Sign_bit)
Definition: cFloat.h:31
static UINT32 toBits(float src) noexcept
Definition: cFloat.h:53
Definition: cMesh.h:22
UNITTEST2_PREDEF(cQuadtree)
Definition: MathDX.h:58
D3DVALUE y
Definition: MathDX.h:60
D3DVALUE z
Definition: MathDX.h:60
D3DVALUE x
Definition: MathDX.h:60