File: TreeSimulation.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (222 lines) | stat: -rw-r--r-- 7,269 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
/******************************************************************************
*       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/tree/TreeSimulation.h>

#include <sofa/simulation/tree/DeleteVisitor.h>
#include <sofa/simulation/common/FindByTypeVisitor.h>
#include <sofa/simulation/common/CleanupVisitor.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/helper/system/PipeProcess.h>

#include <sofa/helper/system/SetDirectory.h>
#include <sofa/helper/system/FileRepository.h>

#include <fstream>
#include <string.h>
#ifndef WIN32
#include <locale.h>
#endif

namespace sofa
{

	namespace simulation
	{

		namespace tree
		{

			using namespace sofa::defaulttype;


			Simulation* getSimulation()
			{
			  if ( simulation::Simulation::theSimulation==NULL )
			    setSimulation( new TreeSimulation );
			  return simulation::getSimulation();
			}

/// Load a scene from a file
		  Node* TreeSimulation::processXML(xml::BaseElement* xml, const char *filename)
			{
				if ( xml==NULL )
				{
					return NULL;
				}

				// We go the the current file's directory so that all relative path are correct
				helper::system::SetDirectory chdir ( filename );

#ifndef WIN32
				// Reset local settings to make sure that floating-point values are interpreted correctly
				setlocale(LC_ALL,"C");
				setlocale(LC_NUMERIC,"C");
#endif

// 				std::cout << "Initializing objects"<<std::endl;
				if ( !xml->init() )
				{
					std::cerr << "Objects initialization failed."<<std::endl;
				}

				GNode* root = dynamic_cast<GNode*> ( xml->getObject() );
				if ( root == NULL )
				{
					std::cerr << "Objects initialization failed."<<std::endl;
					delete xml;
					return NULL;
				}

// 				std::cout << "Initializing simulation "<<root->getName() <<std::endl;

				// Find the Simulation component in the scene
				FindByTypeVisitor<Simulation> findSimu;
				findSimu.execute(root);
				if( !findSimu.found.empty() )
					setSimulation( findSimu.found[0] );

				// As mappings might be initialized after visual models, it is necessary to update them
				// BUGFIX (Jeremie A.): disabled as initTexture was not called yet, and the GUI might not even be up yet
				//root->execute<VisualUpdateVisitor>();

				return root;
			}

			/// Load from a string in memory
			Node* TreeSimulation::loadFromMemory ( const char *filename, const char *data, unsigned int size )
			{
				//::sofa::simulation::init();
// 				std::cerr << "Loading simulation XML file "<<filename<<std::endl;
				xml::BaseElement* xml = xml::loadFromMemory (filename, data, size );

				Node* root = processXML(xml, filename);

// 				std::cout << "load done."<<std::endl;
				delete xml;

				return root;
			}


			/// Load a scene from a file
			Node* TreeSimulation::loadFromFile ( const char *filename )
			{
				//::sofa::simulation::init();
// 				std::cerr << "Loading simulation XML file "<<filename<<std::endl;
				xml::BaseElement* xml = xml::loadFromFile ( filename );

				Node* root = processXML(xml, filename);

// 				std::cout << "load done."<<std::endl;
				delete xml;

				return root;
			}

			/// Load a scene
			Node* TreeSimulation::load ( const char *filename )
			{
				std::string ext = sofa::helper::system::SetDirectory::GetExtension(filename);
				if (ext == "php" || ext == "pscn")
				{
					std::string out="",error="";
					std::vector<std::string> args;


					//TODO : replace when PipeProcess will get file as stdin
					//at the moment, the filename is given as an argument
					args.push_back(std::string("-f" + std::string(filename)));
					//args.push_back("-w");
					std::string newFilename="";
					//std::string newFilename=filename;

					helper::system::FileRepository fp("PATH", ".");
#ifdef WIN32
					std::string command = "php.exe";
#else
					std::string command = "php";
#endif
					if (!fp.findFile(command,""))
					{
						std::cerr << "TreeSimulation : Error : php not found in your PATH environment" << std::endl;
						return NULL;
					}

					sofa::helper::system::PipeProcess::executeProcess(command.c_str(), args,  newFilename, out, error);

					if(error != "")
					{
						std::cerr << "TreeSimulation : load : "<< error << std::endl;
						if (out == "")
							return NULL;
					}

					return loadFromMemory(filename, out.c_str(), out.size());
				}

				if (ext == "scn" || ext == "xml")
				{
					return loadFromFile(filename);
				}

				std::cerr << "TreeSimulation : Error : extension not handled" << std::endl;
				return NULL;

			}

/// Delete a scene from memory. After this call the pointer is invalid
			void TreeSimulation::unload ( Node* root )
			{
			  GNode *groot = dynamic_cast<GNode*>(root);
			  if ( !groot ) return;
			  instruments.clear();
			  instrumentInUse.setValue(-1);
			  groot->execute<CleanupVisitor>();
			  groot->execute<DeleteVisitor>();
			  if ( groot->getParent() !=NULL )
			    groot->getParent()->removeChild ( groot );
			  delete groot;
			}

/// Create a new node
                        Node* TreeSimulation::newNode(const std::string& name)
                        {
                          return new GNode(name);
                        }

			SOFA_DECL_CLASS ( TreeSimulation );
// Register in the Factory
			int TreeSimulationClass = core::RegisterObject ( "Main simulation algorithm, based on tree graph" )
			                      .add< TreeSimulation >()
			                      ;



		} // namespace tree

	} // namespace simulation

} // namespace sofa