• Main Page
  • Classes
  • Files
  • File List
  • File Members

Volume.hpp

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

Generated on Mon Dec 6 2010 11:57:24 for Visualisierung 2010 by  doxygen 1.7.2