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