Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

sglPolytope.hpp

00001 /*****************************************************************************
00002  * SGL: A Scene Graph Library
00003  *
00004  * Copyright (C) 1997-2001  Scott McMillan   All Rights Reserved.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; if not, write to the Free
00018  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  *****************************************************************************
00020  *     File: sglPolytope.hpp
00021  *  Created: 2 January 1999
00022  *  Summary: A polytope is a convex 3D object delimited by plane equations.
00023  *           The inside of the polytope is given by the positive side of the
00024  *           plane equation by convention
00025  *****************************************************************************/
00026 
00027 #ifndef __SGL_POLYTOPE_HPP
00028 #define __SGL_POLYTOPE_HPP
00029 
00030 #include <sgl.h>
00031 #include <sglPlane.hpp>
00032 
00033 #define SGL_MAX_PLANE_PER_POLYTOPE 6
00034 
00035 // ---------------------------------------------------------------------------
00036 // A polytope is a convex 3D object delimited by plane equations.  The inside
00037 // of the polytope is given by the positive side of the plane equation by
00038 // convention
00039 // ---------------------------------------------------------------------------
00040 
00041 //----------------------------------------------------------------------------
00042 // convenience types:
00043 //    template class 'should' only be float or double
00044 
00045 template <class T> class sglPolytope;
00046 
00047 typedef sglPolytope<float>  sglPolytopef;
00048 typedef sglPolytope<double> sglPolytoped;
00049 
00050 //----------------------------------------------------------------------------
00051 
00052 template <class T>
00053 class sglPolytope
00054 {
00055 public:
00056    sglPolytope() : m_num_planes(0) {};
00057 
00058    template <class S>
00059    sglPolytope(const sglPolytope<S> &rhs)
00060       {
00061          m_num_planes = rhs.getNumPlanes();
00062          for (unsigned int i=0; i<m_num_planes; i++)
00063             m_plane[i] = rhs.getPlane(i);
00064       }
00065 
00066    virtual ~sglPolytope() {};  // should this clean up the list?
00067 
00068    template <class S>
00069    sglPolytope &operator=(const sglPolytope<S> &rhs)
00070       {
00071          if (this != (sglPolytope<T>*)&rhs)
00072          {
00073             m_num_planes = rhs.getNumPlanes();
00074             for (unsigned int i=0; i<m_num_planes; i++)
00075                m_plane[i] = rhs.getPlane(i);
00076          }
00077          return *this;
00078       }
00079 
00080    inline unsigned int getNumPlanes(void) const
00081       {
00082          return m_num_planes;
00083       }
00084    inline const sglPlane<T> &getPlane(unsigned int i) const
00085       {
00086          return m_plane[i];
00087       }
00088    void addPlane(sglPlane<T> &plane)
00089       {
00090          if (m_num_planes < SGL_MAX_PLANE_PER_POLYTOPE)
00091             m_plane[m_num_planes++] = plane;
00092       }
00093 
00094    // apply a 3x3 transform
00095    template <class S>
00096    inline void mul(const sglPolytope<T>& p, const sglMat3<S>& m)
00097       {
00098          for (unsigned int i=0; i<p.getNumPlanes(); i++)
00099             m_plane[i].mul(p.getPlane(i),m);
00100 
00101          m_num_planes = p.getNumPlanes();
00102       }
00103 
00104    // apply a 4x4 transform
00105    template <class S>
00106    inline void mul(const sglPolytope<T>& p, const sglMat4<S>& m)
00107       {
00108          for (unsigned int i=0; i< p.getNumPlanes(); i++)
00109             m_plane[i].mul(p.getPlane(i),m);
00110 
00111          m_num_planes = p.getNumPlanes();
00112       }
00113 
00114    // apply a translation
00115    inline void translate(const sglPolytope<T>& p, T x, T y, T z)
00116       {
00117          for (unsigned int i=0; i<p.getNumPlanes(); i++)
00118             m_plane[i].translate(p.getPlane(i), x, y, z);
00119 
00120          m_num_planes = p.getNumPlanes();
00121       }
00122 
00123    // apply a scale
00124    inline void scale(const sglPolytope<T>& p, T scale)
00125       {
00126          for (unsigned int i=0; i<p.getNumPlanes(); i++)
00127             m_plane[i].scale(p.getPlane(i), scale);
00128 
00129          m_num_planes = p.getNumPlanes();
00130       }
00131 
00132    // apply a rotation
00133    template <class S>
00134    inline void rotate(const sglPolytope<T>& p, const sglMat4<S>& m)
00135       {
00136          for (unsigned int i=0; i< p.getNumPlanes(); i++)
00137             m_plane[i].rotate(p.getPlane(i),m);
00138 
00139          m_num_planes = p.getNumPlanes();
00140       }
00141    template <class S>
00142    inline void rotate(const sglPolytope<T>& p, const sglMat3<S>& m)
00143       {
00144          for (unsigned int i=0; i< p.getNumPlanes(); i++)
00145             m_plane[i].rotate(p.getPlane(i),m);
00146 
00147          m_num_planes = p.getNumPlanes();
00148       }
00149 
00150    friend ostream& operator<<(ostream& ostrm, const sglPolytope<T>& p)
00151       {
00152          ostrm << "[" << p.getNumPlanes() << " planes, ";
00153 
00154          for (unsigned int i=0; i<p.getNumPlanes(); ++i)
00155          {
00156             ostrm << " plane " << i << ": " << p.getPlane(i) << ",";
00157          }
00158          return ostrm;
00159       }
00160 
00161 protected:
00162    // replace this with a vector of sglPlanes
00163    unsigned int m_num_planes;
00164    sglPlane<T> m_plane[SGL_MAX_PLANE_PER_POLYTOPE];
00165 };
00166 
00167 #endif

Generated at Mon Jul 1 18:00:05 2002 for SGL by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001