KiCad PCB EDA Suite
cfrustum.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-2017 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2017 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 
31 #include "cfrustum.h"
32 
33 // !TODO: optimize wih SSE
34 //#if(GLM_ARCH != GLM_ARCH_PURE)
35 #if 0
36 #error not implemented
37 #else
38 
39 
40 #endif
41 
42 void CFRUSTUM::GenerateFrustum( const RAY &topLeft,
43  const RAY &topRight,
44  const RAY &bottomLeft,
45  const RAY &bottomRight )
46 {
47  m_point[0] = topLeft.m_Origin;
48  m_point[1] = topRight.m_Origin;
49  m_point[2] = bottomLeft.m_Origin;
50  m_point[3] = topLeft.m_Origin;
51 
52  m_normals[0] = glm::cross( topRight.m_Dir, topLeft.m_Dir ); // TOP
53  m_normals[1] = glm::cross( bottomRight.m_Dir, topRight.m_Dir ); // RIGHT
54  m_normals[2] = glm::cross( bottomLeft.m_Dir, bottomRight.m_Dir ); // BOTTOM
55  m_normals[3] = glm::cross( topLeft.m_Dir, bottomLeft.m_Dir ); // LEFT
56 }
57 
58 
59 // There are multiple implementation of this algorithm on the web,
60 // this one was based on the one find in:
61 // https://github.com/nslo/raytracer/blob/2c2e0ff4bbb6082e07804ec7cf0b92673b98dcb1/src/raytracer/geom_utils.cpp#L66
62 // by Nathan Slobody and Adam Wright
63 // The frustum test is not exllude all the boxes,
64 // when a box is behind and if it is intersecting the planes it will not be discardly but should.
65 bool CFRUSTUM::Intersect( const CBBOX &aBBox ) const
66 {
67  const SFVEC3F box[8] = { aBBox.Min(),
68  aBBox.Max(),
69  SFVEC3F(aBBox.Min().x, aBBox.Min().y, aBBox.Max().z),
70  SFVEC3F(aBBox.Min().x, aBBox.Max().y, aBBox.Min().z),
71  SFVEC3F(aBBox.Min().x, aBBox.Max().y, aBBox.Max().z),
72  SFVEC3F(aBBox.Max().x, aBBox.Min().y, aBBox.Min().z),
73  SFVEC3F(aBBox.Max().x, aBBox.Min().y, aBBox.Max().z),
74  SFVEC3F(aBBox.Max().x, aBBox.Max().y, aBBox.Min().z) };
75 
76  // test each plane of frustum individually; if the point is on the wrong
77  // side of the plane, the box is outside the frustum and we can exit
78  int out_side = 0;
79 
80  for( unsigned int i = 0; i < 4; ++i )
81  {
82  const SFVEC3F &pointPlane = m_point[i];
83  const SFVEC3F &normalPlane = m_normals[i];
84 
85  for( unsigned int j = 0; j < 8; ++j )
86  {
87  const SFVEC3F OP = pointPlane - box[j];
88  const float dot = glm::dot( OP, normalPlane );
89 
90  if( dot < FLT_EPSILON )
91  {
92  out_side++;
93 
94  break;
95  }
96  }
97  }
98 
99  if( out_side == 4 )
100  return true;
101 
102  return false;
103 }
void GenerateFrustum(const RAY &topLeft, const RAY &topRight, const RAY &bottomLeft, const RAY &bottomRight)
GenerateFrustum.
Definition: cfrustum.cpp:42
SFVEC3F m_normals[4]
Definition: cfrustum.h:65
const SFVEC3F & Min() const
Function Min return the minimun vertex pointer.
Definition: cbbox.h:205
Definition: ray.h:43
SFVEC3F m_point[4]
Definition: cfrustum.h:66
implements a frustum that is used to test ray pack tests
const SFVEC3F & Max() const
Function Max return the maximum vertex pointer.
Definition: cbbox.h:212
bool Intersect(const CBBOX &aBBox) const
Intersect - Intersects a bbox with this frustum.
Definition: cfrustum.cpp:65
SFVEC3F m_Dir
Definition: ray.h:48
SFVEC3F m_Origin
Definition: ray.h:45
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
Class CBBOX manages a bounding box defined by two SFVEC3F min max points.
Definition: cbbox.h:40