| [2431] | 1 | /*! \file btGImpactTriangleShape.h | 
|---|
 | 2 | \author Francisco Len Nßjera | 
|---|
 | 3 | */ | 
|---|
 | 4 | /* | 
|---|
 | 5 | This source file is part of GIMPACT Library. | 
|---|
 | 6 |  | 
|---|
 | 7 | For the latest info, see http://gimpact.sourceforge.net/ | 
|---|
 | 8 |  | 
|---|
 | 9 | Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371. | 
|---|
 | 10 | email: projectileman@yahoo.com | 
|---|
 | 11 |  | 
|---|
 | 12 |  | 
|---|
 | 13 | This software is provided 'as-is', without any express or implied warranty. | 
|---|
 | 14 | In no event will the authors be held liable for any damages arising from the use of this software. | 
|---|
 | 15 | Permission is granted to anyone to use this software for any purpose, | 
|---|
 | 16 | including commercial applications, and to alter it and redistribute it freely, | 
|---|
 | 17 | subject to the following restrictions: | 
|---|
 | 18 |  | 
|---|
 | 19 | 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. | 
|---|
 | 20 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | 
|---|
 | 21 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
 | 22 | */ | 
|---|
 | 23 |  | 
|---|
 | 24 | #include "btTriangleShapeEx.h" | 
|---|
 | 25 |  | 
|---|
 | 26 |  | 
|---|
 | 27 |  | 
|---|
 | 28 | void GIM_TRIANGLE_CONTACT::merge_points(const btVector4 & plane, | 
|---|
 | 29 |                                        btScalar margin, const btVector3 * points, int point_count) | 
|---|
 | 30 | { | 
|---|
 | 31 |     m_point_count = 0; | 
|---|
 | 32 |     m_penetration_depth= -1000.0f; | 
|---|
 | 33 |  | 
|---|
 | 34 |     int point_indices[MAX_TRI_CLIPPING]; | 
|---|
 | 35 |  | 
|---|
 | 36 |         int _k; | 
|---|
 | 37 |  | 
|---|
 | 38 |     for ( _k=0;_k<point_count;_k++) | 
|---|
 | 39 |     { | 
|---|
 | 40 |         btScalar _dist = - bt_distance_point_plane(plane,points[_k]) + margin; | 
|---|
 | 41 |  | 
|---|
 | 42 |         if (_dist>=0.0f) | 
|---|
 | 43 |         { | 
|---|
 | 44 |             if (_dist>m_penetration_depth) | 
|---|
 | 45 |             { | 
|---|
 | 46 |                 m_penetration_depth = _dist; | 
|---|
 | 47 |                 point_indices[0] = _k; | 
|---|
 | 48 |                 m_point_count=1; | 
|---|
 | 49 |             } | 
|---|
 | 50 |             else if ((_dist+SIMD_EPSILON)>=m_penetration_depth) | 
|---|
 | 51 |             { | 
|---|
 | 52 |                 point_indices[m_point_count] = _k; | 
|---|
 | 53 |                 m_point_count++; | 
|---|
 | 54 |             } | 
|---|
 | 55 |         } | 
|---|
 | 56 |     } | 
|---|
 | 57 |  | 
|---|
 | 58 |     for ( _k=0;_k<m_point_count;_k++) | 
|---|
 | 59 |     { | 
|---|
 | 60 |         m_points[_k] = points[point_indices[_k]]; | 
|---|
 | 61 |     } | 
|---|
 | 62 | } | 
|---|
 | 63 |  | 
|---|
 | 64 | ///class btPrimitiveTriangle | 
|---|
 | 65 | bool btPrimitiveTriangle::overlap_test_conservative(const btPrimitiveTriangle& other) | 
|---|
 | 66 | { | 
|---|
 | 67 |     btScalar total_margin = m_margin + other.m_margin; | 
|---|
 | 68 |     // classify points on other triangle | 
|---|
 | 69 |     btScalar dis0 = bt_distance_point_plane(m_plane,other.m_vertices[0]) - total_margin; | 
|---|
 | 70 |  | 
|---|
 | 71 |     btScalar dis1 = bt_distance_point_plane(m_plane,other.m_vertices[1]) - total_margin; | 
|---|
 | 72 |  | 
|---|
 | 73 |     btScalar dis2 = bt_distance_point_plane(m_plane,other.m_vertices[2]) - total_margin; | 
|---|
 | 74 |  | 
|---|
 | 75 |     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false; | 
|---|
 | 76 |  | 
|---|
 | 77 |     // classify points on this triangle | 
|---|
 | 78 |     dis0 = bt_distance_point_plane(other.m_plane,m_vertices[0]) - total_margin; | 
|---|
 | 79 |  | 
|---|
 | 80 |     dis1 = bt_distance_point_plane(other.m_plane,m_vertices[1]) - total_margin; | 
|---|
 | 81 |  | 
|---|
 | 82 |     dis2 = bt_distance_point_plane(other.m_plane,m_vertices[2]) - total_margin; | 
|---|
 | 83 |  | 
|---|
 | 84 |     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false; | 
|---|
 | 85 |  | 
|---|
 | 86 |     return true; | 
|---|
 | 87 | } | 
|---|
 | 88 |  | 
|---|
 | 89 | int btPrimitiveTriangle::clip_triangle(btPrimitiveTriangle & other, btVector3 * clipped_points ) | 
|---|
 | 90 | { | 
|---|
 | 91 |     // edge 0 | 
|---|
 | 92 |  | 
|---|
 | 93 |     btVector3 temp_points[MAX_TRI_CLIPPING]; | 
|---|
 | 94 |  | 
|---|
 | 95 |  | 
|---|
 | 96 |     btVector4 edgeplane; | 
|---|
 | 97 |  | 
|---|
 | 98 |     get_edge_plane(0,edgeplane); | 
|---|
 | 99 |  | 
|---|
 | 100 |  | 
|---|
 | 101 |     int clipped_count = bt_plane_clip_triangle( | 
|---|
 | 102 |                             edgeplane,other.m_vertices[0],other.m_vertices[1],other.m_vertices[2],temp_points); | 
|---|
 | 103 |  | 
|---|
 | 104 |     if (clipped_count == 0) return 0; | 
|---|
 | 105 |  | 
|---|
 | 106 |     btVector3 temp_points1[MAX_TRI_CLIPPING]; | 
|---|
 | 107 |  | 
|---|
 | 108 |  | 
|---|
 | 109 |     // edge 1 | 
|---|
 | 110 |     get_edge_plane(1,edgeplane); | 
|---|
 | 111 |  | 
|---|
 | 112 |  | 
|---|
 | 113 |     clipped_count = bt_plane_clip_polygon(edgeplane,temp_points,clipped_count,temp_points1); | 
|---|
 | 114 |  | 
|---|
 | 115 |     if (clipped_count == 0) return 0; | 
|---|
 | 116 |  | 
|---|
 | 117 |     // edge 2 | 
|---|
 | 118 |     get_edge_plane(2,edgeplane); | 
|---|
 | 119 |  | 
|---|
 | 120 |     clipped_count = bt_plane_clip_polygon( | 
|---|
 | 121 |                         edgeplane,temp_points1,clipped_count,clipped_points); | 
|---|
 | 122 |  | 
|---|
 | 123 |     return clipped_count; | 
|---|
 | 124 | } | 
|---|
 | 125 |  | 
|---|
 | 126 | bool btPrimitiveTriangle::find_triangle_collision_clip_method(btPrimitiveTriangle & other, GIM_TRIANGLE_CONTACT & contacts) | 
|---|
 | 127 | { | 
|---|
 | 128 |     btScalar margin = m_margin + other.m_margin; | 
|---|
 | 129 |  | 
|---|
 | 130 |     btVector3 clipped_points[MAX_TRI_CLIPPING]; | 
|---|
 | 131 |     int clipped_count; | 
|---|
 | 132 |     //create planes | 
|---|
 | 133 |     // plane v vs U points | 
|---|
 | 134 |  | 
|---|
 | 135 |     GIM_TRIANGLE_CONTACT contacts1; | 
|---|
 | 136 |  | 
|---|
 | 137 |     contacts1.m_separating_normal = m_plane; | 
|---|
 | 138 |  | 
|---|
 | 139 |  | 
|---|
 | 140 |     clipped_count = clip_triangle(other,clipped_points); | 
|---|
 | 141 |  | 
|---|
 | 142 |     if (clipped_count == 0 ) | 
|---|
 | 143 |     { | 
|---|
 | 144 |         return false;//Reject | 
|---|
 | 145 |     } | 
|---|
 | 146 |  | 
|---|
 | 147 |     //find most deep interval face1 | 
|---|
 | 148 |     contacts1.merge_points(contacts1.m_separating_normal,margin,clipped_points,clipped_count); | 
|---|
 | 149 |     if (contacts1.m_point_count == 0) return false; // too far | 
|---|
 | 150 |     //Normal pointing to this triangle | 
|---|
 | 151 |     contacts1.m_separating_normal *= -1.f; | 
|---|
 | 152 |  | 
|---|
 | 153 |  | 
|---|
 | 154 |     //Clip tri1 by tri2 edges | 
|---|
 | 155 |     GIM_TRIANGLE_CONTACT contacts2; | 
|---|
 | 156 |     contacts2.m_separating_normal = other.m_plane; | 
|---|
 | 157 |  | 
|---|
 | 158 |     clipped_count = other.clip_triangle(*this,clipped_points); | 
|---|
 | 159 |  | 
|---|
 | 160 |     if (clipped_count == 0 ) | 
|---|
 | 161 |     { | 
|---|
 | 162 |         return false;//Reject | 
|---|
 | 163 |     } | 
|---|
 | 164 |  | 
|---|
 | 165 |     //find most deep interval face1 | 
|---|
 | 166 |     contacts2.merge_points(contacts2.m_separating_normal,margin,clipped_points,clipped_count); | 
|---|
 | 167 |     if (contacts2.m_point_count == 0) return false; // too far | 
|---|
 | 168 |  | 
|---|
 | 169 |  | 
|---|
 | 170 |  | 
|---|
 | 171 |  | 
|---|
 | 172 |     ////check most dir for contacts | 
|---|
 | 173 |     if (contacts2.m_penetration_depth<contacts1.m_penetration_depth) | 
|---|
 | 174 |     { | 
|---|
 | 175 |         contacts.copy_from(contacts2); | 
|---|
 | 176 |     } | 
|---|
 | 177 |     else | 
|---|
 | 178 |     { | 
|---|
 | 179 |         contacts.copy_from(contacts1); | 
|---|
 | 180 |     } | 
|---|
 | 181 |     return true; | 
|---|
 | 182 | } | 
|---|
 | 183 |  | 
|---|
 | 184 |  | 
|---|
 | 185 |  | 
|---|
 | 186 | ///class btTriangleShapeEx: public btTriangleShape | 
|---|
 | 187 |  | 
|---|
 | 188 | bool btTriangleShapeEx::overlap_test_conservative(const btTriangleShapeEx& other) | 
|---|
 | 189 | { | 
|---|
 | 190 |     btScalar total_margin = getMargin() + other.getMargin(); | 
|---|
 | 191 |  | 
|---|
 | 192 |     btVector4 plane0; | 
|---|
 | 193 |     buildTriPlane(plane0); | 
|---|
 | 194 |     btVector4 plane1; | 
|---|
 | 195 |     other.buildTriPlane(plane1); | 
|---|
 | 196 |  | 
|---|
 | 197 |     // classify points on other triangle | 
|---|
 | 198 |     btScalar dis0 = bt_distance_point_plane(plane0,other.m_vertices1[0]) - total_margin; | 
|---|
 | 199 |  | 
|---|
 | 200 |     btScalar dis1 = bt_distance_point_plane(plane0,other.m_vertices1[1]) - total_margin; | 
|---|
 | 201 |  | 
|---|
 | 202 |     btScalar dis2 = bt_distance_point_plane(plane0,other.m_vertices1[2]) - total_margin; | 
|---|
 | 203 |  | 
|---|
 | 204 |     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false; | 
|---|
 | 205 |  | 
|---|
 | 206 |     // classify points on this triangle | 
|---|
 | 207 |     dis0 = bt_distance_point_plane(plane1,m_vertices1[0]) - total_margin; | 
|---|
 | 208 |  | 
|---|
 | 209 |     dis1 = bt_distance_point_plane(plane1,m_vertices1[1]) - total_margin; | 
|---|
 | 210 |  | 
|---|
 | 211 |     dis2 = bt_distance_point_plane(plane1,m_vertices1[2]) - total_margin; | 
|---|
 | 212 |  | 
|---|
 | 213 |     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false; | 
|---|
 | 214 |  | 
|---|
 | 215 |     return true; | 
|---|
 | 216 | } | 
|---|
 | 217 |  | 
|---|
 | 218 |  | 
|---|