gim_contact.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of GIMPACT Library.
  4. For the latest info, see http://gimpact.sourceforge.net/
  5. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  6. email: projectileman@yahoo.com
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of EITHER:
  9. (1) The GNU Lesser General Public License as published by the Free
  10. Software Foundation; either version 2.1 of the License, or (at
  11. your option) any later version. The text of the GNU Lesser
  12. General Public License is included with this library in the
  13. file GIMPACT-LICENSE-LGPL.TXT.
  14. (2) The BSD-style license that is included with this library in
  15. the file GIMPACT-LICENSE-BSD.TXT.
  16. (3) The zlib/libpng license that is included with this library in
  17. the file GIMPACT-LICENSE-ZLIB.TXT.
  18. This library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  21. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "gim_contact.h"
  25. #define MAX_COINCIDENT 8
  26. void gim_contact_array::merge_contacts(
  27. const gim_contact_array& contacts, bool normal_contact_average)
  28. {
  29. clear();
  30. if (contacts.size() == 1)
  31. {
  32. push_back(contacts.back());
  33. return;
  34. }
  35. gim_array<GIM_RSORT_TOKEN> keycontacts(contacts.size());
  36. keycontacts.resize(contacts.size(), false);
  37. //fill key contacts
  38. GUINT i;
  39. for (i = 0; i < contacts.size(); i++)
  40. {
  41. keycontacts[i].m_key = contacts[i].calc_key_contact();
  42. keycontacts[i].m_value = i;
  43. }
  44. //sort keys
  45. gim_heap_sort(keycontacts.pointer(), keycontacts.size(), GIM_RSORT_TOKEN_COMPARATOR());
  46. // Merge contacts
  47. GUINT coincident_count = 0;
  48. btVector3 coincident_normals[MAX_COINCIDENT];
  49. GUINT last_key = keycontacts[0].m_key;
  50. GUINT key = 0;
  51. push_back(contacts[keycontacts[0].m_value]);
  52. GIM_CONTACT* pcontact = &back();
  53. for (i = 1; i < keycontacts.size(); i++)
  54. {
  55. key = keycontacts[i].m_key;
  56. const GIM_CONTACT* scontact = &contacts[keycontacts[i].m_value];
  57. if (last_key == key) //same points
  58. {
  59. //merge contact
  60. if (pcontact->m_depth - CONTACT_DIFF_EPSILON > scontact->m_depth) //)
  61. {
  62. *pcontact = *scontact;
  63. coincident_count = 0;
  64. }
  65. else if (normal_contact_average)
  66. {
  67. if (btFabs(pcontact->m_depth - scontact->m_depth) < CONTACT_DIFF_EPSILON)
  68. {
  69. if (coincident_count < MAX_COINCIDENT)
  70. {
  71. coincident_normals[coincident_count] = scontact->m_normal;
  72. coincident_count++;
  73. }
  74. }
  75. }
  76. }
  77. else
  78. { //add new contact
  79. if (normal_contact_average && coincident_count > 0)
  80. {
  81. pcontact->interpolate_normals(coincident_normals, coincident_count);
  82. coincident_count = 0;
  83. }
  84. push_back(*scontact);
  85. pcontact = &back();
  86. }
  87. last_key = key;
  88. }
  89. }
  90. void gim_contact_array::merge_contacts_unique(const gim_contact_array& contacts)
  91. {
  92. clear();
  93. if (contacts.size() == 1)
  94. {
  95. push_back(contacts.back());
  96. return;
  97. }
  98. GIM_CONTACT average_contact = contacts.back();
  99. for (GUINT i = 1; i < contacts.size(); i++)
  100. {
  101. average_contact.m_point += contacts[i].m_point;
  102. average_contact.m_normal += contacts[i].m_normal * contacts[i].m_depth;
  103. }
  104. //divide
  105. GREAL divide_average = 1.0f / ((GREAL)contacts.size());
  106. average_contact.m_point *= divide_average;
  107. average_contact.m_normal *= divide_average;
  108. average_contact.m_depth = average_contact.m_normal.length();
  109. average_contact.m_normal /= average_contact.m_depth;
  110. }