00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_BINARY_WRITE_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_BINARY_WRITE_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
00024 template <typename grid_type>
00025 inline bool binary_write(std::string const & filename, grid_type & grid)
00026 {
00027 typedef typename grid_type::value_type value_type;
00028 typedef typename grid_type::math_types math_types;
00029 typedef typename math_types::vector3_type vector3_type;
00030 typedef typename math_types::real_type real_type;
00031
00032 FILE *stream;
00033
00034 if( (stream = fopen( filename.c_str(), "wb" )) == NULL )
00035 {
00036 std::cerr << "binary_write() : unable to open file " << filename << std::endl;
00037 return false;
00038 }
00039
00040 size_t I = grid.I();
00041 size_t J = grid.J();
00042 size_t K = grid.K();
00043 size_t N = grid.size();
00044 fwrite( &I, sizeof( size_t ), 1, stream );
00045 fwrite( &J, sizeof( size_t ), 1, stream );
00046 fwrite( &K, sizeof( size_t ), 1, stream );
00047 fwrite( &N, sizeof( size_t ), 1, stream );
00048
00049 vector3_type min_coord = grid.min_coord();
00050 real_type min_x = min_coord(0);
00051 real_type min_y = min_coord(1);
00052 real_type min_z = min_coord(0);
00053 fwrite( &min_x, sizeof( real_type ), 1, stream );
00054 fwrite( &min_y, sizeof( real_type ), 1, stream );
00055 fwrite( &min_z, sizeof( real_type ), 1, stream );
00056
00057 vector3_type max_coord = grid.max_coord();
00058 real_type max_x = max_coord(0);
00059 real_type max_y = max_coord(1);
00060 real_type max_z = max_coord(0);
00061 fwrite( &max_x, sizeof( real_type ), 1, stream );
00062 fwrite( &max_y, sizeof( real_type ), 1, stream );
00063 fwrite( &max_z, sizeof( real_type ), 1, stream );
00064
00065 real_type dx = grid.dx();
00066 real_type dy = grid.dy();
00067 real_type dz = grid.dz();
00068 fwrite( &dx, sizeof( real_type ), 1, stream );
00069 fwrite( &dy, sizeof( real_type ), 1, stream );
00070 fwrite( &dz, sizeof( real_type ), 1, stream );
00071
00072 fwrite( grid.data(), sizeof( value_type ), grid.size(), stream );
00073
00074 fclose( stream );
00075 std::cout << "binary_write(): Completed writing file: " << filename << std::endl;
00076 return true;
00077 }
00078
00079 }
00080 }
00081
00082
00083 #endif