KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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
37void 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 SFVEC3F tl_tr = topRight.m_Origin - topLeft.m_Origin;
46 SFVEC3F tr_br = bottomRight.m_Origin - topRight.m_Origin;
47 SFVEC3F br_bl = bottomLeft.m_Origin - bottomRight.m_Origin;
48 SFVEC3F bl_tl = topLeft.m_Origin - bottomLeft.m_Origin;
49
50 m_normals[0] = glm::cross( tl_tr, topLeft.m_Dir ); // TOP
51 m_normals[1] = glm::cross( tr_br, topRight.m_Dir ); // RIGHT
52 m_normals[2] = glm::cross( br_bl, bottomRight.m_Dir ); // BOTTOM
53 m_normals[3] = glm::cross( bl_tl, bottomLeft.m_Dir ); // LEFT
54}
55
56
57// There are multiple implementation of this algorithm on the web,
58// this one was based on the one find in:
59// https://github.com/nslo/raytracer/blob/2c2e0ff4bbb6082e07804ec7cf0b92673b98dcb1/src/raytracer/geom_utils.cpp#L66
60// by Nathan Slobody and Adam Wright
61// The frustum test is not exllude all the boxes,
62// when a box is behind and if it is intersecting the planes it will not be discardly but should.
63bool FRUSTUM::Intersect( const BBOX_3D& aBBox ) const
64{
65 const SFVEC3F box[8] = { aBBox.Min(),
66 aBBox.Max(),
67 SFVEC3F(aBBox.Min().x, aBBox.Min().y, aBBox.Max().z),
68 SFVEC3F(aBBox.Min().x, aBBox.Max().y, aBBox.Min().z),
69 SFVEC3F(aBBox.Min().x, aBBox.Max().y, aBBox.Max().z),
70 SFVEC3F(aBBox.Max().x, aBBox.Min().y, aBBox.Min().z),
71 SFVEC3F(aBBox.Max().x, aBBox.Min().y, aBBox.Max().z),
72 SFVEC3F(aBBox.Max().x, aBBox.Max().y, aBBox.Min().z) };
73
74 // test each plane of frustum individually; if the point is on the wrong
75 // side of the plane, the box is outside the frustum and we can exit
76 int out_side = 0;
77
78 for( unsigned int i = 0; i < 4; ++i )
79 {
80 const SFVEC3F& pointPlane = m_point[i];
81 const SFVEC3F& normalPlane = m_normals[i];
82
83 for( unsigned int j = 0; j < 8; ++j )
84 {
85 const SFVEC3F OP = pointPlane - box[j];
86 const float dot = glm::dot( OP, normalPlane );
87
88 if( dot < FLT_EPSILON )
89 {
90 out_side++;
91
92 break;
93 }
94 }
95 }
96
97 if( out_side == 4 )
98 return true;
99
100 return false;
101}
Implement a frustum that is used for ray packet tests.
Manage a bounding box defined by two SFVEC3F min max points.
Definition: bbox_3d.h:43
const SFVEC3F & Min() const
Return the minimum vertex pointer.
Definition: bbox_3d.h:192
const SFVEC3F & Max() const
Return the maximum vertex pointer.
Definition: bbox_3d.h:199
SFVEC3F m_normals[4]
Definition: frustum.h:56
SFVEC3F m_point[4]
Definition: frustum.h:57
bool Intersect(const BBOX_3D &aBBox) const
Intersect aBBox with this frustum.
Definition: frustum.cpp:63
void GenerateFrustum(const RAY &topLeft, const RAY &topRight, const RAY &bottomLeft, const RAY &bottomRight)
Definition: frustum.cpp:37
Definition: ray.h:63
SFVEC3F m_Dir
Definition: ray.h:67
SFVEC3F m_Origin
Definition: ray.h:64
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44