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