KiCad PCB EDA Suite
ctriangle.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
34 #include "ctriangle.h"
35 
36 
38 {
39  const SFVEC3F &A = m_vertex[0];
40  const SFVEC3F &B = m_vertex[1];
41  const SFVEC3F &C = m_vertex[2];
42  const SFVEC3F c = B - A;
43  const SFVEC3F b = C - A;
44 
45  m_bbox.Reset();
46  m_bbox.Set( A );
47  m_bbox.Union( B );
48  m_bbox.Union( C );
51 
52  m_n = glm::cross( b, c );
53 
54  if( glm::abs( m_n.x ) > glm::abs( m_n.y ) )
55  {
56  if( glm::abs( m_n.x ) > glm::abs( m_n.z ) )
57  m_k = 0;
58  else
59  m_k = 2;
60  }
61  else
62  {
63  if( glm::abs( m_n.y ) > glm::abs( m_n.z ) )
64  m_k = 1;
65  else
66  m_k = 2;
67  }
68 
69  int u = (m_k + 1) % 3;
70  int v = (m_k + 2) % 3;
71 
72 
73  // precomp
74  float krec = 1.0f / m_n[m_k];
75 
76  m_nu = m_n[u] * krec;
77  m_nv = m_n[v] * krec;
78  m_nd = glm::dot( m_n, A ) * krec;
79 
80 
81  // first line equation
82  float reci = 1.0f / (b[u] * c[v] - b[v] * c[u]);
83 
84  m_bnu = b[u] * reci;
85  m_bnv = -b[v] * reci;
86 
87 
88  // second line equation
89  m_cnu = c[v] * reci;
90  m_cnv = -c[u] * reci;
91 
92  // finalize normal
93  m_n = glm::normalize( m_n );
94 
95  m_normal[0] = m_n;
96  m_normal[1] = m_n;
97  m_normal[2] = m_n;
98 }
99 
100 
102  const SFVEC3F &aV2,
103  const SFVEC3F &aV3 ) : COBJECT( OBJ3D_TRIANGLE )
104 {
105  m_vertex[0] = aV1;
106  m_vertex[1] = aV2;
107  m_vertex[2] = aV3;
108 
109  m_vertexColorRGBA[0] = 0xFFFFFFFF;
110  m_vertexColorRGBA[1] = 0xFFFFFFFF;
111  m_vertexColorRGBA[2] = 0xFFFFFFFF;
112 
113  pre_calc_const();
114 }
115 
116 
118  const SFVEC3F &aV2,
119  const SFVEC3F &aV3,
120  const SFVEC3F &aFaceNormal ) : COBJECT( OBJ3D_TRIANGLE )
121 {
122  m_vertex[0] = aV1;
123  m_vertex[1] = aV2;
124  m_vertex[2] = aV3;
125 
126  m_vertexColorRGBA[0] = 0xFFFFFFFF;
127  m_vertexColorRGBA[1] = 0xFFFFFFFF;
128  m_vertexColorRGBA[2] = 0xFFFFFFFF;
129 
130  pre_calc_const();
131 
132  m_normal[0] = aFaceNormal;
133  m_normal[1] = aFaceNormal;
134  m_normal[2] = aFaceNormal;
135 }
136 
137 
139  const SFVEC3F &aV2,
140  const SFVEC3F &aV3,
141  const SFVEC3F &aN1,
142  const SFVEC3F &aN2,
143  const SFVEC3F &aN3 ) : COBJECT( OBJ3D_TRIANGLE )
144 {
145  m_vertex[0] = aV1;
146  m_vertex[1] = aV2;
147  m_vertex[2] = aV3;
148 
149  m_vertexColorRGBA[0] = 0xFFFFFFFF;
150  m_vertexColorRGBA[1] = 0xFFFFFFFF;
151  m_vertexColorRGBA[2] = 0xFFFFFFFF;
152 
153  pre_calc_const();
154 
155  m_normal[0] = aN1;
156  m_normal[1] = aN2;
157  m_normal[2] = aN3;
158 }
159 
160 
161 void CTRIANGLE::SetColor( const SFVEC3F &aColor )
162 {
163  m_vertexColorRGBA[0] = ((unsigned int)(aColor.r * 255) << 24) |
164  ((unsigned int)(aColor.g * 255) << 16) |
165  ((unsigned int)(aColor.b * 255) << 8) | 0xFF;
168 }
169 
170 
171 void CTRIANGLE::SetColor( const SFVEC3F &aVC0,
172  const SFVEC3F &aVC1,
173  const SFVEC3F &aVC2 )
174 {
175  m_vertexColorRGBA[0] = ((unsigned int)(aVC0.r * 255) << 24) |
176  ((unsigned int)(aVC0.g * 255) << 16) |
177  ((unsigned int)(aVC0.b * 255) << 8) | 0xFF;
178  m_vertexColorRGBA[1] = ((unsigned int)(aVC1.r * 255) << 24) |
179  ((unsigned int)(aVC1.g * 255) << 16) |
180  ((unsigned int)(aVC1.b * 255) << 8) | 0xFF;
181  m_vertexColorRGBA[2] = ((unsigned int)(aVC2.r * 255) << 24) |
182  ((unsigned int)(aVC2.g * 255) << 16) |
183  ((unsigned int)(aVC2.b * 255) << 8) | 0xFF;
184 }
185 
186 
187 void CTRIANGLE::SetColor( unsigned int aFaceColorRGBA )
188 {
189  m_vertexColorRGBA[0] = aFaceColorRGBA;
190  m_vertexColorRGBA[1] = aFaceColorRGBA;
191  m_vertexColorRGBA[2] = aFaceColorRGBA;
192 }
193 
194 
195 void CTRIANGLE::SetColor( unsigned int aVertex1ColorRGBA,
196  unsigned int aVertex2ColorRGBA,
197  unsigned int aVertex3ColorRGBA )
198 {
199  m_vertexColorRGBA[0] = aVertex1ColorRGBA;
200  m_vertexColorRGBA[1] = aVertex2ColorRGBA;
201  m_vertexColorRGBA[2] = aVertex3ColorRGBA;
202 }
203 
204 
205 void CTRIANGLE::SetUV( const SFVEC2F &aUV1,
206  const SFVEC2F &aUV2,
207  const SFVEC2F &aUV3 )
208 {
209  m_uv[0] = aUV1;
210  m_uv[1] = aUV2;
211  m_uv[2] = aUV3;
212 }
213 
214 
215 static const unsigned int s_modulo[] = { 0, 1, 2, 0, 1 };
216 
217 bool CTRIANGLE::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
218 {
220 #define ku s_modulo[m_k + 1]
221 #define kv s_modulo[m_k + 2]
222 
223  const SFVEC3F &O = aRay.m_Origin;
224  const SFVEC3F &D = aRay.m_Dir;
225  const SFVEC3F &A = m_vertex[0];
226 
227  const float lnd = 1.0f / (D[m_k] + m_nu * D[ku] + m_nv * D[kv]);
228  const float t = (m_nd - O[m_k] - m_nu * O[ku] - m_nv * O[kv]) * lnd;
229 
230  if( !( (aHitInfo.m_tHit > t) && (t > 0.0f) ) )
231  return false;
232 
233  const float hu = O[ku] + t * D[ku] - A[ku];
234  const float hv = O[kv] + t * D[kv] - A[kv];
235  const float beta = hv * m_bnu + hu * m_bnv;
236 
237  if( beta < 0.0f )
238  return false;
239 
240  const float gamma = hu * m_cnu + hv * m_cnv;
241 
242  if( gamma < 0 )
243  return false;
244 
245  const float v = gamma;
246  const float u = beta;
247 
248  if( (u + v) > 1.0f )
249  return false;
250 
251  if( glm::dot( D, m_n ) > 0.0f )
252  return false;
253 
254  aHitInfo.m_tHit = t;
255  aHitInfo.m_HitPoint = aRay.at( t );
256 
257  // interpolate vertex normals with UVW using Gouraud's shading
258  aHitInfo.m_HitNormal = glm::normalize( (1.0f - u - v) * m_normal[0] +
259  u * m_normal[1] +
260  v * m_normal[2] );
261 
262  m_material->PerturbeNormal( aHitInfo.m_HitNormal, aRay, aHitInfo );
263 
264  aHitInfo.pHitObject = this;
265 
266  return true;
267 #undef ku
268 #undef kv
269 }
270 
271 
272 bool CTRIANGLE::IntersectP( const RAY &aRay,
273  float aMaxDistance ) const
274 {
276 #define ku s_modulo[m_k + 1]
277 #define kv s_modulo[m_k + 2]
278 
279  const SFVEC3F O = aRay.m_Origin;
280  const SFVEC3F D = aRay.m_Dir;
281  const SFVEC3F A = m_vertex[0];
282 
283  const float lnd = 1.0f / (D[m_k] + m_nu * D[ku] + m_nv * D[kv]);
284  const float t = (m_nd - O[m_k] - m_nu * O[ku] - m_nv * O[kv]) * lnd;
285 
286  if( !( (aMaxDistance > t) && (t > 0.0f) ) )
287  return false;
288 
289  const float hu = O[ku] + t * D[ku] - A[ku];
290  const float hv = O[kv] + t * D[kv] - A[kv];
291  const float beta = hv * m_bnu + hu * m_bnv;
292 
293  if( beta < 0.0f )
294  return false;
295 
296  const float gamma = hu * m_cnu + hv * m_cnv;
297 
298  if( gamma < 0.0f )
299  return false;
300 
301  const float v = gamma;
302  const float u = beta;
303 
304  if( (u + v) > 1.0f )
305  return false;
306 
307  if( glm::dot( D, m_n ) > 0.0f )
308  return false;
309 
310  return true;
311 #undef ku
312 #undef kv
313 }
314 
315 
316 bool CTRIANGLE::Intersects( const CBBOX &aBBox ) const
317 {
319  return m_bbox.Intersects( aBBox );
320 }
321 
322 
324 {
325  const unsigned int rgbC1 = m_vertexColorRGBA[0];
326  const unsigned int rgbC2 = m_vertexColorRGBA[1];
327  const unsigned int rgbC3 = m_vertexColorRGBA[2];
328 
329  const SFVEC3F c1 = SFVEC3F( (float)((rgbC1 >> 24) & 0xFF) / 255.0f,
330  (float)((rgbC1 >> 16) & 0xFF) / 255.0f,
331  (float)((rgbC1 >> 8) & 0xFF) / 255.0f );
332  const SFVEC3F c2 = SFVEC3F( (float)((rgbC2 >> 24) & 0xFF) / 255.0f,
333  (float)((rgbC2 >> 16) & 0xFF) / 255.0f,
334  (float)((rgbC2 >> 8) & 0xFF) / 255.0f );
335  const SFVEC3F c3 = SFVEC3F( (float)((rgbC3 >> 24) & 0xFF) / 255.0f,
336  (float)((rgbC3 >> 16) & 0xFF) / 255.0f,
337  (float)((rgbC3 >> 8) & 0xFF) / 255.0f );
338 
339  const float u = aHitInfo.m_UV.x;
340  const float v = aHitInfo.m_UV.y;
341  const float w = 1.0f - u - v;
342 
343  return w * c1 + u * c2 + v * c3;
344 }
float m_bnu
Definition: ctriangle.h:83
#define ku
SFVEC3F m_centroid
Definition: cobject.h:54
bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const override
Functions Intersect.
Definition: ctriangle.cpp:217
SFVEC3F m_n
Definition: ctriangle.h:78
SFVEC2F m_uv[3]
Definition: ctriangle.h:79
float m_nd
Definition: ctriangle.h:81
unsigned int m_vertexColorRGBA[3]
Definition: ctriangle.h:80
SFVEC3F at(float t) const
Definition: ray.h:65
float m_nv
Definition: ctriangle.h:81
bool Intersects(const CBBOX &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox.cpp:230
CBBOX m_bbox
Definition: cobject.h:53
bool IntersectP(const RAY &aRay, float aMaxDistance) const override
Functions Intersect for shadow test.
Definition: ctriangle.cpp:272
const CMATERIAL * m_material
Definition: cobject.h:56
bool Intersects(const CBBOX &aBBox) const override
Function Intersects.
Definition: ctriangle.cpp:316
Definition: ray.h:43
#define kv
#define abs(a)
Definition: auxiliary.h:84
float m_tHit
( 4) distance
Definition: hitinfo.h:43
void Set(const SFVEC3F &aPbMin, const SFVEC3F &aPbMax)
Function Set Set bounding box with new parameters.
Definition: cbbox.cpp:67
void Union(const SFVEC3F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox.cpp:105
SFVEC3F m_HitPoint
(12) hit position
Definition: hitinfo.h:49
void SetColor(const SFVEC3F &aColor)
Definition: ctriangle.cpp:161
float m_bnv
Definition: ctriangle.h:83
SFVEC3F m_vertex[3]
Definition: ctriangle.h:77
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
void pre_calc_const()
Definition: ctriangle.cpp:37
CTRIANGLE(const SFVEC3F &aV1, const SFVEC3F &aV2, const SFVEC3F &aV3)
Definition: ctriangle.cpp:101
void PerturbeNormal(SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo) const
Definition: cmaterial.cpp:88
unsigned int m_k
Definition: ctriangle.h:82
float m_cnu
Definition: ctriangle.h:84
void ScaleNextUp()
Function ScaleNextUp scales a bounding box to the next float representation making it larger...
Definition: cbbox.cpp:206
SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const override
Definition: ctriangle.cpp:323
const COBJECT * pHitObject
( 4) Object that was hitted
Definition: hitinfo.h:45
SFVEC3F m_Dir
Definition: ray.h:48
SFVEC3F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox.cpp:135
SFVEC3F m_Origin
Definition: ray.h:45
Implements a triangle ray intersection based on article http://www.flipcode.com/archives/Raytracing_T...
#define D(x)
Definition: ptree.cpp:41
Stores the hit information of a ray with a point on the surface of a object.
Definition: hitinfo.h:40
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
void SetUV(const SFVEC2F &aUV1, const SFVEC2F &aUV2, const SFVEC2F &aUV3)
Definition: ctriangle.cpp:205
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition: hitinfo.h:42
SFVEC3F m_normal[3]
Definition: ctriangle.h:76
float m_cnv
Definition: ctriangle.h:84
Class CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:40
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox.cpp:98
SFVEC2F m_UV
( 8) 2-D texture coordinates
Definition: hitinfo.h:46
static const unsigned int s_modulo[]
Definition: ctriangle.cpp:215
float m_nu
Definition: ctriangle.h:81