3 This source file is part of GIMPACT Library.
5 For the latest info, see http://gimpact.sourceforge.net/
7 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
8 email: projectileman@yahoo.com
11 This software is provided 'as-is', without any express or implied warranty.
12 In no event will the authors be held liable for any damages arising from the use of this software.
13 Permission is granted to anyone to use this software for any purpose,
14 including commercial applications, and to alter it and redistribute it freely,
15 subject to the following restrictions:
17 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.
18 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "btContactProcessing.h"
23 #define MAX_COINCIDENT 8
25 struct CONTACT_KEY_TOKEN
33 CONTACT_KEY_TOKEN(unsigned int key,int token)
39 CONTACT_KEY_TOKEN(const CONTACT_KEY_TOKEN& rtoken)
42 m_value = rtoken.m_value;
45 inline bool operator <(const CONTACT_KEY_TOKEN& other) const
47 return (m_key < other.m_key);
50 inline bool operator >(const CONTACT_KEY_TOKEN& other) const
52 return (m_key > other.m_key);
57 class CONTACT_KEY_TOKEN_COMP
61 bool operator() ( const CONTACT_KEY_TOKEN& a, const CONTACT_KEY_TOKEN& b ) const
68 void btContactArray::merge_contacts(
69 const btContactArray & contacts, bool normal_contact_average)
74 if(contacts.size()==0) return;
77 if(contacts.size()==1)
79 push_back(contacts[0]);
83 btAlignedObjectArray<CONTACT_KEY_TOKEN> keycontacts;
85 keycontacts.reserve(contacts.size());
89 for ( i = 0;i<contacts.size() ;i++ )
91 keycontacts.push_back(CONTACT_KEY_TOKEN(contacts[i].calc_key_contact(),i));
95 keycontacts.quickSort(CONTACT_KEY_TOKEN_COMP());
98 int coincident_count=0;
99 btVector3 coincident_normals[MAX_COINCIDENT];
101 unsigned int last_key = keycontacts[0].m_key;
102 unsigned int key = 0;
104 push_back(contacts[keycontacts[0].m_value]);
106 GIM_CONTACT * pcontact = &(*this)[0];
108 for( i=1;i<keycontacts.size();i++)
110 key = keycontacts[i].m_key;
111 const GIM_CONTACT * scontact = &contacts[keycontacts[i].m_value];
113 if(last_key == key)//same points
116 if(pcontact->m_depth - CONTACT_DIFF_EPSILON > scontact->m_depth)//)
118 *pcontact = *scontact;
119 coincident_count = 0;
121 else if(normal_contact_average)
123 if(btFabs(pcontact->m_depth - scontact->m_depth)<CONTACT_DIFF_EPSILON)
125 if(coincident_count<MAX_COINCIDENT)
127 coincident_normals[coincident_count] = scontact->m_normal;
136 if(normal_contact_average && coincident_count>0)
138 pcontact->interpolate_normals(coincident_normals,coincident_count);
139 coincident_count = 0;
142 push_back(*scontact);
143 pcontact = &(*this)[this->size()-1];
149 void btContactArray::merge_contacts_unique(const btContactArray & contacts)
153 if(contacts.size()==0) return;
155 if(contacts.size()==1)
157 push_back(contacts[0]);
161 GIM_CONTACT average_contact = contacts[0];
163 for (int i=1;i<contacts.size() ;i++ )
165 average_contact.m_point += contacts[i].m_point;
166 average_contact.m_normal += contacts[i].m_normal * contacts[i].m_depth;
170 btScalar divide_average = 1.0f/((btScalar)contacts.size());
172 average_contact.m_point *= divide_average;
174 average_contact.m_normal *= divide_average;
176 average_contact.m_depth = average_contact.m_normal.length();
178 average_contact.m_normal /= average_contact.m_depth;