KiCad PCB EDA Suite
ccylinder.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 <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 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 
30 #include "3d_fastmath.h"
31 #include "ccylinder.h"
32 
33 
34 CVCYLINDER::CVCYLINDER( SFVEC2F aCenterPoint, float aZmin, float aZmax, float aRadius )
36 {
37  m_center = aCenterPoint;
38  m_radius_squared = aRadius * aRadius;
39  m_inv_radius = 1.0f / aRadius;
40 
41  m_bbox.Set( SFVEC3F( aCenterPoint.x - aRadius,
42  aCenterPoint.y - aRadius,
43  aZmin ),
44  SFVEC3F( aCenterPoint.x + aRadius,
45  aCenterPoint.y + aRadius,
46  aZmax ) );
49 }
50 
51 
52 bool CVCYLINDER::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
53 {
54  // Based on:
55  // http://www.cs.utah.edu/~lha/Code%206620%20/Ray4/Cylinder.cpp
56  // Ray-sphere intersection: geometric
57  // /////////////////////////////////////////////////////////////////////////
58  const double OCx_Start = aRay.m_Origin.x - m_center.x;
59  const double OCy_Start = aRay.m_Origin.y - m_center.y;
60 
61  const double p_dot_p = OCx_Start * OCx_Start + OCy_Start * OCy_Start;
62 
63  const double a = (double)aRay.m_Dir.x * (double)aRay.m_Dir.x +
64  (double)aRay.m_Dir.y * (double)aRay.m_Dir.y;
65  const double b = (double)aRay.m_Dir.x * (double)OCx_Start +
66  (double)aRay.m_Dir.y * (double)OCy_Start;
67  const double c = p_dot_p - m_radius_squared;
68 
69  const float delta = (float)(b * b - a * c);
70 
71  bool hitResult = false;
72 
73  if( delta > FLT_EPSILON )
74  {
75  const float inv_a = 1.0 / a;
76 
77  const float sdelta = sqrtf( delta );
78  const float t = (-b - sdelta) * inv_a;
79  const float z = aRay.m_Origin.z + t * aRay.m_Dir.z;
80 
81  if( (z >= m_bbox.Min().z) &&
82  (z <= m_bbox.Max().z) )
83  {
84  if( t < aHitInfo.m_tHit )
85  {
86  hitResult = true;
87  aHitInfo.m_tHit = t;
88  }
89  }
90 
91  if( !hitResult )
92  {
93  const float t1 = (-b + sdelta) * inv_a;
94  const float z1 = aRay.m_Origin.z + t1 * aRay.m_Dir.z;
95 
96  if( (z1 > m_bbox.Min().z ) &&
97  (z1 < m_bbox.Max().z ) )
98  {
99  if( t1 < aHitInfo.m_tHit )
100  {
101  hitResult = true;
102  aHitInfo.m_tHit = t1;
103  }
104  }
105  }
106  }
107 
108  if( hitResult )
109  {
110  aHitInfo.m_HitPoint = aRay.at( aHitInfo.m_tHit );
111 
112  const SFVEC2F hitPoint2D = SFVEC2F( aHitInfo.m_HitPoint.x,
113  aHitInfo.m_HitPoint.y );
114 
115  aHitInfo.m_HitNormal = SFVEC3F( -(hitPoint2D.x - m_center.x) * m_inv_radius,
116  -(hitPoint2D.y - m_center.y) * m_inv_radius,
117  0.0f );
118 
119  m_material->PerturbeNormal( aHitInfo.m_HitNormal, aRay, aHitInfo );
120 
121  aHitInfo.pHitObject = this;
122  }
123 
124  return hitResult;
125 }
126 
127 
128 bool CVCYLINDER::IntersectP(const RAY &aRay , float aMaxDistance ) const
129 {
130  // Based on:
131  // http://www.cs.utah.edu/~lha/Code%206620%20/Ray4/Cylinder.cpp
132  // Ray-sphere intersection: geometric
133  // /////////////////////////////////////////////////////////////////////////
134  const double OCx_Start = aRay.m_Origin.x - m_center.x;
135  const double OCy_Start = aRay.m_Origin.y - m_center.y;
136 
137  const double p_dot_p = OCx_Start * OCx_Start + OCy_Start * OCy_Start;
138 
139  const double a = (double)aRay.m_Dir.x * (double)aRay.m_Dir.x +
140  (double)aRay.m_Dir.y * (double)aRay.m_Dir.y;
141  const double b = (double)aRay.m_Dir.x * (double)OCx_Start +
142  (double)aRay.m_Dir.y * (double)OCy_Start;
143  const double c = p_dot_p - m_radius_squared;
144 
145  const float delta = (float)(b * b - a * c);
146 
147  if( delta > FLT_EPSILON )
148  {
149  const float inv_a = 1.0 / a;
150 
151  const float sdelta = sqrtf( delta );
152  const float t = (-b - sdelta) * inv_a;
153  const float z = aRay.m_Origin.z + t * aRay.m_Dir.z;
154 
155  if( (z >= m_bbox.Min().z) &&
156  (z <= m_bbox.Max().z) )
157  {
158  if( t < aMaxDistance )
159  return true;
160  }
161 
162  const float t1 = (-b + sdelta) * inv_a;
163  const float z1 = aRay.m_Origin.z + t1 * aRay.m_Dir.z;
164 
165  if( (z1 > m_bbox.Min().z ) &&
166  (z1 < m_bbox.Max().z ) )
167  {
168  if( t1 < aMaxDistance )
169  return true;
170  }
171  }
172 
173  return false;
174 }
175 
176 
177 bool CVCYLINDER::Intersects( const CBBOX &aBBox ) const
178 {
179  // !TODO: improove
180  return m_bbox.Intersects( aBBox );
181 }
182 
184 {
185  (void)aHitInfo; // unused
186 
187  return m_diffusecolor;
188 }
const SFVEC3F & Max() const
Function Max return the maximum vertex pointer.
Definition: cbbox.h:212
Defines math related functions.
SFVEC3F m_centroid
Definition: cobject.h:54
bool Intersects(const CBBOX &aBBox) const
Function Intersects test if a bounding box intersects this box.
Definition: cbbox.cpp:230
CBBOX m_bbox
Definition: cobject.h:53
bool Intersects(const CBBOX &aBBox) const override
Function Intersects.
Definition: ccylinder.cpp:177
const CMATERIAL * m_material
Definition: cobject.h:56
Definition: ray.h:67
float m_tHit
( 4) distance
Definition: hitinfo.h:43
void Set(const SFVEC3F &aPbMin, const SFVEC3F &aPbMax)
Function Set Set bounding box with new parameters.
Definition: cbbox.cpp:67
SFVEC3F at(float t) const
Definition: ray.h:89
SFVEC3F m_HitPoint
(12) hit position
Definition: hitinfo.h:49
SFVEC3F GetCenter() const
Function GetCenter return the center point of the bounding box.
Definition: cbbox.cpp:135
SFVEC3F m_diffusecolor
Definition: ccylinder.h:63
glm::vec2 SFVEC2F
Definition: xv3d_types.h:45
bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const override
Functions Intersect.
Definition: ccylinder.cpp:52
SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const override
Definition: ccylinder.cpp:183
CVCYLINDER(SFVEC2F aCenterPoint, float aZmin, float aZmax, float aRadius)
Constructor CVCYLINDER.
Definition: ccylinder.cpp:34
void ScaleNextUp()
Function ScaleNextUp scales a bounding box to the next float representation making it larger.
Definition: cbbox.cpp:206
const COBJECT * pHitObject
( 4) Object that was hitted
Definition: hitinfo.h:45
SFVEC3F m_Dir
Definition: ray.h:72
const SFVEC3F & Min() const
Function Min return the minimun vertex pointer.
Definition: cbbox.h:205
SFVEC3F m_Origin
Definition: ray.h:69
OBJECT3D_TYPE
Definition: cobject.h:38
Stores the hit information of a ray with a point on the surface of a object.
Definition: hitinfo.h:40
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
bool IntersectP(const RAY &aRay, float aMaxDistance) const override
Functions Intersect for shadow test.
Definition: ccylinder.cpp:128
float m_inv_radius
Definition: ccylinder.h:62
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition: hitinfo.h:42
SFVEC2F m_center
Definition: ccylinder.h:60
float m_radius_squared
Definition: ccylinder.h:61
void PerturbeNormal(SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo) const
Definition: cmaterial.cpp:97
CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:40