-
Notifications
You must be signed in to change notification settings - Fork 70
/
MetaImageOutputDevice.cpp
304 lines (273 loc) · 7.77 KB
/
MetaImageOutputDevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// ================================================================================================
//
// If not explicitly stated: Copyright (C) 2011-2016, all rights reserved,
// Christoph Hennersperger
// EmaiL christoph.hennersperger@tum.de
// Chair for Computer Aided Medical Procedures
// Technische Universität München
// Boltzmannstr. 3, 85748 Garching b. München, Germany
// and
// Rüdiger Göbl
// Email r.goebl@tum.de
//
// ================================================================================================
#include "MetaImageOutputDevice.h"
#include "MhdSequenceWriter.h"
#include <SyncRecordObject.h>
#include <USImage.h>
#include <Beamformer/USRawData.h>
#include <TrackerDataSet.h>
#include <utilities/Logging.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <array>
#include <cassert>
using namespace std;
namespace supra
{
using namespace logging;
MetaImageOutputDevice::MetaImageOutputDevice(tbb::flow::graph& graph, const std::string & nodeID)
: AbstractOutput(graph, nodeID)
, m_filename("output")
, m_createSequences(false)
, m_isRecording(false)
, m_sequencesWritten(0)
, m_active(true)
, m_pWriter(nullptr)
{
m_callFrequency.setName("MHD");
m_valueRangeDictionary.set<string>("filename", "output", "Filename");
m_valueRangeDictionary.set<bool>("createSequences", { false, true }, true, "Sequences");
m_valueRangeDictionary.set<bool>("active", { false, true }, true, "Active");
m_isReady = false;
}
MetaImageOutputDevice::~MetaImageOutputDevice()
{
m_isReady = false;
}
void MetaImageOutputDevice::initializeOutput()
{
if (!m_createSequences)
{
initializeSequence();
m_isReady = m_pWriter->isOpen();
}
else
{
m_isReady = true;
}
}
bool MetaImageOutputDevice::ready()
{
return m_isReady;
}
void MetaImageOutputDevice::startOutput()
{
}
void MetaImageOutputDevice::stopOutput()
{
}
void MetaImageOutputDevice::startSequence()
{
if (m_createSequences)
{
lock_guard<mutex> l(m_writerMutex);
initializeSequence();
m_isRecording = true;
}
}
void MetaImageOutputDevice::stopSequence()
{
if (m_createSequences)
{
lock_guard<mutex> l(m_writerMutex);
m_isRecording = false;
m_pWriter->close();
m_pWriter = nullptr;
m_sequencesWritten++;
}
}
void MetaImageOutputDevice::configurationDone()
{
m_filename = m_configurationDictionary.get<string>("filename");
m_createSequences = m_configurationDictionary.get<bool>("createSequences");
m_active = m_configurationDictionary.get<bool>("active");
}
void MetaImageOutputDevice::writeData(std::shared_ptr<RecordObject> data)
{
lock_guard<mutex> l(m_writerMutex);
if (m_isReady && m_isRecording && getRunning())
{
m_callFrequency.measure();
addData(data);
m_callFrequency.measureEnd();
}
}
void MetaImageOutputDevice::initializeSequence()
{
string filename = m_filename;
if (m_createSequences)
{
filename += "_" + std::to_string(m_sequencesWritten);
}
log_info("MetaImage file Name: ", filename);
if (m_pWriter)
{
m_pWriter->close();
}
m_pWriter = new MhdSequenceWriter();
m_pWriter->open(filename);
m_isRecording = m_pWriter->isOpen();
}
void MetaImageOutputDevice::addData(shared_ptr<const RecordObject> data)
{
switch (data->getType())
{
case TypeSyncRecordObject:
addSyncRecord(data);
break;
case TypeUSImage:
addImage(data);
break;
case TypeUSRawData:
addUSRawData(data);
break;
case TypeTrackerDataSet:
case TypeRecordUnknown:
default:
break;
}
}
void MetaImageOutputDevice::addSyncRecord(shared_ptr<const RecordObject> _syncMessage)
{
auto syncMessage = dynamic_pointer_cast<const SyncRecordObject>(_syncMessage);
if (syncMessage)
{
auto mainRecord = syncMessage->getMainRecord();
if (mainRecord->getType() == TypeUSImage)
{
auto successframeNum = addImage(mainRecord);
bool success = get<0>(successframeNum);
size_t frameNum = get<1>(successframeNum);
if (success)
{
for (shared_ptr<const RecordObject> syncedO : syncMessage->getSyncedRecords())
{
if (syncedO->getType() == TypeTrackerDataSet)
{
addTracking(syncedO, frameNum);
}
}
}
}
}
}
template <typename ElementType>
std::pair<bool, size_t> MetaImageOutputDevice::addImageTemplated(shared_ptr<const USImage<ElementType> > imageData)
{
bool success = false;
size_t frameNum = 0;
if (imageData)
{
auto properties = imageData->getImageProperties();
if (
properties->getImageType() == USImageProperties::BMode ||
properties->getImageType() == USImageProperties::Doppler)
{
double resolution = properties->getImageResolution();
vec3s imageSize = imageData->getSize();
if (imageSize.z == 0)
{
imageSize.z = 1;
}
shared_ptr<Container<ElementType> > pDataCopy;
shared_ptr<const Container<ElementType> > pData;
if (imageData->getData()->isHost())
{
pData = imageData->getData();
}
else {
pDataCopy = make_shared<Container<ElementType> >(LocationHost, *(imageData->getData()));
pData = pDataCopy;
}
auto successframeNum = m_pWriter->addImage(
pData->get(), imageSize.x, imageSize.y, imageSize.z,
imageData->getSyncTimestamp(), resolution,
[pData](const uint8_t*, size_t){}
);
success = get<0>(successframeNum);
frameNum = get<1>(successframeNum);
}
}
return std::make_pair(success, frameNum);
}
template <typename ElementType>
std::pair<bool, size_t> MetaImageOutputDevice::addUSRawDataTemplated(shared_ptr<const USRawData<ElementType> > rawData)
{
bool success = false;
size_t frameNum = 0;
if (rawData)
{
size_t numChannels = rawData->getNumReceivedChannels();
size_t numSamples = rawData->getNumSamples();
size_t numScanlines = rawData->getNumScanlines();
shared_ptr<Container<ElementType> > pDataCopy;
shared_ptr<const Container<ElementType> > pData;
if (rawData->getData()->isHost())
{
pData = rawData->getData();
}
else {
pDataCopy = make_shared<Container<int16_t> >(LocationHost, *(rawData->getData()));
pData = pDataCopy;
}
double resolution = rawData->getImageProperties()->getSampleDistance();
auto successframeNum = m_pWriter->addImage<ElementType>(
pData->get(), numSamples, numChannels, numScanlines,
rawData->getSyncTimestamp(), resolution,
[pData](const uint8_t*, size_t){}
);
success = get<0>(successframeNum);
frameNum = get<1>(successframeNum);
}
return std::make_pair(success, frameNum);
}
std::pair<bool, size_t> MetaImageOutputDevice::addImage(shared_ptr<const RecordObject> _imageData)
{
std::pair<bool, size_t> successframeNum = std::make_pair(false, 0);
auto imageData8Bit = dynamic_pointer_cast<const USImage<uint8_t>>(_imageData);
if (imageData8Bit)
{
successframeNum = addImageTemplated<uint8_t>(imageData8Bit);
}
auto imageData16Bit = dynamic_pointer_cast<const USImage<int16_t>>(_imageData);
if (imageData16Bit)
{
successframeNum = addImageTemplated<int16_t>(imageData16Bit);
}
return successframeNum;
}
std::pair<bool, size_t> MetaImageOutputDevice::addUSRawData(shared_ptr<const RecordObject> _rawData)
{
std::pair<bool, size_t> successframeNum = std::make_pair(false, 0);
auto rawData = dynamic_pointer_cast<const USRawData<int16_t>>(_rawData);
if (rawData)
{
successframeNum = addUSRawDataTemplated<int16_t>(rawData);
}
return successframeNum;
}
void MetaImageOutputDevice::addTracking(shared_ptr<const RecordObject> _trackData, size_t frameNum)
{
double maxAcceptanceQual = 700;
auto trackData = dynamic_pointer_cast<const TrackerDataSet>(_trackData);
if (trackData)
{
for (auto t : trackData->getSensorData())
{
m_pWriter->addTracking(frameNum, t.getMatrix(), t.getQuality() < maxAcceptanceQual, std::to_string(t.getUID()));
}
}
}
}