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, 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
28
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() );
46 m_bbox.ScaleNextUp();
47 m_centroid = m_bbox.GetCenter();
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}
67
68
69bool LAYER_ITEM_2D::Overlaps( const BBOX_2D& aBBox ) const
70{
71 // NOT IMPLEMENTED, why?
72 return false;
73}
74
75
76// Based on ideas and implementation by Nick Chapman
77// http://homepages.paradise.net.nz/nickamy/raytracer/raytracer.htm
78bool LAYER_ITEM_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
79{
80 if( m_objectA->GetObjectType() == OBJECT_2D_TYPE::DUMMYBLOCK )
81 return false;
82
83 SFVEC2F currentRayPos = aSegRay.m_Start;
84 SFVEC2F currentNormal;
85 RAYSEG2D currentRay = aSegRay;
86
87 if( !m_objectA->IsPointInside( aSegRay.m_Start ) )
88 {
89 //move ray point to start of main object
90 float tmpRayDist;
91
92 if( !m_objectA->Intersect( aSegRay, &tmpRayDist, &currentNormal ) )
93 return false;
94
95 currentRayPos = aSegRay.atNormalized( tmpRayDist + 0.003f );
96
97 currentRay = RAYSEG2D( currentRayPos, aSegRay.m_End );
98 }
99
100 // move through the union of subtracted regions
101 if( m_objectB )
102 {
103 for( unsigned int l = 0; l < ( m_objectB->size() * 2 ); ++l )
104 {
105 bool wasCrossedSubVol = false;
106
107 //check against all subbed objects
108 for( unsigned int i = 0; i < m_objectB->size(); ++i )
109 {
110 if( ( (const OBJECT_2D*) ( *m_objectB )[i] )->IsPointInside( currentRayPos ) )
111 {
112 // ray point is inside a subtracted region, so move it to the end of the
113 // subtracted region
114 float hitDist;
115 SFVEC2F tmpNormal;
116
117 if( !( (const OBJECT_2D*) ( *m_objectB )[i] )
118 ->Intersect( currentRay, &hitDist, &tmpNormal ) )
119 return false; // ray hit main object but did not leave subtracted volume
120
121 wxASSERT( hitDist <= 1.0f );
122
123 if( hitDist > FLT_EPSILON )
124 {
125 wasCrossedSubVol = true;
126
127 currentRayPos =
128 currentRay.atNormalized( glm::min( hitDist + 0.0001f, 1.0f ) );
129
130 currentRay = RAYSEG2D( currentRayPos, aSegRay.m_End );
131
132 currentNormal = tmpNormal * -1.0f;
133 }
134 }
135 }
136
137 if( !wasCrossedSubVol )
138 break;
139 }
140 }
141
142 if( aNormalOut )
143 *aNormalOut = currentNormal;
144
145 if( aOutT )
146 *aOutT = glm::min(
147 glm::max( glm::length( currentRayPos - aSegRay.m_Start ) / aSegRay.m_Length, 0.0f ),
148 1.0f );
149
150 return true;
151}
152
153
155{
156
157 // !TODO:
158
160}
161
162
163bool LAYER_ITEM_2D::IsPointInside( const SFVEC2F& aPoint ) const
164{
165 // Perform the operation (A - B) /\ C
166 if( m_objectA->IsPointInside( aPoint ) )
167 {
168
169 if( m_objectB != CSGITEM_EMPTY )
170 {
171 for( unsigned int i = 0; i < m_objectB->size(); i++ )
172 {
173 if( ( *m_objectB )[i]->IsPointInside( aPoint ) )
174 return false;
175 }
176 }
177
178 // !TODO: not yet implemented
179 //if( m_objectC && m_objectC != CSGITEM_FULL )
180 // return m_objectC->IsPointInside( aPoint );
181
182 return true;
183 }
184
185 return false;
186}
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:83
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:110
const BBOX_2D & GetBBox() const
Definition object_2d.h:103
SFVEC2F m_centroid
Definition object_2d.h:111
OBJECT_2D(OBJECT_2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition object_2d.cpp:31
#define CSGITEM_EMPTY
#define CSGITEM_FULL
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
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