Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
math_utils.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlCore/math_utils.hpp>
33 #include <vlCore/AABB.hpp>
34 #include <vlCore/Vector2.hpp>
35 #include <vlCore/Vector4.hpp>
36 #include <vlCore/Plane.hpp>
37 #include <vlCore/checks.hpp>
39 #include <cstdlib>
40 
41 using namespace vl;
42 
43 //-----------------------------------------------------------------------------
44 real vl::random(real min, real max)
45 {
46  real t = (real)defMersenneTwister()->rand53();
47  return min + (max-min)*t;
48 }
49 //-----------------------------------------------------------------------------
51 {
52  return defMersenneTwister()->randInt(max-min) + min;
53 }
54 //-----------------------------------------------------------------------------
56 {
57  return defMersenneTwister()->randInt(max-min) + min;
58 }
59 //-----------------------------------------------------------------------------
61 {
62  int pow2=2;
63  for(int i=0; i<20; ++i) {
64  if (pow2 >= n)
65  return pow2;
66  pow2 = pow2 * 2;
67  }
68  return pow2;
69 }
70 //-----------------------------------------------------------------------------
72 {
73  int pow2=2;
74  for(int i=0; i<20; ++i) {
75  if (pow2 > n)
76  return pow2/2;
77  pow2 = pow2 * 2;
78  }
79  return pow2;
80 }
81 //-----------------------------------------------------------------------------
82 void vl::extractPlanes( Plane* planes, const mat4& modelviewproj )
83 {
84  // see also http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
85  // see also http://zach.in.tu-clausthal.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
86  // the equation is a*x+b*y+x*z+d = 0
87  // where <a b c> is the normal of the plane
88  // the normals are pointing inside the viewfrustum
89 
90  // 1) we want the planes to point outward so we reverse them
91  // 2) because of 1) "d" becomes the distance of the plane from the origin
92 
93  vec3 n;
94  real d;
95 
96  // left clipping plane
97  n.x() = modelviewproj.e(3,0) + modelviewproj.e(0,0);
98  n.y() = modelviewproj.e(3,1) + modelviewproj.e(0,1);
99  n.z() = modelviewproj.e(3,2) + modelviewproj.e(0,2);
100  d = modelviewproj.e(3,3) + modelviewproj.e(0,3);
101  d /= n.length();
102  n.normalize();
103  planes[0] = Plane(d,-n);
104 
105  // right clipping plane
106  n.x() = modelviewproj.e(3,0) - modelviewproj.e(0,0);
107  n.y() = modelviewproj.e(3,1) - modelviewproj.e(0,1);
108  n.z() = modelviewproj.e(3,2) - modelviewproj.e(0,2);
109  d = modelviewproj.e(3,3) - modelviewproj.e(0,3);
110  d /= n.length();
111  n.normalize();
112  planes[1] = Plane(d,-n);
113 
114  // top clipping plane
115  n.x() = modelviewproj.e(3,0) - modelviewproj.e(1,0);
116  n.y() = modelviewproj.e(3,1) - modelviewproj.e(1,1);
117  n.z() = modelviewproj.e(3,2) - modelviewproj.e(1,2);
118  d = modelviewproj.e(3,3) - modelviewproj.e(1,3);
119  d /= n.length();
120  n.normalize();
121  planes[2] = Plane(d,-n);
122 
123  // bottom clipping plane
124  n.x() = modelviewproj.e(3,0) + modelviewproj.e(1,0);
125  n.y() = modelviewproj.e(3,1) + modelviewproj.e(1,1);
126  n.z() = modelviewproj.e(3,2) + modelviewproj.e(1,2);
127  d = modelviewproj.e(3,3) + modelviewproj.e(1,3);
128  d /= n.length();
129  n.normalize();
130  planes[3] = Plane(d,-n);
131 
132  // near clipping plane
133  n.x() = modelviewproj.e(3,0) + modelviewproj.e(2,0);
134  n.y() = modelviewproj.e(3,1) + modelviewproj.e(2,1);
135  n.z() = modelviewproj.e(3,2) + modelviewproj.e(2,2);
136  d = modelviewproj.e(3,3) + modelviewproj.e(2,3);
137  d /= n.length();
138  n.normalize();
139  planes[4] = Plane(d,-n);
140 
141  // far clipping plane
142  n.x() = modelviewproj.e(3,0) - modelviewproj.e(2,0);
143  n.y() = modelviewproj.e(3,1) - modelviewproj.e(2,1);
144  n.z() = modelviewproj.e(3,2) - modelviewproj.e(2,2);
145  d = modelviewproj.e(3,3) - modelviewproj.e(2,3);
146  d /= n.length();
147  n.normalize();
148  planes[5] = Plane(d,-n);
149 }
VLCORE_EXPORT i32 randomI32(i32 min, i32 max)
Returns a random number N between &#39;min&#39; and &#39;max&#39; (included) generated using MersenneTwister->randInt...
Definition: math_utils.cpp:55
VLCORE_EXPORT void extractPlanes(Plane *planes, const mat4 &modelviewproj)
Extracts the 6 frustum planes for the given model-view-projection matrix.
Definition: math_utils.cpp:82
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:228
const T_Scalar & e(int i, int j) const
Definition: Matrix4.hpp:665
The Plane class defines a plane using a normal and an origin.
Definition: Plane.hpp:49
const T_Scalar & z() const
Definition: Vector3.hpp:92
int i32
32 bits signed integer
Definition: std_types.hpp:49
VLCORE_EXPORT real random(real min, real max)
Returns a random number N between &#39;min&#39; and &#39;max&#39; (included) with 53 bits of randomness generated usi...
Definition: math_utils.cpp:44
VLCORE_EXPORT u32 randomU32(u32 min, u32 max)
Returns a random number N between &#39;min&#39; and &#39;max&#39; (included) generated using MersenneTwister->randInt...
Definition: math_utils.cpp:50
Visualization Library main namespace.
float max(float a, float b)
Definition: Vector2.hpp:312
float min(float a, float b)
Definition: Vector2.hpp:308
unsigned int u32
32 bits unsigned integer
Definition: std_types.hpp:51
T_Scalar length() const
Definition: Vector3.hpp:225
const T_Scalar & y() const
Definition: Vector3.hpp:91
VLCORE_EXPORT int greaterEqualPow2(int n)
Returns a number N that is a power of 2 and that is equal to or greater than &#39;n&#39;. ...
Definition: math_utils.cpp:60
const T_Scalar & x() const
Definition: Vector3.hpp:90
VLCORE_EXPORT int smallerEqualPow2(int n)
Returns a number N that is a power of 2 and that is equal to or smaller than &#39;n&#39;. ...
Definition: math_utils.cpp:71
VLCORE_EXPORT MersenneTwister * defMersenneTwister()
Definition: pimpl.cpp:112