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