ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvFrustum.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // cloudViewer
11 #include <RayAndBox.h>
12 
13 // Local
14 #include "ecvGLMatrix.h"
15 
16 class Plane {
17 public:
18  Plane() : normal(0, 0, 1), constCoef(0) {}
19 
20  virtual ~Plane() = default;
21 
22  void setCoefficients(float a, float b, float c, float d) {
23  // set the normal vector
24  normal = CCVector3f(a, b, c);
25  // compute the length of the vector
26  float l = normal.norm();
27  if (l != 0) {
28  // normalize the vector
29  normal /= l;
30  // and divide constCoef as well
31  constCoef = d / l;
32  } else {
33  constCoef = d;
34  }
35  }
36 
37  float distance(const CCVector3f& p) const {
38  return normal.dot(p) + constCoef;
39  }
40 
41 public: // members
43  float constCoef;
44 };
45 
46 class AABox : public AABB<float> {
47 public:
48  AABox(const CCVector3f& A, const CCVector3f& B) : AABB<float>(A, B) {}
49 
51  return {corners[normal.x > 0 ? 1 : 0].x,
52  corners[normal.y > 0 ? 1 : 0].y,
53  corners[normal.z > 0 ? 1 : 0].z};
54  }
56  return {corners[normal.x < 0 ? 1 : 0].x,
57  corners[normal.y < 0 ? 1 : 0].y,
58  corners[normal.z < 0 ? 1 : 0].z};
59  }
60 };
61 
62 class AACube {
63 public:
64  AACube() : O(0, 0, 0), d(0) {}
65 
66  AACube(const CCVector3f& origin, float size) : O(origin), d(size) {}
67 
68  virtual ~AACube() = default;
69 
71  return {normal.x > 0 ? O.x + d : O.x, normal.y > 0 ? O.y + d : O.y,
72  normal.z > 0 ? O.z + d : O.z};
73  }
74 
76  return {normal.x < 0 ? O.x + d : O.x, normal.y < 0 ? O.y + d : O.y,
77  normal.z < 0 ? O.z + d : O.z};
78  }
79 
80 public: // members
82  float d;
83 };
84 
85 class Frustum {
86 public:
87  Frustum() = default;
88 
89  Frustum(const ccGLMatrixd& modelViewMat, const ccGLMatrixd& projMat) {
90  ccGLMatrixd MP = projMat * modelViewMat;
91  initfromMPMatrix(MP);
92  }
93 
94  virtual ~Frustum() = default;
95 
96  enum Intersection {
97  OUTSIDE = 0,
98  INTERSECT = 1,
99  INSIDE = 2,
100  };
101 
102 public: // Intersection tests
104  for (const auto& plane : pl) {
105  if (plane.distance(p) < 0) {
106  return OUTSIDE;
107  }
108  }
109 
110  return INSIDE;
111  }
112 
113  Intersection sphereInFrustum(const CCVector3f& c, float r) const {
115 
116  for (const auto& plane : pl) {
117  float distance = plane.distance(c);
118 
119  if (distance < -r)
120  return OUTSIDE;
121  else if (distance < r)
122  result = INTERSECT;
123  }
124 
125  return result;
126  }
127 
128  Intersection boxInFrustum(const AABox& box) const {
130 
131  for (const auto& plane : pl) {
132  if (plane.distance(box.getVertexP(plane.normal)) < 0)
133  return OUTSIDE;
134  else if (plane.distance(box.getVertexN(plane.normal)) < 0)
135  result = INTERSECT;
136  }
137 
138  return result;
139  }
140 
141  Intersection boxInFrustum(const AACube& cube) const {
143 
144  for (const auto& plane : pl) {
145  if (plane.distance(cube.getVertexP(plane.normal)) < 0)
146  return OUTSIDE;
147  else if (plane.distance(cube.getVertexN(plane.normal)) < 0)
148  result = INTERSECT;
149  }
150 
151  return result;
152  }
153 
154 protected: // protected methods
155  enum PLANE {
156  TOP = 0,
157  BOTTOM = 1,
158  LEFT = 2,
159  RIGHT = 3,
160  NEARP = 4,
161  FARP = 5
162  };
163 
164  template <typename T>
166  const T* m = MP.data();
167  pl[NEARP].setCoefficients(m[3] + m[2], m[7] + m[6], m[11] + m[10],
168  m[15] + m[14]);
169 
170  pl[FARP].setCoefficients(m[3] - m[2], m[7] - m[6], m[11] - m[10],
171  m[15] - m[14]);
172 
173  pl[BOTTOM].setCoefficients(m[3] + m[1], m[7] + m[5], m[11] + m[9],
174  m[15] + m[13]);
175 
176  pl[TOP].setCoefficients(m[3] - m[1], m[7] - m[5], m[11] - m[9],
177  m[15] - m[13]);
178 
179  pl[LEFT].setCoefficients(m[3] + m[0], m[7] + m[4], m[11] + m[8],
180  m[15] + m[12]);
181 
182  pl[RIGHT].setCoefficients(m[3] - m[0], m[7] - m[4], m[11] - m[8],
183  m[15] - m[12]);
184  }
185 
186 protected: // members
187  Plane pl[6];
188 };
Vector3Tpl< float > CCVector3f
Float 3D Vector.
Definition: CVGeom.h:801
double normal[3]
int size
core::Tensor result
Definition: VtkUtils.cpp:76
CCVector3f getVertexP(const CCVector3f &normal) const
Definition: ecvFrustum.h:50
CCVector3f getVertexN(const CCVector3f &normal) const
Definition: ecvFrustum.h:55
AABox(const CCVector3f &A, const CCVector3f &B)
Definition: ecvFrustum.h:48
CCVector3f getVertexP(const CCVector3f &normal) const
Definition: ecvFrustum.h:70
virtual ~AACube()=default
CCVector3f getVertexN(const CCVector3f &normal) const
Definition: ecvFrustum.h:75
float d
Definition: ecvFrustum.h:82
AACube(const CCVector3f &origin, float size)
Definition: ecvFrustum.h:66
CCVector3f O
Definition: ecvFrustum.h:81
AACube()
Definition: ecvFrustum.h:64
void initfromMPMatrix(const ccGLMatrixTpl< T > &MP)
Definition: ecvFrustum.h:165
Plane pl[6]
Definition: ecvFrustum.h:187
Frustum(const ccGLMatrixd &modelViewMat, const ccGLMatrixd &projMat)
Definition: ecvFrustum.h:89
Intersection
Definition: ecvFrustum.h:96
@ OUTSIDE
Definition: ecvFrustum.h:97
@ INSIDE
Definition: ecvFrustum.h:99
@ INTERSECT
Definition: ecvFrustum.h:98
Frustum()=default
Intersection boxInFrustum(const AABox &box) const
Definition: ecvFrustum.h:128
virtual ~Frustum()=default
Intersection boxInFrustum(const AACube &cube) const
Definition: ecvFrustum.h:141
Intersection pointInFrustum(const CCVector3f &p) const
Definition: ecvFrustum.h:103
Intersection sphereInFrustum(const CCVector3f &c, float r) const
Definition: ecvFrustum.h:113
virtual ~Plane()=default
CCVector3f normal
Definition: ecvFrustum.h:42
Plane()
Definition: ecvFrustum.h:18
float constCoef
Definition: ecvFrustum.h:43
float distance(const CCVector3f &p) const
Definition: ecvFrustum.h:37
void setCoefficients(float a, float b, float c, float d)
Definition: ecvFrustum.h:22
Type y
Definition: CVGeom.h:137
Type x
Definition: CVGeom.h:137
Type z
Definition: CVGeom.h:137
Type dot(const Vector3Tpl &v) const
Dot product.
Definition: CVGeom.h:408
Type norm() const
Returns vector norm.
Definition: CVGeom.h:424
A 4x4 'transformation' matrix (column major order)
T * data()
Returns a pointer to internal data.
Double version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:56
Simple axis aligned box structure.
Definition: RayAndBox.h:50
Vector3Tpl< float > corners[2]
Definition: RayAndBox.h:92