2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
This source file is part of GIMPACT Library.
|
|
|
|
|
|
|
|
For the latest info, see http://gimpact.sourceforge.net/
|
|
|
|
|
|
|
|
Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
|
|
|
|
email: projectileman@yahoo.com
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of EITHER:
|
|
|
|
(1) The GNU Lesser General Public License as published by the Free
|
|
|
|
Software Foundation; either version 2.1 of the License, or (at
|
|
|
|
your option) any later version. The text of the GNU Lesser
|
|
|
|
General Public License is included with this library in the
|
|
|
|
file GIMPACT-LICENSE-LGPL.TXT.
|
|
|
|
(2) The BSD-style license that is included with this library in
|
|
|
|
the file GIMPACT-LICENSE-BSD.TXT.
|
|
|
|
(3) The zlib/libpng license that is included with this library in
|
|
|
|
the file GIMPACT-LICENSE-ZLIB.TXT.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
|
|
|
|
GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gim_contact.h"
|
|
|
|
|
|
|
|
#define MAX_COINCIDENT 8
|
|
|
|
|
|
|
|
void gim_contact_array::merge_contacts(
|
2019-01-03 13:26:51 +00:00
|
|
|
const gim_contact_array& contacts, bool normal_contact_average)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (contacts.size() == 1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
push_back(contacts.back());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gim_array<GIM_RSORT_TOKEN> keycontacts(contacts.size());
|
2019-01-03 13:26:51 +00:00
|
|
|
keycontacts.resize(contacts.size(), false);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//fill key contacts
|
|
|
|
|
|
|
|
GUINT i;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 0; i < contacts.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
keycontacts[i].m_key = contacts[i].calc_key_contact();
|
|
|
|
keycontacts[i].m_value = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
//sort keys
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_heap_sort(keycontacts.pointer(), keycontacts.size(), GIM_RSORT_TOKEN_COMPARATOR());
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
// Merge contacts
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
GUINT coincident_count = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
btVector3 coincident_normals[MAX_COINCIDENT];
|
|
|
|
|
|
|
|
GUINT last_key = keycontacts[0].m_key;
|
|
|
|
GUINT key = 0;
|
|
|
|
|
|
|
|
push_back(contacts[keycontacts[0].m_value]);
|
2019-01-03 13:26:51 +00:00
|
|
|
GIM_CONTACT* pcontact = &back();
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = 1; i < keycontacts.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
key = keycontacts[i].m_key;
|
|
|
|
const GIM_CONTACT* scontact = &contacts[keycontacts[i].m_value];
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (last_key == key) //same points
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//merge contact
|
2019-01-03 13:26:51 +00:00
|
|
|
if (pcontact->m_depth - CONTACT_DIFF_EPSILON > scontact->m_depth) //)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
*pcontact = *scontact;
|
2019-01-03 13:26:51 +00:00
|
|
|
coincident_count = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (normal_contact_average)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (btFabs(pcontact->m_depth - scontact->m_depth) < CONTACT_DIFF_EPSILON)
|
|
|
|
{
|
|
|
|
if (coincident_count < MAX_COINCIDENT)
|
|
|
|
{
|
|
|
|
coincident_normals[coincident_count] = scontact->m_normal;
|
|
|
|
coincident_count++;
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-01-03 13:26:51 +00:00
|
|
|
{ //add new contact
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (normal_contact_average && coincident_count > 0)
|
|
|
|
{
|
|
|
|
pcontact->interpolate_normals(coincident_normals, coincident_count);
|
|
|
|
coincident_count = 0;
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
push_back(*scontact);
|
|
|
|
pcontact = &back();
|
|
|
|
}
|
2017-08-01 12:30:58 +00:00
|
|
|
last_key = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void gim_contact_array::merge_contacts_unique(const gim_contact_array& contacts)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (contacts.size() == 1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
push_back(contacts.back());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GIM_CONTACT average_contact = contacts.back();
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (GUINT i = 1; i < contacts.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
average_contact.m_point += contacts[i].m_point;
|
|
|
|
average_contact.m_normal += contacts[i].m_normal * contacts[i].m_depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
//divide
|
2019-01-03 13:26:51 +00:00
|
|
|
GREAL divide_average = 1.0f / ((GREAL)contacts.size());
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
average_contact.m_point *= divide_average;
|
|
|
|
|
|
|
|
average_contact.m_normal *= divide_average;
|
|
|
|
|
|
|
|
average_contact.m_depth = average_contact.m_normal.length();
|
|
|
|
|
|
|
|
average_contact.m_normal /= average_contact.m_depth;
|
|
|
|
}
|