KiCad PCB EDA Suite
Loading...
Searching...
No Matches
layer_item_3d.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-2022 Mario Luzeiro <[email protected]>
5 * Copyright (C) 1992-2022 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#include "layer_item_3d.h"
26#include "3d_fastmath.h"
27#include <wx/debug.h>
28#include <advanced_config.h>
29
30
31extern float g_BevelThickness3DU;
32
33
34LAYER_ITEM::LAYER_ITEM( const OBJECT_2D* aObject2D, float aZMin, float aZMax ) :
36 m_object2d( aObject2D )
37{
38 wxASSERT( aObject2D );
39
40 BBOX_2D bbox2d = m_object2d->GetBBox();
41 bbox2d.ScaleNextUp();
42 bbox2d.ScaleNextUp();
43
44 m_bbox.Reset();
45 m_bbox.Set( SFVEC3F( bbox2d.Min().x, bbox2d.Min().y, aZMin ),
46 SFVEC3F( bbox2d.Max().x, bbox2d.Max().y, aZMax ) );
48 m_bbox.Scale( 1.0001f );
49
50 m_centroid = SFVEC3F( aObject2D->GetCentroid().x, aObject2D->GetCentroid().y,
51 ( aZMax + aZMin ) * 0.5f );
52}
53
54
55bool LAYER_ITEM::Intersect( const RAY& aRay, HITINFO& aHitInfo ) const
56{
57 float tBBoxStart;
58 float tBBoxEnd;
59
60 if( !m_bbox.Intersect( aRay, &tBBoxStart, &tBBoxEnd ) )
61 return false;
62
63 if( tBBoxStart >= aHitInfo.m_tHit )
64 return false;
65
66 if( fabs( tBBoxStart - tBBoxEnd ) <= FLT_EPSILON )
67 return false;
68
69 const bool startedInside = m_bbox.Inside( aRay.m_Origin );
70
71 if( !startedInside )
72 {
73 float tTop = FLT_MAX;
74 float tBot = FLT_MAX;
75 bool hit_top = false;
76 bool hit_bot = false;
77
78 if( (float) fabs( aRay.m_Dir.z ) > FLT_EPSILON )
79 {
80 tBot = ( m_bbox.Min().z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
81 tTop = ( m_bbox.Max().z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
82
83 float tBBoxStartAdjusted = NextFloatUp( tBBoxStart );
84
85 if( tBot > FLT_EPSILON )
86 {
87 hit_bot = tBot <= tBBoxStartAdjusted;
88 tBot = NextFloatDown( tBot );
89 }
90
91 if( tTop > FLT_EPSILON )
92 {
93 hit_top = tTop <= tBBoxStartAdjusted;
94 tTop = NextFloatDown( tTop );
95 }
96 }
97
98 SFVEC2F topHitPoint2d;
99 SFVEC2F botHitPoint2d;
100
101 if( hit_top )
102 topHitPoint2d = SFVEC2F( aRay.m_Origin.x + aRay.m_Dir.x * tTop,
103 aRay.m_Origin.y + aRay.m_Dir.y * tTop );
104
105 if( hit_bot )
106 botHitPoint2d = SFVEC2F( aRay.m_Origin.x + aRay.m_Dir.x * tBot,
107 aRay.m_Origin.y + aRay.m_Dir.y * tBot );
108
109 if( hit_top && hit_bot )
110 {
111 if( tBot < tTop )
112 {
113 if( m_object2d->IsPointInside( botHitPoint2d ) )
114 {
115 if( tBot < aHitInfo.m_tHit )
116 {
117 aHitInfo.m_tHit = tBot;
118 aHitInfo.m_HitPoint = aRay.at( tBot );
119 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, -1.0f );
120 aHitInfo.pHitObject = this;
121
122 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
123
124 return true;
125 }
126
127 return false;
128 }
129 }
130 else
131 {
132 if( m_object2d->IsPointInside( topHitPoint2d ) )
133 {
134 if( tTop < aHitInfo.m_tHit )
135 {
136 aHitInfo.m_tHit = tTop;
137 aHitInfo.m_HitPoint = aRay.at( tTop );
138 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, 1.0f );
139 aHitInfo.pHitObject = this;
140
141 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
142
143 return true;
144 }
145
146 return false;
147 }
148 }
149 }
150 else
151 {
152 if( hit_top )
153 {
154 if( tTop < tBot )
155 {
156 if( m_object2d->IsPointInside( topHitPoint2d ) )
157 {
158 if( tTop < aHitInfo.m_tHit )
159 {
160 aHitInfo.m_tHit = tTop;
161 aHitInfo.m_HitPoint = aRay.at( tTop );
162 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, 1.0f );
163 aHitInfo.pHitObject = this;
164
165 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
166
167 return true;
168 }
169
170 return false;
171 }
172 }
173 }
174 else
175 {
176 if( hit_bot )
177 {
178 if( tBot < tTop )
179 {
180 if( m_object2d->IsPointInside( botHitPoint2d ) )
181 {
182 if( tBot < aHitInfo.m_tHit )
183 {
184 aHitInfo.m_tHit = tBot;
185 aHitInfo.m_HitPoint = aRay.at( tBot );
186 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, -1.0f );
187 aHitInfo.pHitObject = this;
188
189 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
190
191 return true;
192 }
193
194 return false;
195 }
196 }
197 }
198 else
199 {
200 // At this point, the ray miss the two planes but it still
201 // hits the box. It means that the rays are "(almost)parallel"
202 // to the planes, so must calc the intersection
203 }
204 }
205 }
206
207 SFVEC3F boxHitPointStart = aRay.at( tBBoxStart );
208 SFVEC3F boxHitPointEnd = aRay.at( tBBoxEnd );
209
210 SFVEC2F boxHitPointStart2D( boxHitPointStart.x, boxHitPointStart.y );
211 SFVEC2F boxHitPointEnd2D( boxHitPointEnd.x, boxHitPointEnd.y );
212
213 float tOut;
214 SFVEC2F outNormal;
215 RAYSEG2D raySeg( boxHitPointStart2D, boxHitPointEnd2D );
216
217 if( m_object2d->Intersect( raySeg, &tOut, &outNormal ) )
218 {
219 // The hitT is a hit value for the segment length 'start' - 'end',
220 // so it ranges from 0.0 - 1.0. We now convert it to a 3D hit position
221 // and calculate the real hitT of the ray.
222 SFVEC3F hitPoint = boxHitPointStart + ( boxHitPointEnd - boxHitPointStart ) * tOut;
223
224 const float t = glm::length( hitPoint - aRay.m_Origin );
225
226 if( t < aHitInfo.m_tHit )
227 {
228 aHitInfo.m_tHit = t;
229 aHitInfo.m_HitPoint = hitPoint;
230 aHitInfo.pHitObject = this;
231
232 const float zNormalDir = hit_top?1.0f:hit_bot?-1.0f:0.0f;
233
234 if( ( outNormal.x == 0.0f ) && ( outNormal.y == 0.0f ) )
235 {
236 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, zNormalDir );
237 }
238 else
239 {
240 // Calculate smooth bevel normal
241 float zBend = 0.0f;
242
243 if( hit_top || hit_bot )
244 {
245 float zDistanceToTopOrBot;
246
247 // Calculate the distance from hitpoint z to the Max/Min z of the layer
248 if( hit_top )
249 {
250 zDistanceToTopOrBot = ( m_bbox.Max().z - hitPoint.z );
251 }
252 else
253 {
254 zDistanceToTopOrBot = ( hitPoint.z - m_bbox.Min().z );
255 }
256
257 // For items that are > than g_BevelThickness3DU
258 // (eg on board vias / plated holeS) use a factor based on m_bbox.GetExtent().z
259 const float bevelThickness = glm::max( g_BevelThickness3DU,
260 m_bbox.GetExtent().z *
262
263 if( ( zDistanceToTopOrBot > 0.0f ) && ( zDistanceToTopOrBot < bevelThickness ) )
264 {
265 // Invert and Normalize the distance 0..1
266 zBend = ( bevelThickness - zDistanceToTopOrBot ) / bevelThickness;
267 }
268 }
269
270 const SFVEC3F normalLateral = SFVEC3F( outNormal.x, outNormal.y, 0.0f );
271 const SFVEC3F normalTopBot = SFVEC3F( 0.0f, 0.0f, zNormalDir );
272
273 // Interpolate between the regular lateral normal and the top/bot normal
274 aHitInfo.m_HitNormal = glm::mix( normalLateral, normalTopBot, zBend );
275 }
276
277 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
278
279 return true;
280 }
281 }
282
283 return false;
284 }
285 else
286 {
288 // Disabled due to refraction artifacts
289 // this will mostly happen inside the board body
290#if 0
291 // Started inside
292 const SFVEC3F boxHitPointStart = aRay.at( tBBoxStart );
293 const SFVEC3F boxHitPointEnd = aRay.at( tBBoxEnd );
294
295 const SFVEC2F boxHitPointStart2D( boxHitPointStart.x, boxHitPointStart.y );
296
297 const SFVEC2F boxHitPointEnd2D( boxHitPointEnd.x, boxHitPointEnd.y );
298
299 if( !( m_object2d->IsPointInside( boxHitPointStart2D ) &&
300 m_object2d->IsPointInside( boxHitPointEnd2D ) ) )
301 return false;
302
303 float tOut;
304 SFVEC2F outNormal;
305 RAYSEG2D raySeg( boxHitPointStart2D, boxHitPointEnd2D );
306
307 if( ( m_object2d->IsPointInside( boxHitPointStart2D ) &&
308 m_object2d->IsPointInside( boxHitPointEnd2D ) ) )
309 {
310 if( tBBoxEnd < aHitInfo.m_tHit )
311 {
312 aHitInfo.m_tHit = tBBoxEnd;
313 aHitInfo.m_HitPoint = aRay.at( tBBoxEnd );
314 aHitInfo.pHitObject = this;
315
316 if( aRay.m_Dir.z > 0.0f )
317 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, -1.0f );
318 else
319 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, 1.0f );
320
321 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
322
323 return true;
324 }
325 }
326 else
327 {
328 if( m_object2d->Intersect( raySeg, &tOut, &outNormal ) )
329 {
330 // The hitT is a hit value for the segment length 'start' - 'end',
331 // so it ranges from 0.0 - 1.0. We now convert it to a 3D hit position
332 // and calculate the real hitT of the ray.
333 const SFVEC3F hitPoint = boxHitPointStart +
334 ( boxHitPointEnd - boxHitPointStart ) * tOut;
335
336 const float t = glm::length( hitPoint - aRay.m_Origin );
337
338 if( t < aHitInfo.m_tHit )
339 {
340 aHitInfo.m_tHit = t;
341 aHitInfo.m_HitPoint = hitPoint;
342 aHitInfo.m_HitNormal = SFVEC3F( outNormal.x, outNormal.y, 0.0f );
343 aHitInfo.pHitObject = this;
344
345 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
346
347 return true;
348 }
349 }
350 }
351#endif
352 }
353
354 return false;
355}
356
357
358bool LAYER_ITEM::IntersectP( const RAY& aRay, float aMaxDistance ) const
359{
360 float tBBoxStart;
361 float tBBoxEnd;
362
363 if( !m_bbox.Intersect( aRay, &tBBoxStart, &tBBoxEnd ) )
364 return false;
365
366 if( ( tBBoxStart > aMaxDistance ) || ( fabs( tBBoxStart - tBBoxEnd ) < FLT_EPSILON ) )
367 return false;
368
369 float tTop = FLT_MAX;
370 float tBot = FLT_MAX;
371 bool hit_top = false;
372 bool hit_bot = false;
373
374 if( (float)fabs( aRay.m_Dir.z ) > FLT_EPSILON )
375 {
376 tBot = ( m_bbox.Min().z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
377 tTop = ( m_bbox.Max().z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
378
379 const float tBBoxStartAdjusted = NextFloatUp( tBBoxStart );
380
381 if( tBot > FLT_EPSILON )
382 {
383 hit_bot = tBot <= tBBoxStartAdjusted;
384 tBot = NextFloatDown( tBot );
385 }
386
387 if( tTop > FLT_EPSILON )
388 {
389 hit_top = tTop <= tBBoxStartAdjusted;
390 tTop = NextFloatDown( tTop );
391 }
392 }
393
394 tBBoxStart = NextFloatDown( tBBoxStart );
395 tBBoxEnd = NextFloatUp( tBBoxEnd );
396
397 SFVEC2F topHitPoint2d;
398 SFVEC2F botHitPoint2d;
399
400 if( hit_top )
401 topHitPoint2d = SFVEC2F( aRay.m_Origin.x + aRay.m_Dir.x * tTop,
402 aRay.m_Origin.y + aRay.m_Dir.y * tTop );
403
404 if( hit_bot )
405 botHitPoint2d = SFVEC2F( aRay.m_Origin.x + aRay.m_Dir.x * tBot,
406 aRay.m_Origin.y + aRay.m_Dir.y * tBot );
407
408 if( hit_top && hit_bot )
409 {
410 if( tBot < tTop )
411 {
412 if( m_object2d->IsPointInside( botHitPoint2d ) )
413 {
414 if( tBot < aMaxDistance )
415 return true;
416
417 return false;
418 }
419 }
420 else
421 {
422 if( m_object2d->IsPointInside( topHitPoint2d ) )
423 {
424 if( tTop < aMaxDistance )
425 return true;
426
427 return false;
428 }
429 }
430 }
431 else
432 {
433 if( hit_top )
434 {
435 if( tTop < tBot )
436 {
437 if( m_object2d->IsPointInside( topHitPoint2d ) )
438 {
439 if( tTop < aMaxDistance )
440 return true;
441
442 return false;
443 }
444 }
445 }
446 else
447 {
448 if( hit_bot )
449 {
450 if( tBot < tTop )
451 {
452 if( m_object2d->IsPointInside( botHitPoint2d ) )
453 {
454 if( tBot < aMaxDistance )
455 return true;
456
457 return false;
458 }
459 }
460 }
461 else
462 {
463 // At this point, the ray miss the two planes but it still
464 // hits the box. It means that the rays are "(almost)parallel"
465 // to the planes, so must calc the intersection
466 }
467 }
468 }
469
470 SFVEC3F boxHitPointStart = aRay.at( tBBoxStart );
471 SFVEC3F boxHitPointEnd = aRay.at( tBBoxEnd );
472
473 SFVEC2F boxHitPointStart2D( boxHitPointStart.x, boxHitPointStart.y );
474
475 SFVEC2F boxHitPointEnd2D( boxHitPointEnd.x, boxHitPointEnd.y );
476
477 float tOut;
478 SFVEC2F outNormal;
479 RAYSEG2D raySeg( boxHitPointStart2D, boxHitPointEnd2D );
480
481 if( m_object2d->Intersect( raySeg, &tOut, &outNormal ) )
482 {
483 //if( (tOut > FLT_EPSILON) && (tOut < 1.0f) )
484 {
485 // The hitT is a hit value for the segment length 'start' - 'end',
486 // so it ranges from 0.0 - 1.0. We now convert it to a 3D hit position
487 // and calculate the real hitT of the ray.
488 const SFVEC3F hitPoint = boxHitPointStart +
489 ( boxHitPointEnd - boxHitPointStart ) * tOut;
490 const float t = glm::length( hitPoint - aRay.m_Origin );
491
492 if( ( t < aMaxDistance ) && ( t > FLT_EPSILON ) )
493 return true;
494 }
495 }
496
497 return false;
498}
499
500
501bool LAYER_ITEM::Intersects( const BBOX_3D& aBBox ) const
502{
503 if( !m_bbox.Intersects( aBBox ) )
504 return false;
505
506 const BBOX_2D bbox2D( SFVEC2F( aBBox.Min().x, aBBox.Min().y ),
507 SFVEC2F( aBBox.Max().x, aBBox.Max().y ) );
508
509 return m_object2d->Intersects( bbox2D );
510}
511
512
513SFVEC3F LAYER_ITEM::GetDiffuseColor( const HITINFO& /* aHitInfo */ ) const
514{
515 return m_diffusecolor;
516}
Defines math related functions.
float NextFloatDown(float v)
Definition: 3d_fastmath.h:157
float NextFloatUp(float v)
Definition: 3d_fastmath.h:136
float g_BevelThickness3DU
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
bool Intersects(const BBOX_3D &aBBox) const override
SFVEC3F m_diffusecolor
Definition: layer_item_3d.h:48
SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const override
const OBJECT_2D * m_object2d
Definition: layer_item_3d.h:45
LAYER_ITEM(const OBJECT_2D *aObject2D, float aZMin, float aZMax)
bool IntersectP(const RAY &aRay, float aMaxDistance) const override
bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const override
void Generate(SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo) const
Definition: material.cpp:89
virtual bool Intersects(const BBOX_2D &aBBox) const =0
a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)
const SFVEC2F & GetCentroid() const
Definition: object_2d.h:105
const BBOX_2D & GetBBox() const
Definition: object_2d.h:103
virtual bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const =0
virtual bool IsPointInside(const SFVEC2F &aPoint) const =0
BBOX_3D m_bbox
Definition: object_3d.h:97
SFVEC3F m_centroid
Definition: object_3d.h:98
const MATERIAL * m_material
Definition: object_3d.h:100
double m_3DRT_BevelExtentFactor
3D-Viewer raytracing factor applied to Extent.z of the item layer.
float g_BevelThickness3DU
OBJECT_3D_TYPE
Definition: object_3d.h:39
Manage a bounding box defined by two SFVEC2F min max points.
Definition: bbox_2d.h:42
const SFVEC2F & Min() const
Definition: bbox_2d.h:175
const SFVEC2F & Max() const
Definition: bbox_2d.h:180
void ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_2d.cpp:162
Manage a bounding box defined by two SFVEC3F min max points.
Definition: bbox_3d.h:42
void ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_3d.cpp:194
const SFVEC3F GetExtent() const
Definition: bbox_3d.cpp:145
bool Intersect(const RAY &aRay, float *t) const
Definition: bbox_3d_ray.cpp:46
const SFVEC3F & Min() const
Return the minimum vertex pointer.
Definition: bbox_3d.h:191
const SFVEC3F & Max() const
Return the maximum vertex pointer.
Definition: bbox_3d.h:198
void Set(const SFVEC3F &aPbMin, const SFVEC3F &aPbMax)
Set bounding box with new parameters.
Definition: bbox_3d.cpp:68
void Reset()
Reset the bounding box to zero and de-initialize it.
Definition: bbox_3d.cpp:95
bool Intersects(const BBOX_3D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_3d.cpp:218
bool Inside(const SFVEC3F &aPoint) const
Check if a point is inside this bounding box.
Definition: bbox_3d.cpp:231
void Scale(float aScale)
Scales a bounding box by its center.
Definition: bbox_3d.cpp:182
Stores the hit information of a ray with a point on the surface of a object.
Definition: hitinfo.h:36
float m_tHit
( 4) distance
Definition: hitinfo.h:38
const OBJECT_3D * pHitObject
( 4) Object that was hitted
Definition: hitinfo.h:40
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition: hitinfo.h:37
SFVEC3F m_HitPoint
(12) hit position
Definition: hitinfo.h:44
Definition: ray.h:106
Definition: ray.h:63
SFVEC3F m_Dir
Definition: ray.h:67
SFVEC3F m_InvDir
Definition: ray.h:70
SFVEC3F m_Origin
Definition: ray.h:64
SFVEC3F at(float t) const
Definition: ray.h:84
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44