Skip to content

Commit 82839f4

Browse files
epernodfredroy
andauthored
[SofaCarving] Add an example written in python (#3457)
* Add example scene in python * Apply suggestions from code review Co-authored-by: Frederick Roy <fredroy@users.noreply.github.com> * Update SimpleCarving.py * Update SimpleCarving.py * Update SimpleCarving.py --------- Co-authored-by: Frederick Roy <fredroy@users.noreply.github.com>
1 parent daa20ca commit 82839f4

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import Sofa
2+
import Sofa.Simulation
3+
4+
def main():
5+
root = Sofa.Core.Node("root")
6+
createScene(root)
7+
Sofa.Simulation.init(root)
8+
9+
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
10+
Sofa.Gui.GUIManager.createGUI(root, __file__)
11+
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
12+
Sofa.Gui.GUIManager.MainLoop(root)
13+
Sofa.Gui.GUIManager.closeGUI()
14+
15+
16+
# This scene demonstrate how the use the CarvingManager component to perform carving operations (deleting topological element like triangle/tetrahedron) between a tool and a target object on a target object.
17+
# The tool performing the carving as well as the object to be carved need to be represented by collision models.
18+
def createScene(root):
19+
root.gravity=[0, 0, 0]
20+
root.dt=0.05
21+
root.showBoundingTree = 0
22+
23+
# Load required plugins
24+
plugins = root.addChild('Plugins')
25+
plugins.addObject('RequiredPlugin', name="MechanicalLoop", pluginName="Sofa.Component.AnimationLoop Sofa.Component.ODESolver.Backward Sofa.Component.LinearSolver.Iterative Sofa.Component.Mapping.NonLinear Sofa.Component.Mapping.Linear")
26+
plugins.addObject('RequiredPlugin', name="MechanicalModel", pluginName="Sofa.Component.StateContainer Sofa.Component.Mass Sofa.Component.SolidMechanics.FEM.Elastic")
27+
plugins.addObject('RequiredPlugin', name="Collision", pluginName="Sofa.Component.Collision.Detection.Algorithm Sofa.Component.Collision.Detection.Intersection Sofa.Component.Collision.Geometry Sofa.Component.Collision.Response.Contact")
28+
plugins.addObject('RequiredPlugin', name="Constraint", pluginName="Sofa.Component.Constraint.Lagrangian.Correction Sofa.Component.Constraint.Lagrangian.Solver Sofa.Component.Constraint.Projective")
29+
plugins.addObject('RequiredPlugin', name="Topology", pluginName="Sofa.Component.Topology.Container.Constant Sofa.Component.Topology.Container.Dynamic Sofa.Component.Topology.Mapping")
30+
plugins.addObject('RequiredPlugin', name="Visual", pluginName="Sofa.Component.Visual Sofa.GL.Component.Rendering3D")
31+
plugins.addObject('RequiredPlugin', name="Utils", pluginName="Sofa.Component.IO.Mesh SofaCarving")
32+
33+
root.addObject('VisualStyle',displayFlags="showVisualModels")
34+
35+
# Add main scene pipeline components
36+
root.addObject('DefaultVisualManagerLoop')
37+
root.addObject('DefaultAnimationLoop')
38+
root.addObject('CollisionPipeline', verbose=False, draw=False)
39+
root.addObject('BruteForceBroadPhase')
40+
root.addObject('BVHNarrowPhase', name="narrowPhase")
41+
root.addObject('MinProximityIntersection', name="Proximity", alarmDistance=0.08, contactDistance=0.05, useSurfaceNormals=False)
42+
root.addObject('CollisionResponse', response="PenalityContactForceField")
43+
44+
# Add the CarvingManger object, linking the collision pipeline, as well as the collision model of the tool used to carve. The collisions models to be carved are found using the tags: CarvingSurface.
45+
# the carvingDistance need to be lower than the contactDistance of the collision pipeline.
46+
root.addObject('CarvingManager',active=True, carvingDistance=-0.01, narrowPhaseDetection="@narrowPhase", toolModel="@Instrument/CollisionModel/ParticleModel")
47+
48+
# Add Volume mechanical object to be carved
49+
TT = root.addChild('TetraVolume')
50+
51+
TT.addObject('EulerImplicitSolver',name="cg_odesolver", printLog=False, rayleighStiffness=0.1, rayleighMass=0.1)
52+
TT.addObject('CGLinearSolver', name="linear solver", iterations=25, tolerance=1.0e-9, threshold=1.0e-9)
53+
TT.addObject('MeshGmshLoader', name="loader", filename="mesh/liver.msh")
54+
55+
TT.addObject('MechanicalObject',template="Vec3d", name="Volume", src="@loader")
56+
57+
TT.addObject('TetrahedronSetTopologyContainer', name="topo", src="@loader")
58+
TT.addObject('TetrahedronSetTopologyModifier', name="topoMod")
59+
TT.addObject('TetrahedronSetGeometryAlgorithms', template="Vec3d", name="GeomAlgo")
60+
61+
TT.addObject('DiagonalMass', massDensity=0.5)
62+
TT.addObject('FixedConstraint', indices=[1, 3, 50])
63+
TT.addObject('TetrahedralCorotationalFEMForceField', name="CFEM", youngModulus=160, poissonRatio=0.3, method="large")
64+
65+
# Add corresponding surface topology
66+
T = TT.addChild('TriangleSurface')
67+
T.addObject('TriangleSetTopologyContainer', name="Container")
68+
T.addObject('TriangleSetTopologyModifier', name="Modifier")
69+
T.addObject('TriangleSetGeometryAlgorithms', template="Vec3d", name="GeomAlgo")
70+
T.addObject('Tetra2TriangleTopologicalMapping', input="@../topo", output="@Container")
71+
72+
T.addObject('TriangleCollisionModel', tags="CarvingSurface")
73+
74+
Visu = T.addChild('VisualModel')
75+
Visu.addObject('OglModel', name="Visual", material="Default Diffuse 1 0 1 0 1 Ambient 0 1 1 1 1 Specular 1 1 1 0 1 Emissive 0 1 1 0 1 Shininess 1 100")
76+
Visu.addObject('IdentityMapping', input="@../../Volume", output="@Visual")
77+
78+
79+
# Add instrument object
80+
Instrument = root.addChild('Instrument')
81+
Instrument.addObject('EulerImplicitSolver', name="cg_odesolver")
82+
Instrument.addObject('CGLinearSolver', name="linear solver", iterations=25, tolerance=1.0e-9, threshold=1.0e-9)
83+
Instrument.addObject('MechanicalObject', template="Rigid3d", name="instrumentState", rotation=[90, 45, 0], translation=[0, 0, 1])
84+
Instrument.addObject('UniformMass', template="Rigid3d", name="mass", totalMass=5.0)
85+
86+
visuIns = Instrument.addChild('VisualModel')
87+
visuIns.addObject('MeshOBJLoader', name="meshLoader_0", filename="mesh/dental_instrument_light.obj", translation=[-0.412256, -0.067639, 3.35], rotation=[180, 0, 150], handleSeams=True)
88+
visuIns.addObject('OglModel',template="Vec3d", name="InstrumentVisualModel", src="@meshLoader_0", 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")
89+
visuIns.addObject('RigidMapping', name="mecha mapping", input="@instrumentState", output="@InstrumentVisualModel")
90+
91+
colIns = Instrument.addChild('CollisionModel')
92+
colIns.addObject('MechanicalObject', template="Vec3d", name="Particle", position=[-0.2, -0.2, -0.2])
93+
colIns.addObject('SphereCollisionModel', name="ParticleModel", radius=0.2, tags="CarvingTool")
94+
colIns.addObject('RigidMapping', name="mecha mapping", input="@instrumentState", output="@Particle")
95+
96+
return root
97+
98+
99+
#Function used only if this script is called from a python environment
100+
if __name__ == '__main__':
101+
main()

0 commit comments

Comments
 (0)