-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathtest1.py
More file actions
233 lines (173 loc) · 9.32 KB
/
test1.py
File metadata and controls
233 lines (173 loc) · 9.32 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import Sofa
## Pyggy patch ##############################################################
class Parameters(object):
name : str = ""
def __init__(self, **kwargs):
for k,v in kwargs.items():
setattr(self, k, v)
def toDict(self):
return {"name":self.name}
class ModifierParameters(Parameters):
def __init__(self, **kwargs):
Parameters.__init__(self, **kwargs)
def toDict(self):
return Parameters.toDict(self)
def getpythonCallingPoint(depth=0):
import inspect
d = inspect.stack()[depth]
return (d.filename, d.positions.lineno)
class Modifier(Sofa.Core.Controller):
def __init__(self, parameters : ModifierParameters = ModifierParameters()):
Sofa.Core.Controller.__init__(self, **parameters.toDict())
def getModifiedNode(self):
return self.getOwner().parents[0]
Sofa.Core.Modifier = Modifier
def addModifier(self, type, parameters:Parameters, **kwargs) -> Modifier:
if "Modifiers" not in self.children:
self.addChild("Modifiers")
o = self.Modifiers.addObject(type(parameters=parameters, **kwargs))
o.apply()
return o
Sofa.Core.Node.addModifier = addModifier
def add(self, type, parameters:Parameters) -> Sofa.Core.Base:
if issubclass(type, Sofa.Core.Node):
return self.addChild(type(parameters))
Sofa.Core.Node.add = add
#############################################################################
class PrefabParameters(Parameters):
def __init__(self, **kwargs):
Parameters.__init__(self, **kwargs)
def toDict(self):
return Parameters.toDict(self)
class Prefab(Sofa.Core.Node):
def __init__(self, parameters):
Sofa.Core.Node.__init__(self, name=parameters.name)
class MyEntityParameters(PrefabParameters):
def __init__(self, **kwargs):
PrefabParameters.__init__(self, **kwargs)
def toDict(self):
return PrefabParameters.toDict(self)
class MyEntity(Prefab):
def __init__(self, parameters):
Prefab.__init__(self, PrefabParameters(name=parameters.name))
self.addChild("Material")
self.addChild("Geometry")
self.addChild("Collision")
self.addChild("Visual")
self.addChild("Modifiers")
self.Geometry.addObject("MeshOBJLoader", filename="mesh/cube.obj", name="loader")
self.Material.addObject("MechanicalObject", name="state", template="Vec3", position=self.Geometry.loader.position.linkpath)
self.Material.addObject("UniformMass", name="mass", totalMass=1.0)
self.Visual.addObject("OglModel", src=self.Geometry.loader.linkpath)
class FixedPointBoundaryConditionParameters(ModifierParameters):
def __init__(self, **kwargs):
ModifierParameters.__init__(self, **kwargs)
def toDict(self):
return ModifierParameters.toDict(self)
class FixedPointBoundaryCondition(Modifier):
def __init__(self, parameters : FixedPointBoundaryConditionParameters = FixedPointBoundaryConditionParameters()):
Modifier.__init__(self, parameters=parameters)
def apply(self):
self.getModifiedNode().Material.addObject("UnilateralInteractionConstraint")
class BilateralInteractionModifierParameters(ModifierParameters):
object1: Sofa.Core.Object = None
object2: Sofa.Core.Object = None
def __init__(self, **kwargs):
ModifierParameters.__init__(self, **kwargs)
def toDict(self):
return ModifierParameters.toDict(self) | {"object1":self.object1,"object2":self.object2}
class BilateralInteractionModifier(Modifier):
def __init__(self, parameters : BilateralInteractionModifierParameters = BilateralInteractionModifierParameters()):
Modifier.__init__(self, parameters=parameters)
self.object1 = parameters.object1
self.object2 = parameters.object2
if self.object1 is None:
raise Exception("It is mandatory to have 'object1'")
if self.object2 is None:
raise Exception("It is mandatory to have 'object2'")
h = getpythonCallingPoint(3)
self.setInstanciationSourceFileName(h[0])
self.setInstanciationSourceFilePos(h[1])
def apply(self):
a = self.object1.Material.addObject("UnilateralInteractionConstraint")
b = self.object2.Material.addObject("UnilateralInteractionConstraint")
a.setInstanciationSourceFileName(self.getInstanciationSourceFileName())
a.setInstanciationSourceFilePos(self.getInstanciationSourceFilePos())
b.setInstanciationSourceFileName(self.getInstanciationSourceFileName())
b.setInstanciationSourceFilePos(self.getInstanciationSourceFilePos())
class HeaderFromSceneModifierParameters(ModifierParameters):
root : Sofa.Core.Node = None
def __init__(self, **kwargs):
ModifierParameters.__init__(self, **kwargs)
def toDict(self):
return ModifierParameters.toDict(self) | {"root":self.root}
def find_component(cond, node):
c = [component for component in node.objects if cond(component)]
for child in node.children:
c += find_component(cond, child)
return c
class HeaderFromSceneModifier(Modifier):
def __init__(self, parameters : HeaderFromSceneModifierParameters = HeaderFromSceneModifierParameters()):
Modifier.__init__(self, parameters=parameters)
self.root = parameters.root
if self.root is None:
raise Exception("It is mandatory to have 'root' parameter for this modifier")
h = getpythonCallingPoint(3)
self.setInstanciationSourceFileName(h[0])
self.setInstanciationSourceFilePos(h[1])
def apply(self):
# Traverse scene to deduce stuff
constraints = find_component(lambda x: True, self.root)
constraints = [constraint.getClassName() for constraint in constraints]
print(f"Lagrangian {constraints}")
if "UnilateralLagrangianConstraint" in constraints:
self.root.addObject("FreeMotionAnimationLoop")
class HeaderToSceneModifierParameters(ModifierParameters):
root : Sofa.Core.Node = None
def __init__(self, **kwargs):
ModifierParameters.__init__(self, **kwargs)
def toDict(self):
return ModifierParameters.toDict(self) | {"root":self.root}
class HeaderToSceneModifier(Modifier):
def __init__(self, parameters : HeaderFromSceneModifierParameters = HeaderFromSceneModifierParameters()):
Modifier.__init__(self, parameters=parameters)
self.root = parameters.root
if self.root is None:
raise Exception("It is mandatory to have 'root' parameter for this modifier")
def apply(self):
# Traverse scene to deduce stuff
def is_mechanical(object):
return object.getClassName() == "MechanicalObject"
mechanicals = find_component(is_mechanical, self.root)
for mechanical in mechanicals:
mechanical.getOwner().addObject("GenericConstraintCorrection")
def createScene(root):
# Add a modifier in Example1/prefab, it modify the node it is applied to
# The use case is to enrich an object
root.addChild("Example1")
root.Example1.add(MyEntity, parameters=MyEntityParameters(name="prefab"))
root.Example1.prefab.addModifier(FixedPointBoundaryCondition, parameters=FixedPointBoundaryConditionParameters(name="boundary1", stiffness=30.0))
# Add a modifier in Example2, it modify a specific pair of node
# The use case is to explicit an action that involve several possibly disconnected in the graph object
# Personnally I'm not sure I want to express BoundaryCondition as Modifiers.
root.addChild("Example2")
root.Example2.add(MyEntity, parameters=MyEntityParameters(name="prefab1"))
root.Example2.add(MyEntity, parameters=MyEntityParameters(name="prefab2"))
root.Example2.addModifier(BilateralInteractionModifier, parameters=BilateralInteractionModifierParameters(name="constraint",
object1=root.Example2.prefab1,
object2=root.Example2.prefab2))
# The same but with the parameters written before calling the modifier
root.addChild("Example2bis")
root.Example2bis.add(MyEntity, parameters=MyEntityParameters(name="prefab1"))
root.Example2bis.add(MyEntity, parameters=MyEntityParameters(name="prefab2"))
parameters = BilateralInteractionModifierParameters()
parameters.name="constraint"
parameters.object1=root.Example2bis.prefab1
parameters.object2=root.Example2bis.prefab2
root.Example2bis.addModifier(BilateralInteractionModifier, parameters=parameters)
# Add a modifier in the root node, this modifier deduce something from the scene and modify anywhere in the scene.
root.addModifier(HeaderFromSceneModifier, parameters=HeaderFromSceneModifierParameters(name="bottom-up-fix",
root=root))
# Add a modifier in the root node, this modifier deduce something from the scene and modify top down the scene.
root.addModifier(HeaderToSceneModifier, parameters=HeaderToSceneModifierParameters(name="top-down-fix",
root=root))