Commit df756c9f authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Add SceneObject class

Can directly used to create visual objects.

Also use unordered_map in SimulationManager.
parent ac3610fb
......@@ -5,8 +5,10 @@ include(imstkAddLibrary)
imstk_add_library( Scene
H_FILES
imstkScene.h
imstkSceneObject.h
CPP_FILES
imstkScene.cpp
imstkSceneObject.cpp
LIBRARIES
Core
Geometry
......
......@@ -21,11 +21,45 @@
#include "imstkScene.h"
#include <thread>
#include "g3log/g3log.hpp"
#include <g3log/g3log.hpp>
namespace imstk {
bool
Scene::isObjectRegistered(std::string sceneObjectName) const
{
return m_sceneObjectsMap.find(sceneObjectName) != m_sceneObjectsMap.end();
}
void
Scene::addSceneObject(std::shared_ptr<SceneObject>newSceneObject)
{
std::string newSceneObjectName = newSceneObject->getName();
if (this->isObjectRegistered(newSceneObjectName))
{
LOG(WARNING) << "Can not add object: '" << newSceneObjectName
<< "' is already registered in this scene.";
return;
}
m_sceneObjectsMap[newSceneObjectName] = newSceneObject;
LOG(INFO) << newSceneObjectName << " object added to " << m_name;
}
void
Scene::removeSceneObject(std::string sceneObjectName)
{
if (!this->isObjectRegistered(sceneObjectName))
{
LOG(WARNING) << "No object named '" << sceneObjectName
<< "' was registered in this scene.";
return;
}
m_sceneObjectsMap.erase(sceneObjectName);
LOG(INFO) << sceneObjectName << " object removed from " << m_name;
}
void
Scene::initModule()
{
......
......@@ -22,9 +22,11 @@
#ifndef imstkScene_h
#define imstkScene_h
#include <iostream>
#include <unordered_map>
#include <memory>
#include "imstkModule.h"
#include "imstkSceneObject.h"
namespace imstk {
class Scene : public Module
......@@ -35,11 +37,17 @@ public:
~Scene() = default;
bool isObjectRegistered(std::string sceneObjectName) const;
void addSceneObject(std::shared_ptr<SceneObject>newSceneObject);
void removeSceneObject(std::string sceneObjectName);
protected:
void initModule() override;
void runModule() override;
void cleanUpModule() override;
std::unordered_map<std::string, std::shared_ptr<SceneObject> > m_sceneObjectsMap;
};
}
......
/*=========================================================================
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 "imstkSceneObject.h"
namespace imstk {
std::shared_ptr<Geometry>
SceneObject::getVisualGeometry() const
{
return m_visualGeometry;
}
void
SceneObject::setVisualGeometry(std::shared_ptr<Geometry>geometry)
{
m_visualGeometry = geometry;
}
const SceneObjectType&
SceneObject::getType() const
{
return m_type;
}
const std::string&
SceneObject::getName() const
{
return m_name;
}
void
SceneObject::setName(std::string name)
{
m_name = name;
}
}
/*=========================================================================
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.
=========================================================================*/
#ifndef imstkSceneObject_h
#define imstkSceneObject_h
#include <memory>
#include "imstkGeometry.h"
namespace imstk {
enum class SceneObjectType
{
Visual,
Static,
VirtualCoupled,
Rigid,
Deformable
};
class SceneObject
{
public:
SceneObject(std::string name) :
m_name(name)
{}
~SceneObject() = default;
const SceneObjectType & getType() const;
const std::string & getName() const;
void setName(std::string name);
std::shared_ptr<Geometry>getVisualGeometry() const;
void setVisualGeometry(std::shared_ptr<Geometry>geometry);
protected:
SceneObjectType m_type = SceneObjectType::Visual;
std::string m_name;
std::shared_ptr<Geometry> m_visualGeometry;
};
using VisualObject = SceneObject;
}
#endif // ifndef imstkSceneObject_h
......@@ -33,13 +33,13 @@ SimulationStatus& SimulationManager::getStatus() const
}
bool
SimulationManager::isSceneRegistered(std::string sceneName)
SimulationManager::isSceneRegistered(std::string sceneName) const
{
return !(m_sceneMap.find(sceneName) == m_sceneMap.end());
return m_sceneMap.find(sceneName) != m_sceneMap.end();
}
std::shared_ptr<Scene>
SimulationManager::getScene(std::string sceneName)
SimulationManager::getScene(std::string sceneName) const
{
if (!this->isSceneRegistered(sceneName))
{
......
......@@ -22,7 +22,7 @@
#ifndef imstkSimulationManager_h
#define imstkSimulationManager_h
#include <map>
#include <unordered_map>
#include <vector>
#include <thread>
#include <memory>
......@@ -48,12 +48,12 @@ public:
const SimulationStatus& getStatus() const;
// Scene
bool isSceneRegistered(std::string sceneName) const;
std::shared_ptr<Scene> getScene(std::string sceneName) const;
std::shared_ptr<Scene> createNewScene(std::string newSceneName);
std::shared_ptr<Scene> createNewScene();
void addScene(std::shared_ptr<Scene>newScene);
void removeScene(std::string sceneName);
std::shared_ptr<Scene> getScene(std::string sceneName);
bool isSceneRegistered(std::string sceneName);
// Simulation
void startSimulation(std::string sceneName);
......@@ -70,8 +70,8 @@ private:
SimulationStatus m_status = SimulationStatus::INACTIVE;
std::string m_currentSceneName;
std::map<std::string, std::shared_ptr<Scene> > m_sceneMap;
std::map<std::string, std::thread> m_threadMap;
std::unordered_map<std::string, std::shared_ptr<Scene> > m_sceneMap;
std::unordered_map<std::string, std::thread> m_threadMap;
std::shared_ptr<LogUtility> m_logUtil = std::make_shared<LogUtility>();
};
......
......@@ -28,5 +28,4 @@ add_executable(${PROJECT_NAME} main.cpp)
#-----------------------------------------------------------------------------
target_link_libraries(${PROJECT_NAME}
SimulationManager
Geometry
)
......@@ -4,8 +4,9 @@
#include <thread>
#include <iomanip>
#include "imstkSimulationManager.h"
#include "imstkSceneObject.h"
#include "imstkPlane.h"
#include "imstkSimulationManager.h"
int main()
{
......@@ -13,90 +14,102 @@ int main()
<< "Starting Sandbox" << std::endl
<< "****************" << std::endl;
// Geometry
std::cout << std::fixed << std::setprecision(2);
// - Plane
std::cout << "-- Plane : Init" << std::endl;
auto pos = imstk::Vec3d(5, 2, 5);
auto norm = imstk::Vec3d(0, 1, 1);
auto width = 10;
std::cout << "p = " << pos << std::endl;
std::cout << "n = " << norm << std::endl;
std::cout << "w = " << width << std::endl;
std::cout << "-- Plane : Create" << std::endl;
auto plane = std::make_shared<imstk::Plane>(pos, norm, width);
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "n = " << plane->getNormal() << std::endl;
std::cout << "w = " << plane->getWidth() << std::endl;
std::cout << "-- Plane : Set Position" << std::endl;
plane->setPosition(imstk::Vec3d(1, 1, 1));
std::cout << "p = " << plane->getPosition() << std::endl;
// --------------------------------------------
// VisualObject
// --------------------------------------------
auto sdk = std::make_shared<imstk::SimulationManager>();
auto sceneTest = sdk->createNewScene("SceneTest");
auto planeGeometry = std::make_shared<imstk::Plane>();
auto visualPlane = std::make_shared<imstk::VisualObject>("VisualPlane");
std::cout << "-- Plane : Translate" << std::endl;
plane->translate(imstk::Vec3d(2, 1, -3));
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "-- Plane : Set Normal" << std::endl;
plane->setNormal(imstk::FORWARD);
std::cout << "n = " << plane->getNormal() << std::endl;
std::cout << "-- Plane : Rotate" << std::endl;
plane->rotate(imstk::UP, M_PI / 2);
std::cout << "n = " << plane->getNormal() << std::endl;
visualPlane->setVisualGeometry(planeGeometry);
sceneTest->addSceneObject(visualPlane);
/*
// Creating SimulationManager
std::shared_ptr<imstk::SimulationManager> sdk =
std::make_shared<imstk::SimulationManager>();
LOG(INFO) << "-- Test add scenes";
// Create scene and add it (scene1)
std::shared_ptr<imstk::Scene> scene1 =
std::make_shared<imstk::Scene>("scene1");
scene1->setLoopDelay(500);
sdk->addScene(scene1);
// Create new scene through sdk (scene2)
sdk->createNewScene("scene2");
std::shared_ptr<imstk::Scene> scene2 = sdk->getScene("scene2");
scene2->setLoopDelay(500);
// Create new scene through sdk (auto : "Scene_X")
std::shared_ptr<imstk::Scene> scene3 = sdk->createNewScene();
// Remove scene3
sdk->removeScene("Scene_3");
// Test switch
LOG(INFO) << "-- Test scene switch";
sdk->startSimulation("scene1");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene2", false);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene1", true);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// Test pause/run
LOG(INFO) << "-- Test simulation pause/run";
sdk->startSimulation("scene2");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->runSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// Quit
while (sdk->getStatus() != imstk::SimulationStatus::INACTIVE) {}
//--------------------------------------------
// Geometry
//--------------------------------------------
std::cout << std::fixed << std::setprecision(2);
// Plane
std::cout << "-- Plane : Init" << std::endl;
auto pos = imstk::Vec3d(5, 2, 5);
auto norm = imstk::Vec3d(0, 1, 1);
auto width = 10;
std::cout << "p = " << pos << std::endl;
std::cout << "n = " << norm << std::endl;
std::cout << "w = " << width << std::endl;
std::cout << "-- Plane : Create" << std::endl;
auto plane = std::make_shared<imstk::Plane>(pos, norm, width);
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "n = " << plane->getNormal() << std::endl;
std::cout << "w = " << plane->getWidth() << std::endl;
std::cout << "-- Plane : Set Position" << std::endl;
plane->setPosition(imstk::Vec3d(1, 1, 1));
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "-- Plane : Translate" << std::endl;
plane->translate(imstk::Vec3d(2, 1, -3));
std::cout << "p = " << plane->getPosition() << std::endl;
std::cout << "-- Plane : Set Normal" << std::endl;
plane->setNormal(imstk::FORWARD);
std::cout << "n = " << plane->getNormal() << std::endl;
std::cout << "-- Plane : Rotate" << std::endl;
plane->rotate(imstk::UP, M_PI / 2);
std::cout << "n = " << plane->getNormal() << std::endl;
*/
/*
//--------------------------------------------
// SimulationManager
//--------------------------------------------
std::shared_ptr<imstk::SimulationManager> sdk =
std::make_shared<imstk::SimulationManager>();
// Scenes
LOG(INFO) << "-- Test add scenes";
std::shared_ptr<imstk::Scene> scene1 =
std::make_shared<imstk::Scene>("scene1");
scene1->setLoopDelay(500);
sdk->addScene(scene1);
sdk->createNewScene("scene2");
std::shared_ptr<imstk::Scene> scene2 = sdk->getScene("scene2");
scene2->setLoopDelay(500);
std::shared_ptr<imstk::Scene> scene3 = sdk->createNewScene();
sdk->removeScene("Scene_3");
// switch
LOG(INFO) << "-- Test scene switch";
sdk->startSimulation("scene1");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene2", false);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene1", true);
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// pause/run
LOG(INFO) << "-- Test simulation pause/run";
sdk->startSimulation("scene2");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->runSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->endSimulation();
// Quit
while (sdk->getStatus() != imstk::SimulationStatus::INACTIVE) {}
*/
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment