00001 #include "Volume.h"
00002
00003
00004 Volume::Volume()
00005 {
00006 release();
00007 }
00008
00009 Volume::Volume( const std::string &strFilename )
00010 {
00011 release();
00012 load( strFilename );
00013 }
00014
00015 Volume::~Volume(void)
00016 {
00017 release();
00018 }
00019
00020
00021 void Volume::load( const std::string &strFilename )
00022 {
00023 std::cout << "- Loading file '" << strFilename << "' ... " << std::endl;
00024
00025 FILE *fp = NULL;
00026 fopen_s( &fp, strFilename.c_str(), "rb" );
00027
00028 if (!fp)
00029 std::cerr << "- Error loading file!" << std::endl << std::endl;
00030 else
00031 {
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 unsigned short sizeX, sizeY, sizeZ;
00050 fread( &sizeX, sizeof(unsigned short), 1, fp );
00051 fread( &sizeY, sizeof(unsigned short), 1, fp );
00052 fread( &sizeZ, sizeof(unsigned short), 1, fp );
00053
00054 m_sizeX = int(sizeX);
00055 m_sizeY = int(sizeY);
00056 m_sizeZ = int(sizeZ);
00057
00058 std::cout << " - Volume-dimensions: (" <<
00059 m_sizeX << ", " <<
00060 m_sizeY << ", " <<
00061 m_sizeZ << ")" << std::endl;
00062
00063 const int slice = sizeX * sizeY;
00064 const int volumeSize = slice * sizeZ;
00065 m_voxels.resize( volumeSize );
00066
00067 std::vector<unsigned short> data;
00068 data.resize( volumeSize );
00069
00070
00071 fread( (void*) &(data.front()), sizeof(unsigned short), volumeSize, fp );
00072 fclose( fp );
00073
00074 std::cout << "- File loaded." << std::endl;
00075
00076
00077 memset(m_densityArray, 0, sizeof(m_densityArray));
00078 int densityIndex = 0;
00079
00080
00081 for ( int z=0; z<m_sizeZ; z++ )
00082 {
00083 for ( int y=0; y<m_sizeY; y++ )
00084 {
00085 for ( int x=0; x<m_sizeX; x++ )
00086 {
00087
00088 const float fValue = std::min( 1.0f, float( data[ x + y*m_sizeX + z*slice ] ) / 4095.0f );
00089 m_voxels[ x + y*m_sizeX + z*slice ] = Voxel( fValue );
00090
00091
00092 densityIndex = (int) ( Voxel(fValue).getValue() * DENSITY_COUNT );
00093 m_densityArray[densityIndex]++;
00094
00095 if( m_densityArray[densityIndex] > m_maxDensityIndex )
00096 {
00097 m_maxDensityIndex = m_densityArray[densityIndex];
00098 }
00099 }
00100 }
00101 std::cout << "\r- Preparing data (" << (z*100) / (m_sizeZ - 1) << "%) ...";
00102 }
00103 std::cout << std::endl << "- Data prepared." << std::endl;
00104 }
00105 }
00106
00107 void Volume::release()
00108 {
00109 printf("Unload Volume data\n");
00110
00111 memset(m_densityArray, 0, sizeof(m_densityArray));
00112 m_voxels.clear();
00113 m_sizeX = m_sizeY = m_sizeZ = 1;
00114 }