[1963] | 1 | #ifndef BT_CLIP_POLYGON_H_INCLUDED |
---|
| 2 | #define BT_CLIP_POLYGON_H_INCLUDED |
---|
| 3 | |
---|
| 4 | /*! \file btClipPolygon.h |
---|
| 5 | \author Francisco Len Nßjera |
---|
| 6 | */ |
---|
| 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 | /*! \addtogroup GEOMETRIC_OPERATIONS |
---|
| 31 | */ |
---|
| 32 | //! @{ |
---|
| 33 | |
---|
| 34 | SIMD_FORCE_INLINE btScalar bt_distance_point_plane(const btVector4 & plane,const btVector3 &point) |
---|
| 35 | { |
---|
| 36 | return point.dot(plane) - plane[3]; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /*! Vector blending |
---|
| 40 | Takes two vectors a, b, blends them together*/ |
---|
| 41 | SIMD_FORCE_INLINE void bt_vec_blend(btVector3 &vr, const btVector3 &va,const btVector3 &vb, btScalar blend_factor) |
---|
| 42 | { |
---|
| 43 | vr = (1-blend_factor)*va + blend_factor*vb; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | //! This function calcs the distance from a 3D plane |
---|
| 47 | SIMD_FORCE_INLINE void bt_plane_clip_polygon_collect( |
---|
| 48 | const btVector3 & point0, |
---|
| 49 | const btVector3 & point1, |
---|
| 50 | btScalar dist0, |
---|
| 51 | btScalar dist1, |
---|
| 52 | btVector3 * clipped, |
---|
| 53 | int & clipped_count) |
---|
| 54 | { |
---|
| 55 | bool _prevclassif = (dist0>SIMD_EPSILON); |
---|
| 56 | bool _classif = (dist1>SIMD_EPSILON); |
---|
| 57 | if(_classif!=_prevclassif) |
---|
| 58 | { |
---|
| 59 | btScalar blendfactor = -dist0/(dist1-dist0); |
---|
| 60 | bt_vec_blend(clipped[clipped_count],point0,point1,blendfactor); |
---|
| 61 | clipped_count++; |
---|
| 62 | } |
---|
| 63 | if(!_classif) |
---|
| 64 | { |
---|
| 65 | clipped[clipped_count] = point1; |
---|
| 66 | clipped_count++; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | //! Clips a polygon by a plane |
---|
| 72 | /*! |
---|
| 73 | *\return The count of the clipped counts |
---|
| 74 | */ |
---|
| 75 | SIMD_FORCE_INLINE int bt_plane_clip_polygon( |
---|
| 76 | const btVector4 & plane, |
---|
| 77 | const btVector3 * polygon_points, |
---|
| 78 | int polygon_point_count, |
---|
| 79 | btVector3 * clipped) |
---|
| 80 | { |
---|
| 81 | int clipped_count = 0; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | //clip first point |
---|
| 85 | btScalar firstdist = bt_distance_point_plane(plane,polygon_points[0]);; |
---|
| 86 | if(!(firstdist>SIMD_EPSILON)) |
---|
| 87 | { |
---|
| 88 | clipped[clipped_count] = polygon_points[0]; |
---|
| 89 | clipped_count++; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | btScalar olddist = firstdist; |
---|
| 93 | for(int i=1;i<polygon_point_count;i++) |
---|
| 94 | { |
---|
| 95 | btScalar dist = bt_distance_point_plane(plane,polygon_points[i]); |
---|
| 96 | |
---|
| 97 | bt_plane_clip_polygon_collect( |
---|
| 98 | polygon_points[i-1],polygon_points[i], |
---|
| 99 | olddist, |
---|
| 100 | dist, |
---|
| 101 | clipped, |
---|
| 102 | clipped_count); |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | olddist = dist; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | //RETURN TO FIRST point |
---|
| 109 | |
---|
| 110 | bt_plane_clip_polygon_collect( |
---|
| 111 | polygon_points[polygon_point_count-1],polygon_points[0], |
---|
| 112 | olddist, |
---|
| 113 | firstdist, |
---|
| 114 | clipped, |
---|
| 115 | clipped_count); |
---|
| 116 | |
---|
| 117 | return clipped_count; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | //! Clips a polygon by a plane |
---|
| 121 | /*! |
---|
| 122 | *\param clipped must be an array of 16 points. |
---|
| 123 | *\return The count of the clipped counts |
---|
| 124 | */ |
---|
| 125 | SIMD_FORCE_INLINE int bt_plane_clip_triangle( |
---|
| 126 | const btVector4 & plane, |
---|
| 127 | const btVector3 & point0, |
---|
| 128 | const btVector3 & point1, |
---|
| 129 | const btVector3& point2, |
---|
| 130 | btVector3 * clipped // an allocated array of 16 points at least |
---|
| 131 | ) |
---|
| 132 | { |
---|
| 133 | int clipped_count = 0; |
---|
| 134 | |
---|
| 135 | //clip first point0 |
---|
| 136 | btScalar firstdist = bt_distance_point_plane(plane,point0);; |
---|
| 137 | if(!(firstdist>SIMD_EPSILON)) |
---|
| 138 | { |
---|
| 139 | clipped[clipped_count] = point0; |
---|
| 140 | clipped_count++; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | // point 1 |
---|
| 144 | btScalar olddist = firstdist; |
---|
| 145 | btScalar dist = bt_distance_point_plane(plane,point1); |
---|
| 146 | |
---|
| 147 | bt_plane_clip_polygon_collect( |
---|
| 148 | point0,point1, |
---|
| 149 | olddist, |
---|
| 150 | dist, |
---|
| 151 | clipped, |
---|
| 152 | clipped_count); |
---|
| 153 | |
---|
| 154 | olddist = dist; |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | // point 2 |
---|
| 158 | dist = bt_distance_point_plane(plane,point2); |
---|
| 159 | |
---|
| 160 | bt_plane_clip_polygon_collect( |
---|
| 161 | point1,point2, |
---|
| 162 | olddist, |
---|
| 163 | dist, |
---|
| 164 | clipped, |
---|
| 165 | clipped_count); |
---|
| 166 | olddist = dist; |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | //RETURN TO FIRST point0 |
---|
| 171 | bt_plane_clip_polygon_collect( |
---|
| 172 | point2,point0, |
---|
| 173 | olddist, |
---|
| 174 | firstdist, |
---|
| 175 | clipped, |
---|
| 176 | clipped_count); |
---|
| 177 | |
---|
| 178 | return clipped_count; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | //! @} |
---|
| 185 | |
---|
| 186 | #endif // GIM_TRI_COLLISION_H_INCLUDED |
---|