-
Notifications
You must be signed in to change notification settings - Fork 297
/
SceneCreator.cpp
719 lines (576 loc) · 29.5 KB
/
SceneCreator.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include "SceneCreator.h"
#include <SceneCreator/config.h>
#include <sofa/simulation/Simulation.h>
#include <sofa/simulation/graph/DAGSimulation.h>
#include "GetVectorVisitor.h"
#include "GetAssembledSizeVisitor.h"
#include <sofa/defaulttype/VecTypes.h>
using sofa::defaulttype::Vec3Types ;
#include <sofa/helper/system/FileRepository.h>
using sofa::helper::system::DataRepository ;
#include <sofa/simpleapi/SimpleApi.h>
using sofa::simpleapi::str ;
using sofa::simpleapi::createObject ;
using sofa::simpleapi::createChild ;
namespace sofa
{
namespace modeling {
/////////////////// IMPORTING THE DEPENDENCIES INTO THE NAMESPACE ///////////////////////////
using namespace sofa::type;
using namespace sofa::defaulttype ;
using type::vector;
using sofa::simulation::graph::DAGSimulation ;
using sofa::simulation::GetAssembledSizeVisitor ;
using sofa::simulation::GetVectorVisitor ;
using sofa::simulation::Node ;
using sofa::core::objectmodel::BaseData ;
using sofa::core::objectmodel::New ;
using sofa::helper::system::DataRepository ;
static sofa::simulation::Node::SPtr root = nullptr;
using sofa::core::objectmodel::BaseObject ;
Node::SPtr createRootWithCollisionPipeline(const std::string& responseType)
{
root = simulation::getSimulation()->createNewGraph("root");
simpleapi::createObject(root, "CollisionPipeline", {{"name","Collision Pipeline"}}) ;
simpleapi::createObject(root, "BruteForceBroadPhase", {{"name","Broad Phase Detection"}}) ;
simpleapi::createObject(root, "BVHNarrowPhase", {{"name","Narrow Phase Detection"}}) ;
simpleapi::createObject(root, "MinProximityIntersection", {{"name","Proximity"},
{"alarmDistance", "0.3"},
{"contactDistance", "0.2"}}) ;
simpleapi::createObject(root, "CollisionResponse", {
{"name", "Contact Manager"},
{"response", responseType}
});
return root;
}
Node::SPtr createEulerSolverNode(Node::SPtr parent, const std::string& name, const std::string &scheme)
{
Node::SPtr node = simpleapi::createChild(parent, name);
if (scheme == "Explicit")
{
simpleapi::createObject(parent, "RequiredPlugin", {{"name", "Sofa.Component.ODESolver.Forward"}});
simpleapi::createObject(node, "EulerExplicitSolver", {{"name","Euler Explicit"}});
return node ;
}
if (scheme == "Implicit")
{
simpleapi::createObject(parent, "RequiredPlugin", {{"name", "Sofa.Component.ODESolver.Backward"}});
simpleapi::createObject(parent, "RequiredPlugin", {{"name", "Sofa.Component.LinearSolver.Iterative"}});
simpleapi::createObject(node, "EulerImplicitSolver", {{"name","Euler Implicit"},
{"rayleighStiffness","0.01"},
{"rayleighMass", "1.0"}}) ;
simpleapi::createObject(node, "CGLinearSolver", {{"name","Conjugate Gradient"},
{"iterations","25"},
{"threshold", "0.00001"},
{"tolerance", "0.00001"}}) ;
return node;
}
if (scheme == "Implicit_SparseLDL")
{
simpleapi::createObject(parent, "RequiredPlugin", {{"name", "Sofa.Component.ODESolver.Backward"}});
simpleapi::createObject(parent, "RequiredPlugin", {{"name", "Sofa.Component.LinearSolver.Direct"}});
simpleapi::createObject(node, "EulerImplicitSolver", {{"name","Euler Implicit"},
{"rayleighStiffness","0.01"},
{"rayleighMass", "1.0"}}) ;
simpleapi::createObject(node, "SparseLDLSolver", {{"name","Sparse LDL Solver"}});
return node;
}
msg_error("SceneCreator") << scheme << " Integration Scheme not recognized. " ;
return node;
}
Node::SPtr createObstacle(Node::SPtr parent, const std::string &filenameCollision,
const std::string filenameVisual, const std::string& color,
const Deriv3& translation, const Deriv3 &rotation)
{
Node::SPtr nodeFixed = simpleapi::createChild(parent, "Fixed") ;
simpleapi::createObject(nodeFixed, "MeshOBJLoader", {
{"name","loader"},
{"filename", DataRepository.getFile(filenameCollision)}
});
simpleapi::createObject(nodeFixed, "MeshTopology", {
{"name","topology"},
{"src", "@loader"}
});
simpleapi::createObject(nodeFixed, "MechanicalObject", {
{"name","mecha"},
{"template","vec3"},
{"src", "@loader"},
{"translation", str(translation)},
{"rotation", str(rotation)}
});
simpleapi::createObject(nodeFixed, "TriangleCollisionModel", {
{"name", "Collision Fixed"},
{"simulated", "false"},
{"moving", "false"},
});
simpleapi::createObject(nodeFixed, "LineCollisionModel", {
{"name", "Collision Fixed"},
{"simulated", "false"},
{"moving", "false"},
});
simpleapi::createObject(nodeFixed, "PointCollisionModel", {
{"name", "Collision Fixed"},
{"simulated", "false"},
{"moving", "false"},
});
simpleapi::createObject(nodeFixed, "LineCollisionModel", {
{"name", "Collision Fixed"},
{"simulated", "false"},
{"moving", "false"},
});
simpleapi::createObject(nodeFixed, "OglModel", {
{"name", "visual"},
{"filename", DataRepository.getFile(filenameVisual)},
{"color", color},
{"translation", str(translation)},
{"rotation", str(rotation)}
});
return nodeFixed;
}
Node::SPtr createCollisionNodeVec3(Node::SPtr parent, BaseObject::SPtr dof,
const std::string &filename,
const std::vector<std::string> &elements,
const Deriv3& translation, const Deriv3 &rotation)
{
SOFA_UNUSED(dof) ;
Node::SPtr node = simpleapi::createChild(parent, "Collision");
simpleapi::createObject(node, "MeshOBJLoader", {
{"name", "loader"},
{"filename", DataRepository.getFile(filename)}});
simpleapi::createObject(node, "MeshTopology", {{"name", "loader"},
{"src", "@loader"}});
simpleapi::createObject(node, "MechanicalObject", {{"name", "meca"},
{"src", "@loader"},
{"template","vec3"},
{"translation",str(translation)},
{"rotation",str(rotation)}});
addCollisionModels(node, elements);
simpleapi::createObject(node, "BarycentricMapping", {{"name", "mapping"},
{"input", "@.."},
{"output","@."}});
return node;
}
simulation::Node::SPtr createVisualNodeVec3(simulation::Node::SPtr parent,
BaseObject::SPtr dof,
const std::string &filename, const std::string& color,
const Deriv3& translation, const Deriv3 &rotation,
const MappingType &mappingT)
{
SOFA_UNUSED(dof) ;
Node::SPtr node = simpleapi::createChild(parent, "visualNode") ;
std::string mappingType ;
const std::string nameVisual="Visual";
const std::string refVisual = "@" + nameVisual;
const std::string refDof = "@..";
simpleapi::createObject(node, "OglModel", {
{"name", nameVisual},
{"filename", DataRepository.getFile(filename)},
{"color", color},
{"translation", str(translation)},
{"rotation", str(rotation)}
});
if (mappingT == MT_Barycentric) // TODO check if possible to create a baseMapping::SPtr before the switch
mappingType = "BarycentricMapping" ;
else if (mappingT == MT_Identity)
mappingType = "IdentityMapping" ;
else
{
msg_error("SceneCreator") << "Visual Mapping creation not possible. Mapping should be Barycentric or Identity. Found MappingType enum: " << mappingT ;
return node ;
}
simpleapi::createObject(node, mappingType, {
{"name", nameVisual},
{"template", "Vec3,Vec3"},
{"input", refDof},
{"output", refVisual}});
return node;
}
Node::SPtr createCollisionNodeRigid(Node::SPtr parent, BaseObject::SPtr dofRigid,
const std::string &filename,
const std::vector<std::string> &elements,
const Deriv3& translation, const Deriv3 &rotation)
{
const std::string refdofRigid = "@../" + dofRigid->getName();
const std::string dofSurfName = "CollisionObject";
const std::string refdofSurf = "@"+dofSurfName;
Node::SPtr node=simpleapi::createChild(parent, "Collision");
simpleapi::createObject(node, "MeshOBJLoader", {
{"name","loader"},
{"filename", DataRepository.getFile(filename)}}) ;
simpleapi::createObject(node, "MeshTopology", {{"src", "@loader"}});
simpleapi::createObject(node, "MechanicalObject", {
{"name",dofSurfName},
{"translation", str(translation)},
{"rotation", str(rotation)}});
addCollisionModels(node, elements);
simpleapi::createObject(node, "RigidMapping", {
{"input", refdofRigid},
{"output", refdofSurf}});
return node;
}
Node::SPtr createVisualNodeRigid(Node::SPtr parent, BaseObject::SPtr dofRigid,
const std::string &filename, const std::string& color,
const Deriv3& translation, const Deriv3 &rotation)
{
const std::string nameVisual="Visual";
const std::string refVisual="@"+nameVisual;
const std::string refdofRigid="@../"+dofRigid->getName();
Node::SPtr node=simpleapi::createChild(parent, "Visu");
simpleapi::createObject(node, "OglModel", {
{"name",nameVisual},
{"filename", DataRepository.getFile(filename)},
{"color", color},
{"translation", str(translation)},
{"rotation",str(rotation)}});
simpleapi::createObject(node, "RigidMapping", {
{"name", "Mapping Visual"},
{"input", refdofRigid},
{"output", refVisual}
});
return node;
}
void addCollisionModels(Node::SPtr parent, const std::vector<std::string> &elements)
{
std::map<std::string, std::string> alias = {
{"TriangleCollisionModel", "TriangleCollisionModel"},
{"LineCollisionModel", "LineCollisionModel"},
{"PointCollisionModel", "PointCollisionModel"},
{"SphereCollisionModel", "SphereCollisionModel"},
{"CapsuleCollisionModel", "CapsuleCollisionModel"},
{"OBBCollisionModel", "OBBCollisionModel"},
};
for (auto& element : elements)
{
if( alias.find(element) == alias.end() )
{
msg_error(parent.get()) << "Unable to create collision model from '"<< element << "'" ;
continue;
}
simpleapi::createObject(parent, alias[element], {{"name", element+"Collision"}}) ;
}
}
void addTetraFEM(simulation::Node::SPtr parent, const std::string& objectName,
SReal totalMass, SReal young, SReal poisson)
{
simpleapi::createObject(parent, "UniformMass", {
{"name",objectName + "_mass"},
{"totalMass", str(totalMass)},
});
simpleapi::createObject(parent, "TetrahedronFEMForceField", {
{"name",objectName + "_FEM"},
{"computeGlobalMatrix", "false"},
{"method", "large"},
{"poissonRatio", str(poisson)},
{"youngModulus", str(young)}
});
}
void addTriangleFEM(simulation::Node::SPtr node, const std::string& objectName,
SReal totalMass, SReal young, SReal poisson)
{
simpleapi::createObject(node, "UniformMass", {
{"name", objectName+"_mass"},
{"totalMass", str(totalMass)}});
simpleapi::createObject(node, "TriangularFEMForceField", {
{"name", objectName+"_FEM"},
{"method", "large"},
{"poissonRatio", str(poisson)},
{"youngModulus", str(young)}
});
}
simulation::Node::SPtr addCube(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize, SReal totalMass, SReal young, SReal poisson,
const Deriv3& translation, const Deriv3 &rotation, const Deriv3 &scale)
{
//TODO(dmarchal): It is unclear to me if this message should be a msg_ (for end user)
// or dmsg_ for developpers.
if (parent == nullptr){
msg_warning("SceneCreator") << "Parent node is nullptr. Returning Null Pointer." ;
return nullptr;
}
// TODO: epernod: this should be tested in the regularGrid code to avoid crash.
if (gridSize[0] < 1 || gridSize[1] < 1 || gridSize[2] < 1){
msg_warning("SceneCreator") << "Grid Size has a non positive value. Returning Null Pointer." ;
return nullptr;
}
// Check rigid
bool isRigid = false;
if (totalMass < 0.0 || young < 0.0 || poisson < 0.0)
isRigid = true;
// Add Cube Node
sofa::simulation::Node::SPtr cube;
if (isRigid)
cube = parent->createChild(objectName + "_node");
else
cube = sofa::modeling::createEulerSolverNode(parent, objectName + "_node");
const auto dofFEM = simpleapi::createObject(cube, "MechanicalObject", {
{"name", objectName+"_dof"},
{"translation", str(translation)},
{"rotation", str(rotation)},
{"scale", str(scale)}
});
// Add FEM and Mass system
if (!isRigid) // Add FEM and Mass system
addTetraFEM(cube, objectName, totalMass, young, poisson);
simpleapi::createObject(cube, "RegularGridTopology", {
{"name", objectName+"_grid"},
{"n", str(gridSize)},
{"min", "-0.5 -0.5 -0.5"},
{"max", " 0.5 0.5 0.5"}});
// Add collisions models
std::vector<std::string> colElements;
colElements.push_back("TriangleCollisionModel");
colElements.push_back("LineCollisionModel");
colElements.push_back("PointCollisionModel");
sofa::modeling::addCollisionModels(cube, colElements);
//Node VISUAL
createVisualNodeVec3(cube, dofFEM, "", "red", Deriv3(), Deriv3(), MT_Identity);
return cube;
}
simulation::Node::SPtr addRigidCube(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize,
const Deriv3& translation, const Deriv3 &rotation, const Deriv3 &scale)
{
return addCube(parent, objectName, gridSize, -1.f, -1.f, -1.f, translation, rotation, scale);
}
simulation::Node::SPtr addCylinder(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize, const Deriv3& axis, SReal radius, SReal length,
SReal totalMass, SReal young, SReal poisson,
const Deriv3& translation, const Deriv3 &rotation, const Deriv3 &scale)
{
//TODO(dmarchal): It is unclear to me if this message should be a msg_ (for end user)
// or dmsg_ for developpers.
if (parent == nullptr){
msg_warning("SceneCreator") << "Warning: parent node is nullptr. Returning Null Pointer." ;
return nullptr;
}
// TODO: epernod: this should be tested in the regularGrid code to avoid crash.
if (gridSize[0] < 1 || gridSize[1] < 1 || gridSize[2] < 1){
msg_warning("SceneCreator") << "Warning: Grid Size has a non positive value. Returning Null Pointer." ;
return nullptr;
}
// Check rigid
bool isRigid = false;
if (totalMass < 0.0 || young < 0.0 || poisson < 0.0)
isRigid = true;
// Add Cylinder Node
sofa::simulation::Node::SPtr cylinder;
if (isRigid)
cylinder = parent->createChild(objectName + "_node");
else
cylinder = sofa::modeling::createEulerSolverNode(parent, objectName + "_node");
const auto dofFEM = simpleapi::createObject(cylinder, "MechanicalObject", {
{"name", objectName+"_dof"},
{"translation", str(translation)},
{"rotation", str(rotation)},
{"scale", str(scale)}
});
if (!isRigid) // Add FEM and Mass system
addTetraFEM(cylinder, objectName, totalMass, young, poisson);
simpleapi::createObject(cylinder, "CylinderGridTopology", {
{"name", objectName+"_grid"},
{"n", str(gridSize)},
{"radius", str(radius)},
{"length", str(length)},
{"axis", str(axis)}});
addCollisionModels(cylinder, {"TriangleCollisionModel", "LineCollisionModel", "PointCollisionModel"});
//Node VISUAL
createVisualNodeVec3(cylinder, dofFEM, "", "red", Deriv3(), Deriv3(), MT_Identity);
return cylinder;
}
simulation::Node::SPtr addRigidCylinder(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize, const Deriv3& axis, SReal radius, SReal length,
const Deriv3& translation, const Deriv3 &rotation, const Deriv3 &scale)
{
return addCylinder(parent, objectName, gridSize, axis, radius, length, -1.f, -1.f, -1.f, translation, rotation, scale);
}
simulation::Node::SPtr addSphere(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize, const Deriv3& axis, SReal radius,
SReal totalMass, SReal young, SReal poisson,
const Deriv3& translation, const Deriv3 &rotation, const Deriv3 &scale)
{
//TODO(dmarchal): It is unclear to me if this message should be a msg_ (for end user)
// or dmsg_ for developpers.
if (parent == nullptr){
msg_warning("SceneCreator") << "Warning: parent node is nullptr. Returning Null Pointer." ;
return nullptr;
}
// TODO: epernod: this should be tested in the regularGrid code to avoid crash.
if (gridSize[0] < 1 || gridSize[1] < 1 || gridSize[2] < 1){
msg_warning("SceneCreator") << "Warning: Grid Size has a non positive value. Returning Null Pointer." ;
return nullptr;
}
// Check rigid
bool isRigid = false;
if (totalMass < 0.0 || young < 0.0 || poisson < 0.0)
isRigid = true;
// Add Sphere Node
sofa::simulation::Node::SPtr sphere;
if (isRigid)
sphere = parent->createChild(objectName + "_node");
else
sphere = sofa::modeling::createEulerSolverNode(parent, objectName + "_node");
const auto dofFEM = simpleapi::createObject(sphere, "MechanicalObject", {
{"name", objectName+"_dof"},
{"translation", str(translation)},
{"rotation", str(rotation)},
{"scale", str(scale)}
});
if (!isRigid) // Add FEM and Mass system
addTetraFEM(sphere, objectName, totalMass, young, poisson);
simpleapi::createObject(sphere, "SphereGridTopology", {
{"name", objectName+"_grid"},
{"n", str(gridSize)},
{"radius", str(radius)},
{"axis", str(axis)}});
addCollisionModels(sphere, {"TriangleCollisionModel", "LineCollisionModel", "PointCollisionModel"});
createVisualNodeVec3(sphere, dofFEM, "", "red", Deriv3(), Deriv3(), MT_Identity);
return sphere;
}
simulation::Node::SPtr addRigidSphere(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize, const Deriv3& axis, SReal radius,
const Deriv3& translation, const Deriv3 &rotation, const Deriv3 &scale)
{
return addSphere(parent, objectName, gridSize, axis, radius, -1.f, -1.f, -1.f, translation, rotation, scale);
}
simulation::Node::SPtr addPlane(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize, SReal totalMass, SReal young, SReal poisson,
const Deriv3& translation, const Deriv3 &rotation, const Deriv3 &scale)
{
//TODO(dmarchal): It is unclear to me if this message should be a msg_ (for end user)
// or dmsg_ for developpers.
if (parent == nullptr){
msg_warning("SceneCreator") << " Parent node is nullptr. Returning Null Pointer." ;
return nullptr;
}
// TODO: epernod: this should be tested in the regularGrid code to avoid crash.
if (gridSize[0] < 1 || gridSize[1] < 1 || gridSize[2] < 1){
msg_warning("SceneCreator") << " Grid Size has a non positive value. Returning Null Pointer." ;
return nullptr;
}
// Check rigid
bool isRigid = false;
if (totalMass < 0.0 || young < 0.0 || poisson < 0.0)
isRigid = true;
// Add plane node
sofa::simulation::Node::SPtr plane;
if (isRigid)
plane = parent->createChild(objectName + "_node");
else
plane = sofa::modeling::createEulerSolverNode(parent, objectName + "_node");
const auto dofPlane = simpleapi::createObject(plane, "MechanicalObject", {
{"name", objectName+"_dof"},
{"translation", str(translation)},
{"rotation", str(rotation)},
{"scale", str(scale)}
});
if (!isRigid) // Add FEM and Mass system
addTetraFEM(plane, objectName, totalMass, young, poisson);
simpleapi::createObject(plane, "RegularGridTopology", {
{"name", objectName+"_grid"},
{"n", str(gridSize)},
{"min", "-0.5 -0.5 -0.5 "},
{"max", " 0.5 0.5 0.5 "}
});
addCollisionModels(plane, {"TriangleCollisionModel", "LineCollisionModel", "PointCollisionModel"});
createVisualNodeVec3(plane, dofPlane, "", "green", Deriv3(), Deriv3(), MT_Identity);
return plane;
}
simulation::Node::SPtr addRigidPlane(simulation::Node::SPtr parent, const std::string& objectName,
const Deriv3& gridSize, const Deriv3& translation,
const Deriv3 &rotation, const Deriv3 &scale)
{
return addPlane(parent, objectName, gridSize, -1.f, -1.f, -1.f, translation, rotation, scale);
}
/// Create a stiff string
Node::SPtr massSpringString(Node::SPtr parent,
double x0, double y0, double z0, // start point,
double x1, double y1, double z1, // end point
unsigned numParticles,
double totalMass,
double stiffnessValue,
double dampingRatio)
{
static unsigned numObject = 1;
std::ostringstream oss;
oss << "string_" << numObject++;
Vec3d startPoint(x0,y0,z0), endPoint(x1,y1,z1);
SReal totalLength = (endPoint-startPoint).norm();
std::stringstream positions ;
std::stringstream springs ;
for( unsigned i=0; i<numParticles; i++ )
{
double alpha = (double)i/(numParticles-1);
Vec3d currpos = startPoint * (1-alpha) + endPoint * alpha;
positions << str(currpos) << " ";
if(i>0)
{
springs << str(i-1) << " " << str(i) << " " << str(stiffnessValue)
<< " " << str(dampingRatio) << " " << str(totalLength/(numParticles-1)) ;
}
}
Node::SPtr node = simpleapi::createChild(parent, oss.str()) ;
simpleapi::createObject(node, "MechanicalObject", {
{"name", oss.str()+"_DOF"},
{"size", str(numParticles)},
{"position", positions.str()}
});
simpleapi::createObject(node, "UniformMass", {
{"name",oss.str()+"_mass"},
{"vertexMass", str(totalMass/numParticles)}});
simpleapi::createObject(node, "StiffSpringForceField", {
{"name", oss.str()+"_spring"},
{"spring", springs.str()}
});
return node;
}
Node::SPtr initSofa()
{
assert(sofa::simulation::getSimulation());
root = simulation::getSimulation()->createNewGraph("root");
return root;
}
Node::SPtr getRoot()
{
if(root==nullptr)
initSofa();
return root;
}
void initScene(Node::SPtr _root)
{
root = _root;
sofa::simulation::node::initRoot(_root.get());
}
Node::SPtr clearScene()
{
if( root )
sofa::simulation::node::unload( root );
root = simulation::getSimulation()->createNewGraph("");
return root;
}
void setDataLink(BaseData* source, BaseData* target)
{
target->setParent(source);
}
} // modeling
} // sofa