BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
concrete_vtk_writer.hpp
1 // Copyright (C) 2011-2012 by the BEM++ Authors
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef bempp_concrete_vtk_writer_hpp
22 #define bempp_concrete_vtk_writer_hpp
23 
24 #include "../common/common.hpp"
25 
26 #include "p0_vector_vtk_function.hpp"
27 #include "p1_vector_vtk_function.hpp"
28 #include "vtk_writer.hpp"
29 
30 #include "../common/armadillo_fwd.hpp"
31 #include <memory>
32 #include <string>
33 
34 namespace Bempp
35 {
36 
37 // Forward declarations
38 template<typename DuneGridView> class ConcreteGridView;
39 
42 template <typename DuneGridView>
44 {
45  Dune::VTKWriter<DuneGridView> m_dune_vtk_writer;
46  const DuneGridView* m_dune_gv;
47 
48  friend std::auto_ptr<VtkWriter> ConcreteGridView<DuneGridView>::vtkWriter(
49  Dune::VTK::DataMode dm) const;
50 
60  explicit ConcreteVtkWriter(const DuneGridView &dune_gv,
61  Dune::VTK::DataMode dm=Dune::VTK::conforming) :
62  m_dune_vtk_writer(dune_gv, dm), m_dune_gv(&dune_gv) {
63  }
64 
65 public:
66  virtual void clear() {
67  m_dune_vtk_writer.clear();
68  }
69 
70  virtual std::string write(const std::string &name,
71  OutputType type = ASCII) {
72  return m_dune_vtk_writer.write(name, duneVtkOutputType(type));
73  }
74 
75  virtual std::string pwrite(const std::string& name,
76  const std::string& path,
77  const std::string& extendpath,
78  OutputType type = ASCII) {
79  return m_dune_vtk_writer.pwrite(name, path, extendpath,
80  duneVtkOutputType(type));
81  }
82 
83 private:
84  virtual void addCellDataDoubleImpl(const arma::Mat<double>& data,
85  const std::string &name) {
86  addCellDataImpl(data, name);
87  }
88 
89  virtual void addCellDataFloatImpl(const arma::Mat<float>& data,
90  const std::string &name) {
91  addCellDataImpl(data, name);
92  }
93 
94  template <typename ValueType>
95  void addCellDataImpl(const arma::Mat<ValueType>& data,
96  const std::string &name) {
97  const size_t ncomp = data.n_rows;
98  if (ncomp < 1)
99  return; // empty matrix
100  if ((int)data.n_cols != m_dune_gv->size(0 /* cell codim */))
101  throw std::logic_error("VtkWriter::addCellData(): number of columns "
102  "of 'data' different from the number of cells");
103 
104  typedef P0VectorVTKFunction<DuneGridView, arma::Mat<ValueType> > Function;
105  typedef Dune::shared_ptr<Dune::VTKFunction<DuneGridView> >
106  VTKFunctionPtr;
107  VTKFunctionPtr p(new Function(*m_dune_gv, data, name, ncomp));
108  m_dune_vtk_writer.addCellData(p);
109  }
110 
111  virtual void addVertexDataDoubleImpl(const arma::Mat<double>& data,
112  const std::string &name) {
113  addVertexDataImpl(data, name);
114  }
115 
116  virtual void addVertexDataFloatImpl(const arma::Mat<float>& data,
117  const std::string &name) {
118  addVertexDataImpl(data, name);
119  }
120 
121  template <typename ValueType>
122  void addVertexDataImpl(const arma::Mat<ValueType>& data,
123  const std::string &name) {
124  const size_t ncomp = data.n_rows;
125  if (ncomp < 1)
126  return; // empty matrix
127  if ((int)data.n_cols !=
128  m_dune_gv->size(DuneGridView::dimension /* vertex codim */))
129  throw std::logic_error("VtkWriter::addVertexData(): number of columns "
130  "of 'data' different from the number of vertices");
131 
132  typedef P1VectorVTKFunction<DuneGridView, arma::Mat<ValueType> > Function;
133  typedef Dune::shared_ptr<Dune::VTKFunction<DuneGridView> >
134  VTKFunctionPtr;
135  VTKFunctionPtr p(new Function(*m_dune_gv, data, name, ncomp));
136  m_dune_vtk_writer.addVertexData(p);
137 
138  // m_dune_vtk_writer.addVertexData(data, name, ncomp);
139  }
140 
141  Dune::VTK::OutputType duneVtkOutputType(OutputType type) const
142  {
143  switch (type)
144  {
145  case ASCII:
146  return Dune::VTK::ascii;
147  case BASE_64:
148  return Dune::VTK::base64;
149  case APPENDED_RAW:
150  return Dune::VTK::appendedraw;
151  case APPENDED_BASE_64:
152  return Dune::VTK::appendedbase64;
153  default:
154  return static_cast<Dune::VTK::OutputType>(type);
155  }
156  }
157 };
158 
159 } // namespace Bempp
160 
161 #endif
Abstract exporter of data in the vtk format.
Definition: vtk_writer.hpp:42
OutputType
How data should be stored in a VTK file.
Definition: vtk_writer.hpp:46
Output to the file is in inline base64 binary.
Definition: vtk_writer.hpp:50
Wrapper of a Dune VTK writer for a grid view of type DuneGridView.
Definition: concrete_vtk_writer.hpp:43
virtual void clear()
Clear the list of registered functions.
Definition: concrete_vtk_writer.hpp:66
Output to the file is in appended raw binary.
Definition: vtk_writer.hpp:52
ConcreteVtkWriter(const DuneGridView &dune_gv, Dune::VTK::DataMode dm=Dune::VTK::conforming)
Construct a VtkWriter working on a specific DuneGridView.
Definition: concrete_vtk_writer.hpp:60
virtual std::string pwrite(const std::string &name, const std::string &path, const std::string &extendpath, OutputType type=ASCII)
Write output (interface might change later).
Definition: concrete_vtk_writer.hpp:75
virtual std::auto_ptr< VtkWriter > vtkWriter(Dune::VTK::DataMode dm=Dune::VTK::conforming) const
Create a VtkWriter for this grid view.
Definition: concrete_grid_view.hpp:119
virtual std::string write(const std::string &name, OutputType type=ASCII)
Write output (interface might change later).
Definition: concrete_vtk_writer.hpp:70
Output to the file is in ascii.
Definition: vtk_writer.hpp:48
Output to the file is in appended base64 binary.
Definition: vtk_writer.hpp:54