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 -----------------------------------------------------------------------------
36 //! This function calcs the distance from a 3D plane
37 class DISTANCE_PLANE_3D_FUNC
40 template <typename CLASS_POINT, typename CLASS_PLANE>
41 inline GREAL operator()(const CLASS_PLANE& plane, const CLASS_POINT& point)
43 return DISTANCE_PLANE_POINT(plane, point);
47 template <typename CLASS_POINT>
48 SIMD_FORCE_INLINE void PLANE_CLIP_POLYGON_COLLECT(
49 const CLASS_POINT& point0,
50 const CLASS_POINT& point1,
56 GUINT _prevclassif = (dist0 > G_EPSILON);
57 GUINT _classif = (dist1 > G_EPSILON);
58 if (_classif != _prevclassif)
60 GREAL blendfactor = -dist0 / (dist1 - dist0);
61 VEC_BLEND(clipped[clipped_count], point0, point1, blendfactor);
66 VEC_COPY(clipped[clipped_count], point1);
71 //! Clips a polygon by a plane
73 *\return The count of the clipped counts
75 template <typename CLASS_POINT, typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
76 SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON_GENERIC(
77 const CLASS_PLANE& plane,
78 const CLASS_POINT* polygon_points,
79 GUINT polygon_point_count,
80 CLASS_POINT* clipped, DISTANCE_PLANE_FUNC distance_func)
82 GUINT clipped_count = 0;
85 GREAL firstdist = distance_func(plane, polygon_points[0]);
87 if (!(firstdist > G_EPSILON))
89 VEC_COPY(clipped[clipped_count], polygon_points[0]);
93 GREAL olddist = firstdist;
94 for (GUINT _i = 1; _i < polygon_point_count; _i++)
96 GREAL dist = distance_func(plane, polygon_points[_i]);
98 PLANE_CLIP_POLYGON_COLLECT(
99 polygon_points[_i - 1], polygon_points[_i],
108 //RETURN TO FIRST point
110 PLANE_CLIP_POLYGON_COLLECT(
111 polygon_points[polygon_point_count - 1], polygon_points[0],
117 return clipped_count;
120 //! Clips a polygon by a plane
122 *\return The count of the clipped counts
124 template <typename CLASS_POINT, typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
125 SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE_GENERIC(
126 const CLASS_PLANE& plane,
127 const CLASS_POINT& point0,
128 const CLASS_POINT& point1,
129 const CLASS_POINT& point2,
130 CLASS_POINT* clipped, DISTANCE_PLANE_FUNC distance_func)
132 GUINT clipped_count = 0;
135 GREAL firstdist = distance_func(plane, point0);
137 if (!(firstdist > G_EPSILON))
139 VEC_COPY(clipped[clipped_count], point0);
144 GREAL olddist = firstdist;
145 GREAL dist = distance_func(plane, point1);
147 PLANE_CLIP_POLYGON_COLLECT(
157 dist = distance_func(plane, point2);
159 PLANE_CLIP_POLYGON_COLLECT(
167 //RETURN TO FIRST point
168 PLANE_CLIP_POLYGON_COLLECT(
175 return clipped_count;
178 template <typename CLASS_POINT, typename CLASS_PLANE>
179 SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON3D(
180 const CLASS_PLANE& plane,
181 const CLASS_POINT* polygon_points,
182 GUINT polygon_point_count,
183 CLASS_POINT* clipped)
185 return PLANE_CLIP_POLYGON_GENERIC<CLASS_POINT, CLASS_PLANE>(plane, polygon_points, polygon_point_count, clipped, DISTANCE_PLANE_3D_FUNC());
188 template <typename CLASS_POINT, typename CLASS_PLANE>
189 SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE3D(
190 const CLASS_PLANE& plane,
191 const CLASS_POINT& point0,
192 const CLASS_POINT& point1,
193 const CLASS_POINT& point2,
194 CLASS_POINT* clipped)
196 return PLANE_CLIP_TRIANGLE_GENERIC<CLASS_POINT, CLASS_PLANE>(plane, point0, point1, point2, clipped, DISTANCE_PLANE_3D_FUNC());
199 #endif // GIM_TRI_COLLISION_H_INCLUDED