3 -----------------------------------------------------------------------------
4 This source file is part of GIMPACT Library.
6 For the latest info, see http://gimpact.sourceforge.net/
8 Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
9 email: projectileman@yahoo.com
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of EITHER:
13 (1) The GNU Lesser General Public License as published by the Free
14 Software Foundation; either version 2.1 of the License, or (at
15 your option) any later version. The text of the GNU Lesser
16 General Public License is included with this library in the
17 file GIMPACT-LICENSE-LGPL.TXT.
18 (2) The BSD-style license that is included with this library in
19 the file GIMPACT-LICENSE-BSD.TXT.
20 (3) The zlib/libpng license that is included with this library in
21 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.
28 -----------------------------------------------------------------------------
31 #include "gim_contact.h"
33 #define MAX_COINCIDENT 8
35 void gim_contact_array::merge_contacts(
36 const gim_contact_array& contacts, bool normal_contact_average)
40 if (contacts.size() == 1)
42 push_back(contacts.back());
46 gim_array<GIM_RSORT_TOKEN> keycontacts(contacts.size());
47 keycontacts.resize(contacts.size(), false);
53 for (i = 0; i < contacts.size(); i++)
55 keycontacts[i].m_key = contacts[i].calc_key_contact();
56 keycontacts[i].m_value = i;
60 gim_heap_sort(keycontacts.pointer(), keycontacts.size(), GIM_RSORT_TOKEN_COMPARATOR());
64 GUINT coincident_count = 0;
65 btVector3 coincident_normals[MAX_COINCIDENT];
67 GUINT last_key = keycontacts[0].m_key;
70 push_back(contacts[keycontacts[0].m_value]);
71 GIM_CONTACT* pcontact = &back();
73 for (i = 1; i < keycontacts.size(); i++)
75 key = keycontacts[i].m_key;
76 const GIM_CONTACT* scontact = &contacts[keycontacts[i].m_value];
78 if (last_key == key) //same points
81 if (pcontact->m_depth - CONTACT_DIFF_EPSILON > scontact->m_depth) //)
83 *pcontact = *scontact;
86 else if (normal_contact_average)
88 if (btFabs(pcontact->m_depth - scontact->m_depth) < CONTACT_DIFF_EPSILON)
90 if (coincident_count < MAX_COINCIDENT)
92 coincident_normals[coincident_count] = scontact->m_normal;
101 if (normal_contact_average && coincident_count > 0)
103 pcontact->interpolate_normals(coincident_normals, coincident_count);
104 coincident_count = 0;
107 push_back(*scontact);
114 void gim_contact_array::merge_contacts_unique(const gim_contact_array& contacts)
118 if (contacts.size() == 1)
120 push_back(contacts.back());
124 GIM_CONTACT average_contact = contacts.back();
126 for (GUINT i = 1; i < contacts.size(); i++)
128 average_contact.m_point += contacts[i].m_point;
129 average_contact.m_normal += contacts[i].m_normal * contacts[i].m_depth;
133 GREAL divide_average = 1.0f / ((GREAL)contacts.size());
135 average_contact.m_point *= divide_average;
137 average_contact.m_normal *= divide_average;
139 average_contact.m_depth = average_contact.m_normal.length();
141 average_contact.m_normal /= average_contact.m_depth;