dune-grid-glue  2.9
vtksurfacewriter.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-dune-grid-glue-exception
5 /*
6  * Filename: VtkSurfaceWriter.hh
7  * Version: 1.0
8  * Created on: Jan 16, 2009
9  * Author: Gerrit Buse
10  * ---------------------------------
11  * Project: dune-grid-glue
12  * Description: helper class for graphical output of grids in generic representation
13  *
14  */
20 #ifndef DUNE_GRIDGLUE_EXTRACTORS_VTKSURFACEWRITER_HH
21 #define DUNE_GRIDGLUE_EXTRACTORS_VTKSURFACEWRITER_HH
22 
23 #include <fstream>
24 #include <iomanip>
25 #include <vector>
26 #include <cstring>
27 
28 #include "../adapter/gridgluevtkwriter.hh"
29 
30 namespace Dune {
31 
32  namespace GridGlue {
33 
35 {
36 public:
37 
38 
39  VtkSurfaceWriter(const char* filename) : filename_(filename)
40  {}
41 
43  {}
44 
45  void setFilename(const char* name)
46  {
47  if (std::strlen(name) > 0)
48  this->filename_ = name;
49  }
50 
51 
52  template<typename K>
53  void writeSurface(const std::vector<K>& coords, const std::vector<unsigned int>& indices, int corners, int dim)
54  {
55  std::ofstream fos;
56  char buffer[64];
57  sprintf(buffer, "%s.vtk", this->filename_);
58  fos.open(buffer);
59  fos << std::setprecision(8) << std::setw(1);
60  // write preamble
61  fos << "# vtk DataFile Version 2.0\nFilename: " << buffer << "\nASCII" << std::endl;
62  this->writePoints(coords, dim, fos);
63  const int polycount = indices.size()/corners;
64  int corner_count[polycount];
65  for (int i = 0; i < polycount; ++i)
66  corner_count[i] = corners;
67  this->writePolygons(indices, corner_count, polycount, dim, fos);
68  fos.close();
69  }
70 
71 
72  template<typename K, typename T>
73  void writeSurfaceElementData(const std::vector<K>& coords, const std::vector<unsigned int>& indices, int corners, const std::vector<T>& data, const char* dataname, int dim)
74  {
75  std::ofstream fos;
76  char buffer[64];
77  sprintf(buffer, "%s.vtk", this->filename_);
78  fos.open(buffer);
79  fos << std::setprecision(8) << std::setw(1);
80  // write preamble
81  fos << "# vtk DataFile Version 2.0\nFilename: " << buffer << "\nASCII" << std::endl;
82  this->writePoints(coords, dim, fos);
83  const int polycount = indices.size()/corners;
84  int corner_count[polycount];
85  for (int i = 0; i < polycount; ++i)
86  corner_count[i] = corners;
87  this->writePolygons(indices, corner_count, polycount, dim, fos);
88  this->writeCellData(data, dataname, dim, fos);
89  fos.close();
90  }
91 
92 
93  template<typename K, typename T>
94  void writeSurfaceVertexData(const std::vector<K>& coords, const std::vector<unsigned int>& indices, int corners, const std::vector<T>& data, const char* dataname, int dim)
95  {
96  std::ofstream fos;
97  char buffer[64];
98  sprintf(buffer, "%s.vtk", this->filename_);
99  fos.open(buffer);
100  fos << std::setprecision(8) << std::setw(1);
101  // write preamble
102  fos << "# vtk DataFile Version 2.0\nFilename: " << buffer << "\nASCII" << std::endl;
103  this->writePoints(coords, dim, fos);
104  const int polycount = indices.size()/corners;
105  int corner_count[polycount];
106  for (int i = 0; i < polycount; ++i)
107  corner_count[i] = corners;
108  this->writePolygons(indices, corner_count, polycount, dim, fos);
109  this->writePointData(data, dataname, dim, fos);
110  fos.close();
111  }
112 
113 protected:
114 
115  template<typename K>
116  void writePoints(const std::vector<K>& coords, int dim, std::ofstream& fos)
117  {
118  fos << "DATASET POLYDATA\nPOINTS " << coords.size() << " " << TypeNames[Nametraits<K>::nameidx] << std::endl;
119  for (unsigned int i = 0; i < coords.size(); ++i)
120  {
121  fos << coords[i][0];
122  if (dim == 2)
123  fos << " " << coords[i][1] << " 0 \n" << coords[i][0] << " " << coords[i][1] << " 0.01" << std::endl;
124  else // dim == 3
125  fos << " " << coords[i][1] << " " << coords[i][2] << std::endl;
126  }
127  }
128 
129  void writePolygons(const std::vector<unsigned int>& indices, const int* corners, int ncorners, int dim, std::ofstream& fos)
130  {
131  if (dim == 2)
132  {
133  fos << "POLYGONS " << indices.size()/2 << " " << 5*(indices.size() / 2) << std::endl;
134  for (unsigned int i = 0; i < indices.size(); i += 2)
135  fos << "4 " << 2*indices[i] << " " << 2*indices[i+1] << " " << 2*indices[i+1]+1 << " "<< 2*indices[i]+1 << std::endl;
136 
137  // arbitrary shapes - ignored here!
138  // int sum = ncorners;
139  // for (int i = 0; i < ncorners; ++i)
140  // sum += (corners[i] > 2 ? corners[i] : 3);
141  //
142  // fos << "POLYGONS " << ncorners << " " << sum << std::endl;
143  // int index = 0;
144  // for (int i = 0; i < ncorners; ++i)
145  // {
146  // // write the first index twice if it is an egde
147  // // => triangle instead of edge - paraview can display it then
148  // if (corners[i] > 2)
149  // fos << corners[i];
150  // else
151  // fos << "3 " << indices[index];
152  //
153  // for (int j = 0; j < corners[i]; ++j)
154  // fos << " " << indices[index++];
155  // fos << std::endl;
156  // }
157  }
158  else
159  {
160  int sum = ncorners;
161  for (int i = 0; i < ncorners; ++i)
162  sum += corners[i];
163  fos << "POLYGONS " << ncorners << " " << sum << std::endl;
164  int index = 0;
165  for (int i = 0; i < ncorners; ++i)
166  {
167  fos << corners[i];
168  for (int j = 0; j < corners[i]; ++j)
169  fos << " " << indices[index++];
170  fos << std::endl;
171  }
172  }
173  }
174 
175  template<typename T>
176  void writeCellData(const std::vector<T>& data, const char* dataname, int dim, std::ofstream& fos)
177  {
178  fos << "CELL_DATA " << data.size()*(dim == 2 ? 2 : 1) << std::endl;
179  fos << "SCALARS " << dataname << " " << TypeNames[Nametraits<T>::nameidx] << " 1" << std::endl;
180  fos << "LOOKUP_TABLE default" << std::endl;
181  for (unsigned int i = 0; i < data.size(); ++i)
182  {
183  fos << data[i] << std::endl;
184  if (dim == 2)
185  fos << data[i] << std::endl;
186  }
187  }
188 
189  template<typename T>
190  void writePointData(const std::vector<T>& data, const char* dataname, int dim, std::ofstream& fos)
191  {
192  fos << "POINT_DATA " << data.size()*(dim == 2 ? 2 : 1) << std::endl;
193  fos << "SCALARS " << dataname << " " << TypeNames[Nametraits<T>::nameidx] << " 1" << std::endl;
194  fos << "LOOKUP_TABLE default" << std::endl;
195  for (unsigned int i = 0; i < data.size(); ++i)
196  {
197  fos << data[i] << std::endl;
198  if (dim == 2)
199  fos << data[i] << std::endl;
200  }
201  }
202 
203 
204 private:
205  const char* filename_;
206 };
207 
208 } // namespace GridGlue
209 
210 } // namespace Dune
211 
212 #endif // DUNE_GRIDGLUE_EXTRACTORS_VTKSURFACEWRITER_HH
Definition: gridglue.hh:37
Definition: vtksurfacewriter.hh:35
void writeCellData(const std::vector< T > &data, const char *dataname, int dim, std::ofstream &fos)
Definition: vtksurfacewriter.hh:176
void writePointData(const std::vector< T > &data, const char *dataname, int dim, std::ofstream &fos)
Definition: vtksurfacewriter.hh:190
void writeSurfaceVertexData(const std::vector< K > &coords, const std::vector< unsigned int > &indices, int corners, const std::vector< T > &data, const char *dataname, int dim)
Definition: vtksurfacewriter.hh:94
void setFilename(const char *name)
Definition: vtksurfacewriter.hh:45
void writeSurfaceElementData(const std::vector< K > &coords, const std::vector< unsigned int > &indices, int corners, const std::vector< T > &data, const char *dataname, int dim)
Definition: vtksurfacewriter.hh:73
~VtkSurfaceWriter()
Definition: vtksurfacewriter.hh:42
VtkSurfaceWriter(const char *filename)
Definition: vtksurfacewriter.hh:39
void writeSurface(const std::vector< K > &coords, const std::vector< unsigned int > &indices, int corners, int dim)
Definition: vtksurfacewriter.hh:53
void writePoints(const std::vector< K > &coords, int dim, std::ofstream &fos)
Definition: vtksurfacewriter.hh:116
void writePolygons(const std::vector< unsigned int > &indices, const int *corners, int ncorners, int dim, std::ofstream &fos)
Definition: vtksurfacewriter.hh:129