KiCad PCB EDA Suite
ring_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 "ring_2d.h"
30 #include "../../../3d_fastmath.h"
31 #include "../ray.h"
32 #include <wx/debug.h>
33 
34 
35 RING_2D::RING_2D( const SFVEC2F& aCenter, float aInnerRadius, float aOuterRadius,
36  const BOARD_ITEM& aBoardItem ) :
37  OBJECT_2D( OBJECT_2D_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  wxASSERT( m_bbox.IsInitialized() );
55 }
56 
57 
58 bool RING_2D::Overlaps( const BBOX_2D& aBBox ) const
59 {
60  // NOT IMPLEMENTED, why?
61  return false;
62 }
63 
64 
65 bool RING_2D::Intersects( const BBOX_2D& aBBox ) const
66 {
67  // !TODO: check the inside for a great improvement
69 }
70 
71 
72 bool RING_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
73 {
74  // This code used directly from Steve Marschner's CS667 framework
75  // http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
76 
77  // Compute some factors used in computation
78  const float qx = ( aSegRay.m_Start.x - m_center.x );
79  const float qy = ( aSegRay.m_Start.y - m_center.y );
80 
81  const float qd = qx * aSegRay.m_Dir.x + qy * aSegRay.m_Dir.y;
82  const float qq = qx * qx + qy * qy;
83 
84  // solving the quadratic equation for t at the pts of intersection
85  // dd*t^2 + (2*qd)*t + (qq-r^2) = 0
86  const float discriminantsqr = qd * qd - qq;
87  const float discriminantsqr_outer = discriminantsqr + m_outer_radius_squared;
88 
89  // If the discriminant is less than zero, there is no intersection
90  if( discriminantsqr_outer < FLT_EPSILON )
91  return false;
92 
93  // Otherwise check and make sure that the intersections occur on the ray (t
94  // > 0) and return the closer one
95  const float discriminant = sqrt( discriminantsqr_outer );
96  float t = ( -qd - discriminant );
97 
98  if( ( t > FLT_EPSILON ) && ( t < aSegRay.m_Length ) )
99  {
100  if( aNormalOut )
101  {
102  SFVEC2F hitPoint = aSegRay.at( t );
103  *aNormalOut = (hitPoint - m_center) / m_outer_radius;
104  }
105  }
106  else
107  {
108  const float discriminantsqr_inter = discriminantsqr + m_inner_radius_squared;
109 
110  if( discriminantsqr_inter > FLT_EPSILON )
111  {
112  const float discriminant_inner = sqrt( discriminantsqr_inter );
113 
114  const float t2_inner = ( -qd + discriminant_inner );
115 
116  if( ( t2_inner > FLT_EPSILON ) && ( t2_inner < aSegRay.m_Length ) )
117  {
118  t = t2_inner;
119 
120  if( aNormalOut )
121  {
122  const SFVEC2F hitPoint = aSegRay.at( t2_inner );
123 
124  *aNormalOut = ( m_center - hitPoint ) / m_inner_radius;
125  }
126  }
127  else
128  {
129  return false;
130  }
131  }
132  else
133  {
134  return false;
135  }
136  }
137 
138  wxASSERT( (t > 0.0f) && (t <= aSegRay.m_Length) );
139 
140  // Convert the intersection to a normalized 0.0 .. 1.0
141  if( aOutT )
142  *aOutT = t / aSegRay.m_Length;
143 
144  return true;
145 }
146 
147 
149 {
151 }
152 
153 
154 bool RING_2D::IsPointInside( const SFVEC2F& aPoint ) const
155 {
156  const SFVEC2F v = m_center - aPoint;
157 
158  const float dot = glm::dot( v, v );
159 
160  if( ( dot <= m_outer_radius_squared ) && ( dot >= m_inner_radius_squared ) )
161  return true;
162 
163  return false;
164 }
BBOX_2D m_bbox
Definition: object_2d.h:110
float m_outer_radius
Definition: ring_2d.h:56
INTERSECTION_RESULT IsBBoxInside(const BBOX_2D &aBBox) const override
Test this object if it's completely outside, intersects, or is completely inside aBBox.
Definition: ring_2d.cpp:148
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
SFVEC2F GetCenter() const
Definition: bbox_2d.cpp:119
float m_inner_radius
Definition: ring_2d.h:55
void Set(const SFVEC2F &aPbMin, const SFVEC2F &aPbMax)
Set bounding box with new parameters.
Definition: bbox_2d.cpp:61
bool Intersects(const BBOX_2D &aBBox) const override
a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)
Definition: ring_2d.cpp:65
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
Definition: ring_2d.cpp:72
SFVEC2F m_center
Definition: ring_2d.h:54
SFVEC2F m_Dir
Definition: ray.h:110
SFVEC2F at(float t) const
Definition: ray.h:137
SFVEC2F m_centroid
Definition: object_2d.h:111
glm::vec2 SFVEC2F
Definition: xv3d_types.h:42
INTERSECTION_RESULT
Definition: object_2d.h:36
void Reset()
Reset the bounding box to zero and uninitialize it.
Definition: bbox_2d.cpp:86
bool IsPointInside(const SFVEC2F &aPoint) const override
Definition: ring_2d.cpp:154
float m_Length
Definition: ray.h:112
float m_outer_radius_squared
Definition: ring_2d.h:58
Manage a bounding box defined by two SFVEC2F min max points.
Definition: bbox_2d.h:41
SFVEC2F m_Start
Definition: ray.h:107
bool Overlaps(const BBOX_2D &aBBox) const override
Test if the box overlaps the object.
Definition: ring_2d.cpp:58
RING_2D(const SFVEC2F &aCenter, float aInnerRadius, float aOuterRadius, const BOARD_ITEM &aBoardItem)
Definition: ring_2d.cpp:35
OBJECT_2D_TYPE
Definition: object_2d.h:44
float m_inner_radius_squared
Definition: ring_2d.h:57
bool Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition: bbox_2d.cpp:211
void ScaleNextUp()
Scale a bounding box to the next float representation making it larger.
Definition: bbox_2d.cpp:162
bool IsInitialized() const
Check if this bounding box is already initialized.
Definition: bbox_2d.cpp:79
Definition: ray.h:105