40 #ifndef __VMML__T4_CONVERTER__HPP__
41 #define __VMML__T4_CONVERTER__HPP__
43 #include "tensor4.hpp"
48 template<
size_t I1,
size_t I2,
size_t I3,
size_t I4,
typename T =
float >
57 static void export_to( std::vector< T >& data_ ) ;
58 static void import_from(
const std::vector< T >& data_ ) ;
60 static void write_to_raw(
const t4_t& data_,
const std::string& dir_,
const std::string& filename_ );
61 static void read_from_raw(
t4_t& data_,
const std::string& dir_,
const std::string& filename_ ) ;
63 static void write_datfile(
const std::string& dir_,
const std::string& filename_ );
64 static void write_to_csv(
const t4_t& data_,
const std::string& dir_,
const std::string& filename_ );
69 static void concat_path(
const std::string& dir_,
const std::string& filename_, std::string& path_ );
75 #define VMML_TEMPLATE_STRING template< size_t I1, size_t I2, size_t I3, size_t I4, typename T >
76 #define VMML_TEMPLATE_CLASSNAME t4_converter< I1, I2, I3, I4, T >
81 VMML_TEMPLATE_CLASSNAME::concat_path(
const std::string& dir_,
const std::string& filename_, std::string& path_ )
83 int dir_length = dir_.size() -1;
84 int last_separator = dir_.find_last_of(
"/");
86 if (last_separator < dir_length ) {
89 path_.append( filename_ );
92 if( filename_.find(
"raw", filename_.size()-3 ) == std::string::npos)
95 path_.append(
"raw" );
102 VMML_TEMPLATE_CLASSNAME::write_to_csv(
const t4_t& data_,
const std::string& dir_,
const std::string& filename_ )
104 std::string path_raw;
105 concat_path(dir_, filename_, path_raw);
106 path_raw.replace(path_raw.size() - 3, 3,
"csv");
108 std::ofstream outfile(path_raw.c_str());
109 if(outfile.is_open())
111 for(
size_t i4 = 0; i4 < data_.T3S; ++i4 )
113 for(
size_t i3 = 0; i3 < data_.SLICES; ++i3 )
115 for(
size_t i1 = 0; i1 < data_.ROWS; ++i1 )
117 for(
size_t i2 = 0; i2 < data_.COLS; ++i2 )
119 outfile << data_(i1,i2,i3,i4) <<
", ";
121 outfile.seekp( -2, std::ios_base::cur);
122 outfile << std::endl;
124 outfile << std::endl;
126 outfile << std::endl;
132 std::cout <<
"no file open" << std::endl;
140 VMML_TEMPLATE_CLASSNAME::read_from_raw( t4_t& data_,
const std::string& dir_,
const std::string& filename_ )
143 std::string path_raw;
144 concat_path(dir_, filename_, path_raw);
145 std::ifstream infile;
146 infile.open( path_raw.c_str(), std::ios::in );
147 if( infile.is_open() ) {
149 size_t max_file_len = (std::numeric_limits<std::streamsize>::max)() -
sizeof(T);
150 size_t len_data = data_.size();
152 T* dataptr = data_.get_array_ptr();
153 char* chardat =
new char[ len_data*
sizeof(T)];
159 if (len_data*
sizeof(T) > max_file_len)
161 size_t mod = max_file_len %
sizeof(T);
162 len_read = (max_file_len-mod)/
sizeof(T);
168 infile.read( chardat, len_read*
sizeof(T) );
170 for(
size_t index = 0; index+offset < data_.size() && index < len_read; ++index)
172 T* data = (T*)&(chardat[index*
sizeof(T)]);
173 dataptr[index+offset] = *data;
176 len_data -= len_read;
183 std::cout <<
"no file open" << std::endl;
193 VMML_TEMPLATE_CLASSNAME::write_to_raw(
const t4_t& data_,
const std::string& dir_,
const std::string& filename_ )
195 std::string path_raw;
196 concat_path(dir_, filename_, path_raw);
198 std::ofstream outfile;
199 outfile.open( path_raw.c_str() );
200 if( outfile.is_open() ) {
201 size_t type_size =
sizeof(T);
202 size_t chunk_size = 5;
203 const T* dataptr = data_.get_array_ptr();
204 for(
size_t index = 0; index < data_.size(); index += chunk_size )
206 if(chunk_size + index > data_.size())
208 outfile.write( (
char*)dataptr+index*type_size, type_size*(data_.size()-index) );
211 outfile.write( (
char*)dataptr+index*type_size, chunk_size*type_size );
217 std::cout <<
"no file open" << std::endl;
224 #undef VMML_TEMPLATE_STRING
225 #undef VMML_TEMPLATE_CLASSNAME