1 | #ifndef GIM_CLIP_POLYGON_H_INCLUDED |
---|
2 | #define GIM_CLIP_POLYGON_H_INCLUDED |
---|
3 | |
---|
4 | /*! \file gim_tri_collision.h |
---|
5 | \author Francisco Len Nßjera |
---|
6 | */ |
---|
7 | /* |
---|
8 | ----------------------------------------------------------------------------- |
---|
9 | This source file is part of GIMPACT Library. |
---|
10 | |
---|
11 | For the latest info, see http://gimpact.sourceforge.net/ |
---|
12 | |
---|
13 | Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371. |
---|
14 | email: projectileman@yahoo.com |
---|
15 | |
---|
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. |
---|
27 | |
---|
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. |
---|
32 | |
---|
33 | ----------------------------------------------------------------------------- |
---|
34 | */ |
---|
35 | |
---|
36 | /*! \addtogroup GEOMETRIC_OPERATIONS |
---|
37 | */ |
---|
38 | //! @{ |
---|
39 | |
---|
40 | //! This function calcs the distance from a 3D plane |
---|
41 | class DISTANCE_PLANE_3D_FUNC |
---|
42 | { |
---|
43 | public: |
---|
44 | template<typename CLASS_POINT,typename CLASS_PLANE> |
---|
45 | inline GREAL operator()(const CLASS_PLANE & plane, const CLASS_POINT & point) |
---|
46 | { |
---|
47 | return DISTANCE_PLANE_POINT(plane, point); |
---|
48 | } |
---|
49 | }; |
---|
50 | |
---|
51 | |
---|
52 | |
---|
53 | template<typename CLASS_POINT> |
---|
54 | SIMD_FORCE_INLINE void PLANE_CLIP_POLYGON_COLLECT( |
---|
55 | const CLASS_POINT & point0, |
---|
56 | const CLASS_POINT & point1, |
---|
57 | GREAL dist0, |
---|
58 | GREAL dist1, |
---|
59 | CLASS_POINT * clipped, |
---|
60 | GUINT & clipped_count) |
---|
61 | { |
---|
62 | GUINT _prevclassif = (dist0>G_EPSILON); |
---|
63 | GUINT _classif = (dist1>G_EPSILON); |
---|
64 | if(_classif!=_prevclassif) |
---|
65 | { |
---|
66 | GREAL blendfactor = -dist0/(dist1-dist0); |
---|
67 | VEC_BLEND(clipped[clipped_count],point0,point1,blendfactor); |
---|
68 | clipped_count++; |
---|
69 | } |
---|
70 | if(!_classif) |
---|
71 | { |
---|
72 | VEC_COPY(clipped[clipped_count],point1); |
---|
73 | clipped_count++; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | //! Clips a polygon by a plane |
---|
79 | /*! |
---|
80 | *\return The count of the clipped counts |
---|
81 | */ |
---|
82 | template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC> |
---|
83 | SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON_GENERIC( |
---|
84 | const CLASS_PLANE & plane, |
---|
85 | const CLASS_POINT * polygon_points, |
---|
86 | GUINT polygon_point_count, |
---|
87 | CLASS_POINT * clipped,DISTANCE_PLANE_FUNC distance_func) |
---|
88 | { |
---|
89 | GUINT clipped_count = 0; |
---|
90 | |
---|
91 | |
---|
92 | //clip first point |
---|
93 | GREAL firstdist = distance_func(plane,polygon_points[0]);; |
---|
94 | if(!(firstdist>G_EPSILON)) |
---|
95 | { |
---|
96 | VEC_COPY(clipped[clipped_count],polygon_points[0]); |
---|
97 | clipped_count++; |
---|
98 | } |
---|
99 | |
---|
100 | GREAL olddist = firstdist; |
---|
101 | for(GUINT _i=1;_i<polygon_point_count;_i++) |
---|
102 | { |
---|
103 | GREAL dist = distance_func(plane,polygon_points[_i]); |
---|
104 | |
---|
105 | PLANE_CLIP_POLYGON_COLLECT( |
---|
106 | polygon_points[_i-1],polygon_points[_i], |
---|
107 | olddist, |
---|
108 | dist, |
---|
109 | clipped, |
---|
110 | clipped_count); |
---|
111 | |
---|
112 | |
---|
113 | olddist = dist; |
---|
114 | } |
---|
115 | |
---|
116 | //RETURN TO FIRST point |
---|
117 | |
---|
118 | PLANE_CLIP_POLYGON_COLLECT( |
---|
119 | polygon_points[polygon_point_count-1],polygon_points[0], |
---|
120 | olddist, |
---|
121 | firstdist, |
---|
122 | clipped, |
---|
123 | clipped_count); |
---|
124 | |
---|
125 | return clipped_count; |
---|
126 | } |
---|
127 | |
---|
128 | //! Clips a polygon by a plane |
---|
129 | /*! |
---|
130 | *\return The count of the clipped counts |
---|
131 | */ |
---|
132 | template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC> |
---|
133 | SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE_GENERIC( |
---|
134 | const CLASS_PLANE & plane, |
---|
135 | const CLASS_POINT & point0, |
---|
136 | const CLASS_POINT & point1, |
---|
137 | const CLASS_POINT & point2, |
---|
138 | CLASS_POINT * clipped,DISTANCE_PLANE_FUNC distance_func) |
---|
139 | { |
---|
140 | GUINT clipped_count = 0; |
---|
141 | |
---|
142 | //clip first point |
---|
143 | GREAL firstdist = distance_func(plane,point0);; |
---|
144 | if(!(firstdist>G_EPSILON)) |
---|
145 | { |
---|
146 | VEC_COPY(clipped[clipped_count],point0); |
---|
147 | clipped_count++; |
---|
148 | } |
---|
149 | |
---|
150 | // point 1 |
---|
151 | GREAL olddist = firstdist; |
---|
152 | GREAL dist = distance_func(plane,point1); |
---|
153 | |
---|
154 | PLANE_CLIP_POLYGON_COLLECT( |
---|
155 | point0,point1, |
---|
156 | olddist, |
---|
157 | dist, |
---|
158 | clipped, |
---|
159 | clipped_count); |
---|
160 | |
---|
161 | olddist = dist; |
---|
162 | |
---|
163 | |
---|
164 | // point 2 |
---|
165 | dist = distance_func(plane,point2); |
---|
166 | |
---|
167 | PLANE_CLIP_POLYGON_COLLECT( |
---|
168 | point1,point2, |
---|
169 | olddist, |
---|
170 | dist, |
---|
171 | clipped, |
---|
172 | clipped_count); |
---|
173 | olddist = dist; |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | //RETURN TO FIRST point |
---|
178 | PLANE_CLIP_POLYGON_COLLECT( |
---|
179 | point2,point0, |
---|
180 | olddist, |
---|
181 | firstdist, |
---|
182 | clipped, |
---|
183 | clipped_count); |
---|
184 | |
---|
185 | return clipped_count; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | template<typename CLASS_POINT,typename CLASS_PLANE> |
---|
190 | SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON3D( |
---|
191 | const CLASS_PLANE & plane, |
---|
192 | const CLASS_POINT * polygon_points, |
---|
193 | GUINT polygon_point_count, |
---|
194 | CLASS_POINT * clipped) |
---|
195 | { |
---|
196 | return PLANE_CLIP_POLYGON_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,polygon_points,polygon_point_count,clipped,DISTANCE_PLANE_3D_FUNC()); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | template<typename CLASS_POINT,typename CLASS_PLANE> |
---|
201 | SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE3D( |
---|
202 | const CLASS_PLANE & plane, |
---|
203 | const CLASS_POINT & point0, |
---|
204 | const CLASS_POINT & point1, |
---|
205 | const CLASS_POINT & point2, |
---|
206 | CLASS_POINT * clipped) |
---|
207 | { |
---|
208 | return PLANE_CLIP_TRIANGLE_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,point0,point1,point2,clipped,DISTANCE_PLANE_3D_FUNC()); |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | //! @} |
---|
213 | |
---|
214 | #endif // GIM_TRI_COLLISION_H_INCLUDED |
---|