7 #include "imstkObjectControllerGhost.h" 8 #include "imstkGeometry.h" 9 #include "imstkPbdObjectController.h" 10 #include "imstkRenderMaterial.h" 11 #include "imstkRigidObjectController.h" 12 #include "imstkSceneObject.h" 13 #include "imstkVisualModel.h" 17 ObjectControllerGhost::ObjectControllerGhost(
const std::string& name) :
SceneBehaviour(name),
18 m_ghostVisualModel(
std::make_shared<VisualModel>(
"GhostVisualModel"))
20 m_ghostVisualModel->getRenderMaterial()->setColor(Color::Orange);
21 m_ghostVisualModel->getRenderMaterial()->setLineWidth(5.0);
22 m_ghostVisualModel->getRenderMaterial()->setOpacity(0.3);
23 m_ghostVisualModel->getRenderMaterial()->setIsDynamicMesh(
false);
31 std::shared_ptr<Entity> entity =
m_entity.lock();
32 CHECK(entity !=
nullptr) <<
"ObjectControllerGhost must have entity to initialize";
33 if (!entity->containsComponent(m_ghostVisualModel))
35 m_ghostVisualModel->setName(entity->getName() +
"_GhostVisualModel");
36 entity->addComponent(m_ghostVisualModel);
39 CHECK(m_pbdController !=
nullptr || m_rbdController !=
nullptr)
40 <<
"ObjectControllerGhost must have a controller";
43 std::shared_ptr<SceneObject> controlledObj =
nullptr;
44 if (m_pbdController !=
nullptr)
46 controlledObj = m_pbdController->getControlledObject();
50 controlledObj = m_rbdController->getControlledObject();
52 std::shared_ptr<Geometry> ghostGeom = controlledObj->getVisualGeometry()->clone();
53 CHECK(ghostGeom !=
nullptr) <<
"Failed to copy controller geometry";
55 if (m_pbdController !=
nullptr)
57 ghostGeom->translate(-1.0 * m_pbdController->getHapticOffset(), Geometry::TransformType::ApplyToData);
60 m_ghostVisualModel->setGeometry(ghostGeom);
64 ObjectControllerGhost::visualUpdate(
const double&)
66 Quatd orientation = Quatd::Identity();
67 Vec3d position = Vec3d::Zero();
68 Vec3d force = Vec3d::Zero();
69 if (m_pbdController !=
nullptr)
71 orientation = m_pbdController->getOrientation();
72 position = m_pbdController->getPosition();
73 force = m_pbdController->getDeviceForce();
77 orientation = m_rbdController->getOrientation();
78 position = m_rbdController->getPosition();
79 force = m_rbdController->getDeviceForce();
83 std::shared_ptr<Geometry> toolGhostMesh = m_ghostVisualModel->getGeometry();
84 toolGhostMesh->setTranslation(position);
85 toolGhostMesh->setRotation(orientation);
86 toolGhostMesh->updatePostTransformData();
87 toolGhostMesh->postModified();
92 m_ghostVisualModel->getRenderMaterial()->setOpacity(std::min(1.0, force.norm() / 15.0));
std::weak_ptr< Entity > m_entity
Parent entity this component exists on.
void init() override
Initialize the component, called at a later time after all component construction is complete...