File: PMLReader.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 88,224 kB
  • ctags: 26,759
  • sloc: cpp: 151,113; ansic: 2,387; xml: 581; sh: 431; makefile: 101
file content (332 lines) | stat: -rw-r--r-- 10,575 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/******************************************************************************
*       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                             *
******************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "PMLReader.h"

#include <MultiComponent.h>

#include "PMLRigidBody.h"
#include "PMLFemForceField.h"
#include "PMLStiffSpringForceField.h"
#include "PMLInteractionForceField.h"
#include "PMLMappedBody.h"

#include "sofa/component/collision/DefaultPipeline.h"
#include "sofa/component/collision/DefaultContactManager.h"
#include "sofa/component/collision/NewProximityIntersection.h"
#include "sofa/component/collision/DefaultCollisionGroupManager.h"
#include "sofa/component/collision/BruteForceDetection.h"
#include "sofa/simulation/common/VisualVisitor.h"
#include "sofa/simulation/common/Simulation.h"
#include "sofa/simulation/common/Node.h"

using namespace sofa::component::collision;
using namespace sofa::simulation::tree;
using namespace sofa::simulation;

namespace sofa
{

namespace filemanager
{

namespace pml
{

//build sofa structure from a pml file
void PMLReader::BuildStructure(const char* filename, GNode* root){

	if (!filename) return;

	if(pm) {
		delete pm;
		pm = NULL;
	}
	pm = new PhysicalModel(filename);

	if (!pm){
		cerr<<"PML reader error : failed to load PML file "<<filename<<endl;
		return;
	}

	if (pm->getNumberOfAtoms()==0){
		cerr<<"PML reader error : PML file "<<filename<<" not valid"<<endl;
		return;
	}

	this->BuildStructure(root);
}

//build sofa structure from a physicalModel
void PMLReader::BuildStructure(PhysicalModel * model, GNode* root){

	if(pm) delete pm;
	pm = model;
	this->BuildStructure(root);
}

//really build the sofa structure
void PMLReader::BuildStructure(GNode* root){

	bodiesList.clear();

	//get the MultiComponent "Bodies"
	MultiComponent * bodies = (MultiComponent*) (pm->getExclusiveComponents()->getComponentByName("Bodies"));
	//if none, the physical model is not properly defined, so we exit...
	if(!bodies) return;

	bool collisionsExist = false;

	//get each body
	PMLBody * body;
	for (unsigned int i=0 ; i<bodies->getNumberOfSubComponents() ; i++ )
	{
		//create the under structure (mech model, topology, etc) to create the body
		body = createBody( (StructuralComponent*) bodies->getSubComponent(i), root );

		//if no problem, we put it in the scene graph
		if (body) {
			bodiesList.push_back(body);
			if (body->hasCollisions())
				collisionsExist = true;
		}
	}

	//if at least one of the bodies wants to detect contacts, we create all objects to do this
	if (collisionsExist){
		DefaultPipeline * ps = new DefaultPipeline;
		BruteForceDetection * bfd = new BruteForceDetection;
		NewProximityIntersection * mpi = new NewProximityIntersection;
		//computes the distance contact from the bounding box
		VisualComputeBBoxVisitor act;
		getSimulation()->init(root);
		root->execute(act);
		SReal dx=(act.maxBBox[0]-act.minBBox[0]);
		SReal dy=(act.maxBBox[1]-act.minBBox[1]);
		SReal dz=(act.maxBBox[2]-act.minBBox[2]);
		SReal dmax = sqrt(dx*dx + dy*dy + dz*dz);
		//maybe the ratio should be changed...
		mpi->setAlarmDistance(dmax/30);
		mpi->setContactDistance(dmax/40);
		DefaultContactManager * contactManager = new DefaultContactManager;
		DefaultCollisionGroupManager * cgm = new DefaultCollisionGroupManager;

		root->addObject(ps);
		root->addObject(bfd);
		root->addObject(mpi);
		root->addObject(contactManager);
		root->addObject(cgm);
	}

	//if there is 2 bodies with the same type and some nodes in common, we merge them
	processFusions(root);

	sofa::simulation::getSimulation()->init(root);
}

//create the body structure
//each a new body type is created, this method must be updated to take care of it.
PMLBody* PMLReader::createBody(StructuralComponent* SC, GNode * root) {

	string type = SC->getProperties()->getString("bodyType");

	GNode * child = new GNode(SC->getProperties()->getName());

	if (type == "rigid" ) {
	  root->addChild((simulation::Node*)child);
		return new PMLRigidBody(SC, child);
	}
	if (type == "FEM" ){
		root->addChild((simulation::Node*)child);
		return new PMLFemForceField(SC, child);
	}
	if (type == "stiffSpring" ){
		root->addChild((simulation::Node*)child);
		return new PMLStiffSpringForceField(SC, child);
	}
	if (type == "interaction" ){
		delete child;
		std::string name1 = SC->getProperties()->getString("body1");
		std::string name2 = SC->getProperties()->getString("body2");
		PMLBody * body1=NULL;
		PMLBody * body2=NULL;

		std::vector<PMLBody*>::iterator it = bodiesList.begin();
		while((!body1 || !body2) && it != bodiesList.end() )
		{
			if ( (*it)->getName() == name1)
				body1 = *it;
			if ( (*it)->getName() == name2)
				body2 = *it;
			it++;
		}
		if (body1 && body2)
			return new PMLInteractionForceField(SC, body1, body2, root);
	}
	if (type == "mapped"){
		std::string name1 = SC->getProperties()->getString("bodyRef");
		PMLBody * body1=NULL;
		std::vector<PMLBody*>::iterator it = bodiesList.begin();
		while( !body1 && it != bodiesList.end() )
		{
			if ( (*it)->getName() == name1)
				body1 = *it;
			it++;
		}
		if (body1){
		  body1->parentNode->addChild((simulation::Node*)child);
			return new PMLMappedBody(SC, body1, child);
		}else
			cerr<<"mapped body : no body ref named "<<name1<<" found"<<endl;
	}

	return NULL;
}


void PMLReader::processFusions(GNode * root)
{
	std::vector<PMLBody *>::iterator it1 = bodiesList.begin();
	std::vector<PMLBody *>::iterator it2 = bodiesList.begin();

	while (it1 != bodiesList.end()) {
		it2 = it1;
		it2++;
		while(it2 != bodiesList.end() ) {
			bool Fusion = false;
			if ((*it1)->isTypeOf() == (*it2)->isTypeOf() ) {
				map<unsigned int, unsigned int>::iterator ind = (*it1)->AtomsToDOFsIndexes.begin();
				while(ind != (*it1)->AtomsToDOFsIndexes.end() && !Fusion) {
					if((*it2)->AtomsToDOFsIndexes.find(ind->first) != (*it2)->AtomsToDOFsIndexes.end() ) {
						Fusion = (*it1)->FusionBody(*it2);
						if (Fusion) {
							std::vector<PMLBody *>::iterator tmp = it2;
							tmp--;
							(*it1)->parentNode->setName((*it1)->parentNode->getName() + " & "+ (*it2)->parentNode->getName() );
							root->removeChild( (simulation::Node*)(*it2)->parentNode );
							bodiesList.erase(it2);
							it2 = tmp;
						}
					}
					ind++;
				}
			}
			it2++;
		}
		it1++;
	}
}


//save the current scene as a pml filename
void PMLReader::saveAsPML(const char * filename)
{
	std::vector<PMLBody*>::iterator itb = bodiesList.begin();
	StructuralComponent * atoms = pm->getAtoms();
	Atom * atom;

	while (itb != bodiesList.end())
	{
		std::map<unsigned int, unsigned int>::iterator itm = (*itb)->AtomsToDOFsIndexes.begin();
		while(itm != (*itb)->AtomsToDOFsIndexes.end() )
		{
			atom = (Atom*) atoms->getStructureByIndex( (*itm).first );
			Vector3 pos = (*itb)->getDOF((*itm).second);
			atom->setPosition(pos[0], pos[1], pos[2]);

			itm++;
		}
		itb++;
	}
	std::ofstream outputFile(filename);
	pm->xmlPrint(outputFile);
	cout<<"fichier sauvegarde"<<endl;
}


//return a point position giving a physical model atom index
Vector3 PMLReader::getAtomPos(unsigned int atomindex)
{
  Vector3 pos;
	std::vector<PMLBody*>::iterator itb = bodiesList.begin();

	while (itb != bodiesList.end())
	{
		std::map<unsigned int, unsigned int>::iterator itm = (*itb)->AtomsToDOFsIndexes.begin();
		while(itm != (*itb)->AtomsToDOFsIndexes.end())
		{
			if ( (*itm).first == atomindex ) {
				pos = (*itb)->getDOF((*itm).second);
				return pos;
			}
			itm++;
		}
		itb++;
	}
	return Vector3();
}


//update all the physical model atoms positions
void PMLReader::updatePML()
{
	std::vector<PMLBody*>::iterator itb = bodiesList.begin();
	StructuralComponent * atoms = pm->getAtoms();
	Atom * atom;

	while (itb != bodiesList.end())
	{
		if( (*itb)->isTypeOf() != "interaction" )
		{
			if ((*itb)->isTypeOf() != "rigid" || ((PMLRigidBody*)*itb)->bodyFixed == false)
			{
				std::map<unsigned int, unsigned int>::iterator itm = (*itb)->AtomsToDOFsIndexes.begin();
				while(itm != (*itb)->AtomsToDOFsIndexes.end() )
				{
					atom = (Atom*) atoms->getStructureByIndex( (*itm).first );
					Vector3 pos = (*itb)->getDOF((*itm).second);
					atom->setPosition(pos[0], pos[1], pos[2]);

					itm++;
				}
			}
		}
		itb++;
	}
}


}
}
}