/
VolumeHandler.cpp
245 lines (218 loc) · 7.56 KB
/
VolumeHandler.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
/* Copyright (c) 2015-2017, EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
* Responsible Author: Cyrille Favreau <cyrille.favreau@epfl.ch>
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "VolumeHandler.h"
#include <brayns/common/log.h>
#include <fcntl.h>
#include <fstream>
#include <future>
#include <sys/mman.h>
#include <sys/stat.h>
namespace
{
const int NO_DESCRIPTOR = -1;
}
namespace brayns
{
VolumeHandler::VolumeHandler(const VolumeParameters& volumeParameters,
const IndexMode indexMode)
: _volumeParameters(volumeParameters)
, _indexMode(indexMode)
{
}
VolumeHandler::~VolumeHandler()
{
_volumeDescriptors.clear();
}
void VolumeHandler::attachVolumeToFile(const uint32_t index,
const std::string& volumeFile)
{
// Add volume descriptor for specified index
_volumeDescriptors[index].reset(
new VolumeDescriptor(volumeFile, _volumeParameters.getDimensions(),
_volumeParameters.getElementSpacing(),
_volumeParameters.getOffset()));
BRAYNS_INFO << "Attached " << volumeFile << " to index " << index << " ["
<< _volumeDescriptors.begin()->first << ", "
<< _volumeDescriptors.rbegin()->first << "]" << std::endl;
}
void VolumeHandler::setCurrentIndex(uint32_t index)
{
index = _getBoundedIndex(index);
if (index != _currentIndex &&
_volumeDescriptors.find(index) != _volumeDescriptors.end())
{
if (_volumeDescriptors.find(_currentIndex) != _volumeDescriptors.end())
_volumeDescriptors[_currentIndex]->unmap();
_currentIndex = index;
_volumeDescriptors[_currentIndex]->map();
}
}
void* VolumeHandler::getData() const
{
if (_volumeDescriptors.find(_currentIndex) != _volumeDescriptors.end())
return _volumeDescriptors.at(_currentIndex)->getMemoryMapPtr();
return nullptr;
}
float VolumeHandler::getEpsilon(const Vector3f& elementSpacing,
const uint16_t samplesPerRay)
{
if (_volumeDescriptors.find(_currentIndex) == _volumeDescriptors.end())
return 0.f;
const Vector3f diag =
elementSpacing * _volumeDescriptors.at(_currentIndex)->getDimensions();
const float diagMax = diag.find_max();
const float epsilon = diagMax / float(samplesPerRay);
return std::max(1.f, epsilon);
}
Vector3ui VolumeHandler::getDimensions() const
{
if (_volumeDescriptors.find(_currentIndex) != _volumeDescriptors.end())
return _volumeDescriptors.at(_currentIndex)->getDimensions();
return Vector3ui();
}
Vector3f VolumeHandler::getElementSpacing() const
{
if (_volumeDescriptors.find(_currentIndex) != _volumeDescriptors.end())
return _volumeDescriptors.at(_currentIndex)->getElementSpacing();
return Vector3f();
}
Vector3f VolumeHandler::getOffset() const
{
if (_volumeDescriptors.find(_currentIndex) != _volumeDescriptors.end())
return _volumeDescriptors.at(_currentIndex)->getOffset();
return Vector3f();
}
uint64_t VolumeHandler::getSize() const
{
if (_volumeDescriptors.find(_currentIndex) != _volumeDescriptors.end())
return _volumeDescriptors.at(_currentIndex)->getSize();
return 0;
}
uint32_t VolumeHandler::_getBoundedIndex(const uint32_t index) const
{
uint32_t result = 0;
switch (_indexMode)
{
case IndexMode::modulo:
if (_volumeDescriptors.size() != 0)
result = size_t(index + _volumeDescriptors.begin()->first) %
_volumeDescriptors.size();
break;
case IndexMode::bounded:
result = std::max(std::min(index, _volumeDescriptors.rbegin()->first),
_volumeDescriptors.begin()->first);
case IndexMode::unchanged:
default:
result = index;
}
return result;
}
VolumeHandler::VolumeDescriptor::VolumeDescriptor(
const std::string& filename, const Vector3ui& dimensions,
const Vector3f& elementSpacing, const Vector3f& offset)
: _filename(filename)
, _memoryMapPtr(0)
, _cacheFileDescriptor(NO_DESCRIPTOR)
, _dimensions(dimensions)
, _elementSpacing(elementSpacing)
, _offset(offset)
{
}
VolumeHandler::VolumeDescriptor::~VolumeDescriptor()
{
unmap();
}
void VolumeHandler::VolumeDescriptor::map()
{
_cacheFileDescriptor = open(_filename.c_str(), O_RDONLY);
if (_cacheFileDescriptor == NO_DESCRIPTOR)
{
BRAYNS_ERROR << "Failed to attach " << _filename << std::endl;
return;
}
struct stat sb;
if (::fstat(_cacheFileDescriptor, &sb) == NO_DESCRIPTOR)
{
BRAYNS_ERROR << "Failed to attach " << _filename << std::endl;
return;
}
_size = sb.st_size;
_memoryMapPtr =
::mmap(0, _size, PROT_READ, MAP_PRIVATE, _cacheFileDescriptor, 0);
if (_memoryMapPtr == MAP_FAILED)
{
_memoryMapPtr = 0;
::close(_cacheFileDescriptor);
_cacheFileDescriptor = NO_DESCRIPTOR;
BRAYNS_ERROR << "Failed to attach " << _filename << std::endl;
return;
}
}
void VolumeHandler::VolumeDescriptor::unmap()
{
if (_memoryMapPtr)
{
::munmap((void*)_memoryMapPtr, _size);
_memoryMapPtr = 0;
}
if (_cacheFileDescriptor != NO_DESCRIPTOR)
{
::close(_cacheFileDescriptor);
_cacheFileDescriptor = NO_DESCRIPTOR;
}
}
Histogram& VolumeHandler::getHistogram()
{
if (_histograms.find(_currentIndex) != _histograms.end())
return _histograms[_currentIndex];
std::future<bool> computeHistogram =
std::async(std::launch::async, [this]() {
uint8_t* data = static_cast<uint8_t*>(getData());
if (data)
{
BRAYNS_INFO << "Computing volume histogram" << std::endl;
uint8_t minValue = std::numeric_limits<uint8_t>::max();
uint8_t maxValue = 0;
std::map<uint8_t, uint64_t> values;
for (uint64_t i = 0; i < getSize(); ++i)
{
const uint8_t value = data[i];
minValue = std::min(minValue, value);
maxValue = std::max(maxValue, value);
if (values.find(value) == values.end())
values[value] = 1;
else
++values[value];
}
_histograms[_currentIndex].values.clear();
for (const auto& value : values)
_histograms[_currentIndex].values.push_back(value.second);
_histograms[_currentIndex].range = Vector2f(minValue, maxValue);
BRAYNS_INFO
<< "Histogram range: " << _histograms[_currentIndex].range
<< std::endl;
}
return true;
});
computeHistogram.wait();
computeHistogram.get();
return _histograms[_currentIndex];
}
}