LMLReader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "LMLReader.h"
00036
00037 #include <Loads.h>
00038
00039 #include "LMLForce.h"
00040 #include "LMLConstraint.h"
00041
00042 #include "PMLReader.h"
00043 #include "PMLRigidBody.h"
00044
00045 #include "sofa/defaulttype/Vec3Types.h"
00046 #include "sofa/defaulttype/RigidTypes.h"
00047 using namespace sofa::defaulttype;
00048 #include "sofa/core/objectmodel/BaseObject.h"
00049 using namespace sofa::core::objectmodel;
00050 #include "sofa/component/constraint/FixedConstraint.h"
00051 using namespace sofa::component::constraint;
00052
00053 namespace sofa
00054 {
00055
00056 namespace filemanager
00057 {
00058
00059 namespace pml
00060 {
00061
00062
00063 LMLReader::LMLReader(char* filename){
00064 lmlFile = filename;
00065 loadsList = NULL;
00066 }
00067
00068 void LMLReader::BuildStructure(const char* filename, PMLReader * pmlreader)
00069 {
00070 if (filename)
00071 lmlFile = filename;
00072
00073
00074 if (!lmlFile) {
00075 cout<<"LMLReader error : No lml file found"<<endl;
00076 return;
00077 }
00078
00079 if(loadsList) delete loadsList;
00080
00081 loadsList = new Loads(lmlFile);
00082
00083 this->BuildStructure(pmlreader);
00084 }
00085
00086 void LMLReader::BuildStructure(Loads * loads, PMLReader * pmlreader)
00087 {
00088 loadsList = loads;
00089 this->BuildStructure(pmlreader);
00090 }
00091
00092 void LMLReader::BuildStructure(PMLReader * pmlreader)
00093 {
00094
00095 if (!loadsList || loadsList->numberOfLoads()<=0) {
00096 cout<<"LMLReader error : No loads found"<<endl;
00097 return;
00098 }
00099
00100 std::vector<PMLBody*>::iterator it = pmlreader->bodiesList.begin();
00101
00102 while(it!=pmlreader->bodiesList.end())
00103 {
00104
00105 LMLConstraint<Vec3Types> *constraints = new LMLConstraint<Vec3Types>(loadsList, (*it)->AtomsToDOFsIndexes, (MechanicalState<Vec3Types>*)(*it)->getMechanicalState());
00106 if (constraints->getTargets().size() >0)
00107 if( (*it)->isTypeOf() == "rigid"){
00108 delete constraints;
00109 FixedConstraint<RigidTypes> * fixedConstraint = new FixedConstraint<RigidTypes>;
00110
00111 fixedConstraint->setName("loads");
00112 (*it)->parentNode->addObject(fixedConstraint);
00113 ((PMLRigidBody*)*it)->bodyFixed = true;
00114 }
00115 else
00116 (*it)->getPointsNode()->addObject(constraints);
00117 else
00118 delete constraints;
00119
00120 LMLForce<Vec3Types> *forces = new LMLForce<Vec3Types>(loadsList, (*it)->AtomsToDOFsIndexes, (MechanicalState<Vec3Types>*)(*it)->getMechanicalState());
00121 if (forces->getTargets().size() >0)
00122 (*it)->getPointsNode()->addObject(forces);
00123 else
00124 delete forces;
00125
00126 it++;
00127 }
00128 }
00129
00130 void LMLReader::updateStructure(Loads * loads, PMLReader * pmlreader)
00131 {
00132 loadsList = loads;
00133 std::vector<PMLBody *>::iterator it = pmlreader->bodiesList.begin();
00134 GNode * pointsNode;
00135
00136 while (it != pmlreader->bodiesList.end() )
00137 {
00138 pointsNode = (*it)->getPointsNode();
00139
00140
00141 for (unsigned i=0 ; i<pointsNode->constraint.size() ; i++) {
00142 if (pointsNode->constraint[i]->getName() == "loads")
00143 pointsNode->removeObject ( pointsNode->constraint[i] );
00144
00145 }
00146 LMLConstraint<Vec3Types> *constraints = new LMLConstraint<Vec3Types>(loadsList, (*it)->AtomsToDOFsIndexes, (MechanicalState<Vec3Types>*)(*it)->getMechanicalState());
00147 if (constraints->getTargets().size() >0){
00148 if( (*it)->isTypeOf() == "rigid"){
00149 delete constraints;
00150 FixedConstraint<RigidTypes> * fixedConstraint = new FixedConstraint<RigidTypes>;
00151
00152 (*it)->parentNode->addObject(fixedConstraint);
00153 fixedConstraint->setName("loads");
00154 ((PMLRigidBody*)*it)->bodyFixed = true;
00155 }
00156 else
00157 pointsNode->addObject(constraints);
00158 }else
00159 delete constraints;
00160
00161
00162 for (unsigned i=0 ; i<pointsNode->forceField.size() ; i++) {
00163 if (pointsNode->forceField[i]->getName() == "loads")
00164 pointsNode->removeObject ( pointsNode->forceField[i] );
00165
00166 }
00167 LMLForce<Vec3Types> *forces = new LMLForce<Vec3Types>(loadsList, (*it)->AtomsToDOFsIndexes, (MechanicalState<Vec3Types>*)(*it)->getMechanicalState());
00168 if (forces->getTargets().size() >0)
00169 (*it)->getPointsNode()->addObject(forces);
00170 else
00171 delete forces;
00172
00173 it++;
00174 }
00175 }
00176
00177 void LMLReader::saveAsLML(const char * filename)
00178 {
00179 if(!loadsList)
00180 return;
00181
00182 std::ofstream outputFile(filename);
00183 loadsList->xmlPrint(outputFile);
00184 }
00185
00186 }
00187 }
00188 }