Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/math_utils.cpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #include <vlCore/math_utils.hpp>
00033 #include <vlCore/AABB.hpp>
00034 #include <vlCore/Vector2.hpp>
00035 #include <vlCore/Vector4.hpp>
00036 #include <vlCore/Plane.hpp>
00037 #include <vlCore/checks.hpp>
00038 #include <vlCore/MersenneTwister.hpp>
00039 #include <cstdlib>
00040 
00041 using namespace vl;
00042 
00043 //-----------------------------------------------------------------------------
00044 real vl::random(real min, real max)
00045 {
00046   real t = (real)defMersenneTwister()->rand53();
00047   return min + (max-min)*t;
00048 }
00049 //-----------------------------------------------------------------------------
00050 u32 vl::randomU32(u32 min, u32 max)
00051 {
00052   return defMersenneTwister()->randInt(max-min) + min;
00053 }
00054 //-----------------------------------------------------------------------------
00055 i32 vl::randomI32(i32 min, i32 max)
00056 {
00057   return defMersenneTwister()->randInt(max-min) + min;
00058 }
00059 //-----------------------------------------------------------------------------
00060 int vl::greaterEqualPow2(int n)
00061 {
00062   int pow2=2;
00063   for(int i=0; i<20; ++i) {
00064     if (pow2 >= n)
00065       return pow2;
00066     pow2 = pow2 * 2;
00067   }
00068   return pow2;
00069 }
00070 //-----------------------------------------------------------------------------
00071 int vl::smallerEqualPow2(int n)
00072 {
00073   int pow2=2;
00074   for(int i=0; i<20; ++i) {
00075     if (pow2 > n)
00076       return pow2/2;
00077     pow2 = pow2 * 2;
00078   }
00079   return pow2;
00080 }
00081 //-----------------------------------------------------------------------------
00082 void vl::extractPlanes( Plane* planes, const mat4& modelviewproj )
00083 {
00084   // see also http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf
00085   // see also http://zach.in.tu-clausthal.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
00086   // the equation is a*x+b*y+x*z+d = 0
00087   // where <a b c> is the normal of the plane
00088   // the normals are pointing inside the viewfrustum
00089 
00090   // 1) we want the planes to point outward so we reverse them
00091   // 2) because of 1) "d" becomes the distance of the plane from the origin
00092 
00093   vec3 n;
00094   real d;
00095 
00096   // left clipping plane
00097   n.x() = modelviewproj.e(3,0) + modelviewproj.e(0,0);
00098   n.y() = modelviewproj.e(3,1) + modelviewproj.e(0,1);
00099   n.z() = modelviewproj.e(3,2) + modelviewproj.e(0,2);
00100   d = modelviewproj.e(3,3) + modelviewproj.e(0,3);
00101   d /= n.length();
00102   n.normalize();
00103   planes[0] = Plane(d,-n);
00104 
00105   // right clipping plane
00106   n.x() = modelviewproj.e(3,0) - modelviewproj.e(0,0);
00107   n.y() = modelviewproj.e(3,1) - modelviewproj.e(0,1);
00108   n.z() = modelviewproj.e(3,2) - modelviewproj.e(0,2);
00109   d = modelviewproj.e(3,3) - modelviewproj.e(0,3);
00110   d /= n.length();
00111   n.normalize();
00112   planes[1] = Plane(d,-n);
00113 
00114   // top clipping plane
00115   n.x() = modelviewproj.e(3,0) - modelviewproj.e(1,0);
00116   n.y() = modelviewproj.e(3,1) - modelviewproj.e(1,1);
00117   n.z() = modelviewproj.e(3,2) - modelviewproj.e(1,2);
00118   d = modelviewproj.e(3,3) - modelviewproj.e(1,3);
00119   d /= n.length();
00120   n.normalize();
00121   planes[2] = Plane(d,-n);
00122 
00123   // bottom clipping plane
00124   n.x() = modelviewproj.e(3,0) + modelviewproj.e(1,0);
00125   n.y() = modelviewproj.e(3,1) + modelviewproj.e(1,1);
00126   n.z() = modelviewproj.e(3,2) + modelviewproj.e(1,2);
00127   d = modelviewproj.e(3,3) + modelviewproj.e(1,3);
00128   d /= n.length();
00129   n.normalize();
00130   planes[3] = Plane(d,-n);
00131 
00132   // near clipping plane
00133   n.x() = modelviewproj.e(3,0) + modelviewproj.e(2,0);
00134   n.y() = modelviewproj.e(3,1) + modelviewproj.e(2,1);
00135   n.z() = modelviewproj.e(3,2) + modelviewproj.e(2,2);
00136   d = modelviewproj.e(3,3) + modelviewproj.e(2,3);
00137   d /= n.length();
00138   n.normalize();
00139   planes[4] = Plane(d,-n);
00140 
00141   // far clipping plane
00142   n.x() = modelviewproj.e(3,0) - modelviewproj.e(2,0);
00143   n.y() = modelviewproj.e(3,1) - modelviewproj.e(2,1);
00144   n.z() = modelviewproj.e(3,2) - modelviewproj.e(2,2);
00145   d = modelviewproj.e(3,3) - modelviewproj.e(2,3);
00146   d /= n.length();
00147   n.normalize();
00148   planes[5] = Plane(d,-n);
00149 }

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:29.
Permission is granted to use this page to write and publish articles regarding Visualization Library.