gim_contact.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef GIM_CONTACT_H_INCLUDED
  2. #define GIM_CONTACT_H_INCLUDED
  3. /*! \file gim_contact.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. -----------------------------------------------------------------------------
  8. This source file is part of GIMPACT Library.
  9. For the latest info, see http://gimpact.sourceforge.net/
  10. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  11. email: projectileman@yahoo.com
  12. This library is free software; you can redistribute it and/or
  13. modify it under the terms of EITHER:
  14. (1) The GNU Lesser General Public License as published by the Free
  15. Software Foundation; either version 2.1 of the License, or (at
  16. your option) any later version. The text of the GNU Lesser
  17. General Public License is included with this library in the
  18. file GIMPACT-LICENSE-LGPL.TXT.
  19. (2) The BSD-style license that is included with this library in
  20. the file GIMPACT-LICENSE-BSD.TXT.
  21. (3) The zlib/libpng license that is included with this library in
  22. the file GIMPACT-LICENSE-ZLIB.TXT.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  26. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  27. -----------------------------------------------------------------------------
  28. */
  29. #include "gim_geometry.h"
  30. #include "gim_radixsort.h"
  31. #include "gim_array.h"
  32. /**
  33. Configuration var for applying interpolation of contact normals
  34. */
  35. #define NORMAL_CONTACT_AVERAGE 1
  36. #define CONTACT_DIFF_EPSILON 0.00001f
  37. /// Structure for collision results
  38. ///Functions for managing and sorting contacts resulting from a collision query.
  39. ///Contact lists must be create by calling \ref GIM_CREATE_CONTACT_LIST
  40. ///After querys, contact lists must be destroy by calling \ref GIM_DYNARRAY_DESTROY
  41. ///Contacts can be merge for avoid duplicate results by calling \ref gim_merge_contacts
  42. class GIM_CONTACT
  43. {
  44. public:
  45. btVector3 m_point;
  46. btVector3 m_normal;
  47. GREAL m_depth;//Positive value indicates interpenetration
  48. GREAL m_distance;//Padding not for use
  49. GUINT m_feature1;//Face number
  50. GUINT m_feature2;//Face number
  51. public:
  52. GIM_CONTACT()
  53. {
  54. }
  55. GIM_CONTACT(const GIM_CONTACT & contact):
  56. m_point(contact.m_point),
  57. m_normal(contact.m_normal),
  58. m_depth(contact.m_depth),
  59. m_feature1(contact.m_feature1),
  60. m_feature2(contact.m_feature2)
  61. {
  62. m_point = contact.m_point;
  63. m_normal = contact.m_normal;
  64. m_depth = contact.m_depth;
  65. m_feature1 = contact.m_feature1;
  66. m_feature2 = contact.m_feature2;
  67. }
  68. GIM_CONTACT(const btVector3 &point,const btVector3 & normal,
  69. GREAL depth, GUINT feature1, GUINT feature2):
  70. m_point(point),
  71. m_normal(normal),
  72. m_depth(depth),
  73. m_feature1(feature1),
  74. m_feature2(feature2)
  75. {
  76. }
  77. //! Calcs key for coord classification
  78. SIMD_FORCE_INLINE GUINT calc_key_contact() const
  79. {
  80. GINT _coords[] = {
  81. (GINT)(m_point[0]*1000.0f+1.0f),
  82. (GINT)(m_point[1]*1333.0f),
  83. (GINT)(m_point[2]*2133.0f+3.0f)};
  84. GUINT _hash=0;
  85. GUINT *_uitmp = (GUINT *)(&_coords[0]);
  86. _hash = *_uitmp;
  87. _uitmp++;
  88. _hash += (*_uitmp)<<4;
  89. _uitmp++;
  90. _hash += (*_uitmp)<<8;
  91. return _hash;
  92. }
  93. SIMD_FORCE_INLINE void interpolate_normals( btVector3 * normals,GUINT normal_count)
  94. {
  95. btVector3 vec_sum(m_normal);
  96. for(GUINT i=0;i<normal_count;i++)
  97. {
  98. vec_sum += normals[i];
  99. }
  100. GREAL vec_sum_len = vec_sum.length2();
  101. if(vec_sum_len <CONTACT_DIFF_EPSILON) return;
  102. GIM_INV_SQRT(vec_sum_len,vec_sum_len); // 1/sqrt(vec_sum_len)
  103. m_normal = vec_sum*vec_sum_len;
  104. }
  105. };
  106. class gim_contact_array:public gim_array<GIM_CONTACT>
  107. {
  108. public:
  109. gim_contact_array():gim_array<GIM_CONTACT>(64)
  110. {
  111. }
  112. SIMD_FORCE_INLINE void push_contact(const btVector3 &point,const btVector3 & normal,
  113. GREAL depth, GUINT feature1, GUINT feature2)
  114. {
  115. push_back_mem();
  116. GIM_CONTACT & newele = back();
  117. newele.m_point = point;
  118. newele.m_normal = normal;
  119. newele.m_depth = depth;
  120. newele.m_feature1 = feature1;
  121. newele.m_feature2 = feature2;
  122. }
  123. SIMD_FORCE_INLINE void push_triangle_contacts(
  124. const GIM_TRIANGLE_CONTACT_DATA & tricontact,
  125. GUINT feature1,GUINT feature2)
  126. {
  127. for(GUINT i = 0;i<tricontact.m_point_count ;i++ )
  128. {
  129. push_back_mem();
  130. GIM_CONTACT & newele = back();
  131. newele.m_point = tricontact.m_points[i];
  132. newele.m_normal = tricontact.m_separating_normal;
  133. newele.m_depth = tricontact.m_penetration_depth;
  134. newele.m_feature1 = feature1;
  135. newele.m_feature2 = feature2;
  136. }
  137. }
  138. void merge_contacts(const gim_contact_array & contacts, bool normal_contact_average = true);
  139. void merge_contacts_unique(const gim_contact_array & contacts);
  140. };
  141. #endif // GIM_CONTACT_H_INCLUDED