-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SofaKernel] Add SimpleApi to create scene graph in a python like way.
This approach make use of the factory and text description of the object se don't need to include them.
- Loading branch information
1 parent
e9f0ab8
commit 2ca2be1
Showing
4 changed files
with
297 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/****************************************************************************** | ||
* SOFA, Simulation Open-Framework Architecture, development version * | ||
* (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH * | ||
* * | ||
* 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. * | ||
* * | ||
* This program 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 General Public License for * | ||
* more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License along * | ||
* with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
******************************************************************************* | ||
* Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
* * | ||
* Contact information: [email protected] * | ||
******************************************************************************/ | ||
/************************************************************************************ | ||
* Contributors: * | ||
* - [email protected] * | ||
***********************************************************************************/ | ||
#include <sofa/core/ObjectFactory.h> | ||
using sofa::core::ObjectFactory ; | ||
|
||
#include <SofaSimulationGraph/DAGSimulation.h> | ||
using sofa::simulation::graph::DAGSimulation ; | ||
|
||
#include "SimpleApi.h" | ||
using sofa::core::objectmodel::BaseObjectDescription ; | ||
|
||
#include <sofa/simulation/XMLPrintVisitor.h> | ||
using sofa::simulation::XMLPrintVisitor ; | ||
|
||
namespace sofa | ||
{ | ||
namespace simpleapi | ||
{ | ||
|
||
void dumpScene(Node::SPtr root) | ||
{ | ||
XMLPrintVisitor p(sofa::core::ExecParams::defaultInstance(), std::cout) ; | ||
p.execute(root.get()) ; | ||
} | ||
|
||
Simulation::SPtr createSimulation(const std::string& type) | ||
{ | ||
if(type!="DAG") | ||
{ | ||
msg_error("SimpleApi") << "Unable to create simulation of type '"<<type<<"'. Supported type is ['DAG']"; | ||
return nullptr ; | ||
} | ||
|
||
return new simulation::graph::DAGSimulation() ; | ||
} | ||
|
||
|
||
Node::SPtr createRootNode(Simulation::SPtr s, const std::string& name, | ||
const std::map<std::string, std::string>& params) | ||
{ | ||
Node::SPtr root = s->createNewNode(name) ; | ||
|
||
BaseObjectDescription desc(name.c_str(), "Node"); | ||
for(auto& kv : params) | ||
{ | ||
desc.setAttribute(kv.first.c_str(), kv.second.c_str()); | ||
} | ||
root->parse(&desc) ; | ||
|
||
return root ; | ||
} | ||
|
||
|
||
BaseObject::SPtr createObject(Node::SPtr parent, const std::string& type, const std::map<std::string, std::string>& params) | ||
{ | ||
/// temporarily, the name is set to the type name. | ||
/// if a "name" parameter is provided, it will overwrite it. | ||
BaseObjectDescription desc(type.c_str(),type.c_str()); | ||
for(auto& kv : params) | ||
{ | ||
desc.setAttribute(kv.first.c_str(), kv.second.c_str()); | ||
} | ||
|
||
/// Create the object. | ||
BaseObject::SPtr obj = ObjectFactory::getInstance()->createObject(parent.get(), &desc); | ||
if (obj==0) | ||
{ | ||
std::stringstream msg; | ||
msg << "Component '" << desc.getName() << "' of type '" << desc.getAttribute("type","") << "' failed:" << msgendl ; | ||
for (std::vector< std::string >::const_iterator it = desc.getErrors().begin(); it != desc.getErrors().end(); ++it) | ||
msg << " " << *it << msgendl ; | ||
msg_error(parent.get()) << msg.str() ; | ||
return NULL; | ||
} | ||
return obj ; | ||
} | ||
|
||
Node::SPtr createChild(Node::SPtr& node, const std::string& name, const std::map<std::string, std::string>& params) | ||
{ | ||
BaseObjectDescription desc(name.c_str(), "Node"); | ||
for(auto& kv : params) | ||
{ | ||
desc.setAttribute(kv.first.c_str(), kv.second.c_str()); | ||
} | ||
Node::SPtr tmp = node->createChild(name); | ||
tmp->parse(&desc); | ||
return tmp; | ||
} | ||
|
||
} /// simpleapi | ||
} /// sofa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/****************************************************************************** | ||
* SOFA, Simulation Open-Framework Architecture, development version * | ||
* (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH * | ||
* * | ||
* 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. * | ||
* * | ||
* This program 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 General Public License for * | ||
* more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License along * | ||
* with this program. If not, see <http://www.gnu.org/licenses/>. * | ||
******************************************************************************* | ||
* Authors: The SOFA Team and external contributors (see Authors.txt) * | ||
* * | ||
* Contact information: [email protected] * | ||
******************************************************************************/ | ||
/****************************************************************************** | ||
* Contributors: * | ||
* - [email protected] * | ||
*****************************************************************************/ | ||
#ifndef SOFA_SIMPLEAPI_H | ||
#define SOFA_SIMPLEAPI_H | ||
|
||
#include <SceneCreator/config.h> | ||
#include <string> | ||
#include <sstream> | ||
#include <map> | ||
|
||
#include <sofa/simulation/Node.h> | ||
#include <sofa/simulation/Simulation.h> | ||
#include <sofa/core/objectmodel/BaseObject.h> | ||
|
||
namespace sofa | ||
{ | ||
namespace simpleapi | ||
{ | ||
|
||
using sofa::core::objectmodel::BaseObject ; | ||
using sofa::simulation::Simulation ; | ||
using sofa::simulation::Node ; | ||
|
||
void SOFA_SCENECREATOR_API importPlugin(const std::string& name) ; | ||
|
||
Simulation::SPtr SOFA_SCENECREATOR_API createSimulation(const std::string& type="DAG") ; | ||
|
||
Node::SPtr SOFA_SCENECREATOR_API createRootNode(Simulation::SPtr, const std::string& name, | ||
const std::map<std::string, std::string>& params={}) ; | ||
|
||
BaseObject::SPtr SOFA_SCENECREATOR_API createObject(Node::SPtr parent, const std::string& type, | ||
const std::map<std::string, std::string>& params={}) ; | ||
|
||
Node::SPtr SOFA_SCENECREATOR_API createChild(Node::SPtr& node, const std::string& name, | ||
const std::map<std::string, std::string>& params={}) ; | ||
|
||
void SOFA_SCENECREATOR_API dumpScene(Node::SPtr root) ; | ||
|
||
template<class T> | ||
std::string str(const T& t) | ||
{ | ||
std::stringstream s; | ||
s << t; | ||
return s.str() ; | ||
} | ||
} /// simpleapi | ||
|
||
|
||
namespace simpleapi | ||
{ | ||
namespace components { | ||
|
||
namespace BaseObject | ||
{ | ||
static const std::string aobjectname {"BaseObject"} ; | ||
namespace data{ | ||
static const std::string name {"name"} ; | ||
} | ||
}; | ||
|
||
namespace MechanicalObject | ||
{ | ||
static const std::string objectname {"MechanicalObject"} ; | ||
namespace data{ | ||
using namespace BaseObject::data ; | ||
static const std::string position {"position"} ; | ||
} | ||
} | ||
|
||
namespace VisualModel | ||
{ | ||
This comment has been minimized.
Sorry, something went wrong. |
||
static const std::string objectname {"VisualModel"} ; | ||
|
||
namespace data { | ||
using namespace BaseObject::data ; | ||
static const std::string filename {"filename"} ; | ||
} | ||
} | ||
|
||
} | ||
|
||
namespace meca { using namespace simpleapi::components::MechanicalObject ; } | ||
namespace visual { using namespace simpleapi::components::VisualModel ; } | ||
|
||
} | ||
|
||
|
||
} /// sofa | ||
|
||
#endif /// SOFA_SIMPLEAPI |
68 changes: 68 additions & 0 deletions
68
SofaKernel/modules/sofa/simulation/simulation_test/graph/SimpleApi_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <SofaSimulationGraph/testing/BaseSimulationTest.h> | ||
using sofa::helper::testing::BaseSimulationTest ; | ||
|
||
#include <SofaSimulationGraph/SimpleApi.h> | ||
using namespace sofa ; | ||
using namespace sofa::simpleapi ; | ||
using namespace sofa::simpleapi::components ; | ||
|
||
class SimpleApi_test : public BaseSimulationTest | ||
{ | ||
public: | ||
bool testParamAPI(); | ||
bool testParamString(); | ||
}; | ||
|
||
bool SimpleApi_test::testParamAPI() | ||
{ | ||
Simulation::SPtr simu = createSimulation("DAG") ; | ||
Node::SPtr root = createRootNode(simu, "root") ; | ||
|
||
auto meca1 = createObject(root, MechanicalObject::objectname, { | ||
{MechanicalObject::data::name, "aMechanicalObject1"}, | ||
{MechanicalObject::data::position, "1 2 3"} | ||
}); | ||
|
||
|
||
auto meca2 = createObject(root, MechanicalObject::objectname, { | ||
{MechanicalObject::data::name, "aMechanicalObject2"}, | ||
{MechanicalObject::data::position, "1 2 3"} | ||
}); | ||
|
||
EXPECT_EQ( (meca1->getName()), std::string("aMechanicalObject1") ) ; | ||
EXPECT_EQ( (meca2->getName()), std::string("aMechanicalObject2") ) ; | ||
|
||
return true ; | ||
} | ||
|
||
bool SimpleApi_test::testParamString() | ||
{ | ||
Simulation::SPtr simu = createSimulation("DAG") ; | ||
Node::SPtr root = createRootNode(simu, "root") ; | ||
|
||
auto meca1 = createObject(root, "MechanicalObject", { | ||
{"name", "aMechanicalObject1"}, | ||
{"position", "1 2 3"} | ||
}); | ||
|
||
auto meca2 = createObject(root, "MechanicalObject", { | ||
{"name", "aMechanicalObject2"}, | ||
{"position", "1 2 3"} | ||
}); | ||
|
||
EXPECT_EQ( (meca1->getName()), std::string("aMechanicalObject1") ) ; | ||
EXPECT_EQ( (meca2->getName()), std::string("aMechanicalObject2") ) ; | ||
|
||
return true; | ||
} | ||
|
||
|
||
TEST_F(SimpleApi_test, testParamAPI ) | ||
{ | ||
ASSERT_TRUE( testParamAPI() ); | ||
} | ||
|
||
TEST_F(SimpleApi_test, createParamString ) | ||
{ | ||
ASSERT_TRUE( testParamString() ); | ||
} |
Small Hello from the past to #5148 ,
It took some time to make to get rid of these ugly strings in simpleapi.h