DOLFIN
DOLFIN C++ interface
VTKWriter.h
1 // Copyright (C) 2010 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // First added: 2010-07-19
19 // Last changed:
20 
21 #ifndef __VTK_WRITER_H
22 #define __VTK_WRITER_H
23 
24 #include <cstdint>
25 #include <string>
26 #include <vector>
27 #include "Encoder.h"
28 
29 namespace dolfin
30 {
31 
32  class Function;
33  class Mesh;
34 
36 
37  class VTKWriter
38  {
39  public:
40 
42  static void write_mesh(const Mesh& mesh, std::size_t cell_dim,
43  std::string file,
44  bool binary, bool compress);
45 
47  static void write_cell_data(const Function& u, std::string file,
48  bool binary, bool compress);
49 
51  template<typename T>
52  static std::string encode_stream(const std::vector<T>& data,
53  bool compress);
54  //friend class VTKFile;
55 
56  private:
57 
58  // Write cell data (ascii)
59  static std::string ascii_cell_data(const Mesh& mesh,
60  const std::vector<std::size_t>& offset,
61  const std::vector<double>& values,
62  std::size_t dim, std::size_t rank);
63 
64  // Write cell data (base64)
65  static std::string base64_cell_data(const Mesh& mesh,
66  const std::vector<std::size_t>& offset,
67  const std::vector<double>& values,
68  std::size_t dim, std::size_t rank,
69  bool compress);
70 
71  // Mesh writer (ascii)
72  static void write_ascii_mesh(const Mesh& mesh, std::size_t cell_dim,
73  std::string file);
74 
75  // Mesh writer (base64)
76  static void write_base64_mesh(const Mesh& mesh, std::size_t cell_dim,
77  std::string file, bool compress);
78 
79  // Get VTK cell type
80  static std::uint8_t vtk_cell_type(const Mesh& mesh, std::size_t cell_dim);
81 
82  // Compute base64 encoded stream for VTK
83  template<typename T>
84  static std::string encode_inline_base64(const std::vector<T>& data);
85 
86  // Compute compressed base64 encoded stream for VTK
87  template<typename T>
88  static std::string encode_inline_compressed_base64(const std::vector<T>&
89  data);
90 
91  };
92 
93  //--------------------------------------------------------------------------
94  template<typename T>
95  std::string VTKWriter::encode_stream(const std::vector<T>& data,
96  bool compress)
97  {
98  std::stringstream stream;
99 
100  if (compress)
101  {
102  #ifdef HAS_ZLIB
103  return encode_inline_compressed_base64(data);
104  #else
105  warning("zlib must be configured to enable compressed VTK output. Using uncompressed base64 encoding instead.");
106  return encode_inline_base64(data);
107  #endif
108  }
109  else
110  return encode_inline_base64(data);
111  }
112  //--------------------------------------------------------------------------
113  template<typename T>
114  std::string VTKWriter::encode_inline_base64(const std::vector<T>& data)
115  {
116  std::stringstream stream;
117 
118  const std::uint32_t size = data.size()*sizeof(T);
119  Encoder::encode_base64(&size, 1, stream);
120  Encoder::encode_base64(data, stream);
121 
122  return stream.str();
123  }
124  //--------------------------------------------------------------------------
125  #ifdef HAS_ZLIB
126  template<typename T>
127  std::string VTKWriter::encode_inline_compressed_base64(const std::vector<T>&
128  data)
129  {
130  std::stringstream stream;
131 
132  std::uint32_t header[4];
133  header[0] = 1;
134  header[1] = data.size()*sizeof(T);
135  header[2] = 0;
136 
137  // Compress data
138  std::vector<unsigned char> compressed_data
139  = Encoder::compress_data(data);
140 
141  // Length of compressed data
142  header[3] = compressed_data.size();
143 
144  // Encode header
145  Encoder::encode_base64(&header[0], 4, stream);
146 
147  // Encode data
148  Encoder::encode_base64(compressed_data.data(),
149  compressed_data.size(), stream);
150 
151  return stream.str();
152  }
153  #endif
154  //--------------------------------------------------------------------------
155 
156 }
157 
158 #endif
static void write_cell_data(const Function &u, std::string file, bool binary, bool compress)
Cell data writer.
Definition: VTKWriter.cpp:54
void warning(std::string msg,...)
Print warning.
Definition: log.cpp:115
Definition: adapt.h:29
Write VTK Mesh representation.
Definition: VTKWriter.h:37
static std::string encode_stream(const std::vector< T > &data, bool compress)
Form (compressed) base64 encoded string for VTK.
Definition: VTKWriter.h:95
static void write_mesh(const Mesh &mesh, std::size_t cell_dim, std::string file, bool binary, bool compress)
Mesh writer.
Definition: VTKWriter.cpp:45
Definition: Function.h:65
Definition: Mesh.h:82