KiCad PCB EDA Suite
cring2d.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 "cring2d.h"
31 #include "../../../3d_fastmath.h"
32 #include <wx/debug.h>
33 
34 
35 CRING2D::CRING2D( const SFVEC2F& aCenter, float aInnerRadius, float aOuterRadius,
36  const BOARD_ITEM& aBoardItem )
37  : COBJECT2D( OBJECT2D_TYPE::RING, aBoardItem )
38 {
39  wxASSERT( aInnerRadius < aOuterRadius );
40 
41  m_center = aCenter;
42  m_inner_radius = aInnerRadius;
43  m_outer_radius = aOuterRadius;
44 
45  m_inner_radius_squared = aInnerRadius * aInnerRadius;
46  m_outer_radius_squared = aOuterRadius * aOuterRadius;
47 
48  m_bbox.Reset();
49  m_bbox.Set( m_center - SFVEC2F( aOuterRadius, aOuterRadius ),
50  m_center + SFVEC2F( aOuterRadius, aOuterRadius ) );
53 
54 
55  wxASSERT( m_bbox.IsInitialized() );
56 }
57 
58 
59 bool CRING2D::Overlaps( const CBBOX2D &aBBox ) const
60 {
61  // NOT IMPLEMENTED
62  return false;
63 }
64 
65 
66 bool CRING2D::Intersects( const CBBOX2D &aBBox ) const
67 {
68  // !TODO: check the inside for a great improovment
70 }
71 
72 
73 bool CRING2D::Intersect( const RAYSEG2D &aSegRay,
74  float *aOutT,
75  SFVEC2F *aNormalOut ) const
76 {
77  // This code used directly from Steve Marschner's CS667 framework
78  // http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
79 
80  // Compute some factors used in computation
81  const float qx = (aSegRay.m_Start.x - m_center.x);
82  const float qy = (aSegRay.m_Start.y - m_center.y);
83 
84  const float qd = qx * aSegRay.m_Dir.x + qy * aSegRay.m_Dir.y;
85  const float qq = qx * qx + qy * qy;
86 
87  // solving the quadratic equation for t at the pts of intersection
88  // dd*t^2 + (2*qd)*t + (qq-r^2) = 0
89 
90  const float discriminantsqr = qd * qd - qq;
91  const float discriminantsqr_outer = discriminantsqr + m_outer_radius_squared;
92 
93  // If the discriminant is less than zero, there is no intersection
94  if( discriminantsqr_outer < FLT_EPSILON )
95  return false;
96 
97  // Otherwise check and make sure that the intersections occur on the ray (t
98  // > 0) and return the closer one
99  const float discriminant = sqrt( discriminantsqr_outer );
100  float t = (-qd - discriminant);
101 
102  if( (t > FLT_EPSILON) && (t < aSegRay.m_Length) )
103  {
104  SFVEC2F hitPoint = aSegRay.at( t );
105  *aNormalOut = (hitPoint - m_center) / m_outer_radius;
106  }
107  else
108  {
109  const float discriminantsqr_inter = discriminantsqr + m_inner_radius_squared;
110 
111  if( discriminantsqr_inter > FLT_EPSILON )
112  {
113  const float discriminant_inner = sqrt( discriminantsqr_inter );
114 
115  const float t2_inner = (-qd + discriminant_inner);
116 
117  if( (t2_inner > FLT_EPSILON) && (t2_inner < aSegRay.m_Length) )
118  {
119  t = t2_inner;
120 
121  const SFVEC2F hitPoint = aSegRay.at( t2_inner );
122 
123  *aNormalOut = (m_center - hitPoint) / m_inner_radius;
124  }
125  else
126  return false;
127  }
128  else
129  return false;
130  }
131 
132  wxASSERT( (t > 0.0f) && (t <= aSegRay.m_Length) );
133 
134  // Convert the intersection to a normalized 0.0 .. 1.0
135  *aOutT = t / aSegRay.m_Length;
136 
137  return true;
138 }
139 
140 
142 {
143  /*
144  if( !m_bbox.Overlaps( aBBox ) )
145  return INTERSECTION_RESULT::MISSES;
146 
147  SFVEC2F v[4];
148 
149  v[0] = aBBox.Min() - m_center;
150  v[1] = aBBox.Max() - m_center;
151  v[2] = SFVEC2F( aBBox.Min().x, aBBox.Max().y ) - m_center;
152  v[3] = SFVEC2F( aBBox.Max().x, aBBox.Min().y ) - m_center;
153 
154  float s[4];
155 
156  s[0] = v[0].x * v[0].x + v[0].y * v[0].y;
157  s[1] = v[1].x * v[1].x + v[1].y * v[1].y;
158  s[2] = v[2].x * v[2].x + v[2].y * v[2].y;
159  s[3] = v[3].x * v[3].x + v[3].y * v[3].y;
160 
161  bool isInside[4];
162 
163  isInside[0] = s[0] <= m_radius_squared;
164  isInside[1] = s[1] <= m_radius_squared;
165  isInside[2] = s[2] <= m_radius_squared;
166  isInside[3] = s[3] <= m_radius_squared;
167 
168  // Check if all points are inside the circle
169  if( isInside[0] &&
170  isInside[1] &&
171  isInside[2] &&
172  isInside[3] )
173  return INTERSECTION_RESULT::FULL_INSIDE;
174 
175  // Check if any point is inside the circle
176  if( isInside[0] ||
177  isInside[1] ||
178  isInside[2] ||
179  isInside[3] )
180  return INTERSECTION_RESULT::INTERSECTS;
181 */
183 }
184 
185 
186 bool CRING2D::IsPointInside( const SFVEC2F &aPoint ) const
187 {
188  const SFVEC2F v = m_center - aPoint;
189 
190  const float dot = glm::dot( v, v );
191 
192  if( (dot <= m_outer_radius_squared) &&
193  (dot >= m_inner_radius_squared) )
194  return true;
195 
196  return false;
197 }
INTERSECTION_RESULT
Definition: cobject2d.h:38
INTERSECTION_RESULT IsBBoxInside(const CBBOX2D &aBBox) const override
Function IsBBoxInside.
Definition: cring2d.cpp:141
float m_outer_radius
Definition: cring2d.h:48
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
SFVEC2F m_center
Definition: cring2d.h:46
CBBOX manages a bounding box defined by two SFVEC2F min max points.
Definition: cbbox2d.h:40
float m_outer_radius_squared
Definition: cring2d.h:50
float m_inner_radius_squared
Definition: cring2d.h:49
SFVEC2F m_Dir
Definition: ray.h:115
bool IsPointInside(const SFVEC2F &aPoint) const override
Definition: cring2d.cpp:186
SFVEC2F at(float t) const
Definition: ray.h:141
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox2d.cpp:88
CRING2D(const SFVEC2F &aCenter, float aInnerRadius, float aOuterRadius, const BOARD_ITEM &aBoardItem)
Definition: cring2d.cpp:35
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
bool Intersects(const CBBOX2D &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox2d.cpp:213
float m_Length
Definition: ray.h:117
void Set(const SFVEC2F &aPbMin, const SFVEC2F &aPbMax)
Function Set Set bounding box with new parameters.
Definition: cbbox2d.cpp:61
bool IsInitialized() const
Function IsInitialized check if this bounding box is already initialized.
Definition: cbbox2d.cpp:79
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_Start
Definition: ray.h:112
bool Overlaps(const CBBOX2D &aBBox) const override
Function Overlaps.
Definition: cring2d.cpp:59
SFVEC2F m_centroid
Definition: cobject2d.h:66
OBJECT2D_TYPE
Definition: cobject2d.h:46
SFVEC2F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox2d.cpp:121
float m_inner_radius
Definition: cring2d.h:47
bool Intersects(const CBBOX2D &aBBox) const override
Function Intersects.
Definition: cring2d.cpp:66
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
Function Intersect.
Definition: cring2d.cpp:73
Definition: ray.h:110