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