Main Page   Class Hierarchy   Compound List   File List   Compound Members  

planeclp.h

00001 /*
00002     Copyright (C) 2000 by Jorrit Tyberghein
00003     Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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; // Polygon is not visible.
00074 
00075     // If all vertices are visible then everything is ok.
00076     if (cnt_vis == num_vertices)
00077     {
00078       num_verts = num_vertices;
00079       return true;
00080     }
00081 
00082     // We really need to clip.
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__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000