OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
MatLabWriter.hpp
1#ifndef MATLABWRITER_HPP
2#define MATLABWRITER_HPP
3
4#include <iostream>
5#include "tiffio.h"
6#include "map_grid.hpp"
7#include <string>
8
15template<unsigned int dim, typename T>
17{
24 template<typename grid, typename Mem> int operator<<(grid data, std::string file)
25 {
26 // Grid can have several properties we can save only scalar fields
27 // for each properties save one scalar fields
28
29 // Tiff files can be saved up to 5D
30
31 if (dim > 5)
32 {
33 std::cerr << "Error Tiff writer support until 5D images" << "\n";
34 }
35
36 // Open the tiff image
37
38 uint32 width;
39 uint32 height;
40 TIFF *tif = TIFFOpen(file.c_str(),"w");
41
42 // if the file is open
43
44 if(tif)
45 {
46 // set width and height for 2D
47
48 width = data.getGrid().size(0);
49 height = data.getGrid().size(1);
50
51 TIFFSetField(tif,TIFFTAG_IMAGEWIDTH, &width);
52 TIFFSetField(tif,TIFFTAG_IMAGELENGTH, &height);
53
54 // Create the tiff line, in case the grid is CPU, we have only
55 // one property and is a scalar, we can directly copy the line
56
57 typename boost::fusion::result_of::at<T::type,0>::type first_element_type;
58
59 if (typeid(grid).name() == "grid_cpu" && T::num_prop == 1 && boost::is_array<first_element_type>::type::value == true)
60 {
61 // Get the grid key iterator
62
63 grid_key_dx_iterator<dim> key = data.getIterator();
64
65 // write all lines
66
67 for(int i = 0; i < height ; i++)
68 {
69 // select the correct lines
70
71 key.set(1,i);
72
73 // we have only one scalar properties, get the buffer pointer
74 void * buf = &data.template get<0>(key);
75
76 TIFFWriteScanline(tif,buf,i, 0);
77 }
78 }
79 else
80 {
81 // we have to create the a scan line for each properties and index array
82 // each property and index array became a channel
83
84 // Get the grid key iterator
85
86 grid_key_dx_iterator<dim> key = data.getIterator();
87
88 // count how many properties and how many indexes we have
89
90 const int n_prp = total_prop<T>;
91
92 // write all lines
93
94 for(int i = 0; i < height ; i++)
95 {
96 // select the correct lines
97
98 key.set(1,i);
99
100 // we have only one scalar properties, get the buffer pointer
101 void * buf = &data.template get<0>(key);
102
103 TIFFWriteScanline(tif,buf,i, 0);
104 }
105 }
106 }
107 }
108
109};
110
111#endif
This class is able to save grid into tiff files.
int operator<<(grid data, std::string file)
Save grid into tiff files.
void set(int d, size_t sz)
Set the dimension.