GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_frustum.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib 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 Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  *****************************************************************************/
23 
24 #include "glc_frustum.h"
25 #include "glc_viewport.h"
26 
28 : m_PlaneList()
29 , m_PreviousMatrix()
30 {
31  for (int i= 0; i < 6; ++i)
32  {
33  m_PlaneList.append(GLC_Plane());
34  }
35 }
36 
38 : m_PlaneList(frustum.m_PlaneList)
39 , m_PreviousMatrix(frustum.m_PreviousMatrix)
40 {
41 
42 }
43 
45 {
46 
47 }
48 
50 {
51  const GLC_Point3d center= box.center();
52  const double radius= box.boundingSphereRadius();
53 
54  return localizeSphere(center, radius);
55 }
56 
58 {
59  GLC_Frustum::Localisation localisationResult= InFrustum;
60 
61  int i= 0;
62  bool continu= true;
63  while (continu && (i < 6))
64  {
65  //qDebug() << "Localisation of plane " << i;
66  localisationResult= static_cast<GLC_Frustum::Localisation>(localisationResult | localizeSphereToPlane(center, radius, m_PlaneList.at(i)));
67  continu= (localisationResult != GLC_Frustum::OutFrustum);
68  ++i;
69  }
70 
71  return localisationResult;
72 }
73 
74 GLC_Frustum::Localisation GLC_Frustum::localizeSphereToPlane(const GLC_Point3d& center, double radius, const GLC_Plane& plane) const
75 {
76  GLC_Frustum::Localisation localisationResult;
77  const double signedDistance= plane.distanceToPoint(center);
78  const double distance= fabs(signedDistance);
79  if (distance > radius)
80  {
81  if (signedDistance > 0) localisationResult= GLC_Frustum::InFrustum;
82  else localisationResult= GLC_Frustum::OutFrustum;
83  }
84  else
85  {
86  localisationResult= GLC_Frustum::IntersectFrustum;
87  }
88 
89  return localisationResult;
90 }
91 
92 bool GLC_Frustum::update(const GLC_Matrix4x4& compMatrix)
93 {
94 
95  // Test if the frustum change
96  if (compMatrix == m_PreviousMatrix)
97  {
98  //qDebug() << "No change in frustum";
99  return false;
100  }
101  else
102  {
103  m_PreviousMatrix= compMatrix;
104  // Left plane
105  m_PlaneList[LeftPlane].setA(compMatrix.getData()[3] + compMatrix.getData()[0]);
106  m_PlaneList[LeftPlane].setB(compMatrix.getData()[7] + compMatrix.getData()[4]);
107  m_PlaneList[LeftPlane].setC(compMatrix.getData()[11] + compMatrix.getData()[8]);
108  m_PlaneList[LeftPlane].setD(compMatrix.getData()[15] + compMatrix.getData()[12]);
109  m_PlaneList[LeftPlane].normalize();
110 
111  // Right plane
112  m_PlaneList[RightPlane].setA(compMatrix.getData()[3] - compMatrix.getData()[0]);
113  m_PlaneList[RightPlane].setB(compMatrix.getData()[7] - compMatrix.getData()[4]);
114  m_PlaneList[RightPlane].setC(compMatrix.getData()[11] - compMatrix.getData()[8]);
115  m_PlaneList[RightPlane].setD(compMatrix.getData()[15] - compMatrix.getData()[12]);
116  m_PlaneList[RightPlane].normalize();
117 
118  //Top plane
119  m_PlaneList[TopPlane].setA(compMatrix.getData()[3] + compMatrix.getData()[1]);
120  m_PlaneList[TopPlane].setB(compMatrix.getData()[7] + compMatrix.getData()[5]);
121  m_PlaneList[TopPlane].setC(compMatrix.getData()[11] + compMatrix.getData()[9]);
122  m_PlaneList[TopPlane].setD(compMatrix.getData()[15] + compMatrix.getData()[13]);
123  m_PlaneList[TopPlane].normalize();
124 
125  //Bottom plane
126  m_PlaneList[BottomPlane].setA(compMatrix.getData()[3] - compMatrix.getData()[1]);
127  m_PlaneList[BottomPlane].setB(compMatrix.getData()[7] - compMatrix.getData()[5]);
128  m_PlaneList[BottomPlane].setC(compMatrix.getData()[11] - compMatrix.getData()[9]);
129  m_PlaneList[BottomPlane].setD(compMatrix.getData()[15] - compMatrix.getData()[13]);
130  m_PlaneList[BottomPlane].normalize();
131 
132  //Near plane
133  m_PlaneList[NearPlane].setA(compMatrix.getData()[3] + compMatrix.getData()[2]);
134  m_PlaneList[NearPlane].setB(compMatrix.getData()[7] + compMatrix.getData()[6]);
135  m_PlaneList[NearPlane].setC(compMatrix.getData()[11] + compMatrix.getData()[10]);
136  m_PlaneList[NearPlane].setD(compMatrix.getData()[15] + compMatrix.getData()[14]);
137  m_PlaneList[NearPlane].normalize();
138 
139  //Far plane
140  m_PlaneList[FarPlane].setA(compMatrix.getData()[3] - compMatrix.getData()[2]);
141  m_PlaneList[FarPlane].setB(compMatrix.getData()[7] - compMatrix.getData()[6]);
142  m_PlaneList[FarPlane].setC(compMatrix.getData()[11] - compMatrix.getData()[10]);
143  m_PlaneList[FarPlane].setD(compMatrix.getData()[15] - compMatrix.getData()[14]);
144  m_PlaneList[FarPlane].normalize();
145  return true;
146  }
147 }

©2005-2013 Laurent Ribon