CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

frustum.h

Go to the documentation of this file.
00001 /*
00002   Copyright (C) 1998-2001 by Jorrit Tyberghein
00003 
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Library General Public
00006   License as published by the Free Software Foundation; either
00007   version 2 of the License, or (at your option) any later version.
00008 
00009   This library is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012   Library General Public License for more details.
00013 
00014   You should have received a copy of the GNU Library General Public
00015   License along with this library; if not, write to the Free
00016   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_FRUSTRUM_H__
00020 #define __CS_FRUSTRUM_H__
00021 
00028 #include "csextern.h"
00029 
00030 #include "cstypes.h"
00031 #include "csutil/ref.h"
00032 #include "csgeom/vector3.h"
00033 
00034 class csTransform;
00035 class csPlane3;
00036 
00042 
00043 #define CS_FRUST_OUTSIDE  0
00044 
00045 #define CS_FRUST_INSIDE   1
00046 
00047 #define CS_FRUST_COVERED  2
00048 
00049 #define CS_FRUST_PARTIAL  3
00050 
00058 struct CS_CSGEOM_EXPORT csClipInfo
00059 {
00060 # define CS_CLIPINFO_ORIGINAL 0
00061 # define CS_CLIPINFO_ONEDGE 1
00062 # define CS_CLIPINFO_INSIDE 2
00063   int type; // One of CS_CLIPINFO_???
00064   union
00065   {
00066     struct { int idx; } original;
00067     struct { int i1, i2; float r; } onedge;
00068     struct { csClipInfo* ci1, * ci2; float r; } inside;
00069   };
00070 
00071   csClipInfo () : type (CS_CLIPINFO_ORIGINAL) { }
00072   void Clear ();
00073   ~csClipInfo () { Clear (); }
00074 
00076   void Copy (csClipInfo& other)
00077   {
00078     if (&other == this) return;
00079     Clear ();
00080     type = other.type;
00081     if (type == CS_CLIPINFO_INSIDE)
00082     {
00083       inside.r = other.inside.r;
00084       inside.ci1 = new csClipInfo ();
00085       inside.ci1->Copy (*other.inside.ci1);
00086       inside.ci2 = new csClipInfo ();
00087       inside.ci2->Copy (*other.inside.ci2);
00088     }
00089     else if (type == CS_CLIPINFO_ORIGINAL)
00090       original.idx = other.original.idx;
00091     else
00092       onedge = other.onedge;
00093   }
00094 
00096   void Move (csClipInfo& other)
00097   {
00098     if (&other == this) return;
00099     Clear ();
00100     type = other.type;
00101     if (type == CS_CLIPINFO_INSIDE)
00102       inside = other.inside;
00103     else if (type == CS_CLIPINFO_ORIGINAL)
00104       original.idx = other.original.idx;
00105     else
00106       onedge = other.onedge;
00107     other.type = CS_CLIPINFO_ORIGINAL;
00108   }
00109 
00110   void Dump (int indent)
00111   {
00112     char ind[255];
00113     int i;
00114     for (i = 0 ; i < indent ; i++) ind[i] = ' ';
00115     ind[i] = 0;
00116     switch (type)
00117     {
00118       case CS_CLIPINFO_ORIGINAL:
00119         printf ("%s ORIGINAL idx=%d\n", ind, original.idx);
00120         break;
00121       case CS_CLIPINFO_ONEDGE:
00122         printf ("%s ONEDGE i1=%d i2=%d r=%g\n", ind, onedge.i1, onedge.i2,
00123           onedge.r);
00124         break;
00125       case CS_CLIPINFO_INSIDE:
00126         printf ("%s INSIDE r=%g\n", ind, inside.r);
00127         inside.ci1->Dump (indent+2);
00128         inside.ci2->Dump (indent+2);
00129         break;
00130     }
00131     fflush (stdout);
00132   }
00133 };
00134 
00145 class CS_CSGEOM_EXPORT csFrustum
00146 {
00147 private:
00149   csVector3 origin;
00150 
00156   csVector3* vertices;
00158   int num_vertices;
00160   int max_vertices;
00161 
00163   csPlane3* backplane;
00164 
00172   bool wide;
00173 
00178   bool mirrored;
00179 
00181   int ref_count;
00182 
00184   void Clear ();
00185 
00187   void ExtendVertexArray (int num);
00188 
00189 public:
00190 
00192   csFrustum (const csVector3& o) :
00193     origin (o), vertices (0), num_vertices (0), max_vertices (0),
00194     backplane (0), wide (false), mirrored (false), ref_count (1)
00195   { }
00196 
00202   csFrustum (const csVector3& o, csVector3* verts, int num_verts,
00203         csPlane3* backp = 0);
00204 
00210   csFrustum (const csVector3& o, int num_verts,
00211         csPlane3* backp = 0);
00212 
00214   csFrustum (const csFrustum &copy);
00215 
00217   virtual ~csFrustum ();
00218 
00220   void SetOrigin (const csVector3& o) { origin = o; }
00221 
00223   csVector3& GetOrigin () { return origin; }
00224 
00226   const csVector3& GetOrigin () const { return origin; }
00227 
00233   void SetMirrored (bool m) { mirrored = m; }
00234 
00236   bool IsMirrored () const { return mirrored; }
00237 
00244   void SetBackPlane (csPlane3& plane);
00245 
00249   csPlane3* GetBackPlane () { return backplane; }
00250 
00254   void RemoveBackPlane ();
00255 
00259   void AddVertex (const csVector3& v);
00260 
00264   int GetVertexCount () { return num_vertices; }
00265 
00269   csVector3& GetVertex (int idx)
00270   {
00271     CS_ASSERT (idx >= 0 && idx < num_vertices);
00272     return vertices[idx];
00273   }
00274 
00278   csVector3* GetVertices () { return vertices; }
00279 
00283   void Transform (csTransform* trans);
00284 
00290   void ClipToPlane (csVector3& v1, csVector3& v2);
00291 
00300   static void ClipToPlane (csVector3* vertices, int& num_vertices,
00301     csClipInfo* clipinfo, const csVector3& v1, const csVector3& v2);
00302 
00311   static void ClipToPlane (csVector3* vertices, int& num_vertices,
00312     csClipInfo* clipinfo, const csPlane3& plane);
00313 
00320   void ClipPolyToPlane (csPlane3* plane);
00321 
00330   csPtr<csFrustum> Intersect (const csFrustum& other) const;
00331 
00346   csPtr<csFrustum> Intersect (csVector3* poly, int num) const;
00347 
00362   static csPtr<csFrustum> Intersect (
00363     const csVector3& frust_origin, csVector3* frust, int num_frust,
00364     csVector3* poly, int num);
00365 
00380   static csPtr<csFrustum> Intersect (
00381     const csVector3& frust_origin, csVector3* frust, int num_frust,
00382     const csVector3& v1, const csVector3& v2, const csVector3& v3);
00383 
00389   static int Classify (csVector3* frustum, int num_frust,
00390     csVector3* poly, int num_poly);
00391 
00398   static int BatchClassify (csVector3* frustum, csVector3* frustumNormals,
00399         int num_frust, csVector3* poly, int num_poly);
00400 
00405   bool Contains (const csVector3& point);
00406 
00413   static bool Contains (csVector3* frustum, int num_frust,
00414     const csVector3& point);
00415 
00421   static bool Contains (csVector3* frustum, int num_frust,
00422     const csPlane3& plane, const csVector3& point);
00423 
00425   bool IsEmpty () const { return !wide && vertices == 0; }
00426 
00428   bool IsInfinite () const { return wide && vertices == 0 && backplane == 0; }
00429 
00434   bool IsWide () const { return wide && vertices == 0; }
00435 
00440   void MakeInfinite ();
00441 
00445   void MakeEmpty ();
00446 
00448   void IncRef () { ref_count++; }
00450   void DecRef () { if (ref_count == 1) delete this; else ref_count--; }
00452   int GetRefCount () { return ref_count; }
00453 };
00454 
00457 #endif // __CS_FRUSTRUM_H__

Generated for Crystal Space by doxygen 1.3.9.1