GEL
2
GEL is a library for Geometry and Linear Algebra
|
00001 #ifndef __GEOMETRY_SAVE_RAW_H 00002 #define __GEOMETRY_SAVE_RAW_H 00003 00004 #include <iostream> 00005 #include <iomanip> 00006 #include <fstream> 00007 #include "GridAlgorithm.h" 00008 00009 namespace Geometry 00010 { 00011 template<class G> 00012 class VolSaver 00013 { 00014 public: 00015 typedef typename G::DataType DataType; 00016 00017 private: 00018 std::ofstream of; 00019 const float min_val, max_val; 00020 const float diff; 00021 float old; 00022 public: 00023 00024 VolSaver(const std::string& name, float _min_val, float _max_val): 00025 of(name.c_str(), std::ios::binary), 00026 min_val(_min_val), 00027 max_val(_max_val), 00028 diff(max_val - min_val) {} 00029 00030 void operator()(const CGLA::Vec3i& pi, const float& vox_val) 00031 { 00032 float scaled = (vox_val-min_val) / diff; 00033 float clamped = 255.0f *(CGLA::s_min(1.0f,CGLA::s_max(0.0f,scaled))); 00034 unsigned char x = static_cast<unsigned char>(clamped); 00035 of.write((char*) &x, 1); 00036 } 00037 }; 00038 00039 template<class G> 00040 class VolSaverAscii 00041 { 00042 public: 00043 typedef typename G::DataType DataType; 00044 00045 private: 00046 std::ofstream of; 00047 const float min_val, max_val; 00048 float old; 00049 public: 00050 00051 VolSaverAscii(const std::string& name, float _min_val, float _max_val): 00052 of(name.c_str()), 00053 min_val(_min_val), 00054 max_val(_max_val) {} 00055 00056 void operator()(const CGLA::Vec3i& pi, const float& vox_val) 00057 { 00058 if(vox_val > min_val && vox_val < max_val) 00059 of << pi[0] << " " << pi[1] << " " << pi[2] 00060 << " " << vox_val << std::endl; 00061 } 00062 }; 00063 00064 00065 template<class G> 00066 class VolSaverFloat 00067 { 00068 public: 00069 typedef typename G::DataType DataType; 00070 00071 private: 00072 std::ofstream of; 00073 public: 00074 00075 VolSaverFloat(const std::string& name): 00076 of(name.c_str(), std::ios::binary) {} 00077 00078 void operator()(const CGLA::Vec3i& pi, const float& vox_val) 00079 { 00080 of.write((char*) &vox_val, sizeof(float)); 00081 } 00082 }; 00083 00084 template<class G> 00085 void save_raw_float(const std::string& name, G& grid) 00086 { 00087 VolSaverFloat<G> vs(name); 00088 for_each_voxel_ordered_const(grid, vs); 00089 } 00090 00091 template<class G> 00092 void save_raw_byte(const std::string& name, G& grid, 00093 const typename G::DataType& min_val, 00094 const typename G::DataType& max_val) 00095 { 00096 VolSaver<G> vs(name, min_val, max_val); 00097 for_each_voxel_ordered_const(grid, vs); 00098 } 00099 00100 template<class G> 00101 void save_raw_ascii(const std::string& name, G& grid, 00102 const typename G::DataType& min_val, 00103 const typename G::DataType& max_val) 00104 { 00105 VolSaverAscii<G> vs(name, min_val, max_val); 00106 for_each_voxel_ordered_const(grid, vs); 00107 } 00108 00109 00110 } 00111 00112 #endif