iMSTK
Interactive Medical Simulation Toolkit
imstkViewer.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 "imstkViewer.h"
8 #include "imstkAxesModel.h"
9 #include "imstkCamera.h"
10 #include "imstkEntity.h"
11 #include "imstkLogger.h"
12 
13 namespace imstk
14 {
15 Viewer::Viewer(std::string name) :
16  m_activeScene(nullptr),
17  m_debugEntity(std::make_shared<Entity>("DebugEntity")),
18  m_debugCamera(std::make_shared<Camera>()),
19  m_screenCapturer(nullptr),
20  m_config(std::make_shared<ViewerConfig>()),
21  m_lastFpsUpdate(std::chrono::high_resolution_clock::now()),
22  m_lastFps(60.0)
23 {
24  // Set the preferred execution type
25  m_executionType = ExecutionType::SEQUENTIAL;
26 
27  // Add a debug axes
28  m_debugEntity->addComponent<AxesModel>();
29 }
30 
31 void
32 Viewer::setDebugAxesLength(double x, double y, double z)
33 {
34  m_debugEntity->getComponent<AxesModel>()->setScale(Vec3d(x, y, z));
35 }
36 
37 std::shared_ptr<Renderer>
39 {
40  CHECK(m_activeScene != nullptr) << "no active scene!";
41 
42  return m_rendererMap.at(m_activeScene);
43 }
44 
45 void
46 Viewer::setInfoLevel(const int level)
47 {
48  CHECK(level < getInfoLevelCount())
49  << "There are only " << getInfoLevelCount() << " levels and level " << level << " was requested";
50  m_infoLevel = level;
51 }
52 
53 std::shared_ptr<KeyboardDeviceClient>
55 {
56  LOG(FATAL) << "No KeyboardDeviceClient implemented for Viewer";
57  return nullptr;
58 }
59 
60 std::shared_ptr<MouseDeviceClient>
62 {
63  LOG(FATAL) << "No MouseDeviceClient implemented for Viewer";
64  return nullptr;
65 }
66 
67 void
69 {
70  // Update framerate value display
71  auto now = std::chrono::high_resolution_clock::now();
72  m_visualFps = 1e6 / static_cast<double>(std::chrono::duration_cast<std::chrono::microseconds>(now - m_pre).count());
73  m_visualFps = 0.1 * m_visualFps + 0.9 * m_lastFps;
74  m_lastFps = m_visualFps;
75  m_pre = now;
76 }
77 
78 void
79 Viewer::updateModule()
80 {
81  this->postEvent(Event(Module::preUpdate()));
82  this->postEvent(Event(Module::postUpdate()));
83 }
84 } // namespace imstk
virtual void setInfoLevel(const int level)
Set the info level, usually means display framerates and other viewer related information.
Definition: imstkViewer.cpp:46
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
Compound Geometry.
virtual std::shared_ptr< MouseDeviceClient > getMouseDevice() const
Returns the device that emits mouse events.
Definition: imstkViewer.cpp:61
std::shared_ptr< Renderer > getActiveRenderer() const
Retrieve the renderer associated with the current scene.
Definition: imstkViewer.cpp:38
Defines an axes that should be rendered.
virtual std::shared_ptr< KeyboardDeviceClient > getKeyboardDevice() const
Returns the device that emits key events.
Definition: imstkViewer.cpp:54
void updateFps()
Called before render to push back and measure time.
Definition: imstkViewer.cpp:68
void setDebugAxesLength(double x, double y, double z)
Set the length of the debug axes.
Definition: imstkViewer.cpp:32