MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkIGTLMeasurements.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
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
25mitk::IGTLMeasurements::IGTLMeasurements()
26{
27}
28
30{
31 static us::ServiceReference<mitk::IGTLMeasurements> serviceRef;
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
55mitk::IGTLMeasurements::~IGTLMeasurements()
56{
57}
58
59void 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
68bool 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}
Definition diagrams_c.h:5
Is a helper class to make measurments for latency and fps.
static IGTLMeasurements * GetInstance()
bool ExportData(std::string filename)
AddMeasurementPoint.
void AddMeasurement(unsigned int measurementPoint, unsigned int index, long long timestamp=0)
AddMeasurementPoint.
void Reset()
clears all measurements