00001 00002 /* 00003 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 00004 * 00005 * This software is provided 'as-is', without any express or implied 00006 * warranty. In no event will the authors be held liable for any damages 00007 * arising from the use of this software. 00008 * Permission is granted to anyone to use this software for any purpose, 00009 * including commercial applications, and to alter it and redistribute it 00010 * freely, subject to the following restrictions: 00011 * 1. The origin of this software must not be misrepresented; you must not 00012 * claim that you wrote the original software. If you use this software 00013 * in a product, an acknowledgment in the product documentation would be 00014 * appreciated but is not required. 00015 * 2. Altered source versions must be plainly marked as such, and must not be 00016 * misrepresented as being the original software. 00017 * 3. This notice may not be removed or altered from any source distribution. 00018 */ 00019 00020 #ifndef B2_DISTANCE_H 00021 #define B2_DISTANCE_H 00022 00023 #include <Box2D/Common/b2Math.h> 00024 00025 class b2Shape; 00026 00029 struct b2DistanceProxy 00030 { 00031 b2DistanceProxy() : m_vertices(NULL), m_count(0), m_radius(0.0f) {} 00032 00035 void Set(const b2Shape* shape, int32 index); 00036 00038 int32 GetSupport(const b2Vec2& d) const; 00039 00041 const b2Vec2& GetSupportVertex(const b2Vec2& d) const; 00042 00044 int32 GetVertexCount() const; 00045 00047 const b2Vec2& GetVertex(int32 index) const; 00048 00049 b2Vec2 m_buffer[2]; 00050 const b2Vec2* m_vertices; 00051 int32 m_count; 00052 float32 m_radius; 00053 }; 00054 00057 struct b2SimplexCache 00058 { 00059 float32 metric; 00060 uint16 count; 00061 uint8 indexA[3]; 00062 uint8 indexB[3]; 00063 }; 00064 00068 struct b2DistanceInput 00069 { 00070 b2DistanceProxy proxyA; 00071 b2DistanceProxy proxyB; 00072 b2Transform transformA; 00073 b2Transform transformB; 00074 bool useRadii; 00075 }; 00076 00078 struct b2DistanceOutput 00079 { 00080 b2Vec2 pointA; 00081 b2Vec2 pointB; 00082 float32 distance; 00083 int32 iterations; 00084 }; 00085 00089 void b2Distance(b2DistanceOutput* output, 00090 b2SimplexCache* cache, 00091 const b2DistanceInput* input); 00092 00093 00095 00096 inline int32 b2DistanceProxy::GetVertexCount() const 00097 { 00098 return m_count; 00099 } 00100 00101 inline const b2Vec2& b2DistanceProxy::GetVertex(int32 index) const 00102 { 00103 b2Assert(0 <= index && index < m_count); 00104 return m_vertices[index]; 00105 } 00106 00107 inline int32 b2DistanceProxy::GetSupport(const b2Vec2& d) const 00108 { 00109 int32 bestIndex = 0; 00110 float32 bestValue = b2Dot(m_vertices[0], d); 00111 for (int32 i = 1; i < m_count; ++i) 00112 { 00113 float32 value = b2Dot(m_vertices[i], d); 00114 if (value > bestValue) 00115 { 00116 bestIndex = i; 00117 bestValue = value; 00118 } 00119 } 00120 00121 return bestIndex; 00122 } 00123 00124 inline const b2Vec2& b2DistanceProxy::GetSupportVertex(const b2Vec2& d) const 00125 { 00126 int32 bestIndex = 0; 00127 float32 bestValue = b2Dot(m_vertices[0], d); 00128 for (int32 i = 1; i < m_count; ++i) 00129 { 00130 float32 value = b2Dot(m_vertices[i], d); 00131 if (value > bestValue) 00132 { 00133 bestIndex = i; 00134 bestValue = value; 00135 } 00136 } 00137 00138 return m_vertices[bestIndex]; 00139 } 00140 00141 #endif