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