iMSTK
Interactive Medical Simulation Toolkit
RenderingVolumeExample.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkCamera.h"
8 #include "imstkImageData.h"
9 #include "imstkImageReslice.h"
10 #include "imstkKeyboardDeviceClient.h"
11 #include "imstkKeyboardSceneControl.h"
12 #include "imstkMeshIO.h"
13 #include "imstkMouseDeviceClient.h"
14 #include "imstkMouseSceneControl.h"
15 #include "imstkNew.h"
16 #include "imstkScene.h"
17 #include "imstkSceneManager.h"
18 #include "imstkSceneObject.h"
19 #include "imstkSimulationManager.h"
20 #include "imstkSimulationUtils.h"
21 #include "imstkTextVisualModel.h"
22 #include "imstkVisualModel.h"
23 #include "imstkVolumeRenderMaterial.h"
24 #include "imstkVolumeRenderMaterialPresets.h"
25 #include "imstkVTKViewer.h"
26 
27 using namespace imstk;
28 
32 int
33 main()
34 {
36 
37  // SDK and Scene
38  imstkNew<Scene> scene("VolumeRendering");
39 
40  // Read an image
41  auto imageData = MeshIO::read<ImageData>(iMSTK_DATA_ROOT "skullVolume.nrrd");
42 
43  // Rotate that image
44  imstkNew<ImageReslice> reslice;
45  reslice->setInputImage(imageData);
46  // Rotate 1radian around y
47  reslice->setTransform(mat4dRotation(Rotd(1.0, Vec3d(0.0, 1.0, 0.0))));
48  reslice->update();
49 
50  // Create a visual object in the scene for the volume
51  imstkNew<SceneObject> volumeObj("VisualVolume");
52  volumeObj->setVisualGeometry(reslice->getOutputImage());
53  scene->addSceneObject(volumeObj);
54 
55  // Update Camera to position volume close to viewer
56  auto cam = scene->getActiveCamera();
57  cam->setPosition(108.991, -310.752, 109.795);
58  cam->setFocalPoint(41.0774, 35.6817, -33.9048);
59  cam->setViewUp(-0.83121, 0.0660308, 0.552024);
60 
61  // Setup a viewer to render in its own thread
62  imstkNew<VTKViewer> viewer;
63  viewer->setActiveScene(scene);
64  viewer->setBackgroundColors(Color(0.3285, 0.3285, 0.6525), Color(0.13836, 0.13836, 0.2748), true);
65 
66  auto statusTxt = std::make_shared<TextVisualModel>("StatusText");
67  statusTxt->setPosition(TextVisualModel::DisplayPosition::UpperLeft);
68  statusTxt->setFontSize(30);
69 
70  StopWatch timer;
71  timer.start();
72 
73  int currMatId = 0;
74  int prevMatId = -1;
75  auto updateFunc = [&](Event*)
76  {
77  prevMatId = currMatId;
78  currMatId = static_cast<int>(timer.getTimeElapsed() / 1000.0 / 2.0) % 25;
79 
80  if (currMatId != prevMatId)
81  {
82  // Change view background to black every other frame
83  std::cout << "Displaying with volume material preset: " << currMatId << std::endl;
84  // Query for a volume material preset
85  std::shared_ptr<VolumeRenderMaterial> volumeMaterial = VolumeRenderMaterialPresets::getPreset(static_cast<VolumeRenderMaterialPresets::Presets>(currMatId));
86  // Apply the preset to the visual object
87  volumeObj->getVisualModel(0)->setRenderMaterial(volumeMaterial);
88 
89  std::ostringstream ss;
90  ss << "Volume Material Preset: " << imstk::VolumeRenderMaterialPresets::getPresetName(static_cast<VolumeRenderMaterialPresets::Presets>(currMatId));
91  statusTxt->setText(ss.str());
92  }
93  };
94 
95  // Run the simulation
96  {
97  // Setup a scene manager to advance the scene in its own thread
98  imstkNew<SceneManager> sceneManager;
99  sceneManager->setActiveScene(scene);
100  connect<Event>(viewer, &SceneManager::postUpdate, updateFunc);
101 
103  driver->addModule(viewer);
104  driver->addModule(sceneManager);
105 
106  // Add default mouse and keyboard controls to the viewer
107  std::shared_ptr<Entity> mouseAndKeyControls =
108  SimulationUtils::createDefaultSceneControl(driver);
109  mouseAndKeyControls->addComponent(statusTxt);
110  scene->addSceneObject(mouseAndKeyControls);
111 
112  driver->start();
113  }
114 
115  return 0;
116 }
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...
virtual void start()
Start the appropriate timer.
Definition: imstkTimer.cpp:29
Compound Geometry.
virtual void setBackgroundColors(const Color color1, const Color color2=Color(0.0, 0.0, 0.0), const bool gradientBackground=false) override
Set the coloring of the screen background If &#39;gradientBackground&#39; is false or not supplied color1 wil...
static std::shared_ptr< VolumeRenderMaterial > getPreset(Presets p=Presets::CT_BONES)
TODO.
std::shared_ptr<T> obj = std::make_shared<T>(); equivalent, convenience class for STL shared allocati...
Definition: imstkNew.h:29
Color in RGB space.
Definition: imstkColor.h:24
Stop Watch utility class.
Definition: imstkTimer.h:19
void setActiveScene(std::string newSceneName)
Sets the currently updating scene.
virtual double getTimeElapsed(const TimeUnitType unitType=TimeUnitType::milliSeconds)
Returns the time elapsed since calling start.
Definition: imstkTimer.cpp:136
static LoggerG3 & startLogger()
Starts logger with default sinks, use getInstance to create a logger with no sinks.