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