Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkEventRecorder.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3  The Medical Imaging Interaction Toolkit (MITK)
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics.
7  All rights reserved.
8 
9  This software is distributed WITHOUT ANY WARRANTY; without
10  even the implied warranty of MERCHANTABILITY or FITNESS FOR
11  A PARTICULAR PURPOSE.
12 
13  See LICENSE.txt or http://www.mitk.org for details.
14 
15  ===================================================================*/
16 
17 #include "mitkEventRecorder.h"
18 #include "mitkEventFactory.h"
19 #include "mitkInteractionEvent.h"
21 
22 #include "vtkCamera.h"
23 
24 #include "mitkBaseRenderer.h"
25 
26 static void WriteEventXMLHeader(std::ofstream &stream)
27 {
28  stream << mitk::InteractionEventConst::xmlHead() << "\n";
29 }
30 
31 static void WriteEventXMLConfig(std::ofstream &stream)
32 {
33  // <config>
34  stream << " <" << mitk::InteractionEventConst::xmlTagConfigRoot() << ">\n";
35 
36  // write renderer config
37  // for all registered 2D renderers write name and viewdirection.
38  mitk::BaseRenderer::BaseRendererMapType::iterator rendererIterator = mitk::BaseRenderer::baseRendererMap.begin();
39  mitk::BaseRenderer::BaseRendererMapType::iterator end = mitk::BaseRenderer::baseRendererMap.end();
40 
41  for (; rendererIterator != end; ++rendererIterator)
42  {
43  std::string rendererName = (*rendererIterator).second->GetName();
44 
46  (*rendererIterator).second->GetSliceNavigationController()->GetDefaultViewDirection();
47  mitk::BaseRenderer::MapperSlotId mapperID = (*rendererIterator).second->GetMapperID();
48 
49  // <renderer RendererName="stdmulti.widget2" ViewDirection="1" MapperID="1" SizeX="200" SizeY="200" SizeZ="1"/>
50  stream << " <" << mitk::InteractionEventConst::xmlTagRenderer() << " "
51  << mitk::InteractionEventConst::xmlEventPropertyRendererName() << "=\"" << rendererName << "\" "
52  << mitk::InteractionEventConst::xmlEventPropertyViewDirection() << "=\"" << viewDirection << "\" "
53  << mitk::InteractionEventConst::xmlEventPropertyMapperID() << "=\"" << mapperID << "\" "
54  << mitk::InteractionEventConst::xmlRenderSizeX() << "=\"" << (*rendererIterator).second->GetSize()[0]
55  << "\" " << mitk::InteractionEventConst::xmlRenderSizeY() << "=\""
56  << (*rendererIterator).second->GetSize()[1] << "\" " << mitk::InteractionEventConst::xmlRenderSizeZ()
57  << "=\"" << (*rendererIterator).second->GetSize()[2] << "\" ";
58  ;
59 
60  if ((*rendererIterator).second->GetMapperID() == mitk::BaseRenderer::Standard3D)
61  {
62  // For a 3D render window, rotation and zoom settings are determined by the vtkCamera parameters
63  // these are recorded here:
64  stream << mitk::InteractionEventConst::xmlViewUpX() << "=\""
65  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetViewUp()[0] << "\" "
67  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetViewUp()[1] << "\" "
69  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetViewUp()[2] << "\" "
71  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetFocalPoint()[0] << "\" "
73  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetFocalPoint()[1] << "\" "
75  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetFocalPoint()[2] << "\" "
77  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetPosition()[0] << "\" "
79  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetPosition()[1] << "\" "
81  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetPosition()[2] << "\" ";
82  }
83  stream << "/>\n";
84  }
85 
86  // </config>
87  stream << " </" << mitk::InteractionEventConst::xmlTagConfigRoot() << ">\n";
88 }
89 
90 static void WriteEventXMLEventsOpen(std::ofstream &stream)
91 {
92  stream << " <" << mitk::InteractionEventConst::xmlTagEvents() << ">\n";
93 }
94 
95 static void WriteEventXMLEventsClose(std::ofstream &stream)
96 {
97  stream << " </" << mitk::InteractionEventConst::xmlTagEvents() << ">\n";
98 }
99 
100 static void WriteEventXMLInteractionsOpen(std::ofstream &stream)
101 {
102  stream << "<" << mitk::InteractionEventConst::xmlTagInteractions() << ">\n";
103 }
104 
105 static void WriteEventXMLInteractionsClose(std::ofstream &stream)
106 {
107  stream << "</" << mitk::InteractionEventConst::xmlTagInteractions() << ">";
108 }
109 
110 static void WriteEventXMLClose(std::ofstream &stream)
111 {
112  WriteEventXMLEventsClose(stream);
114 }
115 
117 {
118 }
119 
121 {
122  if (m_FileStream.is_open())
123  {
124  m_FileStream.flush();
125  m_FileStream.close();
126  }
127 }
128 
129 void mitk::EventRecorder::Notify(mitk::InteractionEvent *interactionEvent, bool /*isHandled*/)
130 {
131  if (m_FileStream.is_open())
132  m_FileStream << EventFactory::EventToXML(interactionEvent) << "\n";
133 }
134 
135 void mitk::EventRecorder::SetEventIgnoreList(std::vector<std::string> list)
136 {
137  m_IgnoreList = list;
138 }
139 
141 {
142  if (m_FileName == "")
143  {
144  MITK_ERROR << "EventRecorder::StartRecording - Filename needs to be set first.";
145  return;
146  }
147  if (m_FileStream.is_open())
148  {
149  MITK_ERROR << "EventRecorder::StartRecording - Still recording. Stop recording before starting it again.";
150  return;
151  }
152 
153  m_FileStream.open(m_FileName.c_str(), std::ofstream::out);
154  if (!m_FileStream.good())
155  {
156  MITK_ERROR << "File " << m_FileName << " could not be opened!";
157  m_FileStream.close();
158  return;
159  }
160 
161  m_Active = true;
162 
163  // write head and config
164  // <?xml version="1.0"?>
165  // <interactions>
166  // <config>
167  // <renderer RendererName="stdmulti.widget2" ViewDirection="1"/>
168  // <renderer RendererName="stdmulti.widget1" ViewDirection="0"/>
169  // ...
170  // </config>
171  // <events>
172  WriteEventXMLHeader(m_FileStream);
173  WriteEventXMLInteractionsOpen(m_FileStream);
174  WriteEventXMLConfig(m_FileStream);
175  WriteEventXMLEventsOpen(m_FileStream);
176 }
177 
179 {
180  if (m_FileStream.is_open())
181  {
182  // write end tag
183  // </events>
184  // </interactions>
185  WriteEventXMLClose(m_FileStream);
186 
187  m_FileStream.flush();
188  m_FileStream.close();
189 
190  m_Active = false;
191  }
192 }
static const std::string xmlCameraPositionZ()
static void WriteEventXMLInteractionsOpen(std::ofstream &stream)
static std::string EventToXML(InteractionEvent *event)
EventToXML Transforms an event into a XML tag describing it.
static const std::string xmlViewUpX()
static void WriteEventXMLInteractionsClose(std::ofstream &stream)
static const std::string xmlEventPropertyRendererName()
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static void WriteEventXMLConfig(std::ofstream &stream)
static const std::string xmlTagConfigRoot()
static const std::string xmlTagInteractions()
static const std::string xmlEventPropertyMapperID()
static void WriteEventXMLEventsOpen(std::ofstream &stream)
static void WriteEventXMLClose(std::ofstream &stream)
static const std::string xmlCameraFocalPointZ()
static const std::string xmlCameraFocalPointX()
static const std::string xmlCameraFocalPointY()
static const std::string xmlRenderSizeX()
int MapperSlotId
MapperSlotId defines which kind of mapper (e.g., 2D or 3D) shoud be used.
static const std::string xmlRenderSizeY()
static BaseRendererMapType baseRendererMap
void SetEventIgnoreList(std::vector< std::string > list)
SetEventIgnoreList Optional. Provide a list of strings that describe which events are to be ignored...
static const std::string xmlEventPropertyViewDirection()
static void WriteEventXMLEventsClose(std::ofstream &stream)
static const std::string xmlTagRenderer()
static const std::string xmlTagEvents()
static const std::string xmlViewUpY()
static const std::string xmlCameraPositionX()
static const std::string xmlHead()
static const std::string xmlCameraPositionY()
static const std::string xmlRenderSizeZ()
virtual void Notify(InteractionEvent *interactionEvent, bool) override
ViewDirection
Possible view directions, Original will uses the PlaneGeometry instances in a SlicedGeometry3D provid...
static const std::string xmlViewUpZ()
static void WriteEventXMLHeader(std::ofstream &stream)