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 
47 CXYPLANE::CXYPLANE( SFVEC3F aCenterPoint, float aXSize, float aYSize )
49 {
50  m_centerPoint = aCenterPoint;
51  m_xsize = aXSize;
52  m_ysize = aYSize;
53  m_xsize_inv2 = 1.0f / (2.0f * aXSize);
54  m_ysize_inv2 = 1.0f / (2.0f * aYSize);
55  m_bbox.Set( SFVEC3F( aCenterPoint.x - aXSize / 2.0f,
56  aCenterPoint.y - aYSize / 2.0f,
57  aCenterPoint.z ),
58  SFVEC3F( aCenterPoint.x + aXSize / 2.0f,
59  aCenterPoint.y + aYSize / 2.0f,
60  aCenterPoint.z ) );
61  m_centroid = aCenterPoint;
62 }
63 
64 
65 bool CXYPLANE::Intersect( const RAY &aRay, HITINFO &aHitInfo ) const
66 {
67  const float t = (m_centerPoint.z - aRay.m_Origin.z) * aRay.m_InvDir.z;
68 
69  if( ( t < FLT_EPSILON ) ||
70  ( t >= aHitInfo.m_tHit ) )
71  return false;
72 
73  const float vSU = t * aRay.m_Dir.x + aRay.m_Origin.x - m_centerPoint.x;
74 
75  if( (vSU < -m_xsize) || (vSU > m_xsize) )
76  return false;
77 
78  const float vSV = t * aRay.m_Dir.y + aRay.m_Origin.y - m_centerPoint.y;
79 
80  if( (vSV < -m_ysize) || (vSV > m_ysize) )
81  return false;
82 
83  aHitInfo.m_tHit = t;
84  aHitInfo.m_HitPoint = aRay.at( t );
85  aHitInfo.pHitObject = this;
86 
87  if( aRay.m_dirIsNeg[2] )
88  aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f, 1.0f );
89  else
90  aHitInfo.m_HitNormal = SFVEC3F( 0.0f, 0.0f,-1.0f );
91 
92  m_material->PerturbeNormal( aHitInfo.m_HitNormal, aRay, aHitInfo );
93 
94  return true;
95 }
96 
97 
98 bool CXYPLANE::IntersectP(const RAY &aRay , float aMaxDistance ) const
99 {
100  const float t = (m_centerPoint.z - aRay.m_Origin.z) * aRay.m_InvDir.z;
101 
102  if( ( t < FLT_EPSILON ) ||
103  ( t >= aMaxDistance ) )
104  return false;
105 
106  const float vSU = t * aRay.m_Dir.x + aRay.m_Origin.x - m_centerPoint.x;
107 
108  if( (vSU < -m_xsize) || (vSU > m_xsize) )
109  return false;
110 
111  const float vSV = t * aRay.m_Dir.y + aRay.m_Origin.y - m_centerPoint.y;
112 
113  if( (vSV < -m_ysize) || (vSV > m_ysize) )
114  return false;
115 
116  return true;
117 }
118 
119 
120 bool CXYPLANE::Intersects( const CBBOX &aBBox ) const
121 {
122  return m_bbox.Intersects( aBBox );
123 }
124 
125 
126 SFVEC3F CXYPLANE::GetDiffuseColor( const HITINFO &aHitInfo ) const
127 {
128  (void)aHitInfo; // unused
129 
130  return m_diffusecolor;
131 }
SFVEC3F m_centroid
Definition: cobject.h:54
bool IntersectP(const RAY &aRay, float aMaxDistance) const override
Functions Intersect for shadow test.
Definition: cplane.cpp:98
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: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
bool Intersect(const RAY &aRay, HITINFO &aHitInfo) const override
Functions Intersect.
Definition: cplane.cpp:65
SFVEC3F m_InvDir
Definition: ray.h:75
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
float m_xsize
Definition: cplane.h:62
const SFVEC3F GetExtent() const
Function GetExtent.
Definition: cbbox.cpp:148
SFVEC3F GetDiffuseColor(const HITINFO &aHitInfo) const override
Definition: cplane.cpp:126
unsigned int m_dirIsNeg[3]
Definition: ray.h:80
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:72
SFVEC3F m_Origin
Definition: ray.h:69
bool Intersects(const CBBOX &aBBox) const override
Function Intersects.
Definition: cplane.cpp:120
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
SFVEC3F m_HitNormal
(12) normal at the hit point
Definition: hitinfo.h:42
SFVEC3F m_centerPoint
Definition: cplane.h:61
float m_ysize
Definition: cplane.h:63
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
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