Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkIGTLMeasurements.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 "mitkIGTLMeasurements.h"
18 #include <chrono>
19 
20 //Microservices
21 #include "usServiceReference.h"
22 #include "usModuleContext.h"
23 #include "usServiceEvent.h"
24 #include "mitkServiceInterface.h"
25 #include "usGetModuleContext.h"
26 #include <iostream>
27 #include <fstream>
28 
29 mitk::IGTLMeasurements::IGTLMeasurements()
30 {
31 }
32 
34 {
36  static us::ModuleContext* context = us::GetModuleContext();
37  if (!serviceRef)
38  {
39  // This is either the first time GetInstance() was called,
40  // or a mitk::IGTLMeasurements instance has not yet been registered.
41  serviceRef = context->GetServiceReference<mitk::IGTLMeasurements>();
42  }
43  if (serviceRef)
44  {
45  // We have a valid service reference. It always points to the service
46  // with the lowest id (usually the one which was registered first).
47  // This still might return a null pointer, if all mitk::IGTLMeasurements
48  // instances have been unregistered (during unloading of the library,
49  // for example).
50  return context->GetService(serviceRef);
51  }
52  else
53  {
54  // No mitk::IGTLMeasurements instance was registered yet.
55  return 0;
56  }
57 }
58 
59 mitk::IGTLMeasurements::~IGTLMeasurements()
60 {
61 }
62 
63 void mitk::IGTLMeasurements::AddMeasurement(unsigned int measurementPoint, unsigned int index, long long timestamp)
64 {
65  if (timestamp == 0) { timestamp = std::chrono::high_resolution_clock::now().time_since_epoch().count(); }
66  if (m_IsStarted)
67  {
68  m_MeasurementPoints[measurementPoint].push_back(TimeStampIndexPair(timestamp, index));
69  }
70 }
71 
73 {
74  //open file
75  std::ostream* out = new std::ofstream(filename.c_str());
76 
77  //save old locale
78  char * oldLocale;
79  oldLocale = setlocale(LC_ALL, nullptr);
80 
81  //define own locale
82  std::locale C("C");
83  setlocale(LC_ALL, "C");
84 
85  //write header
86  unsigned int numberOfMeasurementPoints = (unsigned int)m_MeasurementPoints.size();
87  *out << numberOfMeasurementPoints << "\n";
88 
89  if (numberOfMeasurementPoints == 0)
90  {
91  delete out;
92  return false;
93  }
94 
95  out->precision(15); // rounding precision because we don't want to loose data.
96 
97  //define an offset that will be subtracted from all timestamps in order to save space
98  long long offset = m_MeasurementPoints.begin()->second.front().first;
99  //the offset shall be the first entry in the map but without its last 6 digits
100  //timestamp is given in microseconds (?)
101  //offset = offset - offset % 1000000;
102 
103  //for each entry of the map
104  for (auto entry : m_MeasurementPoints)
105  {
106  *out << entry.second.size() << ";";
107  *out << entry.first << ";";
108  for (TimeStampIndexPair timestampIndexPair : entry.second)
109  {
110  *out << (timestampIndexPair.first) << ";";
111  *out << (timestampIndexPair.second) << ";";
112  }
113  *out << "\n";
114  }
115 
116  out->flush();
117  delete out;
118  //switch back to old locale
119  setlocale(LC_ALL, oldLocale);
120 
121  return true;
122 }
123 
125 {
126  m_MeasurementPoints.clear();
127 }
128 
130 {
131  m_IsStarted = started;
132 }
static Vector3D offset
void AddMeasurement(unsigned int measurementPoint, unsigned int index, long long timestamp=0)
AddMeasurementPoint.
bool ExportData(std::string filename)
AddMeasurementPoint.
static const std::string filename
static IGTLMeasurements * GetInstance()
Is a helper class to make measurments for latency and fps.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void Reset()
clears all measurements