KiCad PCB EDA Suite
Loading...
Searching...
No Matches
cylinder_3d.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 "3d_fastmath.h"
26#include "cylinder_3d.h"
27
28
29CYLINDER::CYLINDER( SFVEC2F aCenterPoint, float aZmin, float aZmax, float aRadius )
31{
32 m_center = aCenterPoint;
33 m_radius_squared = aRadius * aRadius;
34 m_inv_radius = 1.0f / aRadius;
35
36 m_bbox.Set( SFVEC3F( aCenterPoint.x - aRadius, aCenterPoint.y - aRadius, aZmin ),
37 SFVEC3F( aCenterPoint.x + aRadius, aCenterPoint.y + aRadius, aZmax ) );
38 m_bbox.ScaleNextUp();
39 m_centroid = m_bbox.GetCenter();
40}
41
42
43bool CYLINDER::Intersect( const RAY& aRay, HITINFO& aHitInfo ) const
44{
45 // Based on: http://www.cs.utah.edu/~lha/Code%206620%20/Ray4/Cylinder.cpp
46 // Ray-sphere intersection: geometric
47 const double OCx_Start = aRay.m_Origin.x - m_center.x;
48 const double OCy_Start = aRay.m_Origin.y - m_center.y;
49
50 const double p_dot_p = OCx_Start * OCx_Start + OCy_Start * OCy_Start;
51
52 const double a = (double)aRay.m_Dir.x * (double)aRay.m_Dir.x +
53 (double)aRay.m_Dir.y * (double)aRay.m_Dir.y;
54 const double b = (double)aRay.m_Dir.x * (double)OCx_Start +
55 (double)aRay.m_Dir.y * (double)OCy_Start;
56 const double c = p_dot_p - m_radius_squared;
57
58 const float delta = (float) ( b * b - a * c );
59
60 bool hitResult = false;
61
62 if( delta > FLT_EPSILON )
63 {
64 const float inv_a = 1.0 / a;
65
66 const float sdelta = sqrtf( delta );
67 const float t = (-b - sdelta) * inv_a;
68 const float z = aRay.m_Origin.z + t * aRay.m_Dir.z;
69
70 if( ( z >= m_bbox.Min().z ) && ( z <= m_bbox.Max().z ) )
71 {
72 if( t < aHitInfo.m_tHit )
73 {
74 hitResult = true;
75 aHitInfo.m_tHit = t;
76 }
77 }
78
79 if( !hitResult )
80 {
81 const float t1 = (-b + sdelta) * inv_a;
82 const float z1 = aRay.m_Origin.z + t1 * aRay.m_Dir.z;
83
84 if( ( z1 > m_bbox.Min().z ) && ( z1 < m_bbox.Max().z ) )
85 {
86 if( t1 < aHitInfo.m_tHit )
87 {
88 hitResult = true;
89 aHitInfo.m_tHit = t1;
90 }
91 }
92 }
93 }
94
95 if( hitResult )
96 {
97 aHitInfo.m_HitPoint = aRay.at( aHitInfo.m_tHit );
98
99 const SFVEC2F hitPoint2D = SFVEC2F( aHitInfo.m_HitPoint.x, aHitInfo.m_HitPoint.y );
100
101 aHitInfo.m_HitNormal = SFVEC3F( -( hitPoint2D.x - m_center.x ) * m_inv_radius,
102 -( hitPoint2D.y - m_center.y ) * m_inv_radius, 0.0f );
103
104 m_material->Generate( aHitInfo.m_HitNormal, aRay, aHitInfo );
105
106 aHitInfo.pHitObject = this;
107 }
108
109 return hitResult;
110}
111
112
113bool CYLINDER::IntersectP(const RAY& aRay , float aMaxDistance ) const
114{
115 // Based on: http://www.cs.utah.edu/~lha/Code%206620%20/Ray4/Cylinder.cpp
116 // Ray-sphere intersection: geometric
117 const double OCx_Start = aRay.m_Origin.x - m_center.x;
118 const double OCy_Start = aRay.m_Origin.y - m_center.y;
119
120 const double p_dot_p = OCx_Start * OCx_Start + OCy_Start * OCy_Start;
121
122 const double a = (double)aRay.m_Dir.x * (double)aRay.m_Dir.x +
123 (double)aRay.m_Dir.y * (double)aRay.m_Dir.y;
124 const double b = (double)aRay.m_Dir.x * (double)OCx_Start +
125 (double)aRay.m_Dir.y * (double)OCy_Start;
126 const double c = p_dot_p - m_radius_squared;
127
128 const float delta = (float) ( b * b - a * c );
129
130 if( delta > FLT_EPSILON )
131 {
132 const float inv_a = 1.0 / a;
133
134 const float sdelta = sqrtf( delta );
135 const float t = ( -b - sdelta ) * inv_a;
136 const float z = aRay.m_Origin.z + t * aRay.m_Dir.z;
137
138 if( ( z >= m_bbox.Min().z ) && ( z <= m_bbox.Max().z ) )
139 {
140 if( t < aMaxDistance )
141 return true;
142 }
143
144 const float t1 = ( -b + sdelta ) * inv_a;
145 const float z1 = aRay.m_Origin.z + t1 * aRay.m_Dir.z;
146
147 if( ( z1 > m_bbox.Min().z ) && ( z1 < m_bbox.Max().z ) )
148 {
149 if( t1 < aMaxDistance )
150 return true;
151 }
152 }
153
154 return false;
155}
156
157
158bool CYLINDER::Intersects( const BBOX_3D& aBBox ) const
159{
160 // !TODO: improve
161 return m_bbox.Intersects( aBBox );
162}
163
164
165SFVEC3F CYLINDER::GetDiffuseColor( const HITINFO& /* aHitInfo */ ) const
166{
167 return m_diffusecolor;
168}
Defines math related functions.
SFVEC2F m_center
Definition cylinder_3d.h:52
bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const override
SFVEC3F m_diffusecolor
Definition cylinder_3d.h:55
bool IntersectP(const RAY &aRay, float aMaxDistance) const override
bool Intersects(const BBOX_3D &aBBox) const override
CYLINDER(SFVEC2F aCenterPoint, float aZmin, float aZmax, float aRadius)
float m_radius_squared
Definition cylinder_3d.h:53
SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const override
float m_inv_radius
Definition cylinder_3d.h:54
BBOX_3D m_bbox
Definition object_3d.h:93
SFVEC3F m_centroid
Definition object_3d.h:94
OBJECT_3D(OBJECT_3D_TYPE aObjType)
Definition object_3d.cpp:36
const MATERIAL * m_material
Definition object_3d.h:96
OBJECT_3D_TYPE
Definition object_3d.h:35
Manage a bounding box defined by two SFVEC3F min max points.
Definition bbox_3d.h:39
Stores the hit information of a ray with a point on the surface of a object.
Definition hitinfo.h:32
float m_tHit
( 4) distance
Definition hitinfo.h:34
const OBJECT_3D * pHitObject
( 4) Object that was hitted
Definition hitinfo.h:36
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition hitinfo.h:33
SFVEC3F m_HitPoint
(12) hit position
Definition hitinfo.h:40
Definition ray.h:59
SFVEC3F m_Dir
Definition ray.h:63
SFVEC3F m_Origin
Definition ray.h:60
SFVEC3F at(float t) const
Definition ray.h:80
int delta
glm::vec2 SFVEC2F
Definition xv3d_types.h:38
glm::vec3 SFVEC3F
Definition xv3d_types.h:40