KiCad PCB EDA Suite
Loading...
Searching...
No Matches
layer_item_2d.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 <[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, see <https://www.gnu.org/licenses/>.
19 */
20
24
25#include "layer_item_2d.h"
26#include "../ray.h"
27#include "3d_fastmath.h"
28#include <wx/debug.h>
29
30
31LAYER_ITEM_2D::LAYER_ITEM_2D( const OBJECT_2D* aObjectA, std::vector<const OBJECT_2D*>* aObjectB,
32 const OBJECT_2D* aObjectC, const BOARD_ITEM& aBoardItem ) :
33 OBJECT_2D( OBJECT_2D_TYPE::CSG, aBoardItem ),
34 m_objectA( aObjectA ),
35 m_objectB( aObjectB ),
36 m_objectC( aObjectC )
37{
38 wxASSERT( aObjectA );
39
40 m_bbox.Reset();
41 m_bbox.Set( aObjectA->GetBBox() );
42 m_bbox.ScaleNextUp();
43 m_centroid = m_bbox.GetCenter();
44
45 wxASSERT( m_bbox.IsInitialized() );
46}
47
48
50{
51 if( ( (void*) m_objectB != CSGITEM_EMPTY ) && ( (void*) m_objectB != CSGITEM_FULL ) )
52 {
53 delete m_objectB;
54 m_objectB = nullptr;
55 }
56}
57
58
59bool LAYER_ITEM_2D::Intersects( const BBOX_2D& aBBox ) const
60{
61 return m_bbox.Intersects( aBBox );
62}
63
64
65bool LAYER_ITEM_2D::Overlaps( const BBOX_2D& aBBox ) const
66{
67 // NOT IMPLEMENTED, why?
68 return false;
69}
70
71
72// Based on ideas and implementation by Nick Chapman
73// http://homepages.paradise.net.nz/nickamy/raytracer/raytracer.htm
74bool LAYER_ITEM_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
75{
76 if( m_objectA->GetObjectType() == OBJECT_2D_TYPE::DUMMYBLOCK )
77 return false;
78
79 SFVEC2F currentRayPos = aSegRay.m_Start;
80 SFVEC2F currentNormal;
81 RAYSEG2D currentRay = aSegRay;
82
83 if( !m_objectA->IsPointInside( aSegRay.m_Start ) )
84 {
85 //move ray point to start of main object
86 float tmpRayDist;
87
88 if( !m_objectA->Intersect( aSegRay, &tmpRayDist, &currentNormal ) )
89 return false;
90
91 currentRayPos = aSegRay.atNormalized( tmpRayDist + 0.003f );
92
93 currentRay = RAYSEG2D( currentRayPos, aSegRay.m_End );
94 }
95
96 // move through the union of subtracted regions
97 if( m_objectB )
98 {
99 for( unsigned int l = 0; l < ( m_objectB->size() * 2 ); ++l )
100 {
101 bool wasCrossedSubVol = false;
102
103 //check against all subbed objects
104 for( unsigned int i = 0; i < m_objectB->size(); ++i )
105 {
106 if( ( (const OBJECT_2D*) ( *m_objectB )[i] )->IsPointInside( currentRayPos ) )
107 {
108 // ray point is inside a subtracted region, so move it to the end of the
109 // subtracted region
110 float hitDist;
111 SFVEC2F tmpNormal;
112
113 if( !( (const OBJECT_2D*) ( *m_objectB )[i] )
114 ->Intersect( currentRay, &hitDist, &tmpNormal ) )
115 return false; // ray hit main object but did not leave subtracted volume
116
117 wxASSERT( hitDist <= 1.0f );
118
119 if( hitDist > FLT_EPSILON )
120 {
121 wasCrossedSubVol = true;
122
123 currentRayPos =
124 currentRay.atNormalized( glm::min( hitDist + 0.0001f, 1.0f ) );
125
126 currentRay = RAYSEG2D( currentRayPos, aSegRay.m_End );
127
128 currentNormal = tmpNormal * -1.0f;
129 }
130 }
131 }
132
133 if( !wasCrossedSubVol )
134 break;
135 }
136 }
137
138 if( aNormalOut )
139 *aNormalOut = currentNormal;
140
141 if( aOutT )
142 *aOutT = glm::min(
143 glm::max( glm::length( currentRayPos - aSegRay.m_Start ) / aSegRay.m_Length, 0.0f ),
144 1.0f );
145
146 return true;
147}
148
149
151{
152
153 // !TODO:
154
156}
157
158
159bool LAYER_ITEM_2D::IsPointInside( const SFVEC2F& aPoint ) const
160{
161 // Perform the operation (A - B) /\ C
162 if( m_objectA->IsPointInside( aPoint ) )
163 {
164
165 if( m_objectB != CSGITEM_EMPTY )
166 {
167 for( unsigned int i = 0; i < m_objectB->size(); i++ )
168 {
169 if( ( *m_objectB )[i]->IsPointInside( aPoint ) )
170 return false;
171 }
172 }
173
174 // !TODO: not yet implemented
175 //if( m_objectC && m_objectC != CSGITEM_FULL )
176 // return m_objectC->IsPointInside( aPoint );
177
178 return true;
179 }
180
181 return false;
182}
Defines math related functions.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
bool IsPointInside(const SFVEC2F &aPoint) const override
bool Intersects(const BBOX_2D &aBBox) const override
a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)
const OBJECT_2D * m_objectA
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
LAYER_ITEM_2D(const OBJECT_2D *aObjectA, std::vector< const OBJECT_2D * > *aObjectB, const OBJECT_2D *aObjectC, const BOARD_ITEM &aBoardItem)
INTERSECTION_RESULT IsBBoxInside(const BBOX_2D &aBBox) const override
Test this object if it's completely outside, intersects, or is completely inside aBBox.
bool Overlaps(const BBOX_2D &aBBox) const override
Test if the box overlaps the object.
std::vector< const OBJECT_2D * > * m_objectB
const OBJECT_2D * m_objectC
BBOX_2D m_bbox
Definition object_2d.h:106
const BBOX_2D & GetBBox() const
Definition object_2d.h:99
SFVEC2F m_centroid
Definition object_2d.h:107
OBJECT_2D(OBJECT_2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition object_2d.cpp:27
#define CSGITEM_EMPTY
#define CSGITEM_FULL
OBJECT_2D_TYPE
Definition object_2d.h:41
INTERSECTION_RESULT
Definition object_2d.h:33
Manage a bounding box defined by two SFVEC2F min max points.
Definition bbox_2d.h:38
float m_Length
Definition ray.h:108
SFVEC2F atNormalized(float t) const
Return the position at t.
Definition ray.h:131
SFVEC2F m_Start
Definition ray.h:103
SFVEC2F m_End
Definition ray.h:104
glm::vec2 SFVEC2F
Definition xv3d_types.h:38