Skip to content

Commit 1cd8917

Browse files
committed
Fix plane and some splib deprecated components
1 parent 44f5f4c commit 1cd8917

File tree

5 files changed

+55
-30
lines changed

5 files changed

+55
-30
lines changed

examples/stlib/SofaScene.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
from stlib.geometries.plane import PlaneParameters
22
from stlib.collision import CollisionParameters
33
from stlib.collision import Collision
4+
from stlib.visual import Visual
45
from splib.core.enum_types import CollisionPrimitive
6+
from splib.simulation.headers import setupLagrangianCollision
57
import dataclasses
68
import numpy as np
79

810

911

1012
def createScene(root):
1113
##Solvers
12-
13-
14+
setupLagrangianCollision(root, displayFlags = "showVisualModels",backgroundColor=[0.8, 0.8, 0.8, 1],
15+
parallelComputing = True,alarmDistance=0.3, contactDistance=0.02,
16+
frictionCoef=0.5, tolerance=1.0e-4, maxIterations=20)
1417

1518
##Environement
16-
17-
planes_lengthNormal = np.array([])
18-
planes_length = 1
19-
planes_width = 1
20-
planes_nbEdgeLength = 1
21-
planes_nbEdgeWidth = 1
19+
planes_lengthNormal = np.array([0, 1, 0])
20+
planes_lengthNbEdge = 1
21+
planes_widthNbEdge = 2
22+
planes_lengthSize = 30
23+
planes_widthSize = 70
2224

2325
plane1_collisionParams = CollisionParameters()
2426
plane1_collisionParams.name = "UP"
2527
plane1_collisionParams.primitives = [CollisionPrimitive.TRIANGLES]
2628
plane1_collisionParams.kwargs = {"TriangleCollision" : {"moving" : False, "simulated" : False}}
27-
plane1_collisionParams.geometry = PlaneParameters(np.array([0,0,0]), np.array([0,0,-1]),
28-
planes_lengthNormal, planes_length, planes_width, planes_nbEdgeLength, planes_nbEdgeWidth)
29-
root.add(Collision, plane1_collisionParams)
29+
plane1_collisionParams.geometry = PlaneParameters(np.array([15,0,1]), np.array([0,0,-1]),
30+
planes_lengthNormal, planes_lengthNbEdge, planes_widthNbEdge, planes_lengthSize, planes_widthSize)
31+
plane1 = root.add(Collision, parameters = plane1_collisionParams)
32+
#TODO being able to reuse already loaded geometry of current prefab to add any new sub prefab
33+
plane1_visu = plane1.addChild("Visu")
34+
plane1_visu.addObject("OglModel", name="VisualModel", src="@../Geometry/container")
3035

3136

3237
plane2_collisionParams = CollisionParameters()
3338
plane2_collisionParams.name = "DOWN"
3439
plane2_collisionParams.primitives = [CollisionPrimitive.TRIANGLES]
3540
plane2_collisionParams.kwargs = {"TriangleCollision" : {"moving" : False, "simulated" : False}}
36-
plane2_collisionParams.geometry = PlaneParameters(np.array([0,0,0]), np.array([0,0,1]),
37-
planes_lengthNormal, planes_length, planes_width, planes_nbEdgeLength, planes_nbEdgeWidth)
38-
root.add(Collision, plane2_collisionParams)
41+
plane2_collisionParams.geometry = PlaneParameters(np.array([15,0,-20]), np.array([0,0,1]),
42+
planes_lengthNormal, planes_lengthNbEdge, planes_widthNbEdge, planes_lengthSize, planes_widthSize)
43+
plane2 = root.add(Collision, parameters = plane2_collisionParams)
44+
plane2_visu = plane2.addChild("Visu")
45+
plane2_visu.addObject("OglModel", name="VisualModel", src="@../Geometry/container")
3946

4047

4148
## Real models

splib/simulation/headers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def setupLagrangianCollision(node, displayFlags = "showVisualModels",background
122122
node.addObject('CollisionResponse',name="ContactManager", response="FrictionContactConstraint", responseParams="mu="+str(frictionCoef),**kwargs)
123123

124124
node.addObject('NewProximityIntersection' ,name="Distance", alarmDistance=alarmDistance, contactDistance=contactDistance, **kwargs)
125-
node.addObject('GenericConstraintSolver',name="ConstraintSolver", tolerance=tolerance, maxIterations=maxIterations, multithreading=parallelComputing,**kwargs)
125+
node.addObject('ProjectedGaussSeidelConstraintSolver',name="ConstraintSolver", tolerance=tolerance, maxIterations=maxIterations, multithreading=parallelComputing,**kwargs)
126126
node.addObject("ConstraintAttachButtonSetting")
127127

128128
return node

stlib/collision.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, parameters: CollisionParameters):
2727

2828
def init(self):
2929

30-
geom = self.add(Geometry, self.parameters.geometry)
30+
geom = self.add(Geometry, parameters = self.parameters.geometry)
3131

3232
self.addObject("MechanicalObject", template="Vec3", position=f"@{self.parameters.geometry.name}/container.position")
3333
for primitive in self.parameters.primitives:

stlib/geometries/__geometry__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(self, parameters: GeometryParameters):
5454
def init(self):
5555

5656
# Generate attribute (positions, edges, triangles, quads, tetrahedra, hexahedra) from the internal data provider
57-
if self.parameters.data is not None :
57+
if isinstance(self.parameters.data, InternalDataProvider) :
5858
self.parameters.data.generateAttribute(self)
5959
if self.parameters.dynamicTopology :
6060
if self.parameters.elementType is not None :

stlib/geometries/plane.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
1-
from stlib.geometries import GeometryParameters, InternalDataProvider
1+
from stlib.geometries import GeometryParameters, InternalDataProvider, Geometry
22
import dataclasses
33
import numpy as np
44

55
@dataclasses.dataclass
66
class PlaneDataProvider(InternalDataProvider):
7-
center : np.ndarray[float]
8-
normal : np.ndarray[float]
9-
lengthNormal : np.ndarray[float]
10-
nbEdgeLength : int
11-
nbEdgeWidth : int
12-
width : float
13-
edgeSize : float
7+
center : np.ndarray[float] = dataclasses.field(default_factory = lambda : np.array([0,0,0]))
8+
normal : np.ndarray[float] = dataclasses.field(default_factory = lambda : np.array([0,0,1]))
9+
lengthNormal : np.ndarray[float] = dataclasses.field(default_factory = lambda : np.array([1,0,0]))
10+
lengthNbEdge : int = 1
11+
widthNbEdge : int = 1
12+
lengthSize : float = 1.0
13+
widthSize : float = 1.0
1414

1515
def __post_init__(self, **kwargs):
1616
InternalDataProvider.__init__(self,**kwargs)
1717

18-
def generateAttribute(self):
19-
#TODO
20-
pass
18+
def generateAttribute(self, parent : Geometry):
19+
20+
lengthEdgeSize = self.lengthSize / self.lengthNbEdge
21+
widthEdgeSize = self.widthSize / self.widthNbEdge
22+
23+
self.widthNormal = np.cross(self.normal,self.lengthNormal)
24+
bottomLeftCorner = self.center - self.lengthNormal * self.lengthNbEdge * lengthEdgeSize / 2 - self.widthNormal * self.widthNbEdge * widthEdgeSize / 2
25+
26+
self.position = np.array([[ bottomLeftCorner + j * self.widthNormal * widthEdgeSize + i * self.lengthNormal * lengthEdgeSize for j in range(self.widthNbEdge + 1) ] for i in range(self.lengthNbEdge + 1)])
27+
28+
self.triangles = np.empty((2 * self.widthNbEdge * self.lengthNbEdge, 3), dtype = int)
29+
for i in range(self.lengthNbEdge):
30+
for j in range(self.widthNbEdge):
31+
self.triangles[i*self.widthNbEdge*2 + j * 2 , 0] = j + i * (self.widthNbEdge + 1)
32+
self.triangles[i*self.widthNbEdge*2 + j * 2 , 1] = j + (i+1) * (self.widthNbEdge + 1)
33+
self.triangles[i*self.widthNbEdge*2 + j * 2 , 2] = j + 1 + i * (self.widthNbEdge + 1)
34+
self.triangles[i*self.widthNbEdge*2 + j * 2 + 1, 0] = j + 1 + i * (self.widthNbEdge + 1)
35+
self.triangles[i*self.widthNbEdge*2 + j * 2 + 1, 1] = j + (i+1) * (self.widthNbEdge + 1)
36+
self.triangles[i*self.widthNbEdge*2 + j * 2 + 1, 2] = j + 1 + (i+1) * (self.widthNbEdge + 1)
37+
38+
2139

2240

2341
class PlaneParameters(GeometryParameters):
2442

25-
def __init__(self, center, normal, lengthNormal, length, width, nbEdgeLength, nbEdgeWidth, dynamicTopology = False):
26-
GeometryParameters.__init__(data = PlaneDataProvider(center=center, normal=normal, lengthNormal=lengthNormal, length=length, width=width, nbEdgeLength=nbEdgeLength, nbEdgeWidth=nbEdgeWidth),
43+
def __init__(self, center, normal, lengthNormal, lengthNbEdge, widthNbEdge, lengthSize, widthSize, dynamicTopology = False):
44+
GeometryParameters.__init__(self, data = PlaneDataProvider(center=center, normal=normal, lengthNormal=lengthNormal, lengthNbEdge=lengthNbEdge, widthNbEdge=widthNbEdge, lengthSize=lengthSize, widthSize=widthSize),
2745
dynamicTopology = dynamicTopology)

0 commit comments

Comments
 (0)