Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
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 (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 "mitkIGTLMeasurements.h"
14 #include <chrono>
15 
16 //Microservices
17 #include "usServiceReference.h"
18 #include "usModuleContext.h"
19 #include "usServiceEvent.h"
20 #include "mitkServiceInterface.h"
21 #include "usGetModuleContext.h"
22 #include <iostream>
23 #include <fstream>
24 
25 mitk::IGTLMeasurements::IGTLMeasurements()
26 {
27 }
28 
30 {
32  static us::ModuleContext* context = us::GetModuleContext();
33  if (!serviceRef)
34  {
35  // This is either the first time GetInstance() was called,
36  // or a mitk::IGTLMeasurements instance has not yet been registered.
37  serviceRef = context->GetServiceReference<mitk::IGTLMeasurements>();
38  }
39  if (serviceRef)
40  {
41  // We have a valid service reference. It always points to the service
42  // with the lowest id (usually the one which was registered first).
43  // This still might return a null pointer, if all mitk::IGTLMeasurements
44  // instances have been unregistered (during unloading of the library,
45  // for example).
46  return context->GetService(serviceRef);
47  }
48  else
49  {
50  // No mitk::IGTLMeasurements instance was registered yet.
51  return nullptr;
52  }
53 }
54 
55 mitk::IGTLMeasurements::~IGTLMeasurements()
56 {
57 }
58 
59 void mitk::IGTLMeasurements::AddMeasurement(unsigned int measurementPoint, unsigned int index, long long timestamp)
60 {
61  if (timestamp == 0) { timestamp = std::chrono::high_resolution_clock::now().time_since_epoch().count(); }
62  if (m_IsStarted)
63  {
64  m_MeasurementPoints[measurementPoint].push_back(TimeStampIndexPair(timestamp, index));
65  }
66 }
67 
68 bool mitk::IGTLMeasurements::ExportData(std::string filename)
69 {
70  //open file
71  std::ostream* out = new std::ofstream(filename.c_str());
72 
73  //save old locale
74  char * oldLocale;
75  oldLocale = setlocale(LC_ALL, nullptr);
76 
77  //define own locale
78  std::locale C("C");
79  setlocale(LC_ALL, "C");
80 
81  //write header
82  unsigned int numberOfMeasurementPoints = (unsigned int)m_MeasurementPoints.size();
83  *out << numberOfMeasurementPoints << "\n";
84 
85  if (numberOfMeasurementPoints == 0)
86  {
87  delete out;
88  return false;
89  }
90 
91  out->precision(15); // rounding precision because we don't want to loose data.
92 
93  //for each entry of the map
94  for (auto entry : m_MeasurementPoints)
95  {
96  *out << entry.second.size() << ";";
97  *out << entry.first << ";";
98  for (TimeStampIndexPair timestampIndexPair : entry.second)
99  {
100  *out << (timestampIndexPair.first) << ";";
101  *out << (timestampIndexPair.second) << ";";
102  }
103  *out << "\n";
104  }
105 
106  out->flush();
107  delete out;
108  //switch back to old locale
109  setlocale(LC_ALL, oldLocale);
110 
111  return true;
112 }
113 
115 {
116  m_MeasurementPoints.clear();
117 }
118 
120 {
121  m_IsStarted = started;
122 }
void AddMeasurement(unsigned int measurementPoint, unsigned int index, long long timestamp=0)
AddMeasurementPoint.
bool ExportData(std::string filename)
AddMeasurementPoint.
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