LMLReader.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 
00026 /***************************************************************************
00027  *                                                                         *
00028  *   This program is free software; you can redistribute it and/or modify  *
00029  *   it under the terms of the GNU General Public License as published by  *
00030  *   the Free Software Foundation; either version 2 of the License, or     *
00031  *   (at your option) any later version.                                   *
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     //check if there is a file specified
00074     if (!lmlFile) {
00075         cout<<"LMLReader error : No lml file found"<<endl;
00076         return;
00077     }
00078 
00079     if(loadsList) delete loadsList;
00080     //read the file
00081     loadsList = new Loads(lmlFile);
00082     //loadsList = data.getLoads();
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     //check if loads was read
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         //find forces and constraints in the loads list
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                 //fixedConstraint->addConstraint(0);
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         //update constraints
00141         for (unsigned i=0 ; i<pointsNode->constraint.size() ; i++) {
00142             if (pointsNode->constraint[i]->getName() == "loads")
00143                 pointsNode->removeObject ( pointsNode->constraint[i] );
00144             //delete ?
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                 //fixedConstraint->addConstraint(0);
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         //update forces
00162         for (unsigned i=0 ; i<pointsNode->forceField.size() ; i++) {
00163             if (pointsNode->forceField[i]->getName() == "loads")
00164                 pointsNode->removeObject ( pointsNode->forceField[i] );
00165             //delete ?
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 }

Generated on Sun Nov 23 03:56:14 2008 for Sofa by  doxygen 1.5.7.1