Home › Forum › SOFA › Programming with SOFA › How can I modify the position of the Collision Object from Rigid Mechanical Obj
Tagged: 64_bits, Linux_other, SOFA_1706
- This topic has 3 replies, 2 voices, and was last updated 6 years, 10 months ago by Hugo.
-
AuthorPosts
-
8 February 2018 at 16:45 #10469AnonymousInactive
I need to move an Instrument in a scene, I’m doing a mapping from Rigid mechanical object to OglVisual model from C++ code, and then I made a mapping from Rigid Mechanical object to Collision Object, if I do this from the scene when sofa it’s working, the visual object moves because in the code I made a mapping between regid mechanical object and visual object, but if I made the mapping between Rigid Mechaical Object and Collision model, the collision model don’t move.
I need that the Collision model have the same position of Ridig Mechaniacal Object, because the position of this is modified from external device that give a position to Rigid mechanical object from the C++ code.
The code is the following
getContext()->get<MechanicalObjectType>(&objectsMechTemp, core::objectmodel::BaseContext::SearchDown); int numberM = objectsMechTemp.size(); std::cout << "Number of MechanicalObjects: " << objectsMechTemp[0]->name.getValue() << std::endl; getContext()->get<MechanicalObjectType2>(&collisionRigid, core::objectmodel::BaseContext::SearchDown); int numberM2 = objectsMechTemp.size(); std::cout << "Number of MechanicalObjects: " << objectsMechTemp[0]->name.getValue() << std::endl; if(numberM>0){ //Looking for OglModel Components std::vector<sofa::component::visualmodel::OglModel*> list_oglModels; getContext()->get<sofa::component::visualmodel::OglModel>(&list_oglModels, core::objectmodel::BaseContext::SearchDown); int number = list_oglModels.size(); //std::cout << "Number of visual models: " << number << std::endl; if(number>0){ visualNode[0].visu = list_oglModels[0]; //Looking for Principal Instrument Visual Node visualNode[0].node = (simulation::Node*) list_oglModels[0]->getContext(); std::cout << "Name of the visual model identified: " << visualNode[0].node->name.getValue() << std::endl; visualNode[0].mapping = sofa::core::objectmodel::New< sofa::component::mapping::RigidMapping< Rigid3dTypes, ExtVec3fTypes > > (); visualNode[0].node->addObject(visualNode[0].mapping); visualNode[0].mapping->setModels(objectsMechTemp[0], visualNode[0].visu.get()); visualNode[0].mapping->name.setValue("RigidMapping"); visualNode[0].mapping->f_mapConstraints.setValue(false); visualNode[0].mapping->f_mapForces.setValue(false); visualNode[0].mapping->index.setValue(1); visualNode[0].mapping->init(); visualNode[0].node->updateContext(); sofa::defaulttype::ResizableExtVector< sofa::defaulttype::Vec<3,float> > &scaleMapping = *(visualNode[0].mapping->points.beginEdit()); for(unsigned int i=0; i<scaleMapping.size(); i++){ scaleMapping[i] *= (float)(1.0*scale.getValue()/100.0); } visualNode[0].mapping->points.endEdit(); visualNode[0].mappingColis = sofa::core::objectmodel::New< sofa::component::mapping::RigidMapping< Rigid3dTypes, Vec3dTypes > > (); visualNode[0].node->addObject(visualNode[0].mappingColis); visualNode[0].mappingColis->setModels(objectsMechTemp[0], collisionRigid[0]); visualNode[0].mappingColis->name.setValue("RigidMapping"); visualNode[0].mappingColis->f_mapConstraints.setValue(false); visualNode[0].mappingColis->f_mapForces.setValue(false); visualNode[0].mappingColis->index.setValue(2); visualNode[0].mappingColis->init(); visualNode[0].node->updateContext(); initVisu=true; visuActif=false; }else{ std::cout << "No hay nodos visuales"; } }else{ std::cout << "The scene don't have MechanicalObjects" << std::endl; }
And the code of the scene is this:
<RequiredPlugin name="SerialComunication" pluginName="SerialComunication" /> <SerialDriver name="Serial Driver" listening="1" tags="Omni" forceScale="0.5" scale="50" positionBase="0 1 -0.5" permanent="1" /> <Node name="Instrument" principalGraph="true"> <MechanicalObject template="Rigid" name="instrumentState" tags="Omni" translation="-1 4 -2" rotation="180 0 150"/> <UniformMass template="Rigid" name="mass" totalmass="0.05" /> <Node name="VisualModel" > <OglModel template="ExtVec3f" name="InstrumentVisualModel" fileMesh="mesh/dental_instrument.obj" material="Default Diffuse 1 1 0.2 0.2 1 Ambient 1 0.2 0.04 0.04 1 Specular 0 1 0.2 0.2 1 Emissive 0 1 0.2 0.2 1 Shininess 0 45" /> <RigidMapping template="Rigid,ExtVec3f" name="MM->VM mapping" input="@instrumentState" output="@InstrumentVisualModel"/> </Node> <Node name="CollisionModel"> <MeshObjLoader name="loaderInst" filename="mesh/dental_instrument.obj" /> <Mesh src="@loaderInst" /> <MechanicalObject src="@loaderInst" template="Vec3d" name="Particle" position="0 0 0" /> <Point name="ParticleModel" contactStiffness="2" /> <RigidMapping template="Rigid,Vec3d" name="MM->CM mapping" input="@instrumentState" output="@Particle" /> <EnslavementForceFeedback name="forcefeedback" tags="Omni" collisionModel1="@ParticleModel" collisionModel2="" relativeStiffness="4" attractionDistance="0.3" normalsPointOut="false"/> </Node> </Node>
The operative System is Fedora 24
12 February 2018 at 11:26 #10472HugoKeymasterDear Andres,
If you want two representations of your object (mechanical and collision) to have exactly the same positions, use the same mesh for both representations and a IdentityMapping between them.
Why don’t you use Python for your code ?
In the two pieces of code you wrote above, which one is not working?Finally, you can find a reliable example in applications/plugins/Geomagic/scenes/Geomagic-FEMLiver.scn.
Let us know if this helps.
Cheers,Hugo
12 February 2018 at 17:50 #10491AnonymousInactiveDear Hugo,
The Mapping doesn’t work from the Scene, neither the mapping between Rigid Mechanical Object and Visual Model nor Rigid Mechanical and CollisionModel.
In the C++ code mapping between Rigid Object and Visual Model works, but mapping between Rigid Mechanical and CollisionModel don’t work.
I need that the Collision model moves with the same position of visual or mechanical rigid object, the goal is that the Instrument can collide with other rigid object when the instrument is moved.
Thank you so much for the help.
12 February 2018 at 18:13 #10492HugoKeymasterHi Andres,
It seems the path that you set in the input field of the mapping is wrong. A “@../” is missing.
Here is my version (I needed to add both the ODE and linear system solvers:
<Node name="Instrument" principalGraph="true"> <VisualStyle displayFlags="showVisual hideBehavior showCollision hideMapping" /> <!-- <EulerImplicitSolver name="cg_odesolver" rayleighStiffness="0.1" rayleighMass="0.1" /> <CGLinearSolver name="linear solver" iterations="25" tolerance="1e-09" threshold="1e-09" /> --> <MechanicalObject template="Rigid" name="instrumentState" tags="Omni" translation="-1 4 -2" rotation="180 0 150" showObject="1"/> <UniformMass template="Rigid" name="mass" totalmass="0.05" /> <Node name="VisualModel" > <OglModel template="ExtVec3f" name="InstrumentVisualModel" fileMesh="mesh/dental_instrument.obj" material="Default Diffuse 1 1 0.2 0.2 1 Ambient 1 0.2 0.04 0.04 1 Specular 0 1 0.2 0.2 1 Emissive 0 1 0.2 0.2 1 Shininess 0 45" /> <RigidMapping template="Rigid,ExtVec3f" name="MM->VM mapping" input="@../instrumentState" output="@InstrumentVisualModel"/> </Node> <Node name="CollisionModel"> <MeshObjLoader name="loaderInst" filename="mesh/dental_instrument.obj" /> <Mesh src="@loaderInst" /> <MechanicalObject src="@loaderInst" template="Vec3d" name="Particle" position="0 0 0" /> <Point name="ParticleModel" contactStiffness="2" /> <RigidMapping template="Rigid,Vec3d" name="mapping" input="@../instrumentState" output="@Particle" /> <EnslavementForceFeedback name="forcefeedback" tags="Omni" collisionModel1="@ParticleModel" collisionModel2="" relativeStiffness="4" attractionDistance="0.3" normalsPointOut="false"/> </Node> </Node>
Let me know if it helps.
Cheers,Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.