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