Bullet Collision Detection & Physics Library
gim_clip_polygon.h
Go to the documentation of this file.
1 #ifndef GIM_CLIP_POLYGON_H_INCLUDED
2 #define GIM_CLIP_POLYGON_H_INCLUDED
3 
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 
39 {
40 public:
41  template<typename CLASS_POINT,typename CLASS_PLANE>
42  inline GREAL operator()(const CLASS_PLANE & plane, const CLASS_POINT & point)
43  {
44  return DISTANCE_PLANE_POINT(plane, point);
45  }
46 };
47 
48 
49 
50 template<typename CLASS_POINT>
52  const CLASS_POINT & point0,
53  const CLASS_POINT & point1,
54  GREAL dist0,
55  GREAL dist1,
56  CLASS_POINT * clipped,
57  GUINT & clipped_count)
58 {
59  GUINT _prevclassif = (dist0>G_EPSILON);
60  GUINT _classif = (dist1>G_EPSILON);
61  if(_classif!=_prevclassif)
62  {
63  GREAL blendfactor = -dist0/(dist1-dist0);
64  VEC_BLEND(clipped[clipped_count],point0,point1,blendfactor);
65  clipped_count++;
66  }
67  if(!_classif)
68  {
69  VEC_COPY(clipped[clipped_count],point1);
70  clipped_count++;
71  }
72 }
73 
74 
76 
79 template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
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)
85 {
86  GUINT clipped_count = 0;
87 
88 
89  //clip first point
90  GREAL firstdist = distance_func(plane,polygon_points[0]);;
91  if(!(firstdist>G_EPSILON))
92  {
93  VEC_COPY(clipped[clipped_count],polygon_points[0]);
94  clipped_count++;
95  }
96 
97  GREAL olddist = firstdist;
98  for(GUINT _i=1;_i<polygon_point_count;_i++)
99  {
100  GREAL dist = distance_func(plane,polygon_points[_i]);
101 
103  polygon_points[_i-1],polygon_points[_i],
104  olddist,
105  dist,
106  clipped,
107  clipped_count);
108 
109 
110  olddist = dist;
111  }
112 
113  //RETURN TO FIRST point
114 
116  polygon_points[polygon_point_count-1],polygon_points[0],
117  olddist,
118  firstdist,
119  clipped,
120  clipped_count);
121 
122  return clipped_count;
123 }
124 
126 
129 template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
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)
136 {
137  GUINT clipped_count = 0;
138 
139  //clip first point
140  GREAL firstdist = distance_func(plane,point0);;
141  if(!(firstdist>G_EPSILON))
142  {
143  VEC_COPY(clipped[clipped_count],point0);
144  clipped_count++;
145  }
146 
147  // point 1
148  GREAL olddist = firstdist;
149  GREAL dist = distance_func(plane,point1);
150 
152  point0,point1,
153  olddist,
154  dist,
155  clipped,
156  clipped_count);
157 
158  olddist = dist;
159 
160 
161  // point 2
162  dist = distance_func(plane,point2);
163 
165  point1,point2,
166  olddist,
167  dist,
168  clipped,
169  clipped_count);
170  olddist = dist;
171 
172 
173 
174  //RETURN TO FIRST point
176  point2,point0,
177  olddist,
178  firstdist,
179  clipped,
180  clipped_count);
181 
182  return clipped_count;
183 }
184 
185 
186 template<typename CLASS_POINT,typename CLASS_PLANE>
188  const CLASS_PLANE & plane,
189  const CLASS_POINT * polygon_points,
190  GUINT polygon_point_count,
191  CLASS_POINT * clipped)
192 {
193  return PLANE_CLIP_POLYGON_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,polygon_points,polygon_point_count,clipped,DISTANCE_PLANE_3D_FUNC());
194 }
195 
196 
197 template<typename CLASS_POINT,typename CLASS_PLANE>
199  const CLASS_PLANE & plane,
200  const CLASS_POINT & point0,
201  const CLASS_POINT & point1,
202  const CLASS_POINT & point2,
203  CLASS_POINT * clipped)
204 {
205  return PLANE_CLIP_TRIANGLE_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,point0,point1,point2,clipped,DISTANCE_PLANE_3D_FUNC());
206 }
207 
208 
209 
210 #endif // GIM_TRI_COLLISION_H_INCLUDED
GUINT PLANE_CLIP_TRIANGLE_GENERIC(const CLASS_PLANE &plane, const CLASS_POINT &point0, const CLASS_POINT &point1, const CLASS_POINT &point2, CLASS_POINT *clipped, DISTANCE_PLANE_FUNC distance_func)
Clips a polygon by a plane.
#define G_EPSILON
Definition: gim_math.h:60
float dist(const Point3 &pnt0, const Point3 &pnt1)
GUINT PLANE_CLIP_POLYGON3D(const CLASS_PLANE &plane, const CLASS_POINT *polygon_points, GUINT polygon_point_count, CLASS_POINT *clipped)
#define SIMD_FORCE_INLINE
Definition: btScalar.h:58
#define VEC_COPY(b, a)
Copy 3D vector.
#define GREAL
Definition: gim_math.h:39
GREAL operator()(const CLASS_PLANE &plane, const CLASS_POINT &point)
This function calcs the distance from a 3D plane.
GUINT PLANE_CLIP_TRIANGLE3D(const CLASS_PLANE &plane, const CLASS_POINT &point0, const CLASS_POINT &point1, const CLASS_POINT &point2, CLASS_POINT *clipped)
void PLANE_CLIP_POLYGON_COLLECT(const CLASS_POINT &point0, const CLASS_POINT &point1, GREAL dist0, GREAL dist1, CLASS_POINT *clipped, GUINT &clipped_count)
#define DISTANCE_PLANE_POINT(plane, point)
#define GUINT
Definition: gim_math.h:42
GUINT PLANE_CLIP_POLYGON_GENERIC(const CLASS_PLANE &plane, const CLASS_POINT *polygon_points, GUINT polygon_point_count, CLASS_POINT *clipped, DISTANCE_PLANE_FUNC distance_func)
Clips a polygon by a plane.
#define VEC_BLEND(vr, a, b, s)