Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add igtlink mouse interactor #5

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions scenes/iGTLinkMouseInteractor_Lagrange.scn
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0"?>

<Node name="root" dt="0.01" gravity="0 0 0">

<RequiredPlugin pluginName='SofaPreconditioner'/> <!-- Needed to use components [ShewchukPCGLinearSolver, ]-->
<RequiredPlugin pluginName='SofaSparseSolver'/> <!-- Needed to use components [SparseLUSolver, ]-->
<RequiredPlugin pluginName='SofaOpenglVisual'/> <!-- Needed to use components [OglModel, ]-->
<RequiredPlugin name='SofaConstraint'/> <!-- Needed to use components [LinearSolverConstraintCorrection, ]-->
<RequiredPlugin name='SofaGeneralSimpleFem'/> <!-- Needed to use components [BeamFEMForceField, ]-->
<RequiredPlugin name='SofaImplicitOdeSolver'/> <!-- Needed to use components [EulerImplicitSolver, ]-->
<RequiredPlugin name='SofaMiscMapping'/> <!-- Needed to use components [BeamLinearMapping, TubularMapping, ]-->
<RequiredPlugin name='SofaTopologyMapping'/> <!-- Needed to use components [Edge2QuadTopologicalMapping, ]-->
<RequiredPlugin name="Sofa.Component.Constraint.Projective"/> <!-- Needed to use components [FixedConstraint] -->
<RequiredPlugin name="Sofa.Component.Engine.Select"/> <!-- Needed to use components [BoxROI] -->
<RequiredPlugin name="Sofa.Component.LinearSolver.Direct"/> <!-- Needed to use components [SparseLDLSolver] -->
<RequiredPlugin name="Sofa.Component.Mapping.Linear"/> <!-- Needed to use components [IdentityMapping] -->
<RequiredPlugin name="Sofa.Component.Mass"/> <!-- Needed to use components [UniformMass] -->
<RequiredPlugin name="Sofa.Component.ODESolver.Backward"/> <!-- Needed to use components [EulerImplicitSolver] -->
<RequiredPlugin name="Sofa.Component.SolidMechanics.FEM.Elastic"/> <!-- Needed to use components [TetrahedronFEMForceField] -->
<RequiredPlugin name="Sofa.Component.StateContainer"/> <!-- Needed to use components [MechanicalObject] -->
<RequiredPlugin name="Sofa.Component.Topology.Container.Dynamic"/> <!-- Needed to use components [TetrahedronSetTopologyContainer,TetrahedronSetTopologyModifier,TriangleSetTopologyContainer,TriangleSetTopologyModifier] -->
<RequiredPlugin name="Sofa.Component.Topology.Container.Grid"/> <!-- Needed to use components [RegularGridTopology] -->
<RequiredPlugin name="Sofa.Component.Topology.Mapping"/> <!-- Needed to use components [Hexa2TetraTopologicalMapping,Tetra2TriangleTopologicalMapping] -->
<RequiredPlugin name="Sofa.Component.Visual"/> <!-- Needed to use components [VisualStyle] -->
<RequiredPlugin name="Sofa.GL.Component.Rendering3D"/> <!-- Needed to use components [OglModel] -->
<RequiredPlugin name="SofaIGTLink"/> <!-- Needed to use components [OglModel] -->


<VisualStyle displayFlags=" showForceFields "/>
<FreeMotionAnimationLoop name="FreeMotionAnimationLoop" parallelODESolving="true" parallelCollisionDetectionAndFreeMotion="true"/>-->
<GenericConstraintSolver maxIterations="10" multithreading='true' tolerance="1.0e-3"/>
<CollisionPipeline name="CollisionPipeline" />
<ParallelBruteForceBroadPhase/>
<ParallelBVHNarrowPhase/>
<CollisionResponse name="ContactManager" response="FrictionContactConstraint" responseParams="mu=0.5" />
<NewProximityIntersection name="Intersection" alarmDistance="0.2" contactDistance="0.02" />



<!-- $$$$$$$$$$$$$$$$$$$$$$ TOPOLOGY 1 $$$$$$$$$$$$$$$$$$$$$$ -->
<Node name="BEAMVOLUME">
<RegularGrid name="HexaTop" n="25 5 5" min="0 0 0" max="0.5 0.1 0.1"/>
<TetrahedronSetTopologyContainer name="Container" position="@HexaTop.position"/>
<TetrahedronSetTopologyModifier name="Modifier"/>
<Hexa2TetraTopologicalMapping input="@HexaTop" output="@Container" swapping="true"/>
</Node>



<Node name="FEM">
<EulerImplicitSolver firstOrder="false" rayleighMass="0.1" rayleighStiffness="0.1"/>
<SparseLDLSolver name="precond" template="CompressedRowSparseMatrixMat3x3" parallelInverseProduct="true" />

<TetrahedronSetTopologyContainer name="Container" position="@../BEAMVOLUME/HexaTop.position"
tetrahedra="@../BEAMVOLUME/Container.tetrahedra"/>
<TetrahedronSetTopologyModifier name="Modifier"/>

<MechanicalObject name="mstate" template="Vec3d" src="@Container"/>
<TetrahedronFEMForceField name="forceField" listening="true" youngModulus="6e4" poissonRatio="0.40" />


<BoxROI name="box" box="-0.01 -0.01 -0.01 0.01 0.11 0.11"/>
<FixedConstraint indices="@box.indices"/>

<UniformMass totalMass="1"/>

<Node name="Surface">
<TriangleSetTopologyContainer name="Container"/>
<TriangleSetTopologyModifier name="Modifier"/>
<Tetra2TriangleTopologicalMapping input="@../Container" output="@Container" flipNormals="false"/>
<MechanicalObject name="dofs" rest_position="@../mstate.rest_position"/>
<TriangleCollisionModel name="Torus8CMT" proximity="0.001" contactStiffness="20" color="0.94117647058824 0.93725490196078 0.89411764705882" />
<Node name="Visual" activated="1">
<TriangleSetTopologyContainer name="Container" src="@../Container"/>
<OglModel color="1.0 0.1 0.2 1.0" name="visualModel"/>
<IdentityMapping name="VisualMapping"/>
</Node>
<IdentityMapping name="SurfaceMapping"/>
</Node>

<Node name="AttachPoint">
<PointSetTopologyContainer name="Container" />
<PointSetTopologyModifier name="Modifier"/>
<MechanicalObject name="mstate" template="Vec3d" position="0.5 0.05 0.05" drawMode="1" showObjectScale="0.01" showColor="0 1 0" showObject="false"/>

<UserInteractionController name="UIC" inStiffness="10000" reactionTime="50" position="@../../AttachPointMessage.points" />
<RestShapeSpringsForceField stiffness='@UIC.outStiffness' external_rest_shape='@../../AttachPoint/mstate' points='0' external_points='0'/>

<BarycentricMapping />
</Node>

<LinearSolverConstraintCorrection linearSolver="@precond"/>
</Node>

<Node name="AttachPoint">
<PointSetTopologyContainer name="Container" />
<PointSetTopologyModifier name="Modifier"/>
<MechanicalObject name="mstate" template="Vec3d" position="0.5 0.05 0.05" drawMode="2" showObjectScale="0.01" showObject="false"/>
<iGTLinkMouseInteractor pickingType="constraint" position="0 0 0" reactionTime="20000" destCollisionModel="@../FEM/Surface/Torus8CMT"/>

</Node>

</Node>


101 changes: 101 additions & 0 deletions scenes/iGTLinkMouseInteractor_Spring.scn
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0"?>

<Node name="root" dt="0.01" gravity="0 0 0">

<RequiredPlugin pluginName='SofaPreconditioner'/> <!-- Needed to use components [ShewchukPCGLinearSolver, ]-->
<RequiredPlugin pluginName='SofaSparseSolver'/> <!-- Needed to use components [SparseLUSolver, ]-->
<RequiredPlugin pluginName='SofaOpenglVisual'/> <!-- Needed to use components [OglModel, ]-->
<RequiredPlugin name='SofaConstraint'/> <!-- Needed to use components [LinearSolverConstraintCorrection, ]-->
<RequiredPlugin name='SofaGeneralSimpleFem'/> <!-- Needed to use components [BeamFEMForceField, ]-->
<RequiredPlugin name='SofaImplicitOdeSolver'/> <!-- Needed to use components [EulerImplicitSolver, ]-->
<RequiredPlugin name='SofaMiscMapping'/> <!-- Needed to use components [BeamLinearMapping, TubularMapping, ]-->
<RequiredPlugin name='SofaTopologyMapping'/> <!-- Needed to use components [Edge2QuadTopologicalMapping, ]-->
<RequiredPlugin name="Sofa.Component.Constraint.Projective"/> <!-- Needed to use components [FixedConstraint] -->
<RequiredPlugin name="Sofa.Component.Engine.Select"/> <!-- Needed to use components [BoxROI] -->
<RequiredPlugin name="Sofa.Component.LinearSolver.Direct"/> <!-- Needed to use components [SparseLDLSolver] -->
<RequiredPlugin name="Sofa.Component.Mapping.Linear"/> <!-- Needed to use components [IdentityMapping] -->
<RequiredPlugin name="Sofa.Component.Mass"/> <!-- Needed to use components [UniformMass] -->
<RequiredPlugin name="Sofa.Component.ODESolver.Backward"/> <!-- Needed to use components [EulerImplicitSolver] -->
<RequiredPlugin name="Sofa.Component.SolidMechanics.FEM.Elastic"/> <!-- Needed to use components [TetrahedronFEMForceField] -->
<RequiredPlugin name="Sofa.Component.StateContainer"/> <!-- Needed to use components [MechanicalObject] -->
<RequiredPlugin name="Sofa.Component.Topology.Container.Dynamic"/> <!-- Needed to use components [TetrahedronSetTopologyContainer,TetrahedronSetTopologyModifier,TriangleSetTopologyContainer,TriangleSetTopologyModifier] -->
<RequiredPlugin name="Sofa.Component.Topology.Container.Grid"/> <!-- Needed to use components [RegularGridTopology] -->
<RequiredPlugin name="Sofa.Component.Topology.Mapping"/> <!-- Needed to use components [Hexa2TetraTopologicalMapping,Tetra2TriangleTopologicalMapping] -->
<RequiredPlugin name="Sofa.Component.Visual"/> <!-- Needed to use components [VisualStyle] -->
<RequiredPlugin name="Sofa.GL.Component.Rendering3D"/> <!-- Needed to use components [OglModel] -->
<RequiredPlugin name="SofaIGTLink"/> <!-- Needed to use components [OglModel] -->


<VisualStyle displayFlags=" showForceFields "/>
<DefaultAnimationLoop />




<!-- $$$$$$$$$$$$$$$$$$$$$$ TOPOLOGY 1 $$$$$$$$$$$$$$$$$$$$$$ -->
<Node name="BEAMVOLUME">
<RegularGrid name="HexaTop" n="25 5 5" min="0 0 0" max="0.5 0.1 0.1"/>
<TetrahedronSetTopologyContainer name="Container" position="@HexaTop.position"/>
<TetrahedronSetTopologyModifier name="Modifier"/>
<Hexa2TetraTopologicalMapping input="@HexaTop" output="@Container" swapping="true"/>
</Node>



<Node name="FEM">
<EulerImplicitSolver firstOrder="false" rayleighMass="0.1" rayleighStiffness="0.1"/>
<PCGLinearSolver preconditioner="@precond"/>
<SparseLDLSolver name="precond" template="CompressedRowSparseMatrixMat3x3" parallelInverseProduct="true" />

<TetrahedronSetTopologyContainer name="Container" position="@../BEAMVOLUME/HexaTop.position"
tetrahedra="@../BEAMVOLUME/Container.tetrahedra"/>
<TetrahedronSetTopologyModifier name="Modifier"/>

<MechanicalObject name="mstate" template="Vec3d" src="@Container"/>
<TetrahedronFEMForceField name="forceField" listening="true" youngModulus="6e4" poissonRatio="0.40" />


<BoxROI name="box" box="-0.01 -0.01 -0.01 0.01 0.11 0.11"/>
<FixedConstraint indices="@box.indices"/>

<UniformMass totalMass="1"/>

<Node name="Surface">
<TriangleSetTopologyContainer name="Container"/>
<TriangleSetTopologyModifier name="Modifier"/>
<Tetra2TriangleTopologicalMapping input="@../Container" output="@Container" flipNormals="false"/>
<MechanicalObject name="dofs" rest_position="@../mstate.rest_position"/>
<TriangleCollisionModel name="Torus8CMT" proximity="0.001" contactStiffness="20" color="0.94117647058824 0.93725490196078 0.89411764705882" />
<Node name="Visual" activated="1">
<TriangleSetTopologyContainer name="Container" src="@../Container"/>
<OglModel color="1.0 0.1 0.2 1.0" name="visualModel"/>
<IdentityMapping name="VisualMapping"/>
</Node>
<IdentityMapping name="SurfaceMapping"/>
</Node>

<Node name="AttachPoint">
<PointSetTopologyContainer name="Container" />
<PointSetTopologyModifier name="Modifier"/>
<MechanicalObject name="mstate" template="Vec3d" position="0.5 0.05 0.05" drawMode="1" showObjectScale="0.01" showColor="0 1 0" showObject="false"/>

<UserInteractionController name="UIC" inStiffness="10000" reactionTime="50" position="@../../AttachPointMessage.points" />
<RestShapeSpringsForceField stiffness='@UIC.outStiffness' external_rest_shape='@../../AttachPoint/mstate' points='0' external_points='0'/>

<BarycentricMapping />
</Node>


</Node>

<Node name="AttachPoint">
<PointSetTopologyContainer name="Container" />
<PointSetTopologyModifier name="Modifier"/>
<MechanicalObject name="mstate" template="Vec3d" drawMode="2" showObjectScale="0.01" showObject="false"/>
<iGTLinkMouseInteractor pickingType="spring" reactionTime="20000" destCollisionModel="@../FEM/Surface/Torus8CMT"/>

</Node>

</Node>


11 changes: 11 additions & 0 deletions src/sofa/igtlink/utils/iGTLinkMouseInteractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <sofa/igtlink/utils/iGTLinkMouseInteractor.inl>
#include <sofa/core/ObjectFactory.h>

namespace sofa::openigtlink {

SOFA_DECL_CLASS(iGTLinkMouseInteractor)

int iGTLinkMouseInteractorClass = core::RegisterObject("")
.add<iGTLinkMouseInteractor<defaulttype::Vec3Types>>(true);

}
62 changes: 62 additions & 0 deletions src/sofa/igtlink/utils/iGTLinkMouseInteractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once
#include <sofa/gui/component/config.h>

#include <sofa/gui/component/performer/InteractionPerformer.h>
#include <sofa/gui/component/performer/BaseAttachBodyPerformer.h>
#include <sofa/component/collision/geometry/RayModel.h>
#include <sofa/component/statecontainer/MechanicalObject.h>

#include <sofa/core/collision/DetectionOutput.h>
#include <sofa/core/BehaviorModel.h>
#include <sofa/gui/component/performer/MouseInteractor.h>
#include <sofa/gui/component/AttachBodyButtonSetting.h>
#include <sofa/helper/OptionsGroup.h>
#include <sofa/core/objectmodel/DataCallback.h>
#include <sofa/core/CollisionModel.h>
#include <chrono>


namespace sofa::openigtlink {

template<class DataTypes>
class iGTLinkMouseInteractor : public sofa::gui::component::performer::MouseInteractor<DataTypes> {
public:
SOFA_CLASS(SOFA_TEMPLATE(iGTLinkMouseInteractor,DataTypes), SOFA_TEMPLATE(sofa::gui::component::performer::MouseInteractor,DataTypes));

typedef typename DataTypes::Coord Coord;
typedef typename DataTypes::VecCoord VecCoord;


Data<sofa::type::vector<Coord> > d_positions;
Data< sofa::helper::OptionsGroup > d_pickingType;
Data< double > d_springStiffness;
Data< unsigned > d_reactionTime;

SingleLink<iGTLinkMouseInteractor, core::CollisionModel, BaseLink::FLAG_STOREPATH | BaseLink::FLAG_STRONGLINK> l_destCollisionModel;


iGTLinkMouseInteractor();

virtual void updatePosition( SReal dt) override;

void positionChanged();
void attachmentChanged();
void startPerformer();
void stopPerformer();
void handleEvent(sofa::core::objectmodel::Event *event);
sofa::Index findCollidingElem(const type::Vec3& pos) const;


private:
sofa::core::objectmodel::DataCallback c_positions;
sofa::core::objectmodel::DataCallback c_attachmentType;
std::chrono::high_resolution_clock::time_point m_lastChange;
std::unique_ptr<sofa::gui::component::performer::BaseAttachBodyPerformer<DataTypes>> m_performer;

bool m_performerStarted;



};

}
Loading
Loading