Medical Imaging Interaction Toolkit  2018.4.99-f51274ea
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkEventRecorder.h"
14 #include "mitkEventFactory.h"
15 #include "mitkInteractionEvent.h"
17 
18 #include "vtkCamera.h"
19 
20 #include "mitkBaseRenderer.h"
21 
22 static void WriteEventXMLHeader(std::ofstream &stream)
23 {
24  stream << mitk::InteractionEventConst::xmlHead() << "\n";
25 }
26 
27 static void WriteEventXMLConfig(std::ofstream &stream)
28 {
29  // <config>
30  stream << " <" << mitk::InteractionEventConst::xmlTagConfigRoot() << ">\n";
31 
32  // write renderer config
33  // for all registered 2D renderers write name and viewdirection.
34  auto rendererIterator = mitk::BaseRenderer::baseRendererMap.begin();
35  auto end = mitk::BaseRenderer::baseRendererMap.end();
36 
37  for (; rendererIterator != end; ++rendererIterator)
38  {
39  std::string rendererName = (*rendererIterator).second->GetName();
40 
42  (*rendererIterator).second->GetSliceNavigationController()->GetDefaultViewDirection();
43  mitk::BaseRenderer::MapperSlotId mapperID = (*rendererIterator).second->GetMapperID();
44 
45  // <renderer RendererName="stdmulti.widget1" ViewDirection="1" MapperID="1" SizeX="200" SizeY="200" SizeZ="1"/>
46  stream << " <" << mitk::InteractionEventConst::xmlTagRenderer() << " "
47  << mitk::InteractionEventConst::xmlEventPropertyRendererName() << "=\"" << rendererName << "\" "
48  << mitk::InteractionEventConst::xmlEventPropertyViewDirection() << "=\"" << viewDirection << "\" "
49  << mitk::InteractionEventConst::xmlEventPropertyMapperID() << "=\"" << mapperID << "\" "
50  << mitk::InteractionEventConst::xmlRenderSizeX() << "=\"" << (*rendererIterator).second->GetSize()[0]
51  << "\" " << mitk::InteractionEventConst::xmlRenderSizeY() << "=\""
52  << (*rendererIterator).second->GetSize()[1] << "\" " << mitk::InteractionEventConst::xmlRenderSizeZ()
53  << "=\"" << (*rendererIterator).second->GetSize()[2] << "\" ";
54  ;
55 
56  if ((*rendererIterator).second->GetMapperID() == mitk::BaseRenderer::Standard3D)
57  {
58  // For a 3D render window, rotation and zoom settings are determined by the vtkCamera parameters
59  // these are recorded here:
60  stream << mitk::InteractionEventConst::xmlViewUpX() << "=\""
61  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetViewUp()[0] << "\" "
63  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetViewUp()[1] << "\" "
65  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetViewUp()[2] << "\" "
67  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetFocalPoint()[0] << "\" "
69  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetFocalPoint()[1] << "\" "
71  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetFocalPoint()[2] << "\" "
73  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetPosition()[0] << "\" "
75  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetPosition()[1] << "\" "
77  << (*rendererIterator).second->GetVtkRenderer()->GetActiveCamera()->GetPosition()[2] << "\" ";
78  }
79  stream << "/>\n";
80  }
81 
82  // </config>
83  stream << " </" << mitk::InteractionEventConst::xmlTagConfigRoot() << ">\n";
84 }
85 
86 static void WriteEventXMLEventsOpen(std::ofstream &stream)
87 {
88  stream << " <" << mitk::InteractionEventConst::xmlTagEvents() << ">\n";
89 }
90 
91 static void WriteEventXMLEventsClose(std::ofstream &stream)
92 {
93  stream << " </" << mitk::InteractionEventConst::xmlTagEvents() << ">\n";
94 }
95 
96 static void WriteEventXMLInteractionsOpen(std::ofstream &stream)
97 {
98  stream << "<" << mitk::InteractionEventConst::xmlTagInteractions() << ">\n";
99 }
100 
101 static void WriteEventXMLInteractionsClose(std::ofstream &stream)
102 {
103  stream << "</" << mitk::InteractionEventConst::xmlTagInteractions() << ">";
104 }
105 
106 static void WriteEventXMLClose(std::ofstream &stream)
107 {
108  WriteEventXMLEventsClose(stream);
110 }
111 
113 {
114 }
115 
117 {
118  if (m_FileStream.is_open())
119  {
120  m_FileStream.flush();
121  m_FileStream.close();
122  }
123 }
124 
125 void mitk::EventRecorder::Notify(mitk::InteractionEvent *interactionEvent, bool /*isHandled*/)
126 {
127  if (m_FileStream.is_open())
128  m_FileStream << EventFactory::EventToXML(interactionEvent) << "\n";
129 }
130 
131 void mitk::EventRecorder::SetEventIgnoreList(std::vector<std::string> list)
132 {
133  m_IgnoreList = list;
134 }
135 
137 {
138  if (m_FileName == "")
139  {
140  MITK_ERROR << "EventRecorder::StartRecording - Filename needs to be set first.";
141  return;
142  }
143  if (m_FileStream.is_open())
144  {
145  MITK_ERROR << "EventRecorder::StartRecording - Still recording. Stop recording before starting it again.";
146  return;
147  }
148 
149  m_FileStream.open(m_FileName.c_str(), std::ofstream::out);
150  if (!m_FileStream.good())
151  {
152  MITK_ERROR << "File " << m_FileName << " could not be opened!";
153  m_FileStream.close();
154  return;
155  }
156 
157  m_Active = true;
158 
159  // write head and config
160  // <?xml version="1.0"?>
161  // <interactions>
162  // <config>
163  // <renderer RendererName="stdmulti.widget1" ViewDirection="1"/>
164  // <renderer RendererName="stdmulti.widget0" ViewDirection="0"/>
165  // ...
166  // </config>
167  // <events>
168  WriteEventXMLHeader(m_FileStream);
169  WriteEventXMLInteractionsOpen(m_FileStream);
170  WriteEventXMLConfig(m_FileStream);
171  WriteEventXMLEventsOpen(m_FileStream);
172 }
173 
175 {
176  if (m_FileStream.is_open())
177  {
178  // write end tag
179  // </events>
180  // </interactions>
181  WriteEventXMLClose(m_FileStream);
182 
183  m_FileStream.flush();
184  m_FileStream.close();
185 
186  m_Active = false;
187  }
188 }
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:20
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) should be used.
static const std::string xmlRenderSizeY()
ViewDirection
Possible view directions, Original will uses the PlaneGeometry instances in a SlicedGeometry3D provid...
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()
void Notify(InteractionEvent *interactionEvent, bool) override
static const std::string xmlViewUpZ()
static void WriteEventXMLHeader(std::ofstream &stream)