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 The 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
259 // m_bbox.GetExtent().z
260 const float bevelThickness = glm::max(
262 m_bbox.GetExtent().z
264
265 if( ( zDistanceToTopOrBot > 0.0f )
266 && ( zDistanceToTopOrBot < bevelThickness ) )
267 {
268 // Invert and Normalize the distance 0..1
269 zBend = ( bevelThickness - zDistanceToTopOrBot ) / bevelThickness;
270 }
271 }
272
273 const SFVEC3F normalLateral = SFVEC3F( outNormal.x, outNormal.y, 0.0f );
274 const SFVEC3F normalTopBot = SFVEC3F( 0.0f, 0.0f, zNormalDir );
275
276 // Interpolate between the regular lateral normal and the top/bot normal
277 aHitInfo.m_HitNormal = glm::mix( normalLateral, normalTopBot, zBend );
278 }
279
280 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
281
282 return true;
283 }
284 }
285
286 return false;
287 }
288 else
289 {
291 // Disabled due to refraction artifacts
292 // this will mostly happen inside the board body
293#if 0
294 // Started inside
295 const SFVEC3F boxHitPointStart = aRay.at( tBBoxStart );
296 const SFVEC3F boxHitPointEnd = aRay.at( tBBoxEnd );
297
298 const SFVEC2F boxHitPointStart2D( boxHitPointStart.x, boxHitPointStart.y );
299
300 const SFVEC2F boxHitPointEnd2D( boxHitPointEnd.x, boxHitPointEnd.y );
301
302 if( !( m_object2d->IsPointInside( boxHitPointStart2D ) &&
303 m_object2d->IsPointInside( boxHitPointEnd2D ) ) )
304 return false;
305
306 float tOut;
307 SFVEC2F outNormal;
308 RAYSEG2D raySeg( boxHitPointStart2D, boxHitPointEnd2D );
309
310 if( ( m_object2d->IsPointInside( boxHitPointStart2D )
311 && m_object2d->IsPointInside( boxHitPointEnd2D ) ) )
312 {
313 if( tBBoxEnd < aHitInfo.m_tHit )
314 {
315 aHitInfo.m_tHit = tBBoxEnd;
316 aHitInfo.m_HitPoint = aRay.at( tBBoxEnd );
317 aHitInfo.pHitObject = this;
318
319 if( aRay.m_Dir.z > 0.0f )
320 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, -1.0f );
321 else
322 aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, 1.0f );
323
324 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
325
326 return true;
327 }
328 }
329 else
330 {
331 if( m_object2d->Intersect( raySeg, &tOut, &outNormal ) )
332 {
333 // The hitT is a hit value for the segment length 'start' - 'end',
334 // so it ranges from 0.0 - 1.0. We now convert it to a 3D hit position
335 // and calculate the real hitT of the ray.
336 const SFVEC3F hitPoint = boxHitPointStart +
337 ( boxHitPointEnd - boxHitPointStart ) * tOut;
338
339 const float t = glm::length( hitPoint - aRay.m_Origin );
340
341 if( t < aHitInfo.m_tHit )
342 {
343 aHitInfo.m_tHit = t;
344 aHitInfo.m_HitPoint = hitPoint;
345 aHitInfo.m_HitNormal = SFVEC3F( outNormal.x, outNormal.y, 0.0f );
346 aHitInfo.pHitObject = this;
347
348 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
349
350 return true;
351 }
352 }
353 }
354#endif
355 }
356
357 return false;
358}
359
360
361bool LAYER_ITEM::IntersectP( const RAY& aRay, float aMaxDistance ) const
362{
363 float tBBoxStart;
364 float tBBoxEnd;
365
366 if( !m_bbox.Intersect( aRay, &tBBoxStart, &tBBoxEnd ) )
367 return false;
368
369 if( ( tBBoxStart > aMaxDistance ) || ( fabs( tBBoxStart - tBBoxEnd ) < FLT_EPSILON ) )
370 return false;
371
372 float tTop = FLT_MAX;
373 float tBot = FLT_MAX;
374 bool hit_top = false;
375 bool hit_bot = false;
376
377 if( (float)fabs( aRay.m_Dir.z ) > FLT_EPSILON )
378 {
379 tBot = ( m_bbox.Min().z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
380 tTop = ( m_bbox.Max().z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
381
382 const float tBBoxStartAdjusted = NextFloatUp( tBBoxStart );
383
384 if( tBot > FLT_EPSILON )
385 {
386 hit_bot = tBot <= tBBoxStartAdjusted;
387 tBot = NextFloatDown( tBot );
388 }
389
390 if( tTop > FLT_EPSILON )
391 {
392 hit_top = tTop <= tBBoxStartAdjusted;
393 tTop = NextFloatDown( tTop );
394 }
395 }
396
397 tBBoxStart = NextFloatDown( tBBoxStart );
398 tBBoxEnd = NextFloatUp( tBBoxEnd );
399
400 SFVEC2F topHitPoint2d;
401 SFVEC2F botHitPoint2d;
402
403 if( hit_top )
404 topHitPoint2d = SFVEC2F( aRay.m_Origin.x + aRay.m_Dir.x * tTop,
405 aRay.m_Origin.y + aRay.m_Dir.y * tTop );
406
407 if( hit_bot )
408 botHitPoint2d = SFVEC2F( aRay.m_Origin.x + aRay.m_Dir.x * tBot,
409 aRay.m_Origin.y + aRay.m_Dir.y * tBot );
410
411 if( hit_top && hit_bot )
412 {
413 if( tBot < tTop )
414 {
415 if( m_object2d->IsPointInside( botHitPoint2d ) )
416 {
417 if( tBot < aMaxDistance )
418 return true;
419
420 return false;
421 }
422 }
423 else
424 {
425 if( m_object2d->IsPointInside( topHitPoint2d ) )
426 {
427 if( tTop < aMaxDistance )
428 return true;
429
430 return false;
431 }
432 }
433 }
434 else
435 {
436 if( hit_top )
437 {
438 if( tTop < tBot )
439 {
440 if( m_object2d->IsPointInside( topHitPoint2d ) )
441 {
442 if( tTop < aMaxDistance )
443 return true;
444
445 return false;
446 }
447 }
448 }
449 else
450 {
451 if( hit_bot )
452 {
453 if( tBot < tTop )
454 {
455 if( m_object2d->IsPointInside( botHitPoint2d ) )
456 {
457 if( tBot < aMaxDistance )
458 return true;
459
460 return false;
461 }
462 }
463 }
464 else
465 {
466 // At this point, the ray miss the two planes but it still
467 // hits the box. It means that the rays are "(almost)parallel"
468 // to the planes, so must calc the intersection
469 }
470 }
471 }
472
473 SFVEC3F boxHitPointStart = aRay.at( tBBoxStart );
474 SFVEC3F boxHitPointEnd = aRay.at( tBBoxEnd );
475
476 SFVEC2F boxHitPointStart2D( boxHitPointStart.x, boxHitPointStart.y );
477
478 SFVEC2F boxHitPointEnd2D( boxHitPointEnd.x, boxHitPointEnd.y );
479
480 float tOut;
481 SFVEC2F outNormal;
482 RAYSEG2D raySeg( boxHitPointStart2D, boxHitPointEnd2D );
483
484 if( m_object2d->Intersect( raySeg, &tOut, &outNormal ) )
485 {
486 //if( (tOut > FLT_EPSILON) && (tOut < 1.0f) )
487 {
488 // The hitT is a hit value for the segment length 'start' - 'end',
489 // so it ranges from 0.0 - 1.0. We now convert it to a 3D hit position
490 // and calculate the real hitT of the ray.
491 const SFVEC3F hitPoint = boxHitPointStart +
492 ( boxHitPointEnd - boxHitPointStart ) * tOut;
493 const float t = glm::length( hitPoint - aRay.m_Origin );
494
495 if( ( t < aMaxDistance ) && ( t > FLT_EPSILON ) )
496 return true;
497 }
498 }
499
500 return false;
501}
502
503
504bool LAYER_ITEM::Intersects( const BBOX_3D& aBBox ) const
505{
506 if( !m_bbox.Intersects( aBBox ) )
507 return false;
508
509 const BBOX_2D bbox2D( SFVEC2F( aBBox.Min().x, aBBox.Min().y ),
510 SFVEC2F( aBBox.Max().x, aBBox.Max().y ) );
511
512 return m_object2d->Intersects( bbox2D );
513}
514
515
516SFVEC3F LAYER_ITEM::GetDiffuseColor( const HITINFO& /* aHitInfo */ ) const
517{
518 return m_diffusecolor;
519}
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:43
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:192
const SFVEC3F & Max() const
Return the maximum vertex pointer.
Definition: bbox_3d.h:199
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