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 
101 CTRIANGLE::CTRIANGLE( const SFVEC3F& aV1, const SFVEC3F& aV2, const SFVEC3F& aV3 )
103 {
104  m_vertex[0] = aV1;
105  m_vertex[1] = aV2;
106  m_vertex[2] = aV3;
107 
108  m_vertexColorRGBA[0] = 0xFFFFFFFF;
109  m_vertexColorRGBA[1] = 0xFFFFFFFF;
110  m_vertexColorRGBA[2] = 0xFFFFFFFF;
111 
112  pre_calc_const();
113 }
114 
115 
117  const SFVEC3F& aV1, const SFVEC3F& aV2, const SFVEC3F& aV3, const SFVEC3F& aFaceNormal )
119 {
120  m_vertex[0] = aV1;
121  m_vertex[1] = aV2;
122  m_vertex[2] = aV3;
123 
124  m_vertexColorRGBA[0] = 0xFFFFFFFF;
125  m_vertexColorRGBA[1] = 0xFFFFFFFF;
126  m_vertexColorRGBA[2] = 0xFFFFFFFF;
127 
128  pre_calc_const();
129 
130  m_normal[0] = aFaceNormal;
131  m_normal[1] = aFaceNormal;
132  m_normal[2] = aFaceNormal;
133 }
134 
135 
136 CTRIANGLE::CTRIANGLE( const SFVEC3F& aV1, const SFVEC3F& aV2, const SFVEC3F& aV3,
137  const SFVEC3F& aN1, const SFVEC3F& aN2, const SFVEC3F& aN3 )
139 {
140  m_vertex[0] = aV1;
141  m_vertex[1] = aV2;
142  m_vertex[2] = aV3;
143 
144  m_vertexColorRGBA[0] = 0xFFFFFFFF;
145  m_vertexColorRGBA[1] = 0xFFFFFFFF;
146  m_vertexColorRGBA[2] = 0xFFFFFFFF;
147 
148  pre_calc_const();
149 
150  m_normal[0] = aN1;
151  m_normal[1] = aN2;
152  m_normal[2] = aN3;
153 }
154 
155 
156 void CTRIANGLE::SetColor( const SFVEC3F &aColor )
157 {
158  m_vertexColorRGBA[0] = ((unsigned int)(aColor.r * 255) << 24) |
159  ((unsigned int)(aColor.g * 255) << 16) |
160  ((unsigned int)(aColor.b * 255) << 8) | 0xFF;
163 }
164 
165 
166 void CTRIANGLE::SetColor( const SFVEC3F &aVC0,
167  const SFVEC3F &aVC1,
168  const SFVEC3F &aVC2 )
169 {
170  m_vertexColorRGBA[0] = ((unsigned int)(aVC0.r * 255) << 24) |
171  ((unsigned int)(aVC0.g * 255) << 16) |
172  ((unsigned int)(aVC0.b * 255) << 8) | 0xFF;
173  m_vertexColorRGBA[1] = ((unsigned int)(aVC1.r * 255) << 24) |
174  ((unsigned int)(aVC1.g * 255) << 16) |
175  ((unsigned int)(aVC1.b * 255) << 8) | 0xFF;
176  m_vertexColorRGBA[2] = ((unsigned int)(aVC2.r * 255) << 24) |
177  ((unsigned int)(aVC2.g * 255) << 16) |
178  ((unsigned int)(aVC2.b * 255) << 8) | 0xFF;
179 }
180 
181 
182 void CTRIANGLE::SetColor( unsigned int aFaceColorRGBA )
183 {
184  m_vertexColorRGBA[0] = aFaceColorRGBA;
185  m_vertexColorRGBA[1] = aFaceColorRGBA;
186  m_vertexColorRGBA[2] = aFaceColorRGBA;
187 }
188 
189 
190 void CTRIANGLE::SetColor( unsigned int aVertex1ColorRGBA,
191  unsigned int aVertex2ColorRGBA,
192  unsigned int aVertex3ColorRGBA )
193 {
194  m_vertexColorRGBA[0] = aVertex1ColorRGBA;
195  m_vertexColorRGBA[1] = aVertex2ColorRGBA;
196  m_vertexColorRGBA[2] = aVertex3ColorRGBA;
197 }
198 
199 
200 void CTRIANGLE::SetUV( const SFVEC2F &aUV1,
201  const SFVEC2F &aUV2,
202  const SFVEC2F &aUV3 )
203 {
204  m_uv[0] = aUV1;
205  m_uv[1] = aUV2;
206  m_uv[2] = aUV3;
207 }
208 
209 
210 static const unsigned int s_modulo[] = { 0, 1, 2, 0, 1 };
211 
212 bool CTRIANGLE::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
213 {
215 #define ku s_modulo[m_k + 1]
216 #define kv s_modulo[m_k + 2]
217 
218  const SFVEC3F &O = aRay.m_Origin;
219  const SFVEC3F &D = aRay.m_Dir;
220  const SFVEC3F &A = m_vertex[0];
221 
222  const float lnd = 1.0f / (D[m_k] + m_nu * D[ku] + m_nv * D[kv]);
223  const float t = (m_nd - O[m_k] - m_nu * O[ku] - m_nv * O[kv]) * lnd;
224 
225  if( !( (aHitInfo.m_tHit > t) && (t > 0.0f) ) )
226  return false;
227 
228  const float hu = O[ku] + t * D[ku] - A[ku];
229  const float hv = O[kv] + t * D[kv] - A[kv];
230  const float beta = hv * m_bnu + hu * m_bnv;
231 
232  if( beta < 0.0f )
233  return false;
234 
235  const float gamma = hu * m_cnu + hv * m_cnv;
236 
237  if( gamma < 0 )
238  return false;
239 
240  const float v = gamma;
241  const float u = beta;
242 
243  if( (u + v) > 1.0f )
244  return false;
245 
246  if( glm::dot( D, m_n ) > 0.0f )
247  return false;
248 
249  aHitInfo.m_tHit = t;
250  aHitInfo.m_HitPoint = aRay.at( t );
251 
252  // interpolate vertex normals with UVW using Gouraud's shading
253  aHitInfo.m_HitNormal = glm::normalize( (1.0f - u - v) * m_normal[0] +
254  u * m_normal[1] +
255  v * m_normal[2] );
256 
257  m_material->PerturbeNormal( aHitInfo.m_HitNormal, aRay, aHitInfo );
258 
259  aHitInfo.pHitObject = this;
260 
261  return true;
262 #undef ku
263 #undef kv
264 }
265 
266 
267 bool CTRIANGLE::IntersectP( const RAY &aRay,
268  float aMaxDistance ) const
269 {
271 #define ku s_modulo[m_k + 1]
272 #define kv s_modulo[m_k + 2]
273 
274  const SFVEC3F O = aRay.m_Origin;
275  const SFVEC3F D = aRay.m_Dir;
276  const SFVEC3F A = m_vertex[0];
277 
278  const float lnd = 1.0f / (D[m_k] + m_nu * D[ku] + m_nv * D[kv]);
279  const float t = (m_nd - O[m_k] - m_nu * O[ku] - m_nv * O[kv]) * lnd;
280 
281  if( !( (aMaxDistance > t) && (t > 0.0f) ) )
282  return false;
283 
284  const float hu = O[ku] + t * D[ku] - A[ku];
285  const float hv = O[kv] + t * D[kv] - A[kv];
286  const float beta = hv * m_bnu + hu * m_bnv;
287 
288  if( beta < 0.0f )
289  return false;
290 
291  const float gamma = hu * m_cnu + hv * m_cnv;
292 
293  if( gamma < 0.0f )
294  return false;
295 
296  const float v = gamma;
297  const float u = beta;
298 
299  if( (u + v) > 1.0f )
300  return false;
301 
302  if( glm::dot( D, m_n ) > 0.0f )
303  return false;
304 
305  return true;
306 #undef ku
307 #undef kv
308 }
309 
310 
311 bool CTRIANGLE::Intersects( const CBBOX &aBBox ) const
312 {
314  return m_bbox.Intersects( aBBox );
315 }
316 
317 
319 {
320  const unsigned int rgbC1 = m_vertexColorRGBA[0];
321  const unsigned int rgbC2 = m_vertexColorRGBA[1];
322  const unsigned int rgbC3 = m_vertexColorRGBA[2];
323 
324  const SFVEC3F c1 = SFVEC3F( (float)((rgbC1 >> 24) & 0xFF) / 255.0f,
325  (float)((rgbC1 >> 16) & 0xFF) / 255.0f,
326  (float)((rgbC1 >> 8) & 0xFF) / 255.0f );
327  const SFVEC3F c2 = SFVEC3F( (float)((rgbC2 >> 24) & 0xFF) / 255.0f,
328  (float)((rgbC2 >> 16) & 0xFF) / 255.0f,
329  (float)((rgbC2 >> 8) & 0xFF) / 255.0f );
330  const SFVEC3F c3 = SFVEC3F( (float)((rgbC3 >> 24) & 0xFF) / 255.0f,
331  (float)((rgbC3 >> 16) & 0xFF) / 255.0f,
332  (float)((rgbC3 >> 8) & 0xFF) / 255.0f );
333 
334  const float u = aHitInfo.m_UV.x;
335  const float v = aHitInfo.m_UV.y;
336  const float w = 1.0f - u - v;
337 
338  return w * c1 + u * c2 + v * c3;
339 }
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:212
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
bool Intersects(const CBBOX &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox.cpp:230
float m_nv
Definition: ctriangle.h:81
CBBOX m_bbox
Definition: cobject.h:53
bool IntersectP(const RAY &aRay, float aMaxDistance) const override
Functions Intersect for shadow test.
Definition: ctriangle.cpp:267
const CMATERIAL * m_material
Definition: cobject.h:56
bool Intersects(const CBBOX &aBBox) const override
Function Intersects.
Definition: ctriangle.cpp:311
Definition: ray.h:67
#define kv
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
SFVEC3F at(float t) const
Definition: ray.h:89
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
SFVEC3F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox.cpp:135
void SetColor(const SFVEC3F &aColor)
Definition: ctriangle.cpp:156
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
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:318
const COBJECT * pHitObject
( 4) Object that was hitted
Definition: hitinfo.h:45
SFVEC3F m_Dir
Definition: ray.h:72
SFVEC3F m_Origin
Definition: ray.h:69
Implements a triangle ray intersection based on article http://www.flipcode.com/archives/Raytracing_T...
OBJECT3D_TYPE
Definition: cobject.h:38
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:200
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
void PerturbeNormal(SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo) const
Definition: cmaterial.cpp:97
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:210
float m_nu
Definition: ctriangle.h:81