Visualisierung 2
Comparison of Hue Preserving Rendering to Alpha Composing
Volume.cpp
Go to the documentation of this file.
1 
8 #include "Volume.h"
9 
10 #include <math.h>
11 
12 
13 //-------------------------------------------------------------------------------------------------
14 // Voxel
15 //-------------------------------------------------------------------------------------------------
16 
18 {
19  setValue(0.0f);
20 }
21 
22 Voxel::Voxel(const Voxel &other)
23 {
24  setValue(other.getValue());
25 }
26 
27 Voxel::Voxel(const float value)
28 {
29  setValue(value);
30 }
31 
33 {
34 }
35 
36 void Voxel::setValue(const float value)
37 {
38  m_Value = value;
39 }
40 
41 const float Voxel::getValue() const
42 {
43  return m_Value;
44 }
45 
46 const bool Voxel::operator==(const Voxel &other) const
47 {
48  return (getValue() == other.getValue());
49 }
50 
51 const bool Voxel::operator!=(const Voxel &other) const
52 {
53  return !(*this == other);
54 }
55 
56 const bool Voxel::operator>(const Voxel &other) const
57 {
58  return getValue() > other.getValue();
59 }
60 
61 const bool Voxel::operator>=(const Voxel &other) const
62 {
63  return getValue() >= other.getValue();
64 }
65 
66 const bool Voxel::operator<(const Voxel &other) const
67 {
68  return getValue() < other.getValue();
69 }
70 
71 const bool Voxel::operator<=(const Voxel &other) const
72 {
73  return getValue() <= other.getValue();
74 }
75 
76 const Voxel& Voxel::operator+=(const Voxel &other)
77 {
78  m_Value += other.m_Value;
79  return *this;
80 }
81 
82 const Voxel& Voxel::operator-=(const Voxel &other)
83 {
84  m_Value -= other.m_Value;
85  return *this;
86 }
87 
88 const Voxel& Voxel::operator*=(const float &value)
89 {
90  m_Value *= value;
91  return *this;
92 }
93 
94 const Voxel& Voxel::operator/=(const float &value)
95 {
96  m_Value /= value;
97  return *this;
98 }
99 
100 const Voxel Voxel::operator+(const Voxel &other) const
101 {
102  Voxel voxNew = *this;
103  voxNew += other;
104  return voxNew;
105 }
106 
107 const Voxel Voxel::operator-(const Voxel &other) const
108 {
109  Voxel voxNew = *this;
110  voxNew -= other;
111  return voxNew;
112 }
113 
114 const Voxel Voxel::operator*(const float &value) const
115 {
116  Voxel voxNew = *this;
117  voxNew *= value;
118  return voxNew;
119 }
120 
121 const Voxel Voxel::operator/(const float &value) const
122 {
123  Voxel voxNew = *this;
124  voxNew /= value;
125  return voxNew;
126 }
127 
128 
129 //-------------------------------------------------------------------------------------------------
130 // Volume
131 //-------------------------------------------------------------------------------------------------
132 
134  : m_Width(1), m_Height(1), m_Depth(1), m_Size(0), m_Voxels(1)
135 {
136 }
137 
139 {
140 }
141 
142 const Voxel& Volume::voxel(const int x, const int y, const int z) const
143 {
144  return m_Voxels[x + y*m_Width + z*m_Width*m_Height];
145 }
146 
147 const Voxel& Volume::voxel(const int i) const
148 {
149  return m_Voxels[i];
150 }
151 
152 const Voxel* Volume::voxels() const
153 {
154  return &(m_Voxels.front());
155 }
156 
157 const int Volume::width() const
158 {
159  return m_Width;
160 }
161 
162 const int Volume::height() const
163 {
164  return m_Height;
165 }
166 
167 const int Volume::depth() const
168 {
169  return m_Depth;
170 }
171 
172 const int Volume::size() const
173 {
174  return m_Size;
175 }
176 
177 
178 //-------------------------------------------------------------------------------------------------
179 // Volume File Loader
180 //-------------------------------------------------------------------------------------------------
181 
182 bool Volume::loadFromFile(QString filename, QProgressBar* progressBar)
183 {
184  // load file
185  FILE *fp = NULL;
186  fopen_s(&fp, filename.toStdString().c_str(), "rb");
187  if (!fp)
188  {
189  std::cerr << "+ Error loading file: " << filename.toStdString() << std::endl;
190  return false;
191  }
192 
193  // progress bar
194 
195  progressBar->setRange(0, m_Size + 10);
196  progressBar->setValue(0);
197 
198 
199  // read header and set volume dimensions
200 
201  unsigned short uWidth, uHeight, uDepth;
202  fread(&uWidth, sizeof(unsigned short), 1, fp);
203  fread(&uHeight, sizeof(unsigned short), 1, fp);
204  fread(&uDepth, sizeof(unsigned short), 1, fp);
205 
206  m_Width = int(uWidth);
207  m_Height = int(uHeight);
208  m_Depth = int(uDepth);
209 
210  // check dataset dimensions
211  if (
212  m_Width <= 0 || m_Width > 1000 ||
213  m_Height <= 0 || m_Height > 1000 ||
214  m_Depth <= 0 || m_Depth > 1000)
215  {
216  std::cerr << "+ Error loading file: " << filename.toStdString() << std::endl;
217  std::cerr << "Unvalid dimensions - probably loaded .dat flow file instead of .gri file?" << std::endl;
218  return false;
219  }
220 
221  // compute dimensions
222  int slice = m_Width * m_Height;
223  m_Size = slice * m_Depth;
224  m_Voxels.resize(m_Size);
225 
226 
227  // read volume data
228 
229  // read into vector before writing data into volume to speed up process
230  std::vector<unsigned short> vecData;
231  vecData.resize(m_Size);
232  fread((void*)&(vecData.front()), sizeof(unsigned short), m_Size, fp);
233  fclose(fp);
234 
235  progressBar->setValue(10);
236 
237 
238  // store volume data
239 
240  for (int i = 0; i < m_Size; i++)
241  {
242  // data is converted to FLOAT values in an interval of [0.0 .. 1.0];
243  // uses 4095.0f to normalize the data, because only 12bit are used for the
244  // data values, and then 4095.0f is the maximum possible value
245  const float value = std::fmin(1.0f, float(vecData[i]) / 4095.0f);
246  m_Voxels[i] = Voxel(value);
247  progressBar->setValue(10 + i);
248  }
249 
250  progressBar->setValue(0);
251 
252  std::cout << "Loaded VOLUME with dimensions " << m_Width << " x " << m_Height << " x " << m_Depth << std::endl;
253 
254  return true;
255 }
256 
257 QOpenGLTexture* Volume::generate3DTex() {
258  auto tex = new QOpenGLTexture(QOpenGLTexture::Target3D);
259  if (!tex->create()) {
260  std::cout << "Unable to create texture object" << std::endl;
261  return tex;
262  }
263 
264  std::vector<float> rawData;
265  for (const auto& vox : m_Voxels) {
266  rawData.push_back(vox.getValue());
267  }
268 
269  tex->setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::ClampToBorder);
270  tex->setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::ClampToBorder);
271  tex->setWrapMode(QOpenGLTexture::DirectionR, QOpenGLTexture::ClampToBorder);
272 
273  tex->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
274  tex->setMagnificationFilter(QOpenGLTexture::Nearest);
275 
276  tex->setFormat(QOpenGLTexture::R32F);
277  tex->setSize(m_Width, m_Height, m_Depth);
278 
279  tex->allocateStorage(QOpenGLTexture::Red, QOpenGLTexture::Float32);
280  if (tex->isStorageAllocated()) {
281  tex->setData(0, QOpenGLTexture::Red, QOpenGLTexture::Float32, rawData.data());
282  }
283 
284  return tex;
285 }
const bool operator>=(const Voxel &other) const
Definition: Volume.cpp:61
const Voxel & operator*=(const float &value)
Definition: Volume.cpp:88
Voxel()
Definition: Volume.cpp:17
const Voxel & voxel(const int i) const
Definition: Volume.cpp:147
const Voxel operator+(const Voxel &other) const
Definition: Volume.cpp:100
const bool operator<(const Voxel &other) const
Definition: Volume.cpp:66
const Voxel operator*(const float &value) const
Definition: Volume.cpp:114
~Volume()
Definition: Volume.cpp:138
const int size() const
Definition: Volume.cpp:172
const bool operator<=(const Voxel &other) const
Definition: Volume.cpp:71
void setValue(const float value)
Definition: Volume.cpp:36
QOpenGLTexture * generate3DTex()
generate3DTex transforms the loaded volume data set into a 3D texture which can be sampled in the gls...
Definition: Volume.cpp:257
const Voxel & operator+=(const Voxel &other)
Definition: Volume.cpp:76
const int depth() const
Definition: Volume.cpp:167
const Voxel operator/(const float &value) const
Definition: Volume.cpp:121
const Voxel & operator-=(const Voxel &other)
Definition: Volume.cpp:82
const Voxel * voxels() const
Definition: Volume.cpp:152
const bool operator!=(const Voxel &other) const
Definition: Volume.cpp:51
const Voxel operator-(const Voxel &other) const
Definition: Volume.cpp:107
Helper class for Voxels with basic operators.
Definition: Volume.h:25
const float getValue() const
Definition: Volume.cpp:41
bool loadFromFile(QString filename, QProgressBar *progressBar)
Definition: Volume.cpp:182
~Voxel()
Definition: Volume.cpp:32
const bool operator==(const Voxel &other) const
Definition: Volume.cpp:46
const bool operator>(const Voxel &other) const
Definition: Volume.cpp:56
const int height() const
Definition: Volume.cpp:162
const Voxel & operator/=(const float &value)
Definition: Volume.cpp:94
Volume()
Definition: Volume.cpp:133
const int width() const
Definition: Volume.cpp:157