Skip to content

Commit

Permalink
[SofaKernel] Add SimpleApi to create scene graph in a python like way.
Browse files Browse the repository at this point in the history
This approach make use of the factory and text description of the object se
don't need to include them.
  • Loading branch information
damienmarchal committed Oct 12, 2017
1 parent e9f0ab8 commit 2ca2be1
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 1 deletion.
3 changes: 2 additions & 1 deletion SofaKernel/modules/SofaSimulationGraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(HEADER_FILES
DAGNode.h
DAGNodeMultiMappingElement.h
DAGSimulation.h
SimpleApi.h
init.h
graph.h
)
Expand All @@ -13,8 +14,8 @@ set(SOURCE_FILES
DAGNode.cpp
DAGNodeMultiMappingElement.cpp
DAGSimulation.cpp
SimpleApi.cpp
init.cpp

)

if(SOFA_BUILD_TESTS)
Expand Down
114 changes: 114 additions & 0 deletions SofaKernel/modules/SofaSimulationGraph/SimpleApi.cpp
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
113 changes: 113 additions & 0 deletions SofaKernel/modules/SofaSimulationGraph/SimpleApi.h
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.

Copy link
@damienmarchal

damienmarchal Dec 2, 2024

Author Contributor

Small Hello from the past to #5148 ,
It took some time to make to get rid of these ugly strings in simpleapi.h

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
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() );
}

0 comments on commit 2ca2be1

Please sign in to comment.