KiCad PCB EDA Suite
4pt_polygon_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 "4pt_polygon_2d.h"
30#include <wx/debug.h>
31#include "../ray.h"
32
33
35 const SFVEC2F& v4, const BOARD_ITEM& aBoardItem ) :
36 OBJECT_2D( OBJECT_2D_TYPE::POLYGON4PT, aBoardItem )
37{
38 m_segments[0] = v1;
39 m_segments[1] = v4;
40 m_segments[2] = v3;
41 m_segments[3] = v2;
42
43 unsigned int i;
44 unsigned int j = 4 - 1;
45
46 for( i = 0; i < 4; j = i++ )
47 {
48 SFVEC2F slope = m_segments[j] - m_segments[i];
49 m_precalc_slope[i] = slope;
50 m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
51 }
52
53 m_bbox.Reset();
54 m_bbox.Union( v1 );
55 m_bbox.Union( v2 );
56 m_bbox.Union( v3 );
57 m_bbox.Union( v4 );
64
65 wxASSERT( m_bbox.IsInitialized() );
66}
67
68
69bool POLYGON_4PT_2D::Intersects( const BBOX_2D& aBBox ) const
70{
71 return m_bbox.Intersects( aBBox );
72}
73
74
75bool POLYGON_4PT_2D::Overlaps( const BBOX_2D& aBBox ) const
76{
77 // NOT IMPLEMENTED
78 return true;
79}
80
81
82bool POLYGON_4PT_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
83{
84 bool hited = false;
85 unsigned int hitIndex;
86 float bestHitT;
87
88 for( unsigned int i = 0; i < 4; i++ )
89 {
90 float t;
91
92 if( aSegRay.IntersectSegment( m_segments[i], m_precalc_slope[i], &t ) )
93 {
94 if( ( hited == false ) || ( t < bestHitT ) )
95 {
96 hited = true;
97 hitIndex = i;
98 bestHitT = t;
99 }
100 }
101 }
102
103 if( hited )
104 {
105 wxASSERT( ( bestHitT >= 0.0f ) && ( bestHitT <= 1.0f ) );
106
107 if( aOutT )
108 *aOutT = bestHitT;
109
110 if( aNormalOut )
111 *aNormalOut = m_seg_normal[hitIndex];
112
113 return true;
114 }
115
116 return false;
117}
118
119
121{
122 // !TODO:
123
125}
126
127
128bool POLYGON_4PT_2D::IsPointInside( const SFVEC2F& aPoint ) const
129{
130 unsigned int i;
131 unsigned int j = 4 - 1;
132 bool oddNodes = false;
133
134 for( i = 0; i < 4; j = i++ )
135 {
136 const float polyJY = m_segments[j].y;
137 const float polyIY = m_segments[i].y;
138
139 if( ( ( polyIY <= aPoint.y ) && ( polyJY >= aPoint.y ) )
140 || ( ( polyJY <= aPoint.y ) && ( polyIY >= aPoint.y ) ) )
141 {
142 const float polyJX = m_segments[j].x;
143 const float polyIX = m_segments[i].x;
144
145 if( ( polyIX <= aPoint.x ) || ( polyJX <= aPoint.x ) )
146 {
147 oddNodes ^= ( ( polyIX + ( ( aPoint.y - polyIY ) / ( polyJY - polyIY ) )
148 * ( polyJX - polyIX ) )
149 < aPoint.x );
150 }
151 }
152 }
153
154 return oddNodes;
155}
A simplified non-intersecting 4 point polygon.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
BBOX_2D m_bbox
Definition: object_2d.h:110
SFVEC2F m_centroid
Definition: object_2d.h:111
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
bool Intersects(const BBOX_2D &aBBox) const override
a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)
SFVEC2F m_precalc_slope[4]
SFVEC2F m_seg_normal[4]
bool IsPointInside(const SFVEC2F &aPoint) const override
INTERSECTION_RESULT IsBBoxInside(const BBOX_2D &aBBox) const override
Test this object if it's completely outside, intersects, or is completely inside aBBox.
SFVEC2F m_segments[4]
POLYGON_4PT_2D(const SFVEC2F &v1, const SFVEC2F &v2, const SFVEC2F &v3, const SFVEC2F &v4, const BOARD_ITEM &aBoardItem)
bool Overlaps(const BBOX_2D &aBBox) const override
Test if the box overlaps the object.
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
void Union(const SFVEC2F &aPoint)
Recalculate the bounding box adding a point.
Definition: bbox_2d.cpp:93
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 ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_2d.cpp:162
Definition: ray.h:106
bool IntersectSegment(const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start, float *aOutT) const
Definition: ray.cpp:262
VECTOR3I v1(5, 5, 5)
VECTOR2I v2(1, 0)
Test suite for KiCad math code.
VECTOR3I v3(1, 1, 1)
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42