00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_BINARY_READ_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_BINARY_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <iostream>
00013 #include <string>
00014
00015 #ifdef WIN32
00016 # include <io.h>
00017 #endif
00018
00019 namespace OpenTissue
00020 {
00021 namespace grid
00022 {
00023 template <typename grid_type>
00024 inline bool binary_read(std::string const & filename, grid_type & grid)
00025 {
00026 typedef typename grid_type::value_type value_type;
00027 typedef typename grid_type::math_types math_types;
00028 typedef typename math_types::vector3_type vector3_type;
00029 typedef typename math_types::real_type real_type;
00030
00031 using std::min;
00032 using std::max;
00033
00034 FILE *stream;
00035
00036 if((stream = fopen( filename.c_str(), "rb" )) == NULL )
00037 {
00038 std::cerr << "binary_read(): unable to open file " << filename << std::endl;
00039 return false;
00040 }
00041
00042 size_t I,J,K,N;
00043 if( fread( &I, sizeof( size_t ), 1, stream ) != 1)
00044 throw std::logic_error("binary_read(): did not read I");
00045 if( fread( &J, sizeof( size_t ), 1, stream ) != 1)
00046 throw std::logic_error("binary_read(): did not read J");
00047 if( fread( &K, sizeof( size_t ), 1, stream ) != 1)
00048 throw std::logic_error("binary_read(): did not read K");
00049 if( fread( &N, sizeof( size_t ), 1, stream ) != 1)
00050 throw std::logic_error("binary_read(): did not read N");
00051
00052 real_type min_x,min_y,min_z;
00053
00054 if( fread( &min_x, sizeof( real_type ), 1, stream ) != 1)
00055 throw std::logic_error("binary_read(): could not read min x");
00056 if( fread( &min_y, sizeof( real_type ), 1, stream ) != 1)
00057 throw std::logic_error("binary_read(): could not read min y");
00058 if( fread( &min_z, sizeof( real_type ), 1, stream ) != 1)
00059 throw std::logic_error("binary_read(): could not read min z");
00060
00061 real_type max_x,max_y,max_z;
00062 if( fread( &max_x, sizeof( real_type ), 1, stream ) != 1)
00063 throw std::logic_error("binary_read(): could not read max x");
00064 if( fread( &max_y, sizeof( real_type ), 1, stream ) != 1)
00065 throw std::logic_error("binary_read(): could not read max y");
00066 if( fread( &max_z, sizeof( real_type ), 1, stream ) != 1)
00067 throw std::logic_error("binary_read(): could not read max z");
00068
00069 real_type dx,dy,dz;
00070 if( fread( &dx, sizeof( real_type ), 1, stream ) != 1)
00071 throw std::logic_error("binary_read(): could not read dx");
00072 if( fread( &dy, sizeof( real_type ), 1, stream ) != 1)
00073 throw std::logic_error("binary_read(): could not read dy");
00074 if( fread( &dz, sizeof( real_type ), 1, stream ) != 1)
00075 throw std::logic_error("binary_read(): could not read dz");
00076
00077 grid.create(vector3_type(min_x,min_y,min_z), vector3_type(max_x, max_y, max_z), I, J, K);
00078 if (grid.dx() != dx || grid.dy() != dy || grid.dz() != dz)
00079 {
00080 std::cout << "binary_read(): warning: spacing differs between calculated and stored!" << std::endl;
00081 grid.dx() = dx;
00082 grid.dy() = dy;
00083 grid.dz() = dz;
00084 }
00085
00086 if( fread( grid.data(), sizeof( value_type ), grid.size(), stream ) != grid.size() )
00087 throw std::logic_error("binary_read(): could not read data");
00088
00089 fclose( stream );
00090
00091 std::cout << "binary_read(): Completed reading file: " << filename
00092 << ", min = " << OpenTissue::grid::min_element(grid) << " max = " << OpenTissue::grid::max_element(grid) << std::endl;
00093 return true;
00094 }
00095
00096 }
00097 }
00098
00099
00100 #endif