imstkViewer.cpp 3.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*=========================================================================

   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 "g3log/g3log.hpp"

26
27
#include "imstkRenderDelegate.h"

28
namespace imstk {
Alexis Girault's avatar
Alexis Girault committed
29
30
31
32
33
34
std::shared_ptr<Scene>
Viewer::getCurrentScene() const
{
    return m_currentScene;
}

35
void
Alexis Girault's avatar
Alexis Girault committed
36
Viewer::setCurrentScene(std::shared_ptr<Scene>scene)
37
{
Alexis Girault's avatar
Alexis Girault committed
38
39
    // If already current scene
    if( scene == m_currentScene )
40
    {
Alexis Girault's avatar
Alexis Girault committed
41
        LOG(WARNING) << scene->getName() << " already is the viewer current scene.";
42
43
44
        return;
    }

Alexis Girault's avatar
Alexis Girault committed
45
46
    // If the current scene has a renderer, remove it
    if( m_currentScene )
Alexis Girault's avatar
Alexis Girault committed
47
    {
Alexis Girault's avatar
Alexis Girault committed
48
49
        auto vtkRenderer = m_rendererMap.at(m_currentScene)->getVtkRenderer();
        if(m_vtkRenderWindow->HasRenderer(vtkRenderer))
Alexis Girault's avatar
Alexis Girault committed
50
        {
Alexis Girault's avatar
Alexis Girault committed
51
            m_vtkRenderWindow->RemoveRenderer(vtkRenderer);
Alexis Girault's avatar
Alexis Girault committed
52
53
        }
    }
54

Alexis Girault's avatar
Alexis Girault committed
55
56
    // Update current scene
    m_currentScene = scene;
Alexis Girault's avatar
Alexis Girault committed
57

Alexis Girault's avatar
Alexis Girault committed
58
59
60
61
62
    // Create renderer if it doesn't exist
    if (!m_rendererMap.count(m_currentScene))
    {
        m_rendererMap[m_currentScene] = std::make_shared<Renderer>(m_currentScene);
    }
Sreekanth Arikatla's avatar
Sreekanth Arikatla committed
63

Alexis Girault's avatar
Alexis Girault committed
64
65
    // Set renderer to renderWindow
    m_vtkRenderWindow->AddRenderer(m_rendererMap.at(m_currentScene)->getVtkRenderer());
66
67
68

    // Set name to renderWindow
    m_vtkRenderWindow->SetWindowName(m_currentScene->getName().data());
69
70
}

71
void
Alexis Girault's avatar
Alexis Girault committed
72
Viewer::setRenderingMode(Renderer::Mode mode)
73
{
Alexis Girault's avatar
Alexis Girault committed
74
75
76
77
78
79
    if( !m_currentScene )
    {
        LOG(WARNING) << "Missing scene, can not set rendering mode.\n"
                     << "Use Viewer::setCurrentScene to setup scene.";
        return;
    }
80

Alexis Girault's avatar
Alexis Girault committed
81
82
    // Setup renderer
    m_rendererMap.at(m_currentScene)->setup(mode);
83
84
85
86
87
88
    if( !m_running )
    {
        return;
    }

    // Render to update displayed actors
89
    m_vtkRenderWindow->Render();
90

91
92
    // Setup render window
    if( mode == Renderer::Mode::SIMULATION )
Alexis Girault's avatar
Alexis Girault committed
93
    {
94
95
96
97
        m_interactorStyle->HighlightProp(nullptr);
        m_vtkRenderWindow->HideCursor();
        //m_vtkRenderWindow->BordersOff();
        //m_vtkRenderWindow->FullScreenOn(1);
Alexis Girault's avatar
Alexis Girault committed
98
    }
99
    else
Alexis Girault's avatar
Alexis Girault committed
100
    {
101
102
103
        m_vtkRenderWindow->ShowCursor();
        //m_vtkRenderWindow->BordersOn();
        //m_vtkRenderWindow->FullScreenOff(1);
Alexis Girault's avatar
Alexis Girault committed
104
    }
105
106
107
}

void
Alexis Girault's avatar
Alexis Girault committed
108
Viewer::startRenderingLoop()
109
{
110
111
112
    m_running = true;
    m_vtkRenderWindow->GetInteractor()->Start();
    m_running = false;
113
114
}

Alexis Girault's avatar
Alexis Girault committed
115
116
void
Viewer::endRenderingLoop()
117
{
118
    m_vtkRenderWindow->GetInteractor()->TerminateApp();
119
120
}

Alexis Girault's avatar
Alexis Girault committed
121
122
vtkSmartPointer<vtkRenderWindow>
Viewer::getVtkRenderWindow() const
123
{
Alexis Girault's avatar
Alexis Girault committed
124
    return m_vtkRenderWindow;
125
}
126
127
128
129
130
131

const bool&
Viewer::isRendering() const
{
    return m_running;
}
132
}