-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_cubes.py
234 lines (180 loc) · 6.76 KB
/
two_cubes.py
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
233
234
import Sofa
from stlib3.scene import Scene
from stlib3.physics.rigid import Cube, Floor
from stlib3.physics.deformable import ElasticMaterialObject
from stlib3.physics.collision import CollisionMesh
from stlib3.components import addOrientedBoxRoi
from stlib3.physics.mixedmaterial import Rigidify
class CubeController(Sofa.Core.Controller):
def __init__(self, *a, **kw):
Sofa.Core.Controller.__init__(self, *a, **kw)
self.node = kw["node"]
self.name = "CubeController"
return
def reset(self):
rotation = 0
self.node.findData("angleIn").value = rotation
def onAnimateBeginEvent(self, event):
rotation = self.node.findData("angleIn").value
rotation += 0.05
self.node.findData("angleIn").value = rotation
class CubeBot(Sofa.Prefab):
prefabParameters = []
prefabData = [
{
"name": "angleIn",
"help": "angle of rotation (in radians)",
"type": "float",
"default": 0,
},
]
def __init__(self, *args, **kwargs):
Sofa.Prefab.__init__(self, *args, **kwargs)
def init(self):
cube_red = Cube(
self,
totalMass=50,
name="Red",
color=[1.0, 0.0, 0.0, 1.0],
translation=[10.25, 3.0, 0.0],
rotation=[0.0, 90.0, -90.0],
uniformScale=1.0,
isAStaticObject=False,
)
angle = cube_red.addChild("Articulation")
angle.addObject(
"MechanicalObject",
name="dofs",
template="Vec1",
position=[[self.getData("angleIn")]],
rest_position=self.getData("angleIn").getLinkPath(),
)
angle.addObject("RestShapeSpringsForceField", points=0, stiffness=1e10)
angle.addObject("ArticulatedHierarchyContainer", printLog=False)
articulationCenter = angle.addChild("ArticulationCenter")
articulationCenter.addObject(
"ArticulationCenter",
posOnParent=[0, 0.0, 1.95],
parentIndex=0,
childIndex=0,
posOnChild=[0.0, 0.0, 0.0],
)
articulation = articulationCenter.addChild("Articulations")
articulation.addObject(
"Articulation",
translation=False,
rotation=True,
rotationAxis=[0, 0, 1],
articulationIndex=0,
)
self.addObject(CubeController(node=self))
cube_blue = Cube(
angle,
name="Blue",
totalMass=50,
color=[0.0, 0.0, 1.0, 1.0],
uniformScale=0.9,
isAStaticObject=False,
)
cube_blue.addObject(
"ArticulatedSystemMapping",
input1="@../dofs",
input2="@../../mstate",
output="@./",
)
class NoodleRobot(Sofa.Prefab):
def __init__(self, *args, **kwargs):
Sofa.Prefab.__init__(self, *args, **kwargs)
def init(self):
self.elasticMaterial = self.elasticBody()
self.ElasticBody.init()
cubeBot = CubeBot()
self.addChild(cubeBot)
box = addOrientedBoxRoi(
self,
name="boxROIclamped",
position=[list(i) for i in self.elasticMaterial.dofs.rest_position.value],
translation=[10.0, 3.0, 0.0],
eulerRotation=[0.0, 0.0, 0.0],
scale=[1.5, 1.2, 2.5],
)
box.drawBoxes = True
box.init()
indices = [[ind for ind in box.indices.value]]
frame = [[0, 0, 0, 0, 0, 0, 1]]
rigidifiedStruct = Rigidify(
self,
self.elasticMaterial,
groupIndices=indices,
frames=frame,
name="RigidifiedStructure",
)
self.RigidifiedStructure.RigidParts.addObject(
"RigidRigidMapping", index=0, input=self.CubeBot.Red.mstate.getLinkPath()
)
def elasticBody(self):
body = self.addChild("ElasticBody")
e = body.addChild(
ElasticMaterialObject(
volumeMeshFileName="mesh/100_body.msh",
poissonRatio=0.3,
youngModulus=10080000,
totalMass=50.5,
surfaceColor=[0.4, 1.0, 0.7, 1.0],
surfaceMeshFileName="mesh/3500_body.stl",
translation=[10.0, 5.0, 0.0],
rotation=[0.0, 0.0, 0.0],
scale=[0.01,0.01,0.01],
collisionMesh="mesh/300_body.stl",
)
)
return e
def createScene(rootNode):
"""This is my first scene"""
pluginList = [
"Sofa.Component.AnimationLoop",
"Sofa.Component.Collision.Detection.Algorithm",
"Sofa.Component.Collision.Detection.Intersection",
"Sofa.Component.Collision.Geometry",
"Sofa.Component.Collision.Response.Contact",
"Sofa.Component.Constraint.Lagrangian.Correction",
"Sofa.Component.Constraint.Lagrangian.Solver",
"Sofa.Component.LinearSolver.Direct",
"Sofa.Component.Mapping.Linear",
"Sofa.Component.Mass",
"Sofa.Component.SolidMechanics.FEM.Elastic",
"Sofa.Component.StateContainer",
"Sofa.Component.Topology.Container.Constant",
"Sofa.Component.Topology.Container.Dynamic",
"Sofa.Component.Topology.Container.Grid",
"Sofa.Component.Visual",
"Sofa.Component.Mapping.NonLinear",
"Sofa.GUI.Component",
"Sofa.Component.Engine.Select",
"ArticulatedSystemPlugin",
"Sofa.Component.Constraint.Projective",
"Sofa.Component.SolidMechanics.Spring",
"Sofa.Component.LinearSolver.Iterative",
]
# Mesh generation from Fusion360: https://www.sofa-framework.org/community/forum/topic/easiest-way-from-stl-to-vtk/
scene = Scene(
rootNode,
dt=0.001,
gravity=[0.0, -9.81, 0.0],
iterative=True,
plugins=pluginList,
)
scene.addMainHeader()
scene.addObject(
"CollisionPipeline", name="DefaultPipeline"
) # To surpress warning from contactheader.py
scene.addContact(alarmDistance=1, contactDistance=0.03, frictionCoef=0.1)
noodleRobot = NoodleRobot()
scene.Simulation.addChild(noodleRobot)
noodleRobot.CubeBot.Red.addObject('UncoupledConstraintCorrection')
noodleRobot.CubeBot.Red.Articulation.addObject('UncoupledConstraintCorrection')
noodleRobot.RigidifiedStructure.DeformableParts.addObject('UncoupledConstraintCorrection')
noodleRobot.RigidifiedStructure.RigidParts.addObject('UncoupledConstraintCorrection')
noodleRobot.RigidifiedStructure.RigidParts.RigidifiedParticules.addObject('UncoupledConstraintCorrection')
Floor(scene.Modelling, translation=[0.0, -3.0, 0.0], rotation=[15.0, 0.0, 0.0], uniformScale=0.4, isAStaticObject=True)
return rootNode