KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 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 "4pt_polygon_2d.h"
26#include <wx/debug.h>
27#include "../ray.h"
28
29
31 const SFVEC2F& v4, const BOARD_ITEM& aBoardItem ) :
32 OBJECT_2D( OBJECT_2D_TYPE::POLYGON4PT, aBoardItem )
33{
34 m_segments[0] = v1;
35 m_segments[1] = v4;
36 m_segments[2] = v3;
37 m_segments[3] = v2;
38
39 unsigned int i;
40 unsigned int j = 4 - 1;
41
42 for( i = 0; i < 4; j = i++ )
43 {
44 SFVEC2F slope = m_segments[j] - m_segments[i];
45 m_precalc_slope[i] = slope;
46 m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
47 }
48
49 m_bbox.Reset();
50 m_bbox.Union( v1 );
51 m_bbox.Union( v2 );
52 m_bbox.Union( v3 );
53 m_bbox.Union( v4 );
54 m_bbox.ScaleNextUp();
55 m_bbox.ScaleNextUp();
56 m_bbox.ScaleNextUp();
57 m_bbox.ScaleNextUp();
58 m_bbox.ScaleNextUp();
59 m_centroid = m_bbox.GetCenter();
60
61 wxASSERT( m_bbox.IsInitialized() );
62}
63
64
65bool POLYGON_4PT_2D::Intersects( const BBOX_2D& aBBox ) const
66{
67 return m_bbox.Intersects( aBBox );
68}
69
70
71bool POLYGON_4PT_2D::Overlaps( const BBOX_2D& aBBox ) const
72{
73 // NOT IMPLEMENTED
74 return true;
75}
76
77
78bool POLYGON_4PT_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
79{
80 bool hited = false;
81 unsigned int hitIndex;
82 float bestHitT;
83
84 for( unsigned int i = 0; i < 4; i++ )
85 {
86 float t;
87
88 if( aSegRay.IntersectSegment( m_segments[i], m_precalc_slope[i], &t ) )
89 {
90 if( ( hited == false ) || ( t < bestHitT ) )
91 {
92 hited = true;
93 hitIndex = i;
94 bestHitT = t;
95 }
96 }
97 }
98
99 if( hited )
100 {
101 wxASSERT( ( bestHitT >= 0.0f ) && ( bestHitT <= 1.0f ) );
102
103 if( aOutT )
104 *aOutT = bestHitT;
105
106 if( aNormalOut )
107 *aNormalOut = m_seg_normal[hitIndex];
108
109 return true;
110 }
111
112 return false;
113}
114
115
117{
118 // !TODO:
119
121}
122
123
124bool POLYGON_4PT_2D::IsPointInside( const SFVEC2F& aPoint ) const
125{
126 unsigned int i;
127 unsigned int j = 4 - 1;
128 bool oddNodes = false;
129
130 for( i = 0; i < 4; j = i++ )
131 {
132 const float polyJY = m_segments[j].y;
133 const float polyIY = m_segments[i].y;
134
135 if( ( ( polyIY <= aPoint.y ) && ( polyJY >= aPoint.y ) )
136 || ( ( polyJY <= aPoint.y ) && ( polyIY >= aPoint.y ) ) )
137 {
138 const float polyJX = m_segments[j].x;
139 const float polyIX = m_segments[i].x;
140
141 if( ( polyIX <= aPoint.x ) || ( polyJX <= aPoint.x ) )
142 {
143 oddNodes ^= ( ( polyIX + ( ( aPoint.y - polyIY ) / ( polyJY - polyIY ) )
144 * ( polyJX - polyIX ) )
145 < aPoint.x );
146 }
147 }
148 }
149
150 return oddNodes;
151}
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:81
BBOX_2D m_bbox
Definition object_2d.h:106
SFVEC2F m_centroid
Definition object_2d.h:107
OBJECT_2D(OBJECT_2D_TYPE aObjType, const BOARD_ITEM &aBoardItem)
Definition object_2d.cpp:27
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:41
INTERSECTION_RESULT
Definition object_2d.h:33
Manage a bounding box defined by two SFVEC2F min max points.
Definition bbox_2d.h:38
bool IntersectSegment(const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start, float *aOutT) const
Definition ray.cpp:258
VECTOR3I v1(5, 5, 5)
VECTOR2I v2(1, 0)
VECTOR2I v4(1, 1)
VECTOR2I v3(-2, 1)
glm::vec2 SFVEC2F
Definition xv3d_types.h:38