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