1 #ifndef BT_CLIP_POLYGON_H_INCLUDED
2 #define BT_CLIP_POLYGON_H_INCLUDED
4 /*! \file btClipPolygon.h
5 \author Francisco Leon Najera
8 This source file is part of GIMPACT Library.
10 For the latest info, see http://gimpact.sourceforge.net/
12 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
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:
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.
27 #include "LinearMath/btTransform.h"
28 #include "LinearMath/btGeometryUtil.h"
30 SIMD_FORCE_INLINE btScalar bt_distance_point_plane(const btVector4 &plane, const btVector3 &point)
32 return point.dot(plane) - plane[3];
36 Takes two vectors a, b, blends them together*/
37 SIMD_FORCE_INLINE void bt_vec_blend(btVector3 &vr, const btVector3 &va, const btVector3 &vb, btScalar blend_factor)
39 vr = (1 - blend_factor) * va + blend_factor * vb;
42 //! This function calcs the distance from a 3D plane
43 SIMD_FORCE_INLINE void bt_plane_clip_polygon_collect(
44 const btVector3 &point0,
45 const btVector3 &point1,
51 bool _prevclassif = (dist0 > SIMD_EPSILON);
52 bool _classif = (dist1 > SIMD_EPSILON);
53 if (_classif != _prevclassif)
55 btScalar blendfactor = -dist0 / (dist1 - dist0);
56 bt_vec_blend(clipped[clipped_count], point0, point1, blendfactor);
61 clipped[clipped_count] = point1;
66 //! Clips a polygon by a plane
68 *\return The count of the clipped counts
70 SIMD_FORCE_INLINE int bt_plane_clip_polygon(
71 const btVector4 &plane,
72 const btVector3 *polygon_points,
73 int polygon_point_count,
76 int clipped_count = 0;
79 btScalar firstdist = bt_distance_point_plane(plane, polygon_points[0]);
81 if (!(firstdist > SIMD_EPSILON))
83 clipped[clipped_count] = polygon_points[0];
87 btScalar olddist = firstdist;
88 for (int i = 1; i < polygon_point_count; i++)
90 btScalar dist = bt_distance_point_plane(plane, polygon_points[i]);
92 bt_plane_clip_polygon_collect(
93 polygon_points[i - 1], polygon_points[i],
102 //RETURN TO FIRST point
104 bt_plane_clip_polygon_collect(
105 polygon_points[polygon_point_count - 1], polygon_points[0],
111 return clipped_count;
114 //! Clips a polygon by a plane
116 *\param clipped must be an array of 16 points.
117 *\return The count of the clipped counts
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
127 int clipped_count = 0;
130 btScalar firstdist = bt_distance_point_plane(plane, point0);
132 if (!(firstdist > SIMD_EPSILON))
134 clipped[clipped_count] = point0;
139 btScalar olddist = firstdist;
140 btScalar dist = bt_distance_point_plane(plane, point1);
142 bt_plane_clip_polygon_collect(
152 dist = bt_distance_point_plane(plane, point2);
154 bt_plane_clip_polygon_collect(
162 //RETURN TO FIRST point0
163 bt_plane_clip_polygon_collect(
170 return clipped_count;
173 #endif // GIM_TRI_COLLISION_H_INCLUDED