Home › Forum › SOFA › Programming with SOFA › [SOLVED] sample code
Tagged: 32_bits, Linux_ubuntu, SOFA_1608
- This topic has 12 replies, 3 voices, and was last updated 7 years, 8 months ago by Hugo.
-
AuthorPosts
-
20 February 2017 at 22:17 #8644SdAutoBlocked
Hello,
i am new to SOFA, i ask you please, is it possible to modify the code of liver for take a piece of soft tissue? if it is, please can you give me an example of liver.
thank you for all your help21 February 2017 at 13:48 #8649HugoKeymasterDear SdAuto,
Could you please detail what you mean by “modify the code of liver for take a piece of soft tissue”?
If you download the binaries of SOFA, you will be able to run examples as the one in examples/Demos/liver.scnCheers,
Hugo
21 February 2017 at 17:52 #8653SdAutoBlockedHi sir,
I already saw the example but i want to know if i can remove a part of the liver for examination, or make a wound and suture it.
Thank you sir
21 February 2017 at 18:16 #8656HugoKeymasterHi,
If what you want to do is cutting during the simulation, you can:
- have a look to the examples in examples/Components/topology/TopologicalModifiers
- look at all the threads about cutting and topological changes on the forum like:
If what you need is just to edit a mesh, you should use a mesh editor software (Blender, Paraview ..).
About suturing, there is some private plugin simulating suture. More information can be found about it on the marketplace.
Best regards,Hugo
22 February 2017 at 22:49 #8679SdAutoBlockedHi sir,
Thank’s for your answer,
I have a nother question, I tried to compile the file “src/doc/src_exemple/example3” on Qt creator, it showed me an error at the file “scene.h“, and I didn’t found this file in sofa.Thank’s sir
24 February 2017 at 10:18 #8695HugoKeymasterDear SdAuto,
You should rather focus on:
- the online documentation for reading,
- and the examples available in SOFA (in repository examples/)
Best,
Hugo
27 February 2017 at 22:06 #8719SdAutoBlockedDear Sir,
I would like to write a scene in cpp but the corresponding help link is still under construction. Could you please give me some guidelines to do it ?
Best27 February 2017 at 23:14 #8720HugoKeymasterFirst of all, you can really get inspiration from the Main.cpp from applications/projects/runSofa/.
Here is a short template about how to write a scene in C++:#include <sstream> using std::ostringstream ; #include <fstream> #include <sofa/helper/ArgumentParser.h> #include <SofaSimulationCommon/common.h> #include <sofa/simulation/Node.h> #include <sofa/helper/system/PluginManager.h> #include <sofa/simulation/config.h> // #defines SOFA_HAVE_DAG (or not) #include <SofaSimulationCommon/init.h> #include <SofaSimulationTree/init.h> #include <SofaSimulationTree/TreeSimulation.h> #include <SofaComponentCommon/initComponentCommon.h> #include <SofaComponentBase/initComponentBase.h> #include <SofaComponentGeneral/initComponentGeneral.h> #include <SofaComponentAdvanced/initComponentAdvanced.h> #include <SofaComponentMisc/initComponentMisc.h> #include <sofa/helper/BackTrace.h> // Add any other includes needed by your scene // #include<path_to/myComponent.h> // ... int main(int argc, char** argv) { sofa::helper::BackTrace::autodump(); ExecParams::defaultInstance()->setAspectID(0); // here you can init the GUI you wish // as an example, see : applications/projects/runSofa/Main.cpp sofa::simulation::tree::init(); sofa::component::initComponentBase(); sofa::component::initComponentCommon(); sofa::component::initComponentGeneral(); sofa::component::initComponentAdvanced(); sofa::component::initComponentMisc(); sofa::simulation::setSimulation(new sofa::simulation::tree::TreeSimulation()); std::ostringstream no_error_message; sofa::helper::system::PluginManager::getInstance().loadPlugin("MyPlugin",&no_error_message); sofa::helper::system::PluginManager::getInstance().init(); // Here start the description of your scene in C++ // you can create nodes sofa::simulation::tree::GNode* groot = new sofa::simulation::tree::GNode; groot->setName ("root"); sofa::defaulttype::Vec3d g = sofa::defaulttype::Vec3d (0,-9.81,0); groot->setGravityInWorld(g); // you can create components in these nodes addMyComponent(groot); // you can add new child nodes and repeat the process to build your scene sofa::simulation::tree::GNode* childNode = new sofa::simulation::tree::GNode; childNode->setName( "child_of_root" );\r groot->addChild(childNode); addMyComponent(childNode); sofa::simulation::getSimulation()->init(groot.get()); // Run the simulation groot->setAnimate(true); // close the simulation if (groot!=NULL) sofa::simulation::getSimulation()->unload(groot); sofa::simulation::common::cleanup(); sofa::simulation::tree::cleanup(); return 0; } // this function illustrates how to add a component to a node myComponent *addMyComponent(sofa::simulation::tree::GNode *node) { myComponent* myComp = new myComponent; myComp->setName ("myComponentName"); myComp->addTag ((Tag)"myTag"); myComp->d_data.setValue(0.02); // any public data of the component can thus be defined node->addObject (myComp); return (myComponent *)solver; }
Please let me know if it helps.
If yes, I will add it to the documentation.Best,
Hugo
28 February 2017 at 08:35 #8726maxBlockedHi all,
Just to mention that it is *way* easier to write scenes in Python, unless you *really* have to do it in c++.
In any case, the learning curve for grasping SOFA concepts will be much less steep using Python, since you’ll just focus on the scene creation rather than SOFA internal book-keeping.
Should you choose this option, you need to enable the
SofaPython
plugin during compilation. Then a minimal python scene would look like:def createScene(node): # create a node in the scene graph (i.e. 'Node' in xml scenes) child_node = node.createChild('child name') # create a component under the graph node child_dofs = child_node.createObject('MechanicalObject', template = 'Vec3', name = 'dofs')
Please refer to the documentation for more.
Hope this helps,
28 February 2017 at 09:18 #8727HugoKeymasterYes, thank you max !
I wanted to mention Python and forgot about it !
SdAuto, indeed consider Python which is much more flexible and powerful than C++ only. You could even combine a scene in C++ including a SofaPythonController and use Python scripts as well.Cheers,
Hugo
28 February 2017 at 22:10 #8740SdAutoBlockedDear Sir,
I tried to compile Main.cpp but I found a problem in CMakeLists.txt which seems to me that it is not complete. I mean there are some libraries that are not specified with target_link_librarie. I did not want to add all of them by hand since, I think this will take time to have them all and in the correct order. Could you please help me on this ?
I show you the error I get:
Undefined symbols for architecture x86_64:“sofa::simulation::getSimulation()”, referenced from:
_main in Main.cpp.o
“sofa::simulation::setSimulation(sofa::simulation::Simulation*)”, referenced from:
_main in Main.cpp.o
“sofa::simulation::tree::TreeSimulation::TreeSimulation()”, referenced from:
_main in Main.cpp.o
“sofa::simulation::tree::init()”, referenced from:
_main in Main.cpp.o
“sofa::simulation::tree::cleanup()”, referenced from:
_main in Main.cpp.o
“sofa::simulation::common::cleanup()”, referenced from:
_main in Main.cpp.o
“sofa::simulation::Visitor::Visitor(sofa::core::ExecParams const*)”, referenced from:
sofa::component::misc::ReadStateActivator::ReadStateActivator(sofa::core::ExecParams const*, bool) in Main.cpp.o
“sofa::simulation::Visitor::~Visitor()”, referenced from:
sofa::component::misc::ReadStateActivator::~ReadStateActivator() in Main.cpp.o
sofa::component::misc::CompareStateCreator::~CompareStateCreator() in Main.cpp.o
“sofa::gui::GUIManager::AddGUIOption(char const*)”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::SetDimension(int, int)”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::ListSupportedGUI(char)”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::CurrentSimulation()”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::Init(char const*, char const*)”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::MainLoop(boost::intrusive_ptr<sofa::simulation::Node>, char const*)”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::SetScene(boost::intrusive_ptr<sofa::simulation::Node>, char const*, bool)”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::closeGUI()”, referenced from:
_main in Main.cpp.o
“sofa::gui::GUIManager::createGUI(boost::intrusive_ptr<sofa::simulation::Node>, char const*)”, referenced from:
_main in Main.cpp.o
Best,
9 March 2017 at 11:33 #8763SdAutoBlockedHi sir,
finally, i have added in CMakeLists the missing libraries and it is working correctlycmake_minimum_required(VERSION 2.8.12) project(runSofa) #modifier.......................................................................... set(SofaFramework_DIR /home/user/sofa/v16.08/build/install/lib/cmake/SofaFramework) set(TinyXML_DIR /home/user/sofa/v16.08/build/install/lib/cmake/TinyXML) set(GTest_DIR /home/user/sofa/v16.08/build/install/lib/cmake/GTest) set(SofaGeneral_DIR/home/user/sofa/v16.08/build/install/lib/cmake) #.................................................................................. option(RUNSOFA_INSTALL_AS_BUNDLE "Create a bundle containing all the installation files (Only Mac OS X for now)" OFF) link_directories(/home/user/sofa/v16.08/build/install/lib) link_directories(/home/user/sofa/v16.08/build/install/lib/cmake/SofaGeneral) link_directories(/home/user/sofa/v16.08/build/lib) #..................................................................................... if(APPLE) set(RC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/runSOFA.icns") else() set(RC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/sofa.rc") endif() find_package(SofaFramework REQUIRED) # to get SOFA_HAVE_GLUT if(APPLE) set_source_files_properties(${RC_FILES} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources") endif() if(APPLE AND RUNSOFA_INSTALL_AS_BUNDLE) add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${RC_FILES} Main.cpp ) else() add_executable(${PROJECT_NAME} ${RC_FILES} Main.cpp) endif() if(APPLE) set_target_properties( ${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE_ICON_FILE "runSOFA.icns" ) endif() #modifier............................................................. include_directories("/home/user/sofa/v16.08/build/install/include") include_directories("/home/user/sofa/v16.08/src/applications") include_directories("/home/user/sofa/v16.08/build/install/lib/cmake") target_link_libraries(${PROJECT_NAME} SofaComponentGeneral SofaBaseTopology) #...... target_link_libraries(${PROJECT_NAME} SofaComponentAdvanced SofaComponentMisc) target_link_libraries(${PROJECT_NAME} SofaSimulationGraph) target_link_libraries(${PROJECT_NAME} SofaGuiMain) #....... target_link_libraries(${PROJECT_NAME} SofaValidation) target_link_libraries(${PROJECT_NAME} SofaGeneralLoader) target_link_libraries(${PROJECT_NAME} SofaSimulationCommon) target_link_libraries(${PROJECT_NAME} SofaSimulationCore) target_link_libraries(${PROJECT_NAME} SofaSimulationTree) target_link_libraries(${PROJECT_NAME} SofaCore) target_link_libraries(${PROJECT_NAME} SofaGuiCommon) target_link_libraries(${PROJECT_NAME} SofaComponentBase) target_link_libraries(${PROJECT_NAME} SofaComponentCommon) #........................................................................... if(SOFA_HAVE_GLUT) target_compile_definitions(${PROJECT_NAME} PUBLIC "SOFA_HAVE_GLUT_GUI") endif() if(APPLE AND RUNSOFA_INSTALL_AS_BUNDLE) # set(CPACK_COMPONENTS_ALL BundlePack) set_property(GLOBAL PROPERTY RUNSOFA_CPACK_COMPONENTS_ALL BundlePack) include(cmake/bundle.cmake) else() sofa_install_targets(SofaGui runSofa "") endif()
thank’s
17 March 2017 at 08:18 #8797HugoKeymasterThank you for your feedback.
Here is the documentation page updated.Do not hesitate to contribute to it.
Best,
Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.