CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | 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 "csgeom/vector3.h"
00032 
00033 class csPlane3;
00034 template<class T>
00035 class csPtr;
00036 class csTransform;
00037 
00043 enum
00044 {
00046   CS_FRUST_OUTSIDE = 0,
00048   CS_FRUST_INSIDE = 1,
00050   CS_FRUST_COVERED = 2,
00052   CS_FRUST_PARTIAL = 3
00053 };
00056 enum csClipType
00057 {
00058   CS_CLIPINFO_ORIGINAL = 0,
00059   CS_CLIPINFO_ONEDGE = 1,
00060   CS_CLIPINFO_INSIDE = 2
00061 };
00062 
00069 struct CS_CRYSTALSPACE_EXPORT csClipInfo
00070 {
00071 
00072   csClipType type; // One of CS_CLIPINFO_???
00073   union
00074   {
00075     struct { int idx; } original;
00076     struct { int i1, i2; float r; } onedge;
00077     struct { csClipInfo* ci1, * ci2; float r; } inside;
00078   };
00079 
00080   csClipInfo () : type (CS_CLIPINFO_ORIGINAL) { }
00081   void Clear ();
00082   ~csClipInfo () { Clear (); }
00083 
00085   inline void Copy (csClipInfo& other)
00086   {
00087     if (&other == this) return;
00088     Clear ();
00089     type = other.type;
00090     if (type == CS_CLIPINFO_INSIDE)
00091     {
00092       inside.r = other.inside.r;
00093       inside.ci1 = new csClipInfo ();
00094       inside.ci1->Copy (*other.inside.ci1);
00095       inside.ci2 = new csClipInfo ();
00096       inside.ci2->Copy (*other.inside.ci2);
00097     }
00098     else if (type == CS_CLIPINFO_ORIGINAL)
00099       original.idx = other.original.idx;
00100     else
00101       onedge = other.onedge;
00102   }
00103 
00105   inline void Move (csClipInfo& other)
00106   {
00107     if (&other == this) return;
00108     Clear ();
00109     type = other.type;
00110     if (type == CS_CLIPINFO_INSIDE)
00111       inside = other.inside;
00112     else if (type == CS_CLIPINFO_ORIGINAL)
00113       original.idx = other.original.idx;
00114     else
00115       onedge = other.onedge;
00116     other.type = CS_CLIPINFO_ORIGINAL;
00117   }
00118 
00119   inline void Dump (int indent)
00120   {
00121     char ind[255];
00122     int i;
00123     for (i = 0 ; i < indent ; i++) ind[i] = ' ';
00124     ind[i] = 0;
00125     switch (type)
00126     {
00127       case CS_CLIPINFO_ORIGINAL:
00128         printf ("%s ORIGINAL idx=%d\n", ind, original.idx);
00129         break;
00130       case CS_CLIPINFO_ONEDGE:
00131         printf ("%s ONEDGE i1=%d i2=%d r=%g\n", ind, onedge.i1, onedge.i2,
00132           onedge.r);
00133         break;
00134       case CS_CLIPINFO_INSIDE:
00135         printf ("%s INSIDE r=%g\n", ind, inside.r);
00136         inside.ci1->Dump (indent+2);
00137         inside.ci2->Dump (indent+2);
00138         break;
00139     }
00140     fflush (stdout);
00141   }
00142 };
00143 
00154 class CS_CRYSTALSPACE_EXPORT csFrustum
00155 {
00156 private:
00158   csVector3 origin;
00159 
00165   csVector3* vertices;
00167   int num_vertices;
00169   int max_vertices;
00170 
00172   csPlane3* backplane;
00173 
00181   bool wide;
00182 
00187   bool mirrored;
00188 
00190   int ref_count;
00191 
00193   void Clear ();
00194 
00196   void ExtendVertexArray (int num);
00197 
00198 public:
00199 
00201   csFrustum (const csVector3& o) :
00202     origin (o), vertices (0), num_vertices (0), max_vertices (0),
00203     backplane (0), wide (false), mirrored (false), ref_count (1)
00204   { }
00205 
00211   csFrustum (const csVector3& o, csVector3* verts, int num_verts,
00212         csPlane3* backp = 0);
00213 
00219   csFrustum (const csVector3& o, int num_verts,
00220         csPlane3* backp = 0);
00221 
00223   csFrustum (const csFrustum &copy);
00224 
00226   virtual ~csFrustum ();
00227 
00229   inline void SetOrigin (const csVector3& o) { origin = o; }
00230 
00232   inline csVector3& GetOrigin () { return origin; }
00233 
00235   inline const csVector3& GetOrigin () const { return origin; }
00236 
00242   inline void SetMirrored (bool m) { mirrored = m; }
00243 
00245   inline bool IsMirrored () const { return mirrored; }
00246 
00253   void SetBackPlane (csPlane3& plane);
00254 
00258   inline csPlane3* GetBackPlane () { return backplane; }
00259 
00263   void RemoveBackPlane ();
00264 
00268   void AddVertex (const csVector3& v);
00269 
00273   inline int GetVertexCount () { return num_vertices; }
00274 
00278   inline csVector3& GetVertex (int idx)
00279   {
00280     CS_ASSERT (idx >= 0 && idx < num_vertices);
00281     return vertices[idx];
00282   }
00283 
00287   inline csVector3* GetVertices () { return vertices; }
00288 
00292   void Transform (csTransform* trans);
00293 
00299   void ClipToPlane (csVector3& v1, csVector3& v2);
00300 
00309   static void ClipToPlane (csVector3* vertices, int& num_vertices,
00310     csClipInfo* clipinfo, const csVector3& v1, const csVector3& v2);
00311 
00320   static void ClipToPlane (csVector3* vertices, int& num_vertices,
00321     csClipInfo* clipinfo, const csPlane3& plane);
00322 
00329   void ClipPolyToPlane (csPlane3* plane);
00330 
00339   csPtr<csFrustum> Intersect (const csFrustum& other) const;
00340 
00355   csPtr<csFrustum> Intersect (csVector3* poly, int num) const;
00356 
00371   static csPtr<csFrustum> Intersect (
00372     const csVector3& frust_origin, csVector3* frust, int num_frust,
00373     csVector3* poly, int num);
00374 
00389   static csPtr<csFrustum> Intersect (
00390     const csVector3& frust_origin, csVector3* frust, int num_frust,
00391     const csVector3& v1, const csVector3& v2, const csVector3& v3);
00392 
00398   static int Classify (csVector3* frustum, int num_frust,
00399     csVector3* poly, int num_poly);
00400 
00407   static int BatchClassify (csVector3* frustum, csVector3* frustumNormals,
00408         int num_frust, csVector3* poly, int num_poly);
00409 
00414   bool Contains (const csVector3& point);
00415 
00422   static bool Contains (csVector3* frustum, int num_frust,
00423     const csVector3& point);
00424 
00430   static bool Contains (csVector3* frustum, int num_frust,
00431     const csPlane3& plane, const csVector3& point);
00432 
00434   inline bool IsEmpty () const { return !wide && vertices == 0; }
00435 
00437   inline bool IsInfinite () const { return wide && vertices == 0 && backplane == 0; }
00438 
00443   inline bool IsWide () const { return wide && vertices == 0; }
00444 
00449   void MakeInfinite ();
00450 
00454   void MakeEmpty ();
00455 
00457   inline void IncRef () { ref_count++; }
00459   inline void DecRef () { if (ref_count == 1) delete this; else ref_count--; }
00461   inline int GetRefCount () { return ref_count; }
00462 };
00463 
00466 #endif // __CS_FRUSTRUM_H__

Generated for Crystal Space by doxygen 1.4.4