KiCad PCB EDA Suite
cpolygon4pts2d.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 <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 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 
30 #include "cpolygon4pts2d.h"
31 #include <wx/debug.h>
32 
33 
35  const SFVEC2F &v2,
36  const SFVEC2F &v3,
37  const SFVEC2F &v4,
38  const BOARD_ITEM &aBoardItem ) :
39  COBJECT2D( OBJ2D_POLYGON4PT, aBoardItem )
40 {/*
41  if( (v1.x > v2.x) || (v1.y < v2.y) )
42  {
43  m_segments[0] = v4;
44  m_segments[1] = v3;
45  m_segments[2] = v2;
46  m_segments[3] = v1;
47  }
48  else
49  {*/
50  m_segments[0] = v1;
51  m_segments[1] = v4;
52  m_segments[2] = v3;
53  m_segments[3] = v2;
54  // }
55 
56  unsigned int i;
57  unsigned int j = 4 - 1;
58 
59  for( i = 0; i < 4; j = i++ )
60  {
61  SFVEC2F slope = m_segments[j] - m_segments[i];
62  m_precalc_slope[i] = slope;
63  m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
64  }
65 
66  m_bbox.Reset();
67  m_bbox.Union( v1 );
68  m_bbox.Union( v2 );
69  m_bbox.Union( v3 );
70  m_bbox.Union( v4 );
77 
78  wxASSERT( m_bbox.IsInitialized() );
79 }
80 
81 
82 bool CPOLYGON4PTS2D::Intersects( const CBBOX2D &aBBox ) const
83 {
84  return m_bbox.Intersects( aBBox );
85 
86  // This source code is not working OK.
87  /*
88  if( !m_bbox.Intersects( aBBox ) )
89  return false;
90 
91  // Check if the bouding box complety have inside the small bouding box
92  if( (aBBox.Max().x > m_bbox.Max().x) &&
93  (aBBox.Max().y > m_bbox.Max().x) &&
94  (aBBox.Min().x < m_bbox.Min().x) &&
95  (aBBox.Min().y < m_bbox.Min().y)
96  )
97  return true;
98 
99  SFVEC2F v[4];
100 
101  v[0] = aBBox.Min();
102  v[1] = SFVEC2F( aBBox.Min().x, aBBox.Max().y );
103  v[2] = aBBox.Max();
104  v[3] = SFVEC2F( aBBox.Max().x, aBBox.Min().y );
105 
106  for( unsigned int i = 0; i < 4; i++ )
107  {
108  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[0], v[1] - v[0] ) )
109  return true;
110  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[1], v[2] - v[1] ) )
111  return true;
112  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[2], v[3] - v[2] ) )
113  return true;
114  if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[3], v[0] - v[3] ) )
115  return true;
116  }
117 
118  if( IsPointInside( v[0] ) )
119  return true;
120  if( IsPointInside( v[1] ) )
121  return true;
122  if( IsPointInside( v[2] ) )
123  return true;
124  if( IsPointInside( v[3] ) )
125  return true;
126 
127  return false;*/
128 }
129 
130 
131 bool CPOLYGON4PTS2D::Overlaps( const CBBOX2D &aBBox ) const
132 {
133  // NOT IMPLEMENTED
134  return true;
135 }
136 
137 
138 bool CPOLYGON4PTS2D::Intersect( const RAYSEG2D &aSegRay,
139  float *aOutT,
140  SFVEC2F *aNormalOut ) const
141 {
142  wxASSERT( aOutT );
143  wxASSERT( aNormalOut );
144 
145  bool hited = false;
146  unsigned int hitIndex;
147  float bestHitT;
148 
149  for( unsigned int i = 0; i < 4; i++ )
150  {
151  float t;
152 
153  if( aSegRay.IntersectSegment( m_segments[i], m_precalc_slope[i], &t ) )
154  if( (hited == false) || ( t < bestHitT) )
155  {
156  hited = true;
157  hitIndex = i;
158  bestHitT = t;
159  }
160  }
161 
162  if( hited )
163  {
164  wxASSERT( (bestHitT >= 0.0f) && (bestHitT <= 1.0f) );
165 
166  *aOutT = bestHitT;
167  *aNormalOut = m_seg_normal[hitIndex];
168 
169  return true;
170  }
171 
172  return false;
173 }
174 
175 
177 {
178  // !TODO:
179 
180  return INTR_MISSES;
181 }
182 
183 
184 bool CPOLYGON4PTS2D::IsPointInside( const SFVEC2F &aPoint ) const
185 {
186  unsigned int i;
187  unsigned int j = 4 - 1;
188  bool oddNodes = false;
189 
190  for( i = 0; i < 4; j = i++ )
191  {
192  const float polyJY = m_segments[j].y;
193  const float polyIY = m_segments[i].y;
194 
195  if( ((polyIY <= aPoint.y) && (polyJY >= aPoint.y)) ||
196  ((polyJY <= aPoint.y) && (polyIY >= aPoint.y))
197  )
198  {
199  const float polyJX = m_segments[j].x;
200  const float polyIX = m_segments[i].x;
201 
202  if( (polyIX <= aPoint.x) || (polyJX <= aPoint.x) )
203  {
204  oddNodes ^= ( ( polyIX +
205  ( ( aPoint.y - polyIY ) / ( polyJY - polyIY ) ) *
206  ( polyJX - polyIX ) ) < aPoint.x );
207  }
208  }
209  }
210 
211  return oddNodes;
212 }
INTERSECTION_RESULT
Definition: cobject2d.h:38
bool Overlaps(const CBBOX2D &aBBox) const override
Function Overlaps.
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
void Union(const SFVEC2F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox2d.cpp:95
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
Class CBBOX manages a bounding box defined by two SFVEC2F min max points.
Definition: cbbox2d.h:40
SFVEC2F m_segments[4]
bool Intersects(const CBBOX2D &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox2d.cpp:213
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox2d.cpp:88
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
SFVEC2F m_seg_normal[4]
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
Function Intersect.
void ScaleNextUp()
Function ScaleNextUp scales a bounding box to the next float representation making it larger...
Definition: cbbox2d.cpp:164
CBBOX2D m_bbox
Definition: cobject2d.h:65
SFVEC2F m_precalc_slope[4]
bool Intersects(const CBBOX2D &aBBox) const override
Function Intersects.
SFVEC2F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121
A simplified 4 point polygon.
SFVEC2F m_centroid
Definition: cobject2d.h:66
CPOLYGON4PTS2D(const SFVEC2F &v1, const SFVEC2F &v2, const SFVEC2F &v3, const SFVEC2F &v4, const BOARD_ITEM &aBoardItem)
bool IntersectSegment(const SFVEC2F &aStart, const SFVEC2F &aEnd_minus_start, float *aOutT) const
Definition: ray.cpp:289
INTERSECTION_RESULT IsBBoxInside(const CBBOX2D &aBBox) const override
Function IsBBoxInside.
Definition: ray.h:86
bool IsPointInside(const SFVEC2F &aPoint) const override