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