7 #include "imstkCamera.h" 8 #include "imstkGeometryUtilities.h" 9 #include "imstkKeyboardDeviceClient.h" 10 #include "imstkKeyboardSceneControl.h" 11 #include "imstkLineMesh.h" 12 #include "imstkLogger.h" 13 #include "imstkMouseDeviceClient.h" 14 #include "imstkMouseSceneControl.h" 16 #include "imstkPbdModel.h" 17 #include "imstkPbdModelConfig.h" 18 #include "imstkPbdObject.h" 19 #include "imstkRenderMaterial.h" 20 #include "imstkScene.h" 21 #include "imstkSceneManager.h" 22 #include "imstkSimulationManager.h" 23 #include "imstkSimulationUtils.h" 24 #include "imstkTaskGraphVizWriter.h" 25 #include "imstkVisualModel.h" 26 #include "imstkVTKViewer.h" 28 using namespace imstk;
33 static std::shared_ptr<PbdObject>
35 const std::string& name,
38 const double stringLength,
39 const double bendStiffness,
45 std::shared_ptr<LineMesh> stringMesh =
50 pbdParams->
enableConstraint(PbdModelConfig::ConstraintGenType::Distance, 1e7);
51 pbdParams->
enableConstraint(PbdModelConfig::ConstraintGenType::Bend, bendStiffness);
53 pbdParams->
m_dt = 0.0005;
62 material->setBackFaceCulling(
false);
63 material->setColor(color);
64 material->setLineWidth(2.0f);
65 material->setDisplayMode(RenderMaterial::DisplayMode::Wireframe);
68 visualModel->setGeometry(stringMesh);
72 stringObj->addVisualModel(visualModel);
73 stringObj->setPhysicsGeometry(stringMesh);
74 stringObj->setDynamicalModel(pbdModel);
75 stringObj->getPbdBody()->fixedNodeIds = { 0 };
76 stringObj->getPbdBody()->uniformMassValue = 5.0;
81 static std::vector<std::shared_ptr<PbdObject>>
82 makePbdStrings(
const int numStrings,
84 const double stringSpacing,
85 const double stringLength,
86 const Color& startColor,
87 const Color& endColor)
89 std::vector<std::shared_ptr<PbdObject>> pbdStringObjs(numStrings);
91 const double size = stringSpacing * (numStrings - 1);
93 for (
int i = 0; i < numStrings; i++)
95 const Vec3d tipPos = Vec3d(static_cast<double>(i) * stringSpacing - size * 0.5, stringLength * 0.5, 0.0);
96 const double t =
static_cast<double>(i) / (numStrings - 1);
98 pbdStringObjs[i] = makePbdString(
99 "String " + std::to_string(i),
103 (static_cast<double>(i) * 0.1 / numStrings + 0.001) * 1e6,
104 Color::lerpRgb(startColor, endColor, t));
107 return pbdStringObjs;
110 const double radius = 1.5;
111 const int numStrings = 8;
112 const int numVerts = 30;
113 const double stringSpacing = 2.0;
114 const double stringLength = 10.0;
115 const Color startColor =
Color(1.0, 0.0, 0.0);
130 scene->getActiveCamera()->setPosition(0.0, 0.0, 15.0);
133 std::vector<std::shared_ptr<PbdObject>> pbdStringObjs =
134 makePbdStrings(numStrings, numVerts, stringSpacing, stringLength, startColor, endColor);
136 for (std::shared_ptr<PbdObject> obj : pbdStringObjs)
138 scene->addSceneObject(obj);
150 sceneManager->pause();
152 connect<Event>(sceneManager, &SceneManager::postUpdate, [&](
Event*)
154 const double dt = sceneManager->
getDt();
155 for (
size_t i = 0; i < pbdStringObjs.size(); i++)
157 std::shared_ptr<PbdModel> model = pbdStringObjs[i]->getPbdModel();
158 model->getConfig()->m_dt = dt;
159 std::shared_ptr<VecDataArray<double, 3>> positions = pbdStringObjs[i]->getPbdBody()->vertices;
160 (*positions)[0] += Vec3d(
161 -std::sin(t) * radius * dt,
163 std::cos(t) * radius * dt);
169 driver->addModule(viewer);
170 driver->addModule(sceneManager);
171 driver->setDesiredDt(0.005);
174 std::shared_ptr<Entity> mouseAndKeyControls =
175 SimulationUtils::createDefaultSceneControl(driver);
176 scene->addSceneObject(mouseAndKeyControls);
183 writer->
setInput(scene->getTaskGraph());
184 writer->
setFileName(
"taskGraphBenchmarkExample.svg");
void configure(std::shared_ptr< PbdModelConfig > params)
Set simulation parameters.
void setActiveScene(std::shared_ptr< Scene > scene) override
Set scene to be rendered.
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
void setWriteNodeComputeTimesColor(bool writeNodeComputeTimesColor)
If on, will write the time the node took to complete as a color.
void enableConstraint(ConstraintGenType type, const double stiffness, const int bodyId=2)
Enables a constraint of type defined by ConstraintGenType with given stiffness. If constraint of that...
void write()
Writes the graph to a file given the filename.
void setFileName(std::string fileName)
The filename and path to write too.
unsigned int m_iterations
Internal constraints pbd solver iterations.
void setHighlightCriticalPath(bool highlightCriticalPath)
If on, will highlight the critical path in red.
double getDt() const
Get/Set the time step.
std::shared_ptr<T> obj = std::make_shared<T>(); equivalent, convenience class for STL shared allocati...
std::shared_ptr< LineMesh > toLineGrid(const Vec3d &start, const Vec3d &dir, const double length, const int dim)
Creates a set of connected lines.
void setRenderMaterial(std::shared_ptr< RenderMaterial > renderMaterial)
Set/Get render material.
void setWriteNodeComputeTimesText(bool writeNodeComputeTimesText)
If on, will write the time the node took to complete in name as text.
double m_dt
Time step size.
void setInput(std::shared_ptr< TaskGraph > graph)
The graph to write.
void setActiveScene(std::string newSceneName)
Sets the currently updating scene.
static LoggerG3 & startLogger()
Starts logger with default sinks, use getInstance to create a logger with no sinks.
Vec3d m_gravity
Gravity acceleration.