KiCad PCB EDA Suite
Loading...
Searching...
No Matches
filled_circle_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
25
26#include "filled_circle_2d.h"
27#include "../ray.h"
28#include <wx/debug.h>
29
30
31FILLED_CIRCLE_2D::FILLED_CIRCLE_2D( const SFVEC2F& aCenter, float aRadius,
32 const BOARD_ITEM& aBoardItem ) :
34{
35 wxASSERT( aRadius > 0.0f ); // If that happens, it should be handled before create this circle
36
37 m_center = aCenter;
38 m_radius = aRadius;
39 m_radius_squared = aRadius * aRadius;
40
41 m_bbox.Reset();
42 m_bbox.Set( m_center - SFVEC2F( aRadius, aRadius ),
43 m_center + SFVEC2F( aRadius, aRadius ) );
44 m_bbox.ScaleNextUp();
45 m_centroid = m_bbox.GetCenter();
46
47 wxASSERT( m_bbox.IsInitialized() );
48}
49
50
51bool FILLED_CIRCLE_2D::Overlaps( const BBOX_2D& aBBox ) const
52{
53 // NOT IMPLEMENTED, why?
54 return false;
55}
56
57
58bool FILLED_CIRCLE_2D::Intersects( const BBOX_2D& aBBox ) const
59{
60 return aBBox.Intersects( m_center, m_radius_squared );
61}
62
63
64bool FILLED_CIRCLE_2D::Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const
65{
66 // This code used directly from Steve Marschner's CS667 framework
67 // http://cs665pd.googlecode.com/svn/trunk/photon/sphere.cpp
68
69 // Compute some factors used in computation
70 const float qx = aSegRay.m_Start.x - m_center.x;
71 const float qy = aSegRay.m_Start.y - m_center.y;
72
73 const float qd = qx * aSegRay.m_Dir.x + qy * aSegRay.m_Dir.y;
74 const float qq = qx * qx + qy * qy;
75
76 // solving the quadratic equation for t at the pts of intersection
77 // dd*t^2 + (2*qd)*t + (qq-r^2) = 0
78 const float discriminantsqr = ( qd * qd - ( qq - m_radius_squared ) );
79
80 // If the discriminant is less than zero, there is no intersection
81 if( discriminantsqr < FLT_EPSILON )
82 return false;
83
84 // Otherwise check and make sure that the intersections occur on the ray (t > 0) and
85 // return the closer one.
86 const float discriminant = sqrt( discriminantsqr );
87 const float t1 = ( -qd - discriminant );
88 const float t2 = ( -qd + discriminant );
89 float t;
90
91 if( ( t1 > 0.0f ) && ( t1 < aSegRay.m_Length ) )
92 {
93 t = t1;
94 }
95 else
96 {
97 if( ( t2 > 0.0f ) && ( t2 < aSegRay.m_Length ) )
98 t = t2;
99 else
100 return false; // Neither intersection was in the ray's half line.
101 }
102
103 wxASSERT( ( t > 0.0f ) && ( t <= aSegRay.m_Length ) );
104
105 // Convert the intersection to a normalized 0.0 .. 1.0
106 if( aOutT )
107 *aOutT = t / aSegRay.m_Length;
108
109 const SFVEC2F hitPoint = aSegRay.at( t );
110
111 if( aNormalOut )
112 *aNormalOut = (hitPoint - m_center) / m_radius;
113
114 return true;
115}
116
117
119{
120 if( !m_bbox.Intersects( aBBox ) )
122
123 SFVEC2F v[4];
124
125 v[0] = aBBox.Min() - m_center;
126 v[1] = aBBox.Max() - m_center;
127 v[2] = SFVEC2F( aBBox.Min().x, aBBox.Max().y ) - m_center;
128 v[3] = SFVEC2F( aBBox.Max().x, aBBox.Min().y ) - m_center;
129
130 float s[4];
131
132 s[0] = v[0].x * v[0].x + v[0].y * v[0].y;
133 s[1] = v[1].x * v[1].x + v[1].y * v[1].y;
134 s[2] = v[2].x * v[2].x + v[2].y * v[2].y;
135 s[3] = v[3].x * v[3].x + v[3].y * v[3].y;
136
137 bool isInside[4];
138
139 isInside[0] = s[0] <= m_radius_squared;
140 isInside[1] = s[1] <= m_radius_squared;
141 isInside[2] = s[2] <= m_radius_squared;
142 isInside[3] = s[3] <= m_radius_squared;
143
144 // Check if all points are inside the circle
145 if( isInside[0] && isInside[1] && isInside[2] && isInside[3] )
147
148 // Check if any point is inside the circle
149 if( isInside[0] || isInside[1] || isInside[2] || isInside[3] )
151
153}
154
155
156bool FILLED_CIRCLE_2D::IsPointInside( const SFVEC2F& aPoint ) const
157{
158 const SFVEC2F v = m_center - aPoint;
159
160 if( ( v.x * v.x + v.y * v.y ) <= m_radius_squared )
161 return true;
162
163 return false;
164}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:81
bool Overlaps(const BBOX_2D &aBBox) const override
Test if the box overlaps the object.
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.
bool Intersects(const BBOX_2D &aBBox) const override
a.Intersects(b) ⇔ !a.Disjoint(b) ⇔ !(a ∩ b = ∅)
bool Intersect(const RAYSEG2D &aSegRay, float *aOutT, SFVEC2F *aNormalOut) const override
FILLED_CIRCLE_2D(const SFVEC2F &aCenter, float aRadius, const BOARD_ITEM &aBoardItem)
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
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 Intersects(const BBOX_2D &aBBox) const
Test if a bounding box intersects this box.
Definition bbox_2d.cpp:207
const SFVEC2F & Min() const
Definition bbox_2d.h:171
const SFVEC2F & Max() const
Definition bbox_2d.h:176
float m_Length
Definition ray.h:108
SFVEC2F m_Dir
Definition ray.h:106
SFVEC2F at(float t) const
Definition ray.h:133
SFVEC2F m_Start
Definition ray.h:103
glm::vec2 SFVEC2F
Definition xv3d_types.h:38