Kinetic Visualization
 All Classes Functions Variables Pages
Volume.h
1 #pragma once
2 
3 #include <stdlib.h>
4 #include <windows.h>
5 #include <commdlg.h>
6 #include <iostream>
7 #include <sstream>
8 #include <string>
9 #include <io.h>
10 #include <vector>
11 #include <algorithm>
12 #include <functional>
13 #include <math.h>
14 
15 
16 using std::min;
17 using std::cout;
18 using std::endl;
19 
20 // Simple linear volume class which supports loading from DAT files
21 class Volume
22 {
23 public:
24 
25  class Voxel
26  {
27  public:
28 
29  Voxel()
30  {
31  SetValue(0.0f);
32  };
33 
34  Voxel(const Voxel & datOther)
35  {
36  m_fValue = datOther.m_fValue;
37  };
38 
39  Voxel(const float fValue)
40  {
41  SetValue(fValue);
42  };
43 
44 
45  ~Voxel()
46  {
47  };
48 
49  void SetValue(const float fValue)
50  {
51  m_fValue = fValue;
52  };
53 
54  const float GetValue() const
55  {
56  return m_fValue;
57  };
58 
59  const bool operator==(const Voxel &datOther) const
60  {
61  return (GetValue() == datOther.GetValue());
62  };
63 
64  const bool operator!=(const Voxel &datOther) const
65  {
66  return !(*this == datOther);
67  };
68 
69  const bool operator>(const Voxel &datOther) const
70  {
71  return GetValue() > datOther.GetValue();
72  };
73 
74  const bool operator>=(const Voxel &datOther) const
75  {
76  return GetValue() >= datOther.GetValue();
77  };
78 
79  const bool operator<(const Voxel &datOther) const
80  {
81  return GetValue() < datOther.GetValue();
82  };
83 
84  const bool operator<=(const Voxel &datOther) const
85  {
86  return GetValue() <= datOther.GetValue();
87  };
88 
89  const Voxel & operator+=(const Voxel & datOther)
90  {
91  m_fValue += datOther.m_fValue;
92  return *this;
93  };
94 
95  const Voxel & operator-=(const Voxel & datOther)
96  {
97  m_fValue -= datOther.m_fValue;
98  return *this;
99  };
100 
101  const Voxel & operator*=(const float & fOther)
102  {
103  m_fValue *= fOther;
104  return *this;
105  };
106 
107  const Voxel & operator/=(const float & fOther)
108  {
109  m_fValue /= fOther;
110  return *this;
111  };
112 
113  const Voxel operator+(const Voxel & datOther) const
114  {
115  Voxel voxNew = *this;
116  voxNew += datOther;
117  return voxNew;
118  };
119 
120  const Voxel operator-(const Voxel & datOther) const
121  {
122  Voxel voxNew = *this;
123  voxNew -= datOther;
124  return voxNew;
125  };
126 
127  const Voxel operator*(const float & fOther) const
128  {
129  Voxel voxNew = *this;
130  voxNew *= fOther;
131  return voxNew;
132  };
133 
134  const Voxel operator/(const float & fOther) const
135  {
136  Voxel voxNew = *this;
137  voxNew /= fOther;
138  return voxNew;
139  };
140 
141  private:
142  float m_fValue;
143  };
144 
145 public:
146 
147  Volume() : m_iWidth(1), m_iHeight(1), m_iDepth(1), m_vecVoxels(1), hist(0)
148  {
149  };
150 
151  Volume(const std::string &strFilename) : m_iWidth(1), m_iHeight(1), m_iDepth(1), m_vecVoxels(1), hist(0)
152  {
153  load(strFilename);
154  };
155 
156  ~Volume(void)
157  {
158  if (hist != 0) {
159  delete[] hist;
160  }
161  };
162 
163  const Voxel & Get(const int iX, const int iY, const int iZ) const
164  {
165  return m_vecVoxels[iX + iY*m_iWidth + iZ*m_iWidth*m_iHeight];
166  };
167 
168  const Voxel & Get(const int iIndex) const
169  {
170  return m_vecVoxels[iIndex];
171  };
172 
173  const Voxel * Get() const
174  {
175  return &(m_vecVoxels.front());
176  };
177 
178  const int GetWidth() const
179  {
180  return m_iWidth;
181  };
182 
183  const int GetHeight() const
184  {
185  return m_iHeight;
186  };
187 
188  const int GetDepth() const
189  {
190  return m_iDepth;
191  };
192 
193  const int GetSize() const
194  {
195  return int(m_vecVoxels.size());
196  };
197 
198  //für scale = 8 ergeben sich z.B: 512 werte
199  double* getHistogram(int scale) {
200 
201  if (hist != 0) {
202  delete[] hist;
203  }
204 
205  int index;
206  hist = new double[4096/scale]();
207  for (int i = 0; i < GetSize(); i++) {
208  //intensität auf int aufrunden für index
209  index = (int)floorf(m_vecVoxels[i].GetValue()*4095.0f)/scale;
210  hist[index]++;
211  }
212  //logarithmieren
213  for (int i = 0; i < (4096/scale); i++) {
214  hist[i] = log(hist[i]);
215  }
216  return hist;
217  };
218 
219  void load(const std::string & strFilename)
220  {
221  cout << "- Loading file '" << strFilename << "' ... " << endl;
222  FILE *fp = NULL;
223 
224  fopen_s(&fp,strFilename.c_str(),"rb");
225 
226  if (!fp)
227  {
228  std::cerr << "+ Error loading file." << std::endl << endl;
229  }
230  else
231  {
232  char vcPath[1024];
233  char *pFileName = NULL;
234  GetFullPathName(strFilename.c_str(),1024,vcPath,&pFileName);
235  char vcDrive[1024], vcDirectory[1024], vcFilename[1024], vcExtension[1024];
236  _splitpath_s(vcPath,vcDrive,vcDirectory,vcFilename,vcExtension);
237  const std::string strAdditionalFilename = std::string(vcDrive)+std::string(vcDirectory)+std::string(vcFilename)+std::string(".ini");
238 
239  char vpSpacingX[1024],vpSpacingY[1024],vpSpacingZ[1024];
240  GetPrivateProfileString("DatFile","oldDat Spacing X","1.0",vpSpacingX,256,strAdditionalFilename.c_str());
241  GetPrivateProfileString("DatFile","oldDat Spacing Y","1.0",vpSpacingY,256,strAdditionalFilename.c_str());
242  GetPrivateProfileString("DatFile","oldDat Spacing Z","1.0",vpSpacingZ,256,strAdditionalFilename.c_str());
243 
244  unsigned short uWidth, uHeight, uDepth;
245  fread(&uWidth,sizeof(unsigned short),1,fp);
246  fread(&uHeight,sizeof(unsigned short),1,fp);
247  fread(&uDepth,sizeof(unsigned short),1,fp);
248 
249  m_iWidth = int(uWidth);
250  m_iHeight = int(uHeight);
251  m_iDepth = int(uDepth);
252 
253  const int iSlice = m_iWidth * m_iHeight;
254  const int iSize = iSlice * m_iDepth;
255  m_vecVoxels.resize(iSize);
256 
257  std::vector<unsigned short> vecData;
258  vecData.resize(iSize);
259 
260  fread((void*)&(vecData.front()),sizeof(unsigned short),iSize,fp);
261  fclose(fp);
262 
263  cout << "- File loaded." << endl;
264 
265  for (int k=0;k<m_iDepth;k++)
266  {
267  for (int j=0;j<m_iHeight;j++)
268  {
269  for (int i=0;i<m_iWidth;i++)
270  {
271  //we convert the data to float values in an interval of [0..1]
272  const float fValue = min(1.0f, float(vecData[i + j*m_iWidth + k*iSlice]*16) / 65535.0f);
273  //const float fValue = min(1.0f, float(vecData[i + j*m_iWidth + k*iSlice]) / 4095.0f);
274  m_vecVoxels[i+j*m_iWidth+k*iSlice] = Voxel(fValue);
275  }
276  }
277  cout << "\r- Preparing data (" << (k*100) / (m_iDepth-1) << "%) ...";
278 
279  }
280  cout << std::endl << "- Data prepared." << endl;
281  }
282 
283  cout << "Volume dimensions: " << m_iWidth << " x " << m_iHeight << " x " << m_iDepth << endl;
284  };
285 
286  std::vector<Voxel> m_vecVoxels;
287 protected:
288 
289 private:
290 
291  int m_iWidth,m_iHeight,m_iDepth;
292  double* hist;
293 };