Skip to content

Commit 4151cfb

Browse files
authored
Add post init mechanism to prefabs. Those methods are called after init (#613)
1 parent 09c1c67 commit 4151cfb

4 files changed

Lines changed: 44 additions & 52 deletions

File tree

stlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def checkName(context : Sofa.Core.Node, name):
5252
if isinstance(typeName, type) and issubclass(typeName, BasePrefab):
5353
pref = self.addChild(typeName(**params))
5454
pref.init()
55+
pref.postInit()
5556
elif isinstance(typeName, Sofa.Core.Node):
5657
pref = self.addChild(typeName(**params))
5758
elif isinstance(typeName, type) and issubclass(typeName, Sofa.Core.Object):

stlib/core/basePrefab.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ def __init__(self, parameters: BasePrefabParameters):
1414

1515
def init(self):
1616
raise NotImplemented("To be overridden by child class")
17-
17+
18+
def postInit(self):
19+
pass
1820

1921
def localToGlobalCoordinates(pointCloudInput, pointCloudOutput):
2022
raise NotImplemented("Send an email to Damien, he will help you. Guaranteed :)")

stlib/geometries/__geometry__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from stlib.core.basePrefab import BasePrefab
2-
from stlib.core.baseParameters import BaseParameters, Optional, dataclasses, Any
2+
from stlib.core.baseParameters import BaseParameters, Optional, dataclasses, Any, Callable
33
from splib.topology.dynamic import addDynamicTopology
44
from splib.topology.static import addStaticTopology
55
from splib.core.enum_types import ElementType
@@ -35,6 +35,9 @@ class GeometryParameters(BaseParameters):
3535

3636
dynamicTopology : bool = False
3737

38+
def postInit(self, node):
39+
pass
40+
3841
def Data(self):
3942
return InternalDataProvider()
4043

@@ -79,4 +82,8 @@ def init(self):
7982
"quads": self.parameters.data.quads,
8083
"tetrahedra": self.parameters.data.tetrahedra,
8184
"hexahedra": self.parameters.data.hexahedra
82-
})
85+
})
86+
87+
88+
def postInit(self):
89+
self.parameters.postInit(self)

stlib/geometries/extract.py

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,48 @@
66

77
import Sofa
88
from Sofa.Core import Node
9+
from functools import partial
910

1011

1112
class ExtractInternalDataProvider(InternalDataProvider):
12-
destinationType : ElementType
13-
sourceType : ElementType
14-
sourceName : str
1513

16-
def __init__(self, destinationType : ElementType, sourceType : ElementType, sourceName : str):
17-
self.destinationType = destinationType
18-
self.sourceType = sourceType
19-
self.sourceName = sourceName
20-
21-
def __post_init__(self):
22-
if(not (self.sourceType == ElementType.TETRAHEDRA and self.destinationType == ElementType.TRIANGLES)
23-
and not (self.sourceType == ElementType.HEXAHEDRA and self.destinationType == ElementType.QUADS) ):
24-
raise ValueError("Only configuration possible are 'Tetrahedra to Triangles' and 'Hexahedra to Quads'")
25-
26-
InternalDataProvider.__init__(self)
14+
def __init__(self):
15+
super().__init__()
2716

2817
def generateAttribute(self, parent : Geometry):
29-
node = parent.addChild("ExtractedGeometry")
18+
self.position = parent.parents[0].parents[0].getChild("Geometry").container.position.linkpath
19+
3020

31-
#TODO: Specify somewhere in the doc that this should only be used for mapped topologies that extract parent topology surface
32-
# fromLink = parent.parents[0].parents[0].getChild(self.SourceName).container.linkpath
33-
# TODO: the line above cannot work if the nodes and objects are not added to the graph prior the end of __init__() call
34-
# !!! also, on a fail, nothing is added to the graph, which makes things harder to debug
35-
# !!! also, does not work because of the function canCreate(), which checks the input (not yet created?)
36-
# this is all related
37-
fromLink = "@../../../Geometry/container" # TODO: can we do better than this?
38-
addDynamicTopology(node, elementType=self.destinationType, container={"position" : fromLink + ".position"})
39-
if self.sourceType == ElementType.TETRAHEDRA:
40-
node.addObject("Tetra2TriangleTopologicalMapping", input=fromLink, output=node.container.linkpath)
41-
elif self.sourceType == ElementType.HEXAHEDRA:
42-
node.addObject("Hexa2QuadTopologicalMapping", input=fromLink, output=node.container.linkpath)
43-
else:
44-
Sofa.msg_error("[stlib/geometry/exctrat.py]", "Element type: " + str(self.sourceType) + " not supported.")
21+
def extractGeometry(sourceType : ElementType, parent : Geometry):
22+
#TODO: Specify somewhere in the doc that this should only be used for mapped topologies that extract parent topology surface
23+
# fromLink = parent.parents[0].parents[0].getChild(self.SourceName).container.linkpath
24+
# TODO: the line above cannot work if the nodes and objects are not added to the graph prior the end of __init__() call
25+
# !!! also, on a fail, nothing is added to the graph, which makes things harder to debug
26+
# !!! also, does not work because of the function canCreate(), which checks the input (not yet created?)
27+
# this is all related
28+
fromLink = parent.parents[0].parents[0].getChild("Geometry").container.linkpath # TODO: can we do better than this?
29+
if sourceType == ElementType.TETRAHEDRA:
30+
parent.addObject("Tetra2TriangleTopologicalMapping", input=fromLink, output=parent.container.linkpath)
31+
elif sourceType == ElementType.HEXAHEDRA:
32+
parent.addObject("Hexa2QuadTopologicalMapping", input=fromLink, output=parent.container.linkpath)
33+
else:
34+
Sofa.msg_error("[stlib/geometry/exctrat.py]", "Element type: " + str(sourceType) + " not supported.")
4535

46-
self.position = node.container.position.linkpath
47-
if node.container.findData("edges") is not None:
48-
self.edges = node.container.edges.linkpath
49-
if node.container.findData("triangles") is not None:
50-
self.triangles = node.container.triangles.linkpath
51-
if node.container.findData("quads") is not None:
52-
self.quads = node.container.quads.linkpath
53-
if node.container.findData("hexahedra") is not None:
54-
self.hexahedra = node.container.hexahedra.linkpath
55-
if node.container.findData("tetras") is not None:
56-
self.tetrahedra = node.container.tetras.linkpath
5736

5837

5938

6039
class ExtractParameters(GeometryParameters):
61-
def __init__(self,
62-
sourceParameters : GeometryParameters,
63-
destinationType : ElementType,
64-
dynamicTopology : bool = False):
40+
def __init__(self,
41+
sourceParameters : GeometryParameters,
42+
destinationType : ElementType):
6543
GeometryParameters.__init__(self,
66-
data = ExtractInternalDataProvider(destinationType = destinationType,
67-
sourceType = sourceParameters.elementType,
68-
sourceName = sourceParameters.name),
69-
dynamicTopology = dynamicTopology,
44+
data = ExtractInternalDataProvider(),
45+
dynamicTopology = True,
7046
elementType = destinationType)
71-
47+
48+
self.postInit = partial(extractGeometry, sourceParameters.elementType)
49+
50+
if(not (sourceParameters.elementType == ElementType.TETRAHEDRA and destinationType == ElementType.TRIANGLES)
51+
and not (sourceParameters.elementType == ElementType.HEXAHEDRA and destinationType == ElementType.QUADS) ):
52+
raise ValueError("Only configuration possible are 'Tetrahedra to Triangles' and 'Hexahedra to Quads'")
53+

0 commit comments

Comments
 (0)