00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_PLANECLP_H__
00021 #define __CS_PLANECLP_H__
00022
00023 #include "csgeom/math3d.h"
00024 #include "csutil/garray.h"
00025
00029 class csPlaneClip : public csPlane3
00030 {
00031 protected:
00032 CS_DECLARE_GROWING_ARRAY(verts, csVector3);
00033 CS_DECLARE_GROWING_ARRAY(vis, bool);
00034 void init (int len) { verts.SetLimit (len); vis.SetLimit (len); }
00035
00036 public:
00038 csPlaneClip (const csVector3& plane_norm, float d = 0) :
00039 csPlane3(plane_norm, d) { init(100); }
00041 csPlaneClip (const csPlane3& plane) :
00042 csPlane3(plane) { init(100); }
00043
00045 csPlaneClip (float d = 0) : csPlane3(0, 0, -1, d) { init(100); }
00046
00048 csPlaneClip (float a, float b, float c, float d = 0) :
00049 csPlane3(a,b,c,d) { init(100); }
00050
00056 bool ClipPolygon (csVector3*& pverts, int& num_verts )
00057 {
00058 int i,i1, num_vertices = num_verts, cnt_vis = 0;
00059 bool zs, z1s;
00060 float r;
00061
00062 if (num_verts > verts.Limit())
00063 init(num_verts);
00064
00065 for (i = 0 ; i < num_vertices ; i++)
00066 {
00067 vis[i] = Classify (pverts[i]) >= 0;
00068 if (vis[i])
00069 cnt_vis++;
00070 }
00071
00072 if (cnt_vis == 0)
00073 return false;
00074
00075
00076 if (cnt_vis == num_vertices)
00077 {
00078 num_verts = num_vertices;
00079 return true;
00080 }
00081
00082
00083 num_verts = 0;
00084
00085 i1 = num_vertices - 1;
00086
00087 for (i = 0 ; i < num_vertices ; i++)
00088 {
00089 zs = vis[i];
00090 z1s = vis[i1];
00091
00092 if (!z1s && zs)
00093 {
00094 csIntersect3::Plane(pverts[i1], pverts[i], *this, verts[num_verts], r);
00095 num_verts++;
00096 verts[num_verts++] = pverts[i];
00097 }
00098 else if (z1s && !zs)
00099 {
00100 csIntersect3::Plane(pverts[i1], pverts[i], *this, verts[num_verts], r);
00101 num_verts++;
00102 }
00103 else if (z1s && zs)
00104 {
00105 verts[num_verts++] = pverts[i];
00106 }
00107 i1 = i;
00108 }
00109 pverts = verts.GetArray();
00110 return true;
00111 }
00112 };
00113
00114 #endif // __CS_PLANECLP_H__