Bullet Collision Detection & Physics Library: btClipPolygon.h Source File

Bullet Collision Detection & Physics Library

btClipPolygon.h
Go to the documentation of this file.
1 #ifndef BT_CLIP_POLYGON_H_INCLUDED
2 #define BT_CLIP_POLYGON_H_INCLUDED
3
7 /*
8 This source file is part of GIMPACT Library.
9
10 For the latest info, see http://gimpact.sourceforge.net/
11
12 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
14
15
16 This software is provided 'as-is', without any express or implied warranty.
17 In no event will the authors be held liable for any damages arising from the use of this software.
18 Permission is granted to anyone to use this software for any purpose,
19 including commercial applications, and to alter it and redistribute it freely,
20 subject to the following restrictions:
21
22 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source distribution.
25 */
26
27 #include "LinearMath/btTransform.h"
28 #include "LinearMath/btGeometryUtil.h"
29
30 SIMD_FORCE_INLINE btScalar bt_distance_point_plane(const btVector4 &plane, const btVector3 &point)
31 {
32 return point.dot(plane) - plane[3];
33 }
34
37 SIMD_FORCE_INLINE void bt_vec_blend(btVector3 &vr, const btVector3 &va, const btVector3 &vb, btScalar blend_factor)
38 {
39 vr = (1 - blend_factor) * va + blend_factor * vb;
40 }
41
43 SIMD_FORCE_INLINE void bt_plane_clip_polygon_collect(
44 const btVector3 &point0,
45 const btVector3 &point1,
46 btScalar dist0,
47 btScalar dist1,
48 btVector3 *clipped,
49 int &clipped_count)
50 {
51 bool _prevclassif = (dist0 > SIMD_EPSILON);
52 bool _classif = (dist1 > SIMD_EPSILON);
53 if (_classif != _prevclassif)
54 {
55 btScalar blendfactor = -dist0 / (dist1 - dist0);
56 bt_vec_blend(clipped[clipped_count], point0, point1, blendfactor);
57 clipped_count++;
58 }
59 if (!_classif)
60 {
61 clipped[clipped_count] = point1;
62 clipped_count++;
63 }
64 }
65
67
70 SIMD_FORCE_INLINE int bt_plane_clip_polygon(
71 const btVector4 &plane,
72 const btVector3 *polygon_points,
73 int polygon_point_count,
74 btVector3 *clipped)
75 {
76 int clipped_count = 0;
77
78 //clip first point
79 btScalar firstdist = bt_distance_point_plane(plane, polygon_points[0]);
80 ;
81 if (!(firstdist > SIMD_EPSILON))
82 {
83 clipped[clipped_count] = polygon_points[0];
84 clipped_count++;
85 }
86
87 btScalar olddist = firstdist;
88 for (int i = 1; i < polygon_point_count; i++)
89 {
90 btScalar dist = bt_distance_point_plane(plane, polygon_points[i]);
91
92 bt_plane_clip_polygon_collect(
93 polygon_points[i - 1], polygon_points[i],
94 olddist,
95 dist,
96 clipped,
97 clipped_count);
98
99 olddist = dist;
100 }
101
102 //RETURN TO FIRST point
103
104 bt_plane_clip_polygon_collect(
105 polygon_points[polygon_point_count - 1], polygon_points[0],
106 olddist,
107 firstdist,
108 clipped,
109 clipped_count);
110
111 return clipped_count;
112 }
113
115
119 SIMD_FORCE_INLINE int bt_plane_clip_triangle(
120 const btVector4 &plane,
121 const btVector3 &point0,
122 const btVector3 &point1,
123 const btVector3 &point2,
124 btVector3 *clipped // an allocated array of 16 points at least
125 )
126 {
127 int clipped_count = 0;
128
129 //clip first point0
130 btScalar firstdist = bt_distance_point_plane(plane, point0);
131 ;
132 if (!(firstdist > SIMD_EPSILON))
133 {
134 clipped[clipped_count] = point0;
135 clipped_count++;
136 }
137
138 // point 1
139 btScalar olddist = firstdist;
140 btScalar dist = bt_distance_point_plane(plane, point1);
141
142 bt_plane_clip_polygon_collect(
143 point0, point1,
144 olddist,
145 dist,
146 clipped,
147 clipped_count);
148
149 olddist = dist;
150
151 // point 2
152 dist = bt_distance_point_plane(plane, point2);
153
154 bt_plane_clip_polygon_collect(
155 point1, point2,
156 olddist,
157 dist,
158 clipped,
159 clipped_count);
160 olddist = dist;
161
162 //RETURN TO FIRST point0
163 bt_plane_clip_polygon_collect(
164 point2, point0,
165 olddist,
166 firstdist,
167 clipped,
168 clipped_count);
169
170 return clipped_count;
171 }
172
173 #endif // GIM_TRI_COLLISION_H_INCLUDED
bt_plane_clip_polygon_collect
void bt_plane_clip_polygon_collect(const btVector3 &point0, const btVector3 &point1, btScalar dist0, btScalar dist1, btVector3 *clipped, int &clipped_count)
This function calcs the distance from a 3D plane.
Definition: btClipPolygon.h:43
bt_plane_clip_triangle
int bt_plane_clip_triangle(const btVector4 &plane, const btVector3 &point0, const btVector3 &point1, const btVector3 &point2, btVector3 *clipped)
Clips a polygon by a plane.
Definition: btClipPolygon.h:119
bt_distance_point_plane
btScalar bt_distance_point_plane(const btVector4 &plane, const btVector3 &point)
Definition: btClipPolygon.h:30
bt_vec_blend
void bt_vec_blend(btVector3 &vr, const btVector3 &va, const btVector3 &vb, btScalar blend_factor)
Definition: btClipPolygon.h:37
bt_plane_clip_polygon
int bt_plane_clip_polygon(const btVector4 &plane, const btVector3 *polygon_points, int polygon_point_count, btVector3 *clipped)
Clips a polygon by a plane.
Definition: btClipPolygon.h:70
btGeometryUtil.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:98
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:543
btTransform.h
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btVector4
Definition: btVector3.h:1074