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 (C) 1992-2020 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
29#include "layer_item_2d.h"
30#include "../ray.h"
31#include "3d_fastmath.h"
32#include <wx/debug.h>
33
34
35LAYER_ITEM_2D::LAYER_ITEM_2D( const OBJECT_2D* aObjectA, std::vector<const OBJECT_2D*>* aObjectB,
36 const OBJECT_2D* aObjectC, const BOARD_ITEM& aBoardItem ) :
37 OBJECT_2D( OBJECT_2D_TYPE::CSG, aBoardItem ),
38 m_objectA( aObjectA ),
39 m_objectB( aObjectB ),
40 m_objectC( aObjectC )
41{
42 wxASSERT( aObjectA );
43
44 m_bbox.Reset();
45 m_bbox.Set( aObjectA->GetBBox() );
48
49 wxASSERT( m_bbox.IsInitialized() );
50}
51
52
54{
55 if( ( (void*) m_objectB != CSGITEM_EMPTY ) && ( (void*) m_objectB != CSGITEM_FULL ) )
56 {
57 delete m_objectB;
58 m_objectB = nullptr;
59 }
60}
61
62
63bool LAYER_ITEM_2D::Intersects( const BBOX_2D& aBBox ) const
64{
65 return m_bbox.Intersects( aBBox );
66 // !TODO: improve this implementation
67 //return false;
68}
69
70
71bool LAYER_ITEM_2D::Overlaps( const BBOX_2D& aBBox ) const
72{
73 // NOT IMPLEMENTED, why?
74 return false;
75}
76
77
78// Based on ideas and implementation by Nick Chapman
79// http://homepages.paradise.net.nz/nickamy/raytracer/raytracer.htm
80bool LAYER_ITEM_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
81{
82 if( m_objectA->GetObjectType() == OBJECT_2D_TYPE::DUMMYBLOCK )
83 return false;
84
85 SFVEC2F currentRayPos = aSegRay.m_Start;
86 SFVEC2F currentNormal;
87 RAYSEG2D currentRay = aSegRay;
88
89 if( !m_objectA->IsPointInside( aSegRay.m_Start ) )
90 {
91 //move ray point to start of main object
92 float tmpRayDist;
93
94 if( !m_objectA->Intersect( aSegRay, &tmpRayDist, &currentNormal ) )
95 return false;
96
97 currentRayPos = aSegRay.atNormalized( tmpRayDist + 0.003f );
98
99 currentRay = RAYSEG2D( currentRayPos, aSegRay.m_End );
100 }
101
102 // move through the union of subtracted regions
103 if( m_objectB )
104 {
105 for( unsigned int l = 0; l < ( m_objectB->size() * 2 ); ++l )
106 {
107 bool wasCrossedSubVol = false;
108
109 //check against all subbed objects
110 for( unsigned int i = 0; i < m_objectB->size(); ++i )
111 {
112 if( ( (const OBJECT_2D*) ( *m_objectB )[i] )->IsPointInside( currentRayPos ) )
113 {
114 // ray point is inside a subtracted region, so move it to the end of the
115 // subtracted region
116 float hitDist;
117 SFVEC2F tmpNormal;
118
119 if( !( (const OBJECT_2D*) ( *m_objectB )[i] )
120 ->Intersect( currentRay, &hitDist, &tmpNormal ) )
121 return false; // ray hit main object but did not leave subtracted volume
122
123 wxASSERT( hitDist <= 1.0f );
124
125 if( hitDist > FLT_EPSILON )
126 {
127 wasCrossedSubVol = true;
128
129 currentRayPos =
130 currentRay.atNormalized( glm::min( hitDist + 0.0001f, 1.0f ) );
131
132 currentRay = RAYSEG2D( currentRayPos, aSegRay.m_End );
133
134 currentNormal = tmpNormal * -1.0f;
135 }
136 }
137 }
138
139 if( !wasCrossedSubVol )
140 break;
141 }
142 }
143
144 if( aNormalOut )
145 *aNormalOut = currentNormal;
146
147 if( aOutT )
148 *aOutT = glm::min(
149 glm::max( glm::length( currentRayPos - aSegRay.m_Start ) / aSegRay.m_Length, 0.0f ),
150 1.0f );
151
152 return true;
153}
154
155
157{
158
159 // !TODO:
160
161 return INTERSECTION_RESULT::MISSES;
162}
163
164
165bool LAYER_ITEM_2D::IsPointInside( const SFVEC2F& aPoint ) const
166{
167 // Perform the operation (A - B) /\ C
168 if( m_objectA->IsPointInside( aPoint ) )
169 {
170
171 if( m_objectB != CSGITEM_EMPTY )
172 {
173 for( unsigned int i = 0; i < m_objectB->size(); i++ )
174 {
175 if( ( *m_objectB )[i]->IsPointInside( aPoint ) )
176 return false;
177 }
178 }
179
180 // !TODO: not yet implemented
181 //if( m_objectC && m_objectC != CSGITEM_FULL )
182 // return m_objectC->IsPointInside( aPoint );
183
184 return true;
185 }
186
187 return false;
188}
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:79
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
Definition: layer_item_2d.h:95
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
Definition: layer_item_2d.h:96
BBOX_2D m_bbox
Definition: object_2d.h:110
const BBOX_2D & GetBBox() const
Definition: object_2d.h:103
virtual bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const =0
SFVEC2F m_centroid
Definition: object_2d.h:111
OBJECT_2D_TYPE GetObjectType() const
Definition: object_2d.h:107
virtual bool IsPointInside(const SFVEC2F &aPoint) const =0
#define CSGITEM_EMPTY
Definition: layer_item_2d.h:38
#define CSGITEM_FULL
Definition: layer_item_2d.h:39
OBJECT_2D_TYPE
Definition: object_2d.h:45
INTERSECTION_RESULT
Definition: object_2d.h:37
Manage a bounding box defined by two SFVEC2F min max points.
Definition: bbox_2d.h:42
SFVEC2F GetCenter() const
Definition: bbox_2d.cpp:119
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_2d.cpp:211
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition: bbox_2d.cpp:79
void Reset()
Reset the bounding box to zero and uninitialize it.
Definition: bbox_2d.cpp:86
void Set(const SFVEC2F &aPbMin, const SFVEC2F &aPbMax)
Set bounding box with new parameters.
Definition: bbox_2d.cpp:61
void ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_2d.cpp:162
Definition: ray.h:106
float m_Length
Definition: ray.h:112
SFVEC2F atNormalized(float t) const
Return the position at t.
Definition: ray.h:135
SFVEC2F m_Start
Definition: ray.h:107
SFVEC2F m_End
Definition: ray.h:108
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42