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 00028 #include "csextern.h" 00029 00030 #include "cstypes.h" 00031 #include "csgeom/math3d.h" 00032 00033 class csTransform; 00034 00040 00041 #define CS_FRUST_OUTSIDE 0 00042 00043 #define CS_FRUST_INSIDE 1 00044 00045 #define CS_FRUST_COVERED 2 00046 00047 #define CS_FRUST_PARTIAL 3 00048 00056 struct CS_CSGEOM_EXPORT csClipInfo 00057 { 00058 # define CS_CLIPINFO_ORIGINAL 0 00059 # define CS_CLIPINFO_ONEDGE 1 00060 # define CS_CLIPINFO_INSIDE 2 00061 int type; // One of CS_CLIPINFO_??? 00062 union 00063 { 00064 struct { int idx; } original; 00065 struct { int i1, i2; float r; } onedge; 00066 struct { csClipInfo* ci1, * ci2; float r; } inside; 00067 }; 00068 00069 csClipInfo () : type (CS_CLIPINFO_ORIGINAL) { } 00070 void Clear (); 00071 ~csClipInfo () { Clear (); } 00072 00074 void Copy (csClipInfo& other) 00075 { 00076 if (&other == this) return; 00077 Clear (); 00078 type = other.type; 00079 if (type == CS_CLIPINFO_INSIDE) 00080 { 00081 inside.r = other.inside.r; 00082 inside.ci1 = new csClipInfo (); 00083 inside.ci1->Copy (*other.inside.ci1); 00084 inside.ci2 = new csClipInfo (); 00085 inside.ci2->Copy (*other.inside.ci2); 00086 } 00087 else if (type == CS_CLIPINFO_ORIGINAL) 00088 original.idx = other.original.idx; 00089 else 00090 onedge = other.onedge; 00091 } 00092 00094 void Move (csClipInfo& other) 00095 { 00096 if (&other == this) return; 00097 Clear (); 00098 type = other.type; 00099 if (type == CS_CLIPINFO_INSIDE) 00100 inside = other.inside; 00101 else if (type == CS_CLIPINFO_ORIGINAL) 00102 original.idx = other.original.idx; 00103 else 00104 onedge = other.onedge; 00105 other.type = CS_CLIPINFO_ORIGINAL; 00106 } 00107 00108 void Dump (int indent) 00109 { 00110 char ind[255]; 00111 int i; 00112 for (i = 0 ; i < indent ; i++) ind[i] = ' '; 00113 ind[i] = 0; 00114 switch (type) 00115 { 00116 case CS_CLIPINFO_ORIGINAL: 00117 printf ("%s ORIGINAL idx=%d\n", ind, original.idx); 00118 break; 00119 case CS_CLIPINFO_ONEDGE: 00120 printf ("%s ONEDGE i1=%d i2=%d r=%g\n", ind, onedge.i1, onedge.i2, 00121 onedge.r); 00122 break; 00123 case CS_CLIPINFO_INSIDE: 00124 printf ("%s INSIDE r=%g\n", ind, inside.r); 00125 inside.ci1->Dump (indent+2); 00126 inside.ci2->Dump (indent+2); 00127 break; 00128 } 00129 fflush (stdout); 00130 } 00131 }; 00132 00143 class CS_CSGEOM_EXPORT csFrustum 00144 { 00145 private: 00147 csVector3 origin; 00148 00154 csVector3* vertices; 00156 int num_vertices; 00158 int max_vertices; 00159 00161 csPlane3* backplane; 00162 00170 bool wide; 00171 00176 bool mirrored; 00177 00179 int ref_count; 00180 00182 void Clear (); 00183 00185 void ExtendVertexArray (int num); 00186 00187 public: 00188 00190 csFrustum (const csVector3& o) : 00191 origin (o), vertices (0), num_vertices (0), max_vertices (0), 00192 backplane (0), wide (false), mirrored (false), ref_count (1) 00193 { } 00194 00200 csFrustum (const csVector3& o, csVector3* verts, int num_verts, 00201 csPlane3* backp = 0); 00202 00208 csFrustum (const csVector3& o, int num_verts, 00209 csPlane3* backp = 0); 00210 00212 csFrustum (const csFrustum ©); 00213 00215 virtual ~csFrustum (); 00216 00218 void SetOrigin (const csVector3& o) { origin = o; } 00219 00221 csVector3& GetOrigin () { return origin; } 00222 00224 const csVector3& GetOrigin () const { return origin; } 00225 00231 void SetMirrored (bool m) { mirrored = m; } 00232 00234 bool IsMirrored () { return mirrored; } 00235 00242 void SetBackPlane (csPlane3& plane); 00243 00247 csPlane3* GetBackPlane () { return backplane; } 00248 00252 void RemoveBackPlane (); 00253 00257 void AddVertex (const csVector3& v); 00258 00262 int GetVertexCount () { return num_vertices; } 00263 00267 csVector3& GetVertex (int idx) 00268 { 00269 CS_ASSERT (idx >= 0 && idx < num_vertices); 00270 return vertices[idx]; 00271 } 00272 00276 csVector3* GetVertices () { return vertices; } 00277 00281 void Transform (csTransform* trans); 00282 00288 void ClipToPlane (csVector3& v1, csVector3& v2); 00289 00298 static void ClipToPlane (csVector3* vertices, int& num_vertices, 00299 csClipInfo* clipinfo, const csVector3& v1, const csVector3& v2); 00300 00309 static void ClipToPlane (csVector3* vertices, int& num_vertices, 00310 csClipInfo* clipinfo, const csPlane3& plane); 00311 00318 void ClipPolyToPlane (csPlane3* plane); 00319 00328 csPtr<csFrustum> Intersect (const csFrustum& other); 00329 00344 csPtr<csFrustum> Intersect (csVector3* poly, int num); 00345 00360 static csPtr<csFrustum> Intersect ( 00361 const csVector3& frust_origin, csVector3* frust, int num_frust, 00362 csVector3* poly, int num); 00363 00378 static csPtr<csFrustum> Intersect ( 00379 const csVector3& frust_origin, csVector3* frust, int num_frust, 00380 const csVector3& v1, const csVector3& v2, const csVector3& v3); 00381 00387 static int Classify (csVector3* frustum, int num_frust, 00388 csVector3* poly, int num_poly); 00389 00396 static int BatchClassify (csVector3* frustum, csVector3* frustumNormals, 00397 int num_frust, csVector3* poly, int num_poly); 00398 00403 bool Contains (const csVector3& point); 00404 00411 static bool Contains (csVector3* frustum, int num_frust, 00412 const csVector3& point); 00413 00419 static bool Contains (csVector3* frustum, int num_frust, 00420 const csPlane3& plane, const csVector3& point); 00421 00423 bool IsEmpty () const { return !wide && vertices == 0; } 00424 00426 bool IsInfinite () const { return wide && vertices == 0 && backplane == 0; } 00427 00432 bool IsWide () const { return wide && vertices == 0; } 00433 00438 void MakeInfinite (); 00439 00443 void MakeEmpty (); 00444 00446 void IncRef () { ref_count++; } 00448 void DecRef () { if (ref_count == 1) delete this; else ref_count--; } 00450 int GetRefCount () { return ref_count; } 00451 }; 00452 00455 #endif // __CS_FRUSTRUM_H__
Generated for Crystal Space by doxygen 1.2.18