forked from sofa-framework/SofaPython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-mapping.py
More file actions
100 lines (75 loc) · 3.61 KB
/
example-mapping.py
File metadata and controls
100 lines (75 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Implementation of an identity mapping in python"""
# coding: utf8
import Sofa
import numpy as np
class IdentityMapping(Sofa.Core.Mapping_Vec3d_Vec3d):
"""Implementation of a Mapping in python"""
def __init__(self, *args, **kwargs):
## This is just an ugly trick to transform pointer to object into sofa linkpath.
for k,v in kwargs.items():
if k == "input":
kwargs[k] = v.linkpath
elif k == "output":
kwargs[k] = v.linkpath
Sofa.Core.Mapping_Vec3d_Vec3d.__init__(self, *args, **kwargs)
# let's compute the positions initial delta.
input_position = kwargs["input"].target().position.value.copy()
output_position = kwargs["output"].target().position.value.copy()
self.delta = output_position - input_position
def apply(self, m, outCoord, inCoord):
print("PYTHON(🐍) APPLY ", outCoord, inCoord)
with outCoord.writeableArray() as wa:
wa[:] = inCoord.value + self.delta
def applyJ(self, m, outDeriv, inDeriv):
print("PYTHON(🐍) APPLY-J", outDeriv, inDeriv)
def applyJT(self, m, outDeriv, inDeriv):
print("PYTHON(🐍) APPLY-JT", outDeriv, inDeriv)
def applyConstrainsJT(self, m, outDeriv, inDeriv):
print("PYTHON(🐍) APPLY-JT for constraints, data are [⋱]", m, outDeriv, inDeriv)
print("Constraints ", inDeriv.value)
def createScene(root):
root.addObject("RequiredPlugin", pluginName=["Sofa.GL.Component",
"Sofa.Component.ODESolver.Backward",
"Sofa.Component.LinearSolver.Direct",
"Sofa.Component.LinearSolver.Iterative",
"Sofa.Component.Mass",
"Sofa.Component.StateContainer",
"Sofa.Component.Visual"
])
root.addObject("LineAxis")
root.addObject("DefaultAnimationLoop", name="loop")
root.addChild("Modelling")
######### OBJECT #####################
o = root.Modelling.addChild("Object")
c = o.addObject("MechanicalObject", name="mechanical", position=[0.0,0.0,0.0, 1.0,0.0,0.0])
c.showObject = True
c.showColor = [1.0,0.0,0.0,1.0]
c.drawMode = 1
o.addObject("UniformMass", name="mass", totalMass=1.0)
######### OBJECT #####################
m = o.addChild("MappedDof")
sm = m.addObject("MechanicalObject", name="mapped_quantities", position=[0.0,0.5,0.0, 1.0, 0.5, 0.0])
sm.showObject = True
sm.showColor = [1.0,0.0,1.0,0.7]
sm.drawMode = 1
m.addObject( IdentityMapping(name="CPPObject", input=c, output=sm ) )
root.addChild("Simulation")
root.Simulation.addObject("EulerImplicitSolver")
root.Simulation.addObject("CGLinearSolver", tolerance=1e-12, threshold=1e-12, iterations=25)
root.Simulation.addChild(root.Modelling)
return root
def main():
import SofaRuntime
import Sofa.Gui
import SofaQt
root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Sofa.Gui.GUIManager.closeGUI()
print("End of simulation.")
if __name__ == '__main__':
main()