File: Simulation.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 (351 lines) | stat: -rw-r--r-- 12,366 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/******************************************************************************
*       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/simulation/common/Simulation.h>
#include <sofa/simulation/common/PrintVisitor.h>
#include <sofa/simulation/common/FindByTypeVisitor.h>
#include <sofa/simulation/common/ExportGnuplotVisitor.h>
#include <sofa/simulation/common/InitVisitor.h>
#include <sofa/simulation/common/InstrumentVisitor.h>
#include <sofa/simulation/common/AnimateVisitor.h>
#include <sofa/simulation/common/MechanicalVisitor.h>
#include <sofa/simulation/common/CleanupVisitor.h>
#include <sofa/simulation/common/CollisionVisitor.h>
#include <sofa/simulation/common/UpdateContextVisitor.h>
#include <sofa/simulation/common/UpdateMappingVisitor.h>
#include <sofa/simulation/common/ResetVisitor.h>
#include <sofa/simulation/common/VisualVisitor.h>
#include <sofa/simulation/common/ExportOBJVisitor.h>
#include <sofa/simulation/common/WriteStateVisitor.h>
#include <sofa/simulation/common/XMLPrintVisitor.h>
#include <sofa/simulation/common/PropagateEventVisitor.h>
#include <sofa/simulation/common/BehaviorUpdatePositionVisitor.h>
#include <sofa/simulation/common/AnimateBeginEvent.h>
#include <sofa/simulation/common/AnimateEndEvent.h>
#include <sofa/simulation/common/UpdateMappingEndEvent.h>
#include <sofa/core/ObjectFactory.h>
#include <fstream>
#include <string.h>
namespace sofa
{

	namespace simulation
	{


			using namespace sofa::defaulttype;
Simulation::Simulation()
: numMechSteps( initData(&numMechSteps,(unsigned) 1,"numMechSteps","Number of mechanical steps within one update step. If the update time step is dt, the mechanical time step is dt/numMechSteps.") ),
gnuplotDirectory( initData(&gnuplotDirectory,std::string(""),"gnuplotDirectory","Directory where the gnuplot files will be saved")),
instrumentInUse( initData( &instrumentInUse, -1, "instrumentinuse", "Numero of the instrument currently used"))
{}

Simulation::~Simulation(){
  if (Simulation::theSimulation == this) setSimulation( NULL );
}

/// The (unique) simulation which controls the scene
	                Simulation* Simulation::theSimulation = NULL;

			void setSimulation ( Simulation* s )
			{
			  Simulation::theSimulation = s;
			}

			Simulation* getSimulation()
			{
                          return Simulation::theSimulation;
			}

			/// Print all object in the graph
			void Simulation::print ( Node* root )
			{
				if ( !root ) return;
				root->execute<PrintVisitor>();
			}

/// Print all object in the graph
          void Simulation::printXML ( Node* root, const char* fileName, bool compact )
			{
				if ( !root ) return;
				if ( fileName!=NULL )
				{
					std::ofstream out ( fileName );
					XMLPrintVisitor print ( out,compact );
					root->execute ( print );
				}
				else
				{
					XMLPrintVisitor print ( std::cout,compact );
					root->execute ( print );
				}
			}

/// Initialize the scene.
			void Simulation::init ( Node* root )
			{
				//cerr<<"Simulation::init"<<endl;
				setContext( root->getContext());
				if ( !root ) return;
				root->execute<InitVisitor>();
				// Save reset state for later uses in reset()
				root->execute<MechanicalPropagatePositionAndVelocityVisitor>();
				root->execute<MechanicalPropagateFreePositionVisitor>();
				root->execute<StoreResetStateVisitor>();

				//Get the list of instruments present in the scene graph
				getInstruments(root);
			}

			void Simulation::getInstruments( Node *node)
			{
			  InstrumentVisitor fetchInstrument;
			  fetchInstrument.execute(node);
			  instruments = fetchInstrument.getInstruments();
			}

/// Execute one timestep. If dt is 0, the dt parameter in the graph will be used
			void Simulation::animate ( Node* root, double dt )
			{
				if ( !root ) return;
				if ( root->getMultiThreadSimulation() )
					return;

#ifdef DUMP_VISITOR_INFO
				simulation::Visitor::printComment(std::string("Begin Step"));
#endif
				{
					AnimateBeginEvent ev ( dt );
					PropagateEventVisitor act ( &ev );
					root->execute ( act );
				}

				//std::cout << "animate\n";
				double startTime = root->getTime();
				double mechanicalDt = dt/numMechSteps.getValue();
				//double nextTime = root->getTime() + root->getDt();

				// CHANGE to support MasterSolvers : CollisionVisitor is now activated within AnimateVisitor
				//root->execute<CollisionVisitor>();

				AnimateVisitor act;
				act.setDt ( mechanicalDt );
                                BehaviorUpdatePositionVisitor beh(root->getDt());
				for( unsigned i=0; i<numMechSteps.getValue(); i++ )
                                {
                                        root->execute ( beh );
                                        root->execute ( act );
					root->setTime ( startTime + (i+1)* act.getDt() );
					root->execute<UpdateSimulationContextVisitor>();
				}

                            {
                                AnimateEndEvent ev ( dt );
                                PropagateEventVisitor act ( &ev );
                                root->execute ( act );
                            }

				root->execute<UpdateMappingVisitor>();
                            {
                                UpdateMappingEndEvent ev ( dt );
                                PropagateEventVisitor act ( &ev );
                                root->execute ( act );
                            }
				root->execute<VisualUpdateVisitor>();
#ifdef DUMP_VISITOR_INFO
				simulation::Visitor::printComment(std::string("End Step"));
#endif
			}


/// Reset to initial state
			void Simulation::reset ( Node* root )
			{
				if ( !root ) return;

				root->execute<ResetVisitor>();
				root->execute<MechanicalPropagatePositionAndVelocityVisitor>();
				root->execute<UpdateMappingVisitor>();
                                root->execute<VisualUpdateVisitor>();
			}

/// Initialize the textures
			void Simulation::initTextures ( Node* root )
			{
				if ( !root ) return;
				root->execute<VisualInitVisitor>();
				// Do a visual update now as it is not done in load() anymore
				/// \todo Separate this into another method?
				root->execute<VisualUpdateVisitor>();
			}


/// Compute the bounding box of the scene.
			void Simulation::computeBBox ( Node* root, SReal* minBBox, SReal* maxBBox )
			{
				VisualComputeBBoxVisitor act;
				if ( root )
					root->execute ( act );
				minBBox[0] = (SReal)(act.minBBox[0]);
				minBBox[1] = (SReal)(act.minBBox[1]);
				minBBox[2] = (SReal)(act.minBBox[2]);
				maxBBox[0] = (SReal)(act.maxBBox[0]);
				maxBBox[1] = (SReal)(act.maxBBox[1]);
				maxBBox[2] = (SReal)(act.maxBBox[2]);
			}

/// Update contexts. Required before drawing the scene if root flags are modified.
			void Simulation::updateContext ( Node* root )
			{
				if ( !root ) return;
				root->execute<UpdateContextVisitor>();
			}

/// Update only Visual contexts. Required before drawing the scene if root flags are modified.( can filter by specifying a specific element)
			void Simulation::updateVisualContext ( Node* root, int FILTER)
			{
			  if ( !root ) return;
			  UpdateVisualContextVisitor vis(FILTER);
			  vis.execute(root);
			}
/// Render the scene
			void Simulation::draw ( Node* root, helper::gl::VisualParameters* params )
			{
				if ( !root ) return;

				if (root->visualManager.empty())
				{
					VisualDrawVisitor act ( core::VisualModel::Std );
					root->execute ( &act );

					VisualDrawVisitor act2 ( core::VisualModel::Transparent );
					root->execute ( &act2 );
				}
				else
				{
					Node::Sequence<core::VisualManager>::iterator begin = root->visualManager.begin(), end = root->visualManager.end(), it;
					for (it = begin; it != end; ++it)
						(*it)->preDrawScene(params);
					bool rendered = false; // true if a manager did the rendering
					for (it = begin; it != end; ++it)
						if ((*it)->drawScene(params))
						{
							rendered = true;
							break;
						}
					if (!rendered) // do the rendering
					{
						VisualDrawVisitor act ( core::VisualModel::Std );
						root->execute ( &act );

						VisualDrawVisitor act2 ( core::VisualModel::Transparent );
						root->execute ( &act2 );
					}
					Node::Sequence<core::VisualManager>::reverse_iterator rbegin = root->visualManager.rbegin(), rend = root->visualManager.rend(), rit;
					for (rit = rbegin; rit != rend; ++rit)
						(*rit)->postDrawScene(params);
				}
			}


/// Render the scene - shadow pass
			void Simulation::drawShadows ( Node* root )
			{
				if ( !root ) return;

				//VisualDrawVisitor act ( core::VisualModel::Shadow );
				//root->execute ( &act );
			}
/// Export a scene to an OBJ 3D Scene
			void Simulation::exportOBJ ( Node* root, const char* filename, bool exportMTL )
			{
				if ( !root ) return;
				std::ofstream fout ( filename );

				fout << "# Generated from SOFA Simulation" << std::endl;

				if ( !exportMTL )
				{
					ExportOBJVisitor act ( &fout );
					root->execute ( &act );
				}
				else
				{
					const char *path1 = strrchr ( filename, '/' );
					const char *path2 = strrchr ( filename, '\\' );
					const char* path = ( path1==NULL ) ? ( ( path2==NULL ) ?filename : path2+1 ) : ( path2==NULL ) ? path1+1 : ( ( path1-filename ) > ( path2-filename ) ) ? path1+1 : path2+1;

					const char *ext = strrchr ( path, '.' );

					if ( !ext ) ext = path + strlen ( path );
					std::string mtlfilename ( path, ext );
					mtlfilename += ".mtl";
					std::string mtlpathname ( filename, ext );
					mtlpathname += ".mtl";
					std::ofstream mtl ( mtlpathname.c_str() );
					mtl << "# Generated from SOFA Simulation" << std::endl;
					fout << "mtllib "<<mtlfilename<<'\n';

					ExportOBJVisitor act ( &fout,&mtl );
					root->execute ( &act );
				}
			}

/// Export a scene to XML
			void Simulation::exportXML ( Node* root, const char* filename )
			{
				if ( !root ) return;
				std::ofstream fout ( filename );
				XMLPrintVisitor act ( fout );
				root->execute ( &act );
			}

			void Simulation::dumpState ( Node* root, std::ofstream& out )
			{
				out<<root->getTime() <<" ";
				WriteStateVisitor ( out ).execute ( root );
				out<<endl;
			}

/// Initialize gnuplot file output
			void Simulation::initGnuplot ( Node* root )
			{
				if ( !root ) return;
                                InitGnuplotVisitor v(gnuplotDirectory.getFullPath());
				root->execute( v );
			}

/// Update gnuplot file output
			void Simulation::exportGnuplot ( Node* root, double time )
			{
				if ( !root ) return;
				ExportGnuplotVisitor expg ( time );
				root->execute ( expg );
			}


	} // namespace simulation

} // namespace sofa