btContactProcessing.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. This source file is part of GIMPACT Library.
  3. For the latest info, see http://gimpact.sourceforge.net/
  4. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  5. email: projectileman@yahoo.com
  6. This software is provided 'as-is', without any express or implied warranty.
  7. In no event will the authors be held liable for any damages arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it freely,
  10. subject to the following restrictions:
  11. 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.
  12. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. 3. This notice may not be removed or altered from any source distribution.
  14. */
  15. #include "btContactProcessing.h"
  16. #define MAX_COINCIDENT 8
  17. struct CONTACT_KEY_TOKEN
  18. {
  19. unsigned int m_key;
  20. int m_value;
  21. CONTACT_KEY_TOKEN()
  22. {
  23. }
  24. CONTACT_KEY_TOKEN(unsigned int key,int token)
  25. {
  26. m_key = key;
  27. m_value = token;
  28. }
  29. CONTACT_KEY_TOKEN(const CONTACT_KEY_TOKEN& rtoken)
  30. {
  31. m_key = rtoken.m_key;
  32. m_value = rtoken.m_value;
  33. }
  34. inline bool operator <(const CONTACT_KEY_TOKEN& other) const
  35. {
  36. return (m_key < other.m_key);
  37. }
  38. inline bool operator >(const CONTACT_KEY_TOKEN& other) const
  39. {
  40. return (m_key > other.m_key);
  41. }
  42. };
  43. class CONTACT_KEY_TOKEN_COMP
  44. {
  45. public:
  46. bool operator() ( const CONTACT_KEY_TOKEN& a, const CONTACT_KEY_TOKEN& b ) const
  47. {
  48. return ( a < b );
  49. }
  50. };
  51. void btContactArray::merge_contacts(
  52. const btContactArray & contacts, bool normal_contact_average)
  53. {
  54. clear();
  55. int i;
  56. if(contacts.size()==0) return;
  57. if(contacts.size()==1)
  58. {
  59. push_back(contacts[0]);
  60. return;
  61. }
  62. btAlignedObjectArray<CONTACT_KEY_TOKEN> keycontacts;
  63. keycontacts.reserve(contacts.size());
  64. //fill key contacts
  65. for ( i = 0;i<contacts.size() ;i++ )
  66. {
  67. keycontacts.push_back(CONTACT_KEY_TOKEN(contacts[i].calc_key_contact(),i));
  68. }
  69. //sort keys
  70. keycontacts.quickSort(CONTACT_KEY_TOKEN_COMP());
  71. // Merge contacts
  72. int coincident_count=0;
  73. btVector3 coincident_normals[MAX_COINCIDENT];
  74. unsigned int last_key = keycontacts[0].m_key;
  75. unsigned int key = 0;
  76. push_back(contacts[keycontacts[0].m_value]);
  77. GIM_CONTACT * pcontact = &(*this)[0];
  78. for( i=1;i<keycontacts.size();i++)
  79. {
  80. key = keycontacts[i].m_key;
  81. const GIM_CONTACT * scontact = &contacts[keycontacts[i].m_value];
  82. if(last_key == key)//same points
  83. {
  84. //merge contact
  85. if(pcontact->m_depth - CONTACT_DIFF_EPSILON > scontact->m_depth)//)
  86. {
  87. *pcontact = *scontact;
  88. coincident_count = 0;
  89. }
  90. else if(normal_contact_average)
  91. {
  92. if(btFabs(pcontact->m_depth - scontact->m_depth)<CONTACT_DIFF_EPSILON)
  93. {
  94. if(coincident_count<MAX_COINCIDENT)
  95. {
  96. coincident_normals[coincident_count] = scontact->m_normal;
  97. coincident_count++;
  98. }
  99. }
  100. }
  101. }
  102. else
  103. {//add new contact
  104. if(normal_contact_average && coincident_count>0)
  105. {
  106. pcontact->interpolate_normals(coincident_normals,coincident_count);
  107. coincident_count = 0;
  108. }
  109. push_back(*scontact);
  110. pcontact = &(*this)[this->size()-1];
  111. }
  112. last_key = key;
  113. }
  114. }
  115. void btContactArray::merge_contacts_unique(const btContactArray & contacts)
  116. {
  117. clear();
  118. if(contacts.size()==0) return;
  119. if(contacts.size()==1)
  120. {
  121. push_back(contacts[0]);
  122. return;
  123. }
  124. GIM_CONTACT average_contact = contacts[0];
  125. for (int i=1;i<contacts.size() ;i++ )
  126. {
  127. average_contact.m_point += contacts[i].m_point;
  128. average_contact.m_normal += contacts[i].m_normal * contacts[i].m_depth;
  129. }
  130. //divide
  131. btScalar divide_average = 1.0f/((btScalar)contacts.size());
  132. average_contact.m_point *= divide_average;
  133. average_contact.m_normal *= divide_average;
  134. average_contact.m_depth = average_contact.m_normal.length();
  135. average_contact.m_normal /= average_contact.m_depth;
  136. }