File: DefaultContactManager.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (239 lines) | stat: -rw-r--r-- 8,251 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of 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.                                             *
*                                                                             *
* 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 GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
#include <sofa/component/collision/DefaultContactManager.h>
#include <sofa/core/ObjectFactory.h>




namespace sofa
{

namespace component
{

namespace collision
{

SOFA_DECL_CLASS(DefaultContactManager)

int DefaultContactManagerClass = core::RegisterObject("Default class to create reactions to the collisions")
.add< DefaultContactManager >()
.addAlias("CollisionResponse")
;

DefaultContactManager::DefaultContactManager()
: response(initData(&response, std::string("default"), "response", "contact response class"))
{
}

DefaultContactManager::~DefaultContactManager()
{
	// Contacts are now attached to the graph.
	// So they will be deleted by the DeleteVisitor
	// FIX crash on unload bug. -- J. Allard
	//clear();
}

void DefaultContactManager::clear()
{
	for (sofa::helper::vector<core::componentmodel::collision::Contact*>::iterator it=contacts.begin(); it!=contacts.end(); ++it)
	{
		(*it)->removeResponse();
		(*it)->cleanup();
		delete *it;
	}
	contacts.clear();
	contactMap.clear();
}

void DefaultContactManager::createContacts(DetectionOutputMap& outputsMap)
{
	 //serr<<"DefaultContactManager::createContacts"<<sendl;

	//outputsMap.clear();
	//for (sofa::helper::vector<core::componentmodel::collision::DetectionOutput*>::const_iterator it = outputs.begin(); it!=outputs.end(); ++it)
	//{
	//	core::componentmodel::collision::DetectionOutput* o = *it;
	//	outputsMap[std::make_pair(o->elem.first.getCollisionModel(),o->elem.second.getCollisionModel())].push_back(o);
	//}

	// Remove any inactive contacts or add any new contact
	DetectionOutputMap::iterator outputsIt = outputsMap.begin();
	std::map< std::pair<core::CollisionModel*,core::CollisionModel*>, core::componentmodel::collision::Contact* >::iterator contactIt = contactMap.begin();
	int nbContact = 0;
	//static DetectionOutputVector emptyContacts;
	while (outputsIt!=outputsMap.end() || contactIt!=contactMap.end())
	{
		if (outputsIt!=outputsMap.end() && (contactIt == contactMap.end() || outputsIt->first < contactIt->first))
		{ // new contact
			//sout << "Creation new "<<contacttype<<" contact"<<sendl;
                    core::CollisionModel* model1 = outputsIt->first.first;
                    core::CollisionModel* model2 = outputsIt->first.second;
                    std::string responseUsed = getContactResponse(model1, model2);
                    core::componentmodel::collision::Contact* contact = core::componentmodel::collision::Contact::Create(responseUsed, model1, model2, intersectionMethod);
		    if (contact == NULL) serr << "Contact "<<responseUsed<<" between " << model1->getClassName()<<" and "<<model2->getClassName() << " creation failed"<<sendl;
			else
			{
				contactMap[std::make_pair(model1, model2)] = contact;
				contact->setName(model1->getName()+std::string("-")+model2->getName());
				contact->f_printLog.setValue(this->f_printLog.getValue());
				contact->init();
				contact->setDetectionOutputs(outputsIt->second);
				++nbContact;
			}
			++outputsIt;
		}
		else if (contactIt!=contactMap.end() && (outputsIt == outputsMap.end() || contactIt->first < outputsIt->first))
		{ // inactive contact
			//sout << "Deleting inactive "<<contacttype<<" contact"<<sendl;
			if (contactIt->second->keepAlive())
			{
				contactIt->second->setDetectionOutputs(NULL);
				++nbContact;
				++contactIt;
			}
			else
			{
				std::map< std::pair<core::CollisionModel*,core::CollisionModel*>, core::componentmodel::collision::Contact* >::iterator contactIt2 = contactIt;
				++contactIt2;
				contactIt->second->removeResponse();
				contactIt->second->cleanup();
				delete contactIt->second;
				contactMap.erase(contactIt);
				contactIt = contactIt2;
			}
		}
		else
		{ // corresponding contact and outputs
			contactIt->second->setDetectionOutputs(outputsIt->second);
			++nbContact;
			++outputsIt;
			++contactIt;
		}
	}
	// now update contactVec
	contacts.clear();
	contacts.reserve(nbContact);
	contactIt = contactMap.begin();
	while (contactIt!=contactMap.end())
	{
		contacts.push_back(contactIt->second);
		++contactIt;
	}
	// compute number of contacts attached to each collision model
	std::map<core::CollisionModel*,int> nbContactsMap;
	for (unsigned int i=0;i<contacts.size();++i)
	{
		std::pair< core::CollisionModel*, core::CollisionModel* > cms = contacts[i]->getCollisionModels();
		nbContactsMap[cms.first]++;
		if (cms.second != cms.first)
			nbContactsMap[cms.second]++;
	}
	sofa::helper::vector<core::CollisionModel*> collisionModels;
	simulation::Node* context = dynamic_cast<simulation::Node*>(getContext());
	context->getTreeObjects<core::CollisionModel>(&collisionModels);
	for (unsigned int i=0;i<collisionModels.size();++i)
	{
		collisionModels[i]->setNumberOfContacts(nbContactsMap[collisionModels[i]]);
	}
}

std::string DefaultContactManager::getContactResponse(core::CollisionModel* model1, core::CollisionModel* model2)
{
    std::string responseUsed = response.getValue();
    std::string response1 = model1->getContactResponse();
    std::string response2 = model2->getContactResponse();
    if (!response1.empty()) responseUsed = response1;
    else if (!response2.empty()) responseUsed = response2;
    return responseUsed;
}

void DefaultContactManager::draw()
{
	for (sofa::helper::vector<core::componentmodel::collision::Contact*>::iterator it = contacts.begin(); it!=contacts.end(); it++)
	{
		if ((*it)!=NULL)
			(*it)->draw();
	}
}


void DefaultContactManager::removeContacts(const ContactVector &c)
{
	ContactVector::const_iterator remove_it = c.begin();
	ContactVector::const_iterator remove_itEnd = c.end();

	ContactVector::iterator it;
	ContactVector::iterator itEnd;

	ContactMap::iterator map_it;
	ContactMap::iterator map_itEnd;

	while (remove_it != remove_itEnd)
	{
		// Whole scene contacts
		it = contacts.begin();
		itEnd = contacts.end();

		while (it != itEnd)
		{
			if (*it == *remove_it)
			{
				contacts.erase(it);
				break;
			}

			++it;
		}

		// Stored contacts (keeping alive)
		map_it = contactMap.begin();
		map_itEnd = contactMap.end();

		while (map_it != map_itEnd)
		{
			if (map_it->second == *remove_it)
			{
				ContactMap::iterator erase_it = map_it;
				++map_it;
				contactMap.erase(erase_it);
			}
			else
			{
				++map_it;
			}
		}
		
		++remove_it;
	}

}

} // namespace collision

} // namespace component

} // namespace sofa