1 #ifndef GIM_CLIP_POLYGON_H_INCLUDED
2 #define GIM_CLIP_POLYGON_H_INCLUDED
4 /*! \file gim_tri_collision.h
5 \author Francisco Leon Najera
8 -----------------------------------------------------------------------------
9 This source file is part of GIMPACT Library.
11 For the latest info, see http://gimpact.sourceforge.net/
13 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
14 email: projectileman@yahoo.com
16 This library is free software; you can redistribute it and/or
17 modify it under the terms of EITHER:
18 (1) The GNU Lesser General Public License as published by the Free
19 Software Foundation; either version 2.1 of the License, or (at
20 your option) any later version. The text of the GNU Lesser
21 General Public License is included with this library in the
22 file GIMPACT-LICENSE-LGPL.TXT.
23 (2) The BSD-style license that is included with this library in
24 the file GIMPACT-LICENSE-BSD.TXT.
25 (3) The zlib/libpng license that is included with this library in
26 the file GIMPACT-LICENSE-ZLIB.TXT.
28 This library is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
31 GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
33 -----------------------------------------------------------------------------
37 //! This function calcs the distance from a 3D plane
38 class DISTANCE_PLANE_3D_FUNC
41 template<typename CLASS_POINT,typename CLASS_PLANE>
42 inline GREAL operator()(const CLASS_PLANE & plane, const CLASS_POINT & point)
44 return DISTANCE_PLANE_POINT(plane, point);
50 template<typename CLASS_POINT>
51 SIMD_FORCE_INLINE void PLANE_CLIP_POLYGON_COLLECT(
52 const CLASS_POINT & point0,
53 const CLASS_POINT & point1,
56 CLASS_POINT * clipped,
57 GUINT & clipped_count)
59 GUINT _prevclassif = (dist0>G_EPSILON);
60 GUINT _classif = (dist1>G_EPSILON);
61 if(_classif!=_prevclassif)
63 GREAL blendfactor = -dist0/(dist1-dist0);
64 VEC_BLEND(clipped[clipped_count],point0,point1,blendfactor);
69 VEC_COPY(clipped[clipped_count],point1);
75 //! Clips a polygon by a plane
77 *\return The count of the clipped counts
79 template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
80 SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON_GENERIC(
81 const CLASS_PLANE & plane,
82 const CLASS_POINT * polygon_points,
83 GUINT polygon_point_count,
84 CLASS_POINT * clipped,DISTANCE_PLANE_FUNC distance_func)
86 GUINT clipped_count = 0;
90 GREAL firstdist = distance_func(plane,polygon_points[0]);;
91 if(!(firstdist>G_EPSILON))
93 VEC_COPY(clipped[clipped_count],polygon_points[0]);
97 GREAL olddist = firstdist;
98 for(GUINT _i=1;_i<polygon_point_count;_i++)
100 GREAL dist = distance_func(plane,polygon_points[_i]);
102 PLANE_CLIP_POLYGON_COLLECT(
103 polygon_points[_i-1],polygon_points[_i],
113 //RETURN TO FIRST point
115 PLANE_CLIP_POLYGON_COLLECT(
116 polygon_points[polygon_point_count-1],polygon_points[0],
122 return clipped_count;
125 //! Clips a polygon by a plane
127 *\return The count of the clipped counts
129 template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
130 SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE_GENERIC(
131 const CLASS_PLANE & plane,
132 const CLASS_POINT & point0,
133 const CLASS_POINT & point1,
134 const CLASS_POINT & point2,
135 CLASS_POINT * clipped,DISTANCE_PLANE_FUNC distance_func)
137 GUINT clipped_count = 0;
140 GREAL firstdist = distance_func(plane,point0);;
141 if(!(firstdist>G_EPSILON))
143 VEC_COPY(clipped[clipped_count],point0);
148 GREAL olddist = firstdist;
149 GREAL dist = distance_func(plane,point1);
151 PLANE_CLIP_POLYGON_COLLECT(
162 dist = distance_func(plane,point2);
164 PLANE_CLIP_POLYGON_COLLECT(
174 //RETURN TO FIRST point
175 PLANE_CLIP_POLYGON_COLLECT(
182 return clipped_count;
186 template<typename CLASS_POINT,typename CLASS_PLANE>
187 SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON3D(
188 const CLASS_PLANE & plane,
189 const CLASS_POINT * polygon_points,
190 GUINT polygon_point_count,
191 CLASS_POINT * clipped)
193 return PLANE_CLIP_POLYGON_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,polygon_points,polygon_point_count,clipped,DISTANCE_PLANE_3D_FUNC());
197 template<typename CLASS_POINT,typename CLASS_PLANE>
198 SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE3D(
199 const CLASS_PLANE & plane,
200 const CLASS_POINT & point0,
201 const CLASS_POINT & point1,
202 const CLASS_POINT & point2,
203 CLASS_POINT * clipped)
205 return PLANE_CLIP_TRIANGLE_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,point0,point1,point2,clipped,DISTANCE_PLANE_3D_FUNC());
210 #endif // GIM_TRI_COLLISION_H_INCLUDED