vmmlib  1.7.0
 All Classes Namespaces Functions Pages
t4_converter.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* @author David Klaper
34  * @author Susanne Suter
35  *
36  * class to read/write and convert tensor4 files (from raw, to csv...)
37  *
38  */
39 
40 #ifndef __VMML__T4_CONVERTER__HPP__
41 #define __VMML__T4_CONVERTER__HPP__
42 
43 #include "tensor4.hpp"
44 
45 namespace vmml
46 {
47 
48  template< size_t I1, size_t I2, size_t I3, size_t I4, typename T = float >
50  {
51 
52  public:
53 
54 
56 
57  static void export_to( std::vector< T >& data_ ) ;
58  static void import_from( const std::vector< T >& data_ ) ;
59 
60  static void write_to_raw( const t4_t& data_, const std::string& dir_, const std::string& filename_ ); //TODO: DK done
61  static void read_from_raw( t4_t& data_, const std::string& dir_, const std::string& filename_ ) ; //TODO: DK done
62 
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_ ); //TODO: DK done
65 
66 
67  protected:
68 
69  static void concat_path( const std::string& dir_, const std::string& filename_, std::string& path_ );
70 
71  }; //end t4_converter
72 
73 
74 
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 >
77 
78 
79  VMML_TEMPLATE_STRING
80  void
81  VMML_TEMPLATE_CLASSNAME::concat_path( const std::string& dir_, const std::string& filename_, std::string& path_ )
82  {
83  int dir_length = dir_.size() -1;
84  int last_separator = dir_.find_last_of( "/");
85  path_ = dir_;
86  if (last_separator < dir_length ) {
87  path_.append( "/" );
88  }
89  path_.append( filename_ );
90 
91  //check for format
92  if( filename_.find( "raw", filename_.size()-3 ) == std::string::npos)
93  {
94  path_.append( ".");
95  path_.append( "raw" );
96  }
97  }
98 
99 
100  VMML_TEMPLATE_STRING
101  void
102  VMML_TEMPLATE_CLASSNAME::write_to_csv( const t4_t& data_, const std::string& dir_, const std::string& filename_ )
103  {
104  std::string path_raw;
105  concat_path(dir_, filename_, path_raw);
106  path_raw.replace(path_raw.size() - 3, 3, "csv");
107 
108  std::ofstream outfile(path_raw.c_str());
109  if(outfile.is_open())
110  {
111  for( size_t i4 = 0; i4 < data_.T3S; ++i4 )
112  {
113  for( size_t i3 = 0; i3 < data_.SLICES; ++i3 )
114  {
115  for( size_t i1 = 0; i1 < data_.ROWS; ++i1 )
116  {
117  for( size_t i2 = 0; i2 < data_.COLS; ++i2 )
118  {
119  outfile << data_(i1,i2,i3,i4) << ", ";
120  }
121  outfile.seekp( -2, std::ios_base::cur); // remove superfluous comma signs
122  outfile << std::endl;
123  }
124  outfile << std::endl;
125  }
126  outfile << std::endl;
127  }
128 
129  outfile.close();
130  }else {
131  outfile.close();
132  std::cout << "no file open" << std::endl;
133  }
134 
135  }
136 
137 
138  VMML_TEMPLATE_STRING
139  void
140  VMML_TEMPLATE_CLASSNAME::read_from_raw( t4_t& data_, const std::string& dir_, const std::string& filename_ )
141  {
142  data_.zero();
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() ) {
148 
149  size_t max_file_len = (std::numeric_limits<std::streamsize>::max)() - sizeof(T);
150  size_t len_data = data_.size();
151  size_t len_read = 0;
152  T* dataptr = data_.get_array_ptr();
153  char* chardat = new char[ len_data*sizeof(T)];
154  size_t offset = 0;
155 
156  while(len_data > 0)
157  {
158 
159  if (len_data*sizeof(T) > max_file_len)
160  {
161  size_t mod = max_file_len % sizeof(T);
162  len_read = (max_file_len-mod)/sizeof(T);
163  }else
164  {
165  len_read = len_data;
166  }
167 
168  infile.read( chardat, len_read*sizeof(T) );
169 
170  for(size_t index = 0; index+offset < data_.size() && index < len_read; ++index)
171  {
172  T* data = (T*)&(chardat[index*sizeof(T)]);
173  dataptr[index+offset] = *data;
174  }
175  offset += len_read;
176  len_data -= len_read;
177 
178  }
179  delete[] chardat;
180  infile.close();
181  } else {
182  infile.close();
183  std::cout << "no file open" << std::endl;
184  }
185 
186  }
187 
188 
189 
190 
191  VMML_TEMPLATE_STRING
192  void
193  VMML_TEMPLATE_CLASSNAME::write_to_raw( const t4_t& data_, const std::string& dir_, const std::string& filename_ )
194  {
195  std::string path_raw;
196  concat_path(dir_, filename_, path_raw);
197 
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; // How many data values are written at once
203  const T* dataptr = data_.get_array_ptr();
204  for( size_t index = 0; index < data_.size(); index += chunk_size )
205  {
206  if(chunk_size + index > data_.size()) // only write remaining values
207  {
208  outfile.write( (char*)dataptr+index*type_size, type_size*(data_.size()-index) );
209  }else // write whole chunk
210  {
211  outfile.write( (char*)dataptr+index*type_size, chunk_size*type_size );
212  }
213  }
214  outfile.close();
215  } else {
216  outfile.close();
217  std::cout << "no file open" << std::endl;
218  }
219  }
220 
221 
222 
223 
224 #undef VMML_TEMPLATE_STRING
225 #undef VMML_TEMPLATE_CLASSNAME
226 
227 
228 }//end vmml namespace
229 
230 #endif