Commit 07a9cd09 authored by Sreekanth Arikatla's avatar Sreekanth Arikatla
Browse files

Merge branch 'Pbd1dChanges' into 'master'

Pbd1d changes

See merge request iMSTK/iMSTK!377
parents adae8a49 e4590b02
......@@ -84,7 +84,12 @@ main()
// Constraints
pbdParams->enableConstraint(PbdConstraint::Type::Distance, 0.1);
pbdParams->enableConstraint(PbdConstraint::Type::Dihedral, 0.001);
pbdParams->m_fixedNodeIds = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
std::vector<size_t> fixedNodes(nCols);
for (size_t i = 0; i < fixedNodes.size(); i++)
{
fixedNodes[i] = i;
}
pbdParams->m_fixedNodeIds = fixedNodes;
// Other parameters
pbdParams->m_uniformMassValue = 1.0;
......
###########################################################################
#
# Copyright (c) Kitware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###########################################################################
project(Example-PBDString)
#-----------------------------------------------------------------------------
# Create executable
#-----------------------------------------------------------------------------
imstk_add_executable(${PROJECT_NAME} pbdStringExample.cpp)
#-----------------------------------------------------------------------------
# Add the target to Examples folder
#-----------------------------------------------------------------------------
SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES FOLDER Examples)
#-----------------------------------------------------------------------------
# Link libraries to executable
#-----------------------------------------------------------------------------
target_link_libraries(${PROJECT_NAME} SimulationManager apiUtilities)
#-----------------------------------------------------------------------------
# Set MSVC working directory to the install/bin directory
#-----------------------------------------------------------------------------
if(MSVC) # Configure running executable out of MSVC
set_property(TARGET ${PROJECT_NAME} PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${iMSTK_INSTALL_BIN_DIR}")
endif()
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkAPIUtilities.h"
#include "imstkLineMesh.h"
#include "imstkPbdModel.h"
#include "imstkPbdObject.h"
#include "imstkPbdSolver.h"
#include "imstkSimulationManager.h"
using namespace imstk;
///
/// \brief This example demonstrates string simulation
/// using Position based dynamics with varying bend stiffnesses
///
int
main()
{
auto sdk = std::make_shared<SimulationManager>();
auto scene = sdk->createNewScene("PBDString");
// Setup N separate string simulations with varying bend stiffnesses
const unsigned int numStrings = 8;
const unsigned int numVerts = 30;
const double stringSpacing = 2.0; // How far each string is apart
const double stringLength = 10.0; // Total length of string
const Color startColor = Color::Red; // Color of first string
const Color endColor = Color::Green; // Color of last string
struct PbdSim
{
std::shared_ptr<LineMesh> geometry;
std::shared_ptr<PbdObject> object;
std::shared_ptr<PbdModel> model;
std::shared_ptr<PBDModelConfig> params;
std::shared_ptr<VisualModel> visualModel;
std::shared_ptr<PbdSolver> solver;
};
std::vector<PbdSim> sims(numStrings);
const double size = stringSpacing * (numStrings - 1);
const double vertexSpacing = stringLength / numVerts;
for (unsigned int i = 0; i < numStrings; i++)
{
// Setup the line mesh
sims[i].geometry = std::make_shared<LineMesh>();
StdVectorOfVec3d vertList;
vertList.resize(numVerts);
for (size_t j = 0; j < numVerts; j++)
{
vertList[j] = Vec3d(
static_cast<double>(i) * stringSpacing - size * 0.5,
stringLength * 0.5 - static_cast<double>(j) * vertexSpacing, 0.0);
}
sims[i].geometry->setInitialVertexPositions(vertList);
sims[i].geometry->setVertexPositions(vertList);
// Add connectivity data
std::vector<LineMesh::LineArray> segments;
for (size_t j = 0; j < numVerts - 1; j++)
{
LineMesh::LineArray seg = { j, j + 1 };
segments.push_back(seg);
}
sims[i].geometry->setLinesVertices(segments);
sims[i].object = std::make_shared<PbdObject>("String " + std::to_string(i));
sims[i].model = std::make_shared<PbdModel>();
sims[i].model->setModelGeometry(sims[i].geometry);
// Configure the parameters with bend stiffnesses varying from 0.001 to ~0.1
sims[i].params = std::make_shared<PBDModelConfig>();
sims[i].params->enableConstraint(PbdConstraint::Type::Distance, 0.001);
sims[i].params->enableConstraint(PbdConstraint::Type::Bend, static_cast<double>(i) * 0.1 / numStrings + 0.001);
sims[i].params->m_fixedNodeIds = { 0 }; // Fix the first node in each string
sims[i].params->m_uniformMassValue = 5.0;
sims[i].params->m_gravity = Vec3d(0, -9.8, 0);
sims[i].params->m_dt = 0.0005;
sims[i].params->m_maxIter = 5;
// Set the parameters
sims[i].model->configure(sims[i].params);
sims[i].object->setDynamicalModel(sims[i].model);
sims[i].object->setPhysicsGeometry(sims[i].geometry);
sims[i].visualModel = std::make_shared<VisualModel>(sims[i].geometry);
std::shared_ptr<RenderMaterial> material = std::make_shared<RenderMaterial>();
material->setDisplayMode(RenderMaterial::DisplayMode::WIREFRAME);
material->setDebugColor(Color::lerpRgb(startColor, endColor, static_cast<double>(i) / (numStrings - 1)));
material->setLineWidth(2.0f);
sims[i].visualModel->setRenderMaterial(material);
sims[i].object->addVisualModel(sims[i].visualModel);
// Solver
sims[i].solver = std::make_shared<PbdSolver>();
sims[i].solver->setPbdObject(sims[i].object);
scene->addNonlinearSolver(sims[i].solver);
// Add in scene
scene->addSceneObject(sims[i].object);
}
// Adjust the camera
scene->getCamera()->setFocalPoint(0.0, 0.0, 0.0);
scene->getCamera()->setPosition(0.0, 0.0, 15.0);
// Move the points every frame
double t = 0.0;
const double dt = 0.0005;
const double radius = 1.5;
auto movePoints =
[&sims, &t, dt, radius](Module* module)
{
for (unsigned int i = 0; i < sims.size(); i++)
{
Vec3d pos = sims[i].model->getCurrentState()->getVertexPosition(0);
// Move in circle, derivatives of parametric eq of circle
sims[i].model->getCurrentState()->setVertexPosition(0, imstk::Vec3d(
pos.x() + -std::sin(t) * radius * dt,
pos.y(),
pos.z() + std::cos(t) * radius * dt));
}
t += dt;
};
sdk->getSceneManager(scene)->setPostUpdateCallback(movePoints);
// Start
sdk->setActiveScene(scene);
sdk->startSimulation(SimulationStatus::RUNNING);
return 0;
}
......@@ -9,7 +9,7 @@
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
......@@ -35,172 +35,172 @@ using namespace imstk;
std::shared_ptr<imstk::RigidObject>
addMeshRigidObject(std::string& name, std::shared_ptr<Scene> scene, Vec3d pos)
{
// create cube object
auto meshObj = std::make_shared<RigidObject>(name);
// Load a tetrahedral mesh
auto tetMesh = imstk::MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.veg");
if (!tetMesh)
{
LOG(FATAL) << "Could not read mesh from file.";
}
// Extract the surface mesh
auto surfMesh = std::make_shared<SurfaceMesh>();
auto volTetMesh = std::dynamic_pointer_cast<TetrahedralMesh>(tetMesh);
if (!volTetMesh)
{
LOG(FATAL) << "Dynamic pointer cast from PointSet to TetrahedralMesh failed!";
}
volTetMesh->scale(15., Geometry::TransformType::ApplyToData);
volTetMesh->translate(pos, Geometry::TransformType::ApplyToData);
volTetMesh->extractSurfaceMesh(surfMesh, true);
// add visual model
auto renderModel = std::make_shared<VisualModel>(surfMesh);
auto mat = std::make_shared<RenderMaterial>();
mat->setDisplayMode(RenderMaterial::WIREFRAME_SURFACE);
mat->setLineWidth(2.);
mat->setColor(Color::Green);
renderModel->setRenderMaterial(mat);
meshObj->addVisualModel(renderModel);
// add dynamic model
auto rigidModel = std::make_shared<RigidBodyModel>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
rigidModel->configure(surfMesh, rigidProp, RigidBodyType::Kinematic);
meshObj->setPhysicsGeometry(surfMesh);
meshObj->setDynamicalModel(rigidModel);
// add cube to scene
scene->addSceneObject(meshObj);
return meshObj;
// create cube object
auto meshObj = std::make_shared<RigidObject>(name);
// Load a tetrahedral mesh
auto tetMesh = imstk::MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.veg");
if (!tetMesh)
{
LOG(FATAL) << "Could not read mesh from file.";
}
// Extract the surface mesh
auto surfMesh = std::make_shared<SurfaceMesh>();
auto volTetMesh = std::dynamic_pointer_cast<TetrahedralMesh>(tetMesh);
if (!volTetMesh)
{
LOG(FATAL) << "Dynamic pointer cast from PointSet to TetrahedralMesh failed!";
}
volTetMesh->scale(15., Geometry::TransformType::ApplyToData);
volTetMesh->translate(pos, Geometry::TransformType::ApplyToData);
volTetMesh->extractSurfaceMesh(surfMesh, true);
// add visual model
auto renderModel = std::make_shared<VisualModel>(surfMesh);
auto mat = std::make_shared<RenderMaterial>();
mat->setDisplayMode(RenderMaterial::WIREFRAME_SURFACE);
mat->setLineWidth(2.);
mat->setColor(Color::Green);
renderModel->setRenderMaterial(mat);
meshObj->addVisualModel(renderModel);
// add dynamic model
auto rigidModel = std::make_shared<RigidBodyModel>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
rigidModel->configure(surfMesh, rigidProp, RigidBodyType::Kinematic);
meshObj->setPhysicsGeometry(surfMesh);
meshObj->setDynamicalModel(rigidModel);
// add cube to scene
scene->addSceneObject(meshObj);
return meshObj;
}
std::shared_ptr<imstk::RigidObject>
addCubeRigidObject(std::string& name, std::shared_ptr<Scene> scene, Vec3d pos, const bool isStatic = false)
{
// create cube object
auto cubeObj = std::make_shared<RigidObject>(name);
// Create Cube object
auto cubeGeom = std::make_shared<Cube>();
cubeGeom->setWidth(20.);
cubeGeom->translate(pos);
// cube visual model
auto mesh = imstk::MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.obj");
auto SurfaceMesh = std::dynamic_pointer_cast<imstk::SurfaceMesh>(mesh);
SurfaceMesh->scale(5., Geometry::TransformType::ApplyToData);
auto renderModel = std::make_shared<VisualModel>(cubeGeom);
auto mat = std::make_shared<RenderMaterial>();
mat->setDisplayMode(RenderMaterial::WIREFRAME_SURFACE);
mat->setLineWidth(2.);
mat->setColor(Color::Orange);
renderModel->setRenderMaterial(mat);
cubeObj->addVisualModel(renderModel);
auto rigidMap = std::make_shared<IsometricMap>();
rigidMap->setMaster(cubeGeom);
rigidMap->setSlave(SurfaceMesh);
// cube dynamic model
auto rigidModel = std::make_shared<RigidBodyModel>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
// create cube object
auto cubeObj = std::make_shared<RigidObject>(name);
// Create Cube object
auto cubeGeom = std::make_shared<Cube>();
cubeGeom->setWidth(20.);
cubeGeom->translate(pos);
// cube visual model
auto mesh = imstk::MeshIO::read(iMSTK_DATA_ROOT "/asianDragon/asianDragon.obj");
auto SurfaceMesh = std::dynamic_pointer_cast<imstk::SurfaceMesh>(mesh);
SurfaceMesh->scale(5., Geometry::TransformType::ApplyToData);
auto renderModel = std::make_shared<VisualModel>(cubeGeom);
auto mat = std::make_shared<RenderMaterial>();
mat->setDisplayMode(RenderMaterial::WIREFRAME_SURFACE);
mat->setLineWidth(2.);
mat->setColor(Color::Orange);
renderModel->setRenderMaterial(mat);
cubeObj->addVisualModel(renderModel);
auto rigidMap = std::make_shared<IsometricMap>();
rigidMap->setMaster(cubeGeom);
rigidMap->setSlave(SurfaceMesh);
// cube dynamic model
auto rigidModel = std::make_shared<RigidBodyModel>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
rigidProp->m_dynamicFriction = 0.01;
rigidProp->m_restitution = 0.01;
rigidProp->m_staticFriction = 0.005;
rigidProp->m_restitution = 0.01;
rigidProp->m_staticFriction = 0.005;
rigidModel->configure(cubeGeom, rigidProp, RigidBodyType::Dynamic);
cubeObj->setDynamicalModel(rigidModel);
rigidModel->configure(cubeGeom, rigidProp, RigidBodyType::Dynamic);
cubeObj->setDynamicalModel(rigidModel);
//cubeObj->setPhysicsToVisualMap(rigidMap);
//cubeObj->setPhysicsToVisualMap(rigidMap);
// add cube to scene
scene->addSceneObject(cubeObj);
return cubeObj;
// add cube to scene
scene->addSceneObject(cubeObj);
return cubeObj;
}
void
addPlaneRigidObject(std::shared_ptr<Scene> scene)
{
// create plane object
auto planeObj = std::make_shared<RigidObject>("Plane");
auto planeGeom = std::make_shared<Plane>();
planeGeom->setWidth(400.);
// visual model
auto renderModel2 = std::make_shared<VisualModel>(planeGeom);
renderModel2->setRenderMaterial(std::make_shared<RenderMaterial>());
planeObj->addVisualModel(renderModel2);
// dynamic model
auto rigidModel2 = std::make_shared<RigidBodyModel>();
auto rigidProp2 = std::make_shared<RigidBodyPropertyDesc>();
/* rigidProp2->m_staticFriction = 0.1;
rigidProp2->m_dynamicFriction = 0.1;
rigidProp2->m_restitution = 0.1;*/
rigidModel2->configure(planeGeom, rigidProp2, RigidBodyType::Static);
planeObj->setDynamicalModel(rigidModel2);
scene->addSceneObject(planeObj);
// create plane object
auto planeObj = std::make_shared<RigidObject>("Plane");
auto planeGeom = std::make_shared<Plane>();
planeGeom->setWidth(400.);
// visual model
auto renderModel2 = std::make_shared<VisualModel>(planeGeom);
renderModel2->setRenderMaterial(std::make_shared<RenderMaterial>());
planeObj->addVisualModel(renderModel2);
// dynamic model
auto rigidModel2 = std::make_shared<RigidBodyModel>();
auto rigidProp2 = std::make_shared<RigidBodyPropertyDesc>();
/* rigidProp2->m_staticFriction = 0.1;
rigidProp2->m_dynamicFriction = 0.1;
rigidProp2->m_restitution = 0.1;*/
rigidModel2->configure(planeGeom, rigidProp2, RigidBodyType::Static);
planeObj->setDynamicalModel(rigidModel2);
scene->addSceneObject(planeObj);
}
void
addSphereRigidObject(std::shared_ptr<Scene> scene, Vec3d t = Vec3d(0., 0., 0.))
{
// create cube object
auto sphereObj = std::make_shared<RigidObject>("Sphere");
// Create Cube object
auto sphereGeom = std::make_shared<Sphere>();
sphereGeom->setRadius(10.);
sphereGeom->translate(t);
// cube visual model
auto renderModel = std::make_shared<VisualModel>(sphereGeom);
renderModel->setRenderMaterial(std::make_shared<RenderMaterial>());
sphereObj->addVisualModel(renderModel);
// cube dynamic model
auto rigidModel3 = std::make_shared<RigidBodyModel>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
rigidModel3->configure(sphereGeom, rigidProp, RigidBodyType::Dynamic);
sphereObj->setDynamicalModel(rigidModel3);
// add cube to scene
scene->addSceneObject(sphereObj);
// create cube object
auto sphereObj = std::make_shared<RigidObject>("Sphere");
// Create Cube object
auto sphereGeom = std::make_shared<Sphere>();
sphereGeom->setRadius(10.);
sphereGeom->translate(t);
// cube visual model
auto renderModel = std::make_shared<VisualModel>(sphereGeom);
renderModel->setRenderMaterial(std::make_shared<RenderMaterial>());
sphereObj->addVisualModel(renderModel);
// cube dynamic model
auto rigidModel3 = std::make_shared<RigidBodyModel>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
rigidModel3->configure(sphereGeom, rigidProp, RigidBodyType::Dynamic);
sphereObj->setDynamicalModel(rigidModel3);
// add cube to scene
scene->addSceneObject(sphereObj);
}
int
main()
{
//SDK and Scene
auto sdk = std::make_shared<SimulationManager>();
auto scene = sdk->createNewScene("Rigid Body Dynamics");
//SDK and Scene
auto sdk = std::make_shared<SimulationManager>();
auto scene = sdk->createNewScene("Rigid Body Dynamics");
for (int i = 0; i < 10; ++i)
{
addCubeRigidObject(std::string("cube_").append(std::to_string(i)), scene, Vec3d(0., 10. + i * 21, 0.));
}
for (int i = 0; i < 10; ++i)
{
addCubeRigidObject(std::string("cube_").append(std::to_string(i)), scene, Vec3d(0., 10. + i * 21, 0.));
}
addPlaneRigidObject(scene);
//addSphereRigidObject(scene, Vec3d(0., 200, 0.));
//auto rigidObj = addMeshRigidObject(std::string("dragon"), scene, Vec3d(0., 30., 0.));
addPlaneRigidObject(scene);
//addSphereRigidObject(scene, Vec3d(0., 200, 0.));
//auto rigidObj = addMeshRigidObject(std::string("dragon"), scene, Vec3d(0., 30., 0.));
// Set Camera configuration
auto cam = scene->getCamera();
cam->setPosition(Vec3d(300, 300, 300));
// Set Camera configuration
auto cam = scene->getCamera();
cam->setPosition(Vec3d(300, 300, 300));
// Light
auto light = std::make_shared<DirectionalLight>("light");
light->setIntensity(1);
scene->addLight(light);
// Light
auto light = std::make_shared<DirectionalLight>("light");
light->setIntensity(1);
scene->addLight(light);
// Run
sdk->setActiveScene(scene);
sdk->startSimulation(SimulationStatus::PAUSED);
// Run
sdk->setActiveScene(scene);
sdk->startSimulation(SimulationStatus::PAUSED);
return 0;
return 0;
}
......@@ -122,8 +122,8 @@ addCubeRigidObject(std::string& name, std::shared_ptr<Scene> scene, Vec3d pos, c
// cube dynamic model
auto rigidModel = std::make_shared<RigidBodyModel>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
auto rigidProp = std::make_shared<RigidBodyPropertyDesc>();
rigidModel->configure(cubeGeom, rigidProp, RigidBodyType::Dynamic);
cubeObj->setDynamicalModel(rigidModel);
......
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkPbdBendConstraint.h"
#include "imstkPbdModel.h"
namespace imstk
{
void
PbdBendConstraint::initConstraint(PbdModel& model,
const size_t& pIdx1, const size_t& pIdx2,
const size_t& pIdx3, const double k)
{
m_vertexIds[0] = pIdx1;