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