Commit 0fa2bd51 authored by Alexis Girault's avatar Alexis Girault
Browse files

WIP: Incorporates Viewer using VTK

parent d943052d
#-----------------------------------------------------------------------------
# Create target
#-----------------------------------------------------------------------------
include(imstkAddLibrary)
imstk_add_library( Rendering
H_FILES
imstkViewer.h
CPP_FILES
imstkViewer.cpp
LIBRARIES
${VTK_LIBRARIES}
Core
Scene
)
#-----------------------------------------------------------------------------
# Testing
#-----------------------------------------------------------------------------
if( iMSTK_BUILD_TESTING )
add_subdirectory( Testing )
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 "imstkViewer.h"
#include "vtkRenderer.h"
#include "vtkPlaneSource.h"
#include "vtkPolyDataMapper.h"
#include "g3log/g3log.hpp"
namespace imstk {
void
Viewer::initRenderer()
{
// Check current Scene
if (m_currentScene == nullptr)
{
LOG(WARNING) << "No scene specified, nothing to render.";
return;
}
// Check RenderWindow
if (m_renderWindow == nullptr)
{
LOG(INFO) << "No render window specified, creating default render window.";
m_renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
}
// Create renderer
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
m_renderWindow->AddRenderer(renderer);
// WIP : Create renderdelegates with actors
// based on the current scene objects.
vtkSmartPointer<vtkPlaneSource> planeSource =
vtkSmartPointer<vtkPlaneSource>::New();
planeSource->SetCenter(1.0, 0.0, 0.0);
planeSource->SetNormal(1.0, 0.0, 1.0);
planeSource->Update();
vtkPolyData *plane = planeSource->GetOutput();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(plane);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
renderer->AddActor(actor);
}
vtkSmartPointer<vtkRenderWindow>
Viewer::getRenderWindow() const
{
return m_renderWindow;
}
void
Viewer::setRenderWindow(vtkSmartPointer<vtkRenderWindow>renWin)
{
m_renderWindow = renWin;
}
std::shared_ptr<Scene>
Viewer::getCurrentScene() const
{
return m_currentScene;
}
void
Viewer::setCurrentScene(std::shared_ptr<Scene>scene)
{
m_currentScene = scene;
}
void
Viewer::initModule()
{
LOG(DEBUG) << m_name << " : init";
this->initRenderer();
m_renderWindow->SetWindowName(m_name.data());
m_renderWindow->Start();
}
void
Viewer::cleanUpModule()
{
LOG(DEBUG) << m_name << " : cleanUp";
m_renderWindow->Finalize();
}
void
Viewer::runModule()
{
LOG(DEBUG) << m_name << " : running";
m_renderWindow->Render();
}
}
/*=========================================================================
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 imstkViewer_h
#define imstkViewer_h
#include <memory>
#include "imstkModule.h"
#include "imstkScene.h"
#include "vtkSmartPointer.h"
#include "vtkRenderWindow.h"
namespace imstk {
class Viewer : public Module
{
public:
Viewer(std::string name = "iMSTK Viewer") : Module(name) {}
~Viewer() = default;
void initRenderer();
vtkSmartPointer<vtkRenderWindow>getRenderWindow() const;
void setRenderWindow(vtkSmartPointer<vtkRenderWindow>renWin);
std::shared_ptr<Scene> getCurrentScene() const;
void setCurrentScene(std::shared_ptr<Scene>scene);
protected:
void initModule() override;
void runModule() override;
void cleanUpModule() override;
vtkSmartPointer<vtkRenderWindow> m_renderWindow;
std::shared_ptr<Scene> m_currentScene;
};
}
#endif // ifndef imstkViewer_h
......@@ -9,6 +9,7 @@ imstk_add_library( SimulationManager
imstkSimulationManager.cpp
LIBRARIES
Scene
Rendering
Utilities
)
......
......@@ -107,6 +107,12 @@ SimulationManager::removeScene(std::string sceneName)
LOG(INFO) << "Scene removed: " << sceneName;
}
std::shared_ptr<Viewer>
SimulationManager::getViewer() const
{
return m_viewer;
}
void
SimulationManager::startSimulation(std::string sceneName)
{
......@@ -133,6 +139,10 @@ SimulationManager::startSimulation(std::string sceneName)
return;
}
// Start viewer
m_viewer->setCurrentScene(startingScene);
this->startModuleInNewThread(m_viewer);
// Start scene
this->startModuleInNewThread(startingScene);
m_currentSceneName = sceneName;
......@@ -167,6 +177,8 @@ SimulationManager::switchScene(std::string newSceneName, bool unloadCurrentScene
return;
}
// TODO : update viewer
if (unloadCurrentScene)
{
// Stop current scene
......@@ -204,8 +216,13 @@ SimulationManager::runSimulation()
return;
}
// Run viewer
m_viewer->run();
// Run scene
m_sceneMap.at(m_currentSceneName)->run();
// Update simulation status
m_status = SimulationStatus::RUNNING;
}
......@@ -220,6 +237,9 @@ SimulationManager::pauseSimulation()
return;
}
// Pause viewer
m_viewer->pause();
// Pause scene
m_sceneMap.at(m_currentSceneName)->pause();
......@@ -239,6 +259,10 @@ SimulationManager::endSimulation()
return;
}
// End viewer
m_viewer->end();
m_threadMap.at(m_viewer->getName()).join();
// End all scenes
for (auto pair : m_sceneMap)
{
......
......@@ -28,6 +28,7 @@
#include <memory>
#include "imstkScene.h"
#include "imstkViewer.h"
#include "imstkLogUtility.h"
namespace imstk {
......@@ -55,6 +56,9 @@ public:
void addScene(std::shared_ptr<Scene>newScene);
void removeScene(std::string sceneName);
// Viewer
std::shared_ptr<Viewer> getViewer() const;
// Simulation
void startSimulation(std::string sceneName);
void switchScene(std::string newSceneName,
......@@ -73,6 +77,8 @@ private:
std::unordered_map<std::string, std::shared_ptr<Scene> > m_sceneMap;
std::unordered_map<std::string, std::thread> m_threadMap;
std::shared_ptr<Viewer> m_viewer = std::make_shared<Viewer>();
std::shared_ptr<LogUtility> m_logUtil = std::make_shared<LogUtility>();
};
}
......
......@@ -117,6 +117,7 @@ add_subdirectory(Base/Utilities)
add_subdirectory(Base/Core)
add_subdirectory(Base/Geometry)
add_subdirectory(Base/Scene)
add_subdirectory(Base/Rendering)
add_subdirectory(Base/SimulationManager)
#--------------------------------------------------------------------------
......
......@@ -8,6 +8,9 @@
#include "imstkPlane.h"
#include "imstkSimulationManager.h"
#include "vtkSmartPointer.h"
#include "vtkRenderer.h"
int main()
{
std::cout << "****************" << std::endl
......@@ -15,15 +18,36 @@ int main()
<< "****************" << std::endl;
// --------------------------------------------
// VisualObject
// Rendering
// --------------------------------------------
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");
auto sdk = std::make_shared<imstk::SimulationManager>();
sdk->getViewer()->setLoopDelay(500);
auto sceneTest = sdk->createNewScene("SceneTest");
sceneTest->setLoopDelay(500);
visualPlane->setVisualGeometry(planeGeometry);
sceneTest->addSceneObject(visualPlane);
sdk->startSimulation("SceneTest");
std::this_thread::sleep_for(std::chrono::seconds(3));
sdk->pauseSimulation();
std::this_thread::sleep_for(std::chrono::seconds(3));
sdk->runSimulation();
std::this_thread::sleep_for(std::chrono::seconds(3));
sdk->endSimulation();
/*
// --------------------------------------------
// 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");
visualPlane->setVisualGeometry(planeGeometry);
sceneTest->addSceneObject(visualPlane);
*/
/*
//--------------------------------------------
......
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