00001 #pragma once
00002
00003 #include "common.h"
00004 #include <stdlib.h>
00005 #include <glew.h>
00006 #include <openglut.h>
00007 #include <vector>
00008
00009 #include <algorithm>
00010 #include <functional>
00011
00012 #include "Matrix.h"
00013
00015
00019 class Volume
00020 {
00021 public:
00022
00024
00028 class Voxel
00029 {
00030 public:
00031
00032 Voxel()
00033 {
00034 SetValue(0.0f);
00035 };
00036
00037 Voxel(const Voxel & datOther)
00038 {
00039 m_fValue = datOther.m_fValue;
00040 };
00041
00042 Voxel(const float fValue)
00043 {
00044 SetValue(fValue);
00045 };
00046
00047
00048 ~Voxel()
00049 {
00050 };
00051
00052 void SetValue(const float fValue)
00053 {
00054 m_fValue = fValue;
00055 };
00056
00057 const float GetValue() const
00058 {
00059 return m_fValue;
00060 };
00061
00062 const bool operator==(const Voxel &datOther) const
00063 {
00064 return (GetValue() == datOther.GetValue());
00065 };
00066
00067 const bool operator!=(const Voxel &datOther) const
00068 {
00069 return !(*this == datOther);
00070 };
00071
00072 const bool operator>(const Voxel &datOther) const
00073 {
00074 return GetValue() > datOther.GetValue();
00075 };
00076
00077 const bool operator>=(const Voxel &datOther) const
00078 {
00079 return GetValue() >= datOther.GetValue();
00080 };
00081
00082 const bool operator<(const Voxel &datOther) const
00083 {
00084 return GetValue() < datOther.GetValue();
00085 };
00086
00087 const bool operator<=(const Voxel &datOther) const
00088 {
00089 return GetValue() <= datOther.GetValue();
00090 };
00091
00092 const Voxel & operator+=(const Voxel & datOther)
00093 {
00094 m_fValue += datOther.m_fValue;
00095 return *this;
00096 };
00097
00098 const Voxel & operator-=(const Voxel & datOther)
00099 {
00100 m_fValue -= datOther.m_fValue;
00101 return *this;
00102 };
00103
00104 const Voxel & operator*=(const float & fOther)
00105 {
00106 m_fValue *= fOther;
00107 return *this;
00108 };
00109
00110 const Voxel & operator/=(const float & fOther)
00111 {
00112 m_fValue /= fOther;
00113 return *this;
00114 };
00115
00116 const Voxel operator+(const Voxel & datOther) const
00117 {
00118 Voxel voxNew = *this;
00119 voxNew += datOther;
00120 return voxNew;
00121 };
00122
00123 const Voxel operator-(const Voxel & datOther) const
00124 {
00125 Voxel voxNew = *this;
00126 voxNew -= datOther;
00127 return voxNew;
00128 };
00129
00130 const Voxel operator*(const float & fOther) const
00131 {
00132 Voxel voxNew = *this;
00133 voxNew *= fOther;
00134 return voxNew;
00135 };
00136
00137 const Voxel operator/(const float & fOther) const
00138 {
00139 Voxel voxNew = *this;
00140 voxNew /= fOther;
00141 return voxNew;
00142 };
00143
00144 private:
00145 float m_fValue;
00146 };
00147
00148 public:
00149
00150
00151 Volume() : m_iWidth(1), m_iHeight(1), m_iDepth(1), m_vecVoxels(1)
00152 {
00153 };
00154
00155 Volume(const std::string &strFilename) : m_iWidth(1), m_iHeight(1), m_iDepth(1), m_vecVoxels(1)
00156 {
00157 load(strFilename);
00158 };
00159
00160 ~Volume(void)
00161 {
00162 };
00163
00164 const Voxel & Get(const int iX, const int iY, const int iZ) const
00165 {
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 return m_vecVoxels[iX + iY*m_iWidth + iZ*m_iWidth*m_iHeight];
00180 };
00181
00182 const Voxel & Get(const int iIndex) const
00183 {
00184 return m_vecVoxels[iIndex];
00185 };
00186
00187 const Voxel * Get() const
00188 {
00189 return &(m_vecVoxels.front());
00190 };
00191
00192 void Set(int iX, int iY, int iZ, float value)
00193 {
00194 m_vecVoxels[iX + iY*m_iWidth + iZ*m_iWidth*m_iHeight] = value;
00195 };
00196
00197 const int GetWidth() const
00198 {
00199 return m_iWidth;
00200 };
00201
00202 const int GetHeight() const
00203 {
00204 return m_iHeight;
00205 };
00206
00207 const int GetDepth() const
00208 {
00209 return m_iDepth;
00210 };
00211
00212 const int GetSize() const
00213 {
00214 return int(m_vecVoxels.size());
00215 };
00216
00217
00218 void load(const std::string & strFilename)
00219 {
00220 std::cout << "- Loading file '" << strFilename << "' ... " << std::endl;
00221 FILE *fp = NULL;
00222
00223 fopen_s(&fp,strFilename.c_str(),"rb");
00224
00225 if (!fp)
00226 {
00227 std::cerr << "+ Error loading file." << std::endl << std::endl;
00228 }
00229 else
00230 {
00231
00232 char vcPath[1024];
00233 char *pFileName = NULL;
00234 GetFullPathName(strFilename.c_str(),1024,vcPath,&pFileName);
00235 char vcDrive[1024], vcDirectory[1024], vcFilename[1024], vcExtension[1024];
00236 _splitpath_s(vcPath,vcDrive,vcDirectory,vcFilename,vcExtension);
00237 const std::string strAdditionalFilename = std::string(vcDrive)+std::string(vcDirectory)+std::string(vcFilename)+std::string(".ini");
00238
00239 char vpSpacingX[1024],vpSpacingY[1024],vpSpacingZ[1024];
00240 GetPrivateProfileString("DatFile","oldDat Spacing X","1.0",vpSpacingX,256,strAdditionalFilename.c_str());
00241 GetPrivateProfileString("DatFile","oldDat Spacing Y","1.0",vpSpacingY,256,strAdditionalFilename.c_str());
00242 GetPrivateProfileString("DatFile","oldDat Spacing Z","1.0",vpSpacingZ,256,strAdditionalFilename.c_str());
00243
00244
00245 unsigned short uWidth, uHeight, uDepth;
00246 fread(&uWidth,sizeof(unsigned short),1,fp);
00247 fread(&uHeight,sizeof(unsigned short),1,fp);
00248 fread(&uDepth,sizeof(unsigned short),1,fp);
00249
00250 m_iWidth = int(uWidth);
00251 m_iHeight = int(uHeight);
00252 m_iDepth = int(uDepth);
00253
00254 const int iSlice = m_iWidth * m_iHeight;
00255 const int iSize = iSlice * m_iDepth;
00256 m_vecVoxels.resize(iSize);
00257
00258 std::vector<unsigned short> vecData;
00259 vecData.resize(iSize);
00260
00261 fread((void*)&(vecData.front()),sizeof(unsigned short),iSize,fp);
00262 fclose(fp);
00263
00264 std::cout << "- File loaded." << std::endl;
00265
00266 for (int k=0;k<m_iDepth;k++)
00267 {
00268 for (int j=0;j<m_iHeight;j++)
00269 {
00270 for (int i=0;i<m_iWidth;i++)
00271 {
00272
00273 const float fValue = std::min(1.0f,float(vecData[i + j*m_iWidth + k*iSlice]) / 4095.0f);
00274 m_vecVoxels[i+j*m_iWidth+k*iSlice] = Voxel(fValue);
00275 }
00276 }
00277 std::cout << "\r- Preparing data (" << (k*100) / (m_iDepth-1) << "%) ...";
00278 }
00279 std::cout << std::endl << "- Data prepared." << std::endl;
00280 }
00281 };
00282
00283 protected:
00284
00285 private:
00286 std::vector<Voxel> m_vecVoxels;
00287 int m_iWidth,m_iHeight,m_iDepth;
00288 };