Blender V2.61 - r43446

btTriangleShapeEx.cpp

Go to the documentation of this file.
00001 
00004 /*
00005 This source file is part of GIMPACT Library.
00006 
00007 For the latest info, see http://gimpact.sourceforge.net/
00008 
00009 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
00010 email: projectileman@yahoo.com
00011 
00012 
00013 This software is provided 'as-is', without any express or implied warranty.
00014 In no event will the authors be held liable for any damages arising from the use of this software.
00015 Permission is granted to anyone to use this software for any purpose,
00016 including commercial applications, and to alter it and redistribute it freely,
00017 subject to the following restrictions:
00018 
00019 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.
00020 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00021 3. This notice may not be removed or altered from any source distribution.
00022 */
00023 
00024 #include "btTriangleShapeEx.h"
00025 
00026 
00027 
00028 void GIM_TRIANGLE_CONTACT::merge_points(const btVector4 & plane,
00029                                        btScalar margin, const btVector3 * points, int point_count)
00030 {
00031     m_point_count = 0;
00032     m_penetration_depth= -1000.0f;
00033 
00034     int point_indices[MAX_TRI_CLIPPING];
00035 
00036     int _k;
00037 
00038     for ( _k=0;_k<point_count;_k++)
00039     {
00040         btScalar _dist = - bt_distance_point_plane(plane,points[_k]) + margin;
00041 
00042         if (_dist>=0.0f)
00043         {
00044             if (_dist>m_penetration_depth)
00045             {
00046                 m_penetration_depth = _dist;
00047                 point_indices[0] = _k;
00048                 m_point_count=1;
00049             }
00050             else if ((_dist+SIMD_EPSILON)>=m_penetration_depth)
00051             {
00052                 point_indices[m_point_count] = _k;
00053                 m_point_count++;
00054             }
00055         }
00056     }
00057 
00058     for ( _k=0;_k<m_point_count;_k++)
00059     {
00060         m_points[_k] = points[point_indices[_k]];
00061     }
00062 }
00063 
00065 bool btPrimitiveTriangle::overlap_test_conservative(const btPrimitiveTriangle& other)
00066 {
00067     btScalar total_margin = m_margin + other.m_margin;
00068     // classify points on other triangle
00069     btScalar dis0 = bt_distance_point_plane(m_plane,other.m_vertices[0]) - total_margin;
00070 
00071     btScalar dis1 = bt_distance_point_plane(m_plane,other.m_vertices[1]) - total_margin;
00072 
00073     btScalar dis2 = bt_distance_point_plane(m_plane,other.m_vertices[2]) - total_margin;
00074 
00075     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false;
00076 
00077     // classify points on this triangle
00078     dis0 = bt_distance_point_plane(other.m_plane,m_vertices[0]) - total_margin;
00079 
00080     dis1 = bt_distance_point_plane(other.m_plane,m_vertices[1]) - total_margin;
00081 
00082     dis2 = bt_distance_point_plane(other.m_plane,m_vertices[2]) - total_margin;
00083 
00084     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false;
00085 
00086     return true;
00087 }
00088 
00089 int btPrimitiveTriangle::clip_triangle(btPrimitiveTriangle & other, btVector3 * clipped_points )
00090 {
00091     // edge 0
00092 
00093     btVector3 temp_points[MAX_TRI_CLIPPING];
00094 
00095 
00096     btVector4 edgeplane;
00097 
00098     get_edge_plane(0,edgeplane);
00099 
00100 
00101     int clipped_count = bt_plane_clip_triangle(
00102                             edgeplane,other.m_vertices[0],other.m_vertices[1],other.m_vertices[2],temp_points);
00103 
00104     if (clipped_count == 0) return 0;
00105 
00106     btVector3 temp_points1[MAX_TRI_CLIPPING];
00107 
00108 
00109     // edge 1
00110     get_edge_plane(1,edgeplane);
00111 
00112 
00113     clipped_count = bt_plane_clip_polygon(edgeplane,temp_points,clipped_count,temp_points1);
00114 
00115     if (clipped_count == 0) return 0;
00116 
00117     // edge 2
00118     get_edge_plane(2,edgeplane);
00119 
00120     clipped_count = bt_plane_clip_polygon(
00121                         edgeplane,temp_points1,clipped_count,clipped_points);
00122 
00123     return clipped_count;
00124 }
00125 
00126 bool btPrimitiveTriangle::find_triangle_collision_clip_method(btPrimitiveTriangle & other, GIM_TRIANGLE_CONTACT & contacts)
00127 {
00128     btScalar margin = m_margin + other.m_margin;
00129 
00130     btVector3 clipped_points[MAX_TRI_CLIPPING];
00131     int clipped_count;
00132     //create planes
00133     // plane v vs U points
00134 
00135     GIM_TRIANGLE_CONTACT contacts1;
00136 
00137     contacts1.m_separating_normal = m_plane;
00138 
00139 
00140     clipped_count = clip_triangle(other,clipped_points);
00141 
00142     if (clipped_count == 0 )
00143     {
00144         return false;//Reject
00145     }
00146 
00147     //find most deep interval face1
00148     contacts1.merge_points(contacts1.m_separating_normal,margin,clipped_points,clipped_count);
00149     if (contacts1.m_point_count == 0) return false; // too far
00150     //Normal pointing to this triangle
00151     contacts1.m_separating_normal *= -1.f;
00152 
00153 
00154     //Clip tri1 by tri2 edges
00155     GIM_TRIANGLE_CONTACT contacts2;
00156     contacts2.m_separating_normal = other.m_plane;
00157 
00158     clipped_count = other.clip_triangle(*this,clipped_points);
00159 
00160     if (clipped_count == 0 )
00161     {
00162         return false;//Reject
00163     }
00164 
00165     //find most deep interval face1
00166     contacts2.merge_points(contacts2.m_separating_normal,margin,clipped_points,clipped_count);
00167     if (contacts2.m_point_count == 0) return false; // too far
00168 
00169 
00170 
00171 
00173     if (contacts2.m_penetration_depth<contacts1.m_penetration_depth)
00174     {
00175         contacts.copy_from(contacts2);
00176     }
00177     else
00178     {
00179         contacts.copy_from(contacts1);
00180     }
00181     return true;
00182 }
00183 
00184 
00185 
00187 
00188 bool btTriangleShapeEx::overlap_test_conservative(const btTriangleShapeEx& other)
00189 {
00190     btScalar total_margin = getMargin() + other.getMargin();
00191 
00192     btVector4 plane0;
00193     buildTriPlane(plane0);
00194     btVector4 plane1;
00195     other.buildTriPlane(plane1);
00196 
00197     // classify points on other triangle
00198     btScalar dis0 = bt_distance_point_plane(plane0,other.m_vertices1[0]) - total_margin;
00199 
00200     btScalar dis1 = bt_distance_point_plane(plane0,other.m_vertices1[1]) - total_margin;
00201 
00202     btScalar dis2 = bt_distance_point_plane(plane0,other.m_vertices1[2]) - total_margin;
00203 
00204     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false;
00205 
00206     // classify points on this triangle
00207     dis0 = bt_distance_point_plane(plane1,m_vertices1[0]) - total_margin;
00208 
00209     dis1 = bt_distance_point_plane(plane1,m_vertices1[1]) - total_margin;
00210 
00211     dis2 = bt_distance_point_plane(plane1,m_vertices1[2]) - total_margin;
00212 
00213     if (dis0>0.0f&&dis1>0.0f&&dis2>0.0f) return false;
00214 
00215     return true;
00216 }
00217 
00218