Bullet Collision Detection & Physics Library
gim_contact.h
Go to the documentation of this file.
1 #ifndef GIM_CONTACT_H_INCLUDED
2 #define GIM_CONTACT_H_INCLUDED
3 
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 
43 #define NORMAL_CONTACT_AVERAGE 1
44 #define CONTACT_DIFF_EPSILON 0.00001f
45 
51 class GIM_CONTACT
52 {
53 public:
56  GREAL m_depth;//Positive value indicates interpenetration
57  GREAL m_distance;//Padding not for use
58  GUINT m_feature1;//Face number
59  GUINT m_feature2;//Face number
60 public:
62  {
63  }
64 
65  GIM_CONTACT(const GIM_CONTACT & contact):
66  m_point(contact.m_point),
67  m_normal(contact.m_normal),
68  m_depth(contact.m_depth),
69  m_feature1(contact.m_feature1),
70  m_feature2(contact.m_feature2)
71  {
72  m_point = contact.m_point;
73  m_normal = contact.m_normal;
74  m_depth = contact.m_depth;
75  m_feature1 = contact.m_feature1;
76  m_feature2 = contact.m_feature2;
77  }
78 
79  GIM_CONTACT(const btVector3 &point,const btVector3 & normal,
80  GREAL depth, GUINT feature1, GUINT feature2):
81  m_point(point),
82  m_normal(normal),
83  m_depth(depth),
84  m_feature1(feature1),
85  m_feature2(feature2)
86  {
87  }
88 
91  {
92  GINT _coords[] = {
93  (GINT)(m_point[0]*1000.0f+1.0f),
94  (GINT)(m_point[1]*1333.0f),
95  (GINT)(m_point[2]*2133.0f+3.0f)};
96  GUINT _hash=0;
97  GUINT *_uitmp = (GUINT *)(&_coords[0]);
98  _hash = *_uitmp;
99  _uitmp++;
100  _hash += (*_uitmp)<<4;
101  _uitmp++;
102  _hash += (*_uitmp)<<8;
103  return _hash;
104  }
105 
106  SIMD_FORCE_INLINE void interpolate_normals( btVector3 * normals,GUINT normal_count)
107  {
108  btVector3 vec_sum(m_normal);
109  for(GUINT i=0;i<normal_count;i++)
110  {
111  vec_sum += normals[i];
112  }
113 
114  GREAL vec_sum_len = vec_sum.length2();
115  if(vec_sum_len <CONTACT_DIFF_EPSILON) return;
116 
117  GIM_INV_SQRT(vec_sum_len,vec_sum_len); // 1/sqrt(vec_sum_len)
118 
119  m_normal = vec_sum*vec_sum_len;
120  }
121 
122 };
123 
124 
125 class gim_contact_array:public gim_array<GIM_CONTACT>
126 {
127 public:
129  {
130  }
131 
132  SIMD_FORCE_INLINE void push_contact(const btVector3 &point,const btVector3 & normal,
133  GREAL depth, GUINT feature1, GUINT feature2)
134  {
135  push_back_mem();
136  GIM_CONTACT & newele = back();
137  newele.m_point = point;
138  newele.m_normal = normal;
139  newele.m_depth = depth;
140  newele.m_feature1 = feature1;
141  newele.m_feature2 = feature2;
142  }
143 
145  const GIM_TRIANGLE_CONTACT_DATA & tricontact,
146  GUINT feature1,GUINT feature2)
147  {
148  for(GUINT i = 0;i<tricontact.m_point_count ;i++ )
149  {
150  push_back_mem();
151  GIM_CONTACT & newele = back();
152  newele.m_point = tricontact.m_points[i];
153  newele.m_normal = tricontact.m_separating_normal;
154  newele.m_depth = tricontact.m_penetration_depth;
155  newele.m_feature1 = feature1;
156  newele.m_feature2 = feature2;
157  }
158  }
159 
160  void merge_contacts(const gim_contact_array & contacts, bool normal_contact_average = true);
161  void merge_contacts_unique(const gim_contact_array & contacts);
162 };
163 
164 #endif // GIM_CONTACT_H_INCLUDED
btVector3 m_points[MAX_TRI_CLIPPING]
btVector3 m_normal
GREAL m_depth
Definition: gim_contact.h:56
#define GIM_INV_SQRT(va, isva)
Definition: gim_math.h:119
void push_back_mem()
Simply increase the m_size, doesn't call the new element constructor.
Definition: gim_array.h:222
#define CONTACT_DIFF_EPSILON
Definition: gim_contact.h:44
GIM_CONTACT & back()
Definition: gim_array.h:198
void push_contact(const btVector3 &point, const btVector3 &normal, GREAL depth, GUINT feature1, GUINT feature2)
Definition: gim_contact.h:132
#define SIMD_FORCE_INLINE
Definition: btScalar.h:58
void merge_contacts_unique(const gim_contact_array &contacts)
void interpolate_normals(btVector3 *normals, GUINT normal_count)
Definition: gim_contact.h:106
GIM_CONTACT(const GIM_CONTACT &contact)
Definition: gim_contact.h:65
#define GREAL
Definition: gim_math.h:39
GUINT m_feature2
Definition: gim_contact.h:59
void merge_contacts(const gim_contact_array &contacts, bool normal_contact_average=true)
Definition: gim_contact.cpp:35
#define GINT
Definition: gim_math.h:41
Very simple array container with fast access and simd memory.
Definition: gim_array.h:43
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:257
Structure for collision.
GREAL m_distance
Definition: gim_contact.h:57
GUINT m_feature1
Definition: gim_contact.h:58
GIM_CONTACT(const btVector3 &point, const btVector3 &normal, GREAL depth, GUINT feature1, GUINT feature2)
Definition: gim_contact.h:79
#define GUINT
Definition: gim_math.h:42
The GIM_CONTACT is an internal GIMPACT structure, similar to btManifoldPoint.
GUINT calc_key_contact() const
Calcs key for coord classification.
Definition: gim_contact.h:90
void push_triangle_contacts(const GIM_TRIANGLE_CONTACT_DATA &tricontact, GUINT feature1, GUINT feature2)
Definition: gim_contact.h:144