KiCad PCB EDA Suite
cfilledcircle2d.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 "cfilledcircle2d.h"
31 #include <wx/debug.h>
32 
33 
35  float aRadius,
36  const BOARD_ITEM &aBoardItem ) :
37  COBJECT2D( OBJ2D_FILLED_CIRCLE, aBoardItem )
38 {
39  wxASSERT( aRadius > 0.0f ); // If that happens, it should be handled before create this circle
40 
41  m_center = aCenter;
42  m_radius = aRadius;
43  m_radius_squared = aRadius * aRadius;
44 
45  m_bbox.Reset();
46  m_bbox.Set( m_center - SFVEC2F( aRadius, aRadius ),
47  m_center + SFVEC2F( aRadius, aRadius ) );
50 
51  wxASSERT( m_bbox.IsInitialized() );
52 }
53 
54 
55 bool CFILLEDCIRCLE2D::Overlaps( const CBBOX2D &aBBox ) const
56 {
57  // NOT IMPLEMENTED
58  return false;
59 }
60 
61 
62 bool CFILLEDCIRCLE2D::Intersects( const CBBOX2D &aBBox ) const
63 {
64  return aBBox.Intersects( m_center, m_radius_squared );
65 }
66 
67 
68 bool CFILLEDCIRCLE2D::Intersect( const RAYSEG2D &aSegRay,
69  float *aOutT,
70  SFVEC2F *aNormalOut ) const
71 {
72  wxASSERT( aOutT );
73  wxASSERT( aNormalOut );
74 
75  // This code used directly from Steve Marschner's CS667 framework
76  // http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
77 
78  // Compute some factors used in computation
79  const float qx = aSegRay.m_Start.x - m_center.x;
80  const float qy = aSegRay.m_Start.y - m_center.y;
81 
82  const float qd = qx * aSegRay.m_Dir.x + qy * aSegRay.m_Dir.y;
83  const float qq = qx * qx + qy * qy;
84 
85  // solving the quadratic equation for t at the pts of intersection
86  // dd*t^2 + (2*qd)*t + (qq-r^2) = 0
87  const float discriminantsqr = (qd * qd - (qq - m_radius_squared));
88 
89  // If the discriminant is less than zero, there is no intersection
90  if( discriminantsqr < FLT_EPSILON )
91  return false;
92 
93 
94  // Otherwise check and make sure that the intersections occur on the ray (t
95  // > 0) and return the closer one
96  const float discriminant = sqrt(discriminantsqr);
97  const float t1 = (-qd - discriminant);
98  const float t2 = (-qd + discriminant);
99  float t;
100 
101 
102  if( (t1 > 0.0f) && (t1 < aSegRay.m_Length) )
103  t = t1;
104  else
105  {
106  if( (t2 > 0.0f) && (t2 < aSegRay.m_Length) )
107  t = t2;
108  else
109  return false; // Neither intersection was in the ray's half line.
110  }
111 
112  wxASSERT( (t > 0.0f) && (t <= aSegRay.m_Length) );
113 
114  // Convert the intersection to a normalized 0.0 .. 1.0
115  *aOutT = t / aSegRay.m_Length;
116 
117  const SFVEC2F hitPoint = aSegRay.at( t );
118 
119  *aNormalOut = (hitPoint - m_center) / m_radius;
120 
121  return true;
122 }
123 
124 
126 {
127  if( !m_bbox.Intersects( aBBox ) )
128  return INTR_MISSES;
129 
130  SFVEC2F v[4];
131 
132  v[0] = aBBox.Min() - m_center;
133  v[1] = aBBox.Max() - m_center;
134  v[2] = SFVEC2F( aBBox.Min().x, aBBox.Max().y ) - m_center;
135  v[3] = SFVEC2F( aBBox.Max().x, aBBox.Min().y ) - m_center;
136 
137  float s[4];
138 
139  s[0] = v[0].x * v[0].x + v[0].y * v[0].y;
140  s[1] = v[1].x * v[1].x + v[1].y * v[1].y;
141  s[2] = v[2].x * v[2].x + v[2].y * v[2].y;
142  s[3] = v[3].x * v[3].x + v[3].y * v[3].y;
143 
144  bool isInside[4];
145 
146  isInside[0] = s[0] <= m_radius_squared;
147  isInside[1] = s[1] <= m_radius_squared;
148  isInside[2] = s[2] <= m_radius_squared;
149  isInside[3] = s[3] <= m_radius_squared;
150 
151  // Check if all points are inside the circle
152  if( isInside[0] &&
153  isInside[1] &&
154  isInside[2] &&
155  isInside[3] )
156  return INTR_FULL_INSIDE;
157 
158  // Check if any point is inside the circle
159  if( isInside[0] ||
160  isInside[1] ||
161  isInside[2] ||
162  isInside[3] )
163  return INTR_INTERSECTS;
164 
165  return INTR_MISSES;
166 }
167 
168 
169 bool CFILLEDCIRCLE2D::IsPointInside( const SFVEC2F &aPoint ) const
170 {
171  const SFVEC2F v = m_center - aPoint;
172 
173  if( (v.x * v.x + v.y * v.y) <= m_radius_squared )
174  return true;
175 
176  return false;
177 }
INTERSECTION_RESULT
Definition: cobject2d.h:38
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
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
CFILLEDCIRCLE2D(const SFVEC2F &aCenter, float aRadius, const BOARD_ITEM &aBoardItem)
SFVEC2F m_Dir
Definition: ray.h:91
SFVEC2F at(float t) const
Definition: ray.h:117
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
bool Overlaps(const CBBOX2D &aBBox) const override
Function Overlaps.
float m_Length
Definition: ray.h:93
void Set(const SFVEC2F &aPbMin, const SFVEC2F &aPbMax)
Function Set Set bounding box with new parameters.
Definition: cbbox2d.cpp:61
INTERSECTION_RESULT IsBBoxInside(const CBBOX2D &aBBox) const override
Function IsBBoxInside.
void ScaleNextUp()
Function ScaleNextUp scales a bounding box to the next float representation making it larger...
Definition: cbbox2d.cpp:164
bool Intersects(const CBBOX2D &aBBox) const override
Function Intersects.
CBBOX2D m_bbox
Definition: cobject2d.h:65
SFVEC2F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
Function Intersect.
SFVEC2F m_Start
Definition: ray.h:88
SFVEC2F m_centroid
Definition: cobject2d.h:66
bool IsPointInside(const SFVEC2F &aPoint) const override
const SFVEC2F & Min() const
Function Min return the minimun vertex pointer.
Definition: cbbox2d.h:176
const SFVEC2F & Max() const
Function Max return the maximum vertex pointer.
Definition: cbbox2d.h:183
Definition: ray.h:86