KiCad PCB EDA Suite
cplane.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 "cplane.h"
31 
32 
34 {
35  m_centerPoint = aBBox.GetCenter();
37 
38  m_bbox.Reset();
39  m_bbox.Set( aBBox );
40  m_xsize = aBBox.GetExtent().x;
41  m_ysize = aBBox.GetExtent().y;
42  m_xsize_inv2 = 1.0f / (2.0f * m_xsize);
43  m_ysize_inv2 = 1.0f / (2.0f * m_ysize);
44 }
45 
46 
48  float aXSize,
49  float aYSize ) : COBJECT( OBJ3D_XYPLANE )
50 {
51  m_centerPoint = aCenterPoint;
52  m_xsize = aXSize;
53  m_ysize = aYSize;
54  m_xsize_inv2 = 1.0f / (2.0f * aXSize);
55  m_ysize_inv2 = 1.0f / (2.0f * aYSize);
56  m_bbox.Set( SFVEC3F( aCenterPoint.x - aXSize / 2.0f,
57  aCenterPoint.y - aYSize / 2.0f,
58  aCenterPoint.z ),
59  SFVEC3F( aCenterPoint.x + aXSize / 2.0f,
60  aCenterPoint.y + aYSize / 2.0f,
61  aCenterPoint.z ) );
62  m_centroid = aCenterPoint;
63 }
64 
65 
66 bool CXYPLANE::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
67 {
68  const float t = (m_centerPoint.z - aRay.m_Origin.z) * aRay.m_InvDir.z;
69 
70  if( ( t < FLT_EPSILON ) ||
71  ( t >= aHitInfo.m_tHit ) )
72  return false;
73 
74  const float vSU = t * aRay.m_Dir.x + aRay.m_Origin.x - m_centerPoint.x;
75 
76  if( (vSU < -m_xsize) || (vSU > m_xsize) )
77  return false;
78 
79  const float vSV = t * aRay.m_Dir.y + aRay.m_Origin.y - m_centerPoint.y;
80 
81  if( (vSV < -m_ysize) || (vSV > m_ysize) )
82  return false;
83 
84  aHitInfo.m_tHit = t;
85  aHitInfo.m_HitPoint = aRay.at( t );
86  aHitInfo.pHitObject = this;
87 
88  if( aRay.m_dirIsNeg[2] )
89  aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, 1.0f );
90  else
91  aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f,-1.0f );
92 
93  m_material->PerturbeNormal( aHitInfo.m_HitNormal, aRay, aHitInfo );
94 
95  return true;
96 }
97 
98 
99 bool CXYPLANE::IntersectP(const RAY &aRay , float aMaxDistance ) const
100 {
101  const float t = (m_centerPoint.z - aRay.m_Origin.z) * aRay.m_InvDir.z;
102 
103  if( ( t < FLT_EPSILON ) ||
104  ( t >= aMaxDistance ) )
105  return false;
106 
107  const float vSU = t * aRay.m_Dir.x + aRay.m_Origin.x - m_centerPoint.x;
108 
109  if( (vSU < -m_xsize) || (vSU > m_xsize) )
110  return false;
111 
112  const float vSV = t * aRay.m_Dir.y + aRay.m_Origin.y - m_centerPoint.y;
113 
114  if( (vSV < -m_ysize) || (vSV > m_ysize) )
115  return false;
116 
117  return true;
118 }
119 
120 
121 bool CXYPLANE::Intersects( const CBBOX &aBBox ) const
122 {
123  return m_bbox.Intersects( aBBox );
124 }
125 
126 
127 SFVEC3F CXYPLANE::GetDiffuseColor( const HITINFO &aHitInfo ) const
128 {
129  (void)aHitInfo; // unused
130 
131  return m_diffusecolor;
132 }
SFVEC3F m_centroid
Definition: cobject.h:54
bool IntersectP(const RAY &aRay, float aMaxDistance) const override
Functions Intersect for shadow test.
Definition: cplane.cpp:99
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
const CMATERIAL * m_material
Definition: cobject.h:56
Definition: ray.h:43
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
bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const override
Functions Intersect.
Definition: cplane.cpp:66
SFVEC3F m_InvDir
Definition: ray.h:51
SFVEC3F m_HitPoint
(12) hit position
Definition: hitinfo.h:49
float m_xsize
Definition: cplane.h:62
SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const override
Definition: cplane.cpp:127
void PerturbeNormal(SFVEC3F &aNormal, const RAY &aRay, const HITINFO &aHitInfo) const
Definition: cmaterial.cpp:88
unsigned int m_dirIsNeg[3]
Definition: ray.h:56
float m_xsize_inv2
Definition: cplane.h:64
CXYPLANE(const CBBOX &aBBox)
Definition: cplane.cpp:33
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
bool Intersects(const CBBOX &aBBox) const override
Function Intersects.
Definition: cplane.cpp:121
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
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition: hitinfo.h:42
SFVEC3F m_centerPoint
Definition: cplane.h:61
const SFVEC3F GetExtent() const
Function GetExtent.
Definition: cbbox.cpp:148
float m_ysize
Definition: cplane.h:63
Class CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:40
void Reset()
Function Reset reset the bounding box to zero and de-initialized it.
Definition: cbbox.cpp:98
SFVEC3F m_diffusecolor
Definition: cplane.h:66
float m_ysize_inv2
Definition: cplane.h:65