## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgeom/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
    00029 #include "csextern.h"
    00030
    00031 #include "cstypes.h"
    00032 #include "csgeom/vector3.h"
    00033
    00034 class csPlane3;
    00035 template<class T>
    00036 class csPtr;
    00037 class csTransform;
    00038
    00044 enum
    00045 {
    00047   CS_FRUST_OUTSIDE = 0,
    00049   CS_FRUST_INSIDE = 1,
    00051   CS_FRUST_COVERED = 2,
    00053   CS_FRUST_PARTIAL = 3
    00054 };
    00057 enum csClipType
    00058 {
    00059   CS_CLIPINFO_ORIGINAL = 0,
    00060   CS_CLIPINFO_ONEDGE = 1,
    00061   CS_CLIPINFO_INSIDE = 2
    00062 };
    00063
    00070 struct CS_CRYSTALSPACE_EXPORT csClipInfo
    00071 {
    00072
    00073   csClipType type; // One of CS_CLIPINFO_???
    00074   union
    00075   {
    00076     struct { int idx; } original;
    00077     struct { int i1, i2; float r; } onedge;
    00078     struct { csClipInfo* ci1, * ci2; float r; } inside;
    00079   };
    00080
    00081   csClipInfo () : type (CS_CLIPINFO_ORIGINAL) { }
    00082   void Clear ();
    00083   ~csClipInfo () { Clear (); }
    00084
    00086   inline void Copy (csClipInfo& other)
    00087   {
    00088     if (&other == this) return;
    00089     Clear ();
    00090     type = other.type;
    00091     if (type == CS_CLIPINFO_INSIDE)
    00092     {
    00093       inside.r = other.inside.r;
    00094       inside.ci1 = new csClipInfo ();
    00095       inside.ci1->Copy (*other.inside.ci1);
    00096       inside.ci2 = new csClipInfo ();
    00097       inside.ci2->Copy (*other.inside.ci2);
    00098     }
    00099     else if (type == CS_CLIPINFO_ORIGINAL)
    00100       original.idx = other.original.idx;
    00101     else
    00102       onedge = other.onedge;
    00103   }
    00104
    00106   inline void Move (csClipInfo& other)
    00107   {
    00108     if (&other == this) return;
    00109     Clear ();
    00110     type = other.type;
    00111     if (type == CS_CLIPINFO_INSIDE)
    00112       inside = other.inside;
    00113     else if (type == CS_CLIPINFO_ORIGINAL)
    00114       original.idx = other.original.idx;
    00115     else
    00116       onedge = other.onedge;
    00117     other.type = CS_CLIPINFO_ORIGINAL;
    00118   }
    00119
    00120   inline void Dump (int indent)
    00121   {
    00122     char ind[255];
    00123     int i;
    00124     for (i = 0 ; i < indent ; i++) ind[i] = ' ';
    00125     ind[i] = 0;
    00126     switch (type)
    00127     {
    00128       case CS_CLIPINFO_ORIGINAL:
    00129         printf ("%s ORIGINAL idx=%d\n", ind, original.idx);
    00130         break;
    00131       case CS_CLIPINFO_ONEDGE:
    00132         printf ("%s ONEDGE i1=%d i2=%d r=%g\n", ind, onedge.i1, onedge.i2,
    00133           onedge.r);
    00134         break;
    00135       case CS_CLIPINFO_INSIDE:
    00136         printf ("%s INSIDE r=%g\n", ind, inside.r);
    00137         inside.ci1->Dump (indent+2);
    00138         inside.ci2->Dump (indent+2);
    00139         break;
    00140     }
    00141     fflush (stdout);
    00142   }
    00143 };
    00144
    00155 class CS_CRYSTALSPACE_EXPORT csFrustum
    00156 {
    00157 private:
    00159   csVector3 origin;
    00160
    00166   csVector3* vertices;
    00168   int num_vertices;
    00170   int max_vertices;
    00171
    00173   csPlane3* backplane;
    00174
    00182   bool wide;
    00183
    00188   bool mirrored;
    00189
    00191   int ref_count;
    00192
    00194   void Clear ();
    00195
    00197   void ExtendVertexArray (int num);
    00198
    00199 public:
    00200
    00202   csFrustum (const csVector3& o) :
    00203     origin (o), vertices (0), num_vertices (0), max_vertices (0),
    00204     backplane (0), wide (false), mirrored (false), ref_count (1)
    00205   { }
    00206
    00212   csFrustum (const csVector3& o, csVector3* verts, int num_verts,
    00213         csPlane3* backp = 0);
    00214
    00220   csFrustum (const csVector3& o, int num_verts,
    00221         csPlane3* backp = 0);
    00222
    00224   csFrustum (const csFrustum &copy);
    00225
    00227   virtual ~csFrustum ();
    00228
    00230   inline void SetOrigin (const csVector3& o) { origin = o; }
    00231
    00233   inline csVector3& GetOrigin () { return origin; }
    00234
    00236   inline const csVector3& GetOrigin () const { return origin; }
    00237
    00243   inline void SetMirrored (bool m) { mirrored = m; }
    00244
    00246   inline bool IsMirrored () const { return mirrored; }
    00247
    00254   void SetBackPlane (const csPlane3& plane);
    00255
    00259   inline csPlane3* GetBackPlane () const { return backplane; }
    00260
    00264   void RemoveBackPlane ();
    00265
    00269   void AddVertex (const csVector3& v);
    00270
    00274   inline int GetVertexCount () const { return num_vertices; }
    00275
    00279   inline csVector3& GetVertex (int idx)
    00280   {
    00281     CS_ASSERT (idx >= 0 && idx < num_vertices);
    00282     return vertices[idx];
    00283   }
    00284
    00288   inline const csVector3& GetVertex (int idx) const
    00289   {
    00290     CS_ASSERT (idx >= 0 && idx < num_vertices);
    00291     return vertices[idx];
    00292   }
    00293
    00297   inline csVector3* GetVertices () const { return vertices; }
    00298
    00302   void Transform (csTransform* trans);
    00303
    00309   void ClipToPlane (csVector3& v1, csVector3& v2);
    00310
    00319   static void ClipToPlane (csVector3* vertices, int& num_vertices,
    00320     csClipInfo* clipinfo, const csVector3& v1, const csVector3& v2);
    00321
    00330   static void ClipToPlane (csVector3* vertices, int& num_vertices,
    00331     csClipInfo* clipinfo, const csPlane3& plane);
    00332
    00339   void ClipPolyToPlane (csPlane3* plane);
    00340
    00346   csPtr<csFrustum> Intersect (const csFrustum& other) const;
    00347
    00362   csPtr<csFrustum> Intersect (csVector3* poly, int num) const;
    00363
    00378   static csPtr<csFrustum> Intersect (
    00379     const csVector3& frust_origin, csVector3* frust, int num_frust,
    00380     csVector3* poly, int num);
    00381
    00396   static csPtr<csFrustum> Intersect (
    00397     const csVector3& frust_origin, csVector3* frust, int num_frust,
    00398     const csVector3& v1, const csVector3& v2, const csVector3& v3);
    00399
    00405   static int Classify (csVector3* frustum, int num_frust,
    00406     csVector3* poly, int num_poly);
    00407
    00414   static int BatchClassify (csVector3* frustum, csVector3* frustumNormals,
    00415         int num_frust, csVector3* poly, int num_poly);
    00416
    00421   bool Contains (const csVector3& point);
    00422
    00429   static bool Contains (csVector3* frustum, int num_frust,
    00430     const csVector3& point);
    00431
    00437   static bool Contains (csVector3* frustum, int num_frust,
    00438     const csPlane3& plane, const csVector3& point);
    00439
    00441   inline bool IsEmpty () const { return !wide && vertices == 0; }
    00442
    00444   inline bool IsInfinite () const { return wide && vertices == 0 && backplane == 0; }
    00445
    00450   inline bool IsWide () const { return wide && vertices == 0; }
    00451
    00456   void MakeInfinite ();
    00457
    00461   void MakeEmpty ();
    00462
    00464   inline void IncRef () { ref_count++; }
    00466   inline void DecRef () { if (ref_count == 1) delete this; else ref_count--; }
    00468   inline int GetRefCount () { return ref_count; }
    00469 };
    00470
    00473 #endif // __CS_FRUSTRUM_H__