00001 #include <vector>
00002
00003 #include <algorithm>
00004 #include <functional>
00005
00006 #include <iostream>
00007
00008
00009 class Volume
00010 {
00011 public:
00012
00013 class Voxel
00014 {
00015 public:
00016
00017 Voxel()
00018 {
00019 SetValue(0.0f);
00020 };
00021
00022 Voxel(const Voxel & datOther)
00023 {
00024 m_fValue = datOther.m_fValue;
00025 };
00026
00027 Voxel(const float fValue)
00028 {
00029 SetValue(fValue);
00030 };
00031
00032
00033 ~Voxel()
00034 {
00035 };
00036
00037 void SetValue(const float fValue)
00038 {
00039 m_fValue = fValue;
00040 };
00041
00042 float GetValue() const
00043 {
00044 return m_fValue;
00045 };
00046
00047 bool operator==(const Voxel &datOther) const
00048 {
00049 return (GetValue() == datOther.GetValue());
00050 };
00051
00052 bool operator!=(const Voxel &datOther) const
00053 {
00054 return !(*this == datOther);
00055 };
00056
00057 bool operator>(const Voxel &datOther) const
00058 {
00059 return GetValue() > datOther.GetValue();
00060 };
00061
00062 bool operator>=(const Voxel &datOther) const
00063 {
00064 return GetValue() >= datOther.GetValue();
00065 };
00066
00067 bool operator<(const Voxel &datOther) const
00068 {
00069 return GetValue() < datOther.GetValue();
00070 };
00071
00072 bool operator<=(const Voxel &datOther) const
00073 {
00074 return GetValue() <= datOther.GetValue();
00075 };
00076
00077 const Voxel & operator+=(const Voxel & datOther)
00078 {
00079 m_fValue += datOther.m_fValue;
00080 return *this;
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 float & fOther)
00090 {
00091 m_fValue *= fOther;
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 Voxel & datOther) const
00102 {
00103 Voxel voxNew = *this;
00104 voxNew += datOther;
00105 return voxNew;
00106 };
00107
00108 const Voxel operator-(const Voxel & datOther) const
00109 {
00110 Voxel voxNew = *this;
00111 voxNew -= datOther;
00112 return voxNew;
00113 };
00114
00115 const Voxel operator*(const float & fOther) const
00116 {
00117 Voxel voxNew = *this;
00118 voxNew *= fOther;
00119 return voxNew;
00120 };
00121
00122 const Voxel operator/(const float & fOther) const
00123 {
00124 Voxel voxNew = *this;
00125 voxNew /= fOther;
00126 return voxNew;
00127 };
00128
00129 private:
00130 float m_fValue;
00131 };
00132
00133 public:
00134 Volume() : m_iWidth(1), m_iHeight(1), m_iDepth(1), m_vecVoxels(1)
00135 {
00136 };
00137
00138 Volume(const std::string &strFilename) : m_iWidth(1), m_iHeight(1), m_iDepth(1), m_vecVoxels(1)
00139 {
00140 load(strFilename);
00141 };
00142
00143 ~Volume(void)
00144 {
00145 };
00146
00147 const Voxel & Get(const int iX, const int iY, const int iZ) const
00148 {
00149 return m_vecVoxels[iX + iY*m_iWidth + iZ*m_iWidth*m_iHeight];
00150 };
00151
00152 const Voxel & Get(const int iIndex) const
00153 {
00154 return m_vecVoxels[iIndex];
00155 };
00156
00157 const Voxel * Get() const
00158 {
00159 return &(m_vecVoxels.front());
00160 };
00161
00162 int GetWidth() const
00163 {
00164 return m_iWidth;
00165 };
00166
00167 int GetHeight() const
00168 {
00169 return m_iHeight;
00170 };
00171
00172 int GetDepth() const
00173 {
00174 return m_iDepth;
00175 };
00176
00177 int GetSize() const
00178 {
00179 return int(m_vecVoxels.size());
00180 };
00181
00182
00183 void load(const std::string & strFilename)
00184 {
00185 std::cout << "- Loading file '" << strFilename << "' ... " << std::endl;
00186 FILE* fp = NULL;
00187
00188 fp = fopen(strFilename.c_str(),"rb");
00189 if (!fp) {
00190 std::cerr << "+ Error loading file." << std::endl << std::endl;
00191 } else {
00192 unsigned short uWidth, uHeight, uDepth;
00193 fread(&uWidth,sizeof(unsigned short),1,fp);
00194 fread(&uHeight,sizeof(unsigned short),1,fp);
00195 fread(&uDepth,sizeof(unsigned short),1,fp);
00196
00197 m_iWidth = int(uWidth);
00198 m_iHeight = int(uHeight);
00199 m_iDepth = int(uDepth);
00200
00201 const int iSlice = m_iWidth * m_iHeight;
00202 const int iSize = iSlice * m_iDepth;
00203 m_vecVoxels.resize(iSize);
00204
00205 std::vector<unsigned short> vecData;
00206 vecData.resize(iSize);
00207
00208 fread((void*)&(vecData.front()),sizeof(unsigned short),iSize,fp);
00209 fclose(fp);
00210
00211 std::cout << "- File loaded." << std::endl;
00212
00213 for (int k=0;k<m_iDepth;k++) {
00214 for (int j=0;j<m_iHeight;j++) {
00215 for (int i=0;i<m_iWidth;i++) {
00216
00217 const float fValue = std::min(1.0f,float(vecData[i + j*m_iWidth + k*iSlice]) / 4095.0f);
00218 m_vecVoxels[i+j*m_iWidth+k*iSlice] = Voxel(fValue);
00219 }
00220 }
00221
00222 std::cout << "\r- Preparing data (" << (k*100) / (m_iDepth-1) << "%) ...";
00223 }
00224
00225 std::cout << std::endl << "- Data prepared." << std::endl;
00226 }
00227 };
00228
00229 protected:
00230
00231 private:
00232 int m_iWidth,m_iHeight,m_iDepth;
00233 std::vector<Voxel> m_vecVoxels;
00234
00235 };