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