Programming tasks to Scientific Computing I
legacyVtkWriter.h
Go to the documentation of this file.
1 #ifndef __MESHWITHDATA_H
2 #define __MESHWITHDATA_H
3 
4 namespace shellFE {
5 
10 
13 template <class MeshType>
15 public:
16  typedef typename MeshType::RealType RealType;
19  typedef typename MeshType::VectorType VectorType;
20 
21  LegacyVtkWriter ( const MeshType & mesh ) : _mesh ( mesh ), _precision( 8 ) {}
22  LegacyVtkWriter ( const MeshType & mesh, int precision ) : _mesh ( mesh ), _precision( precision ) {}
23 
24  LegacyVtkWriter & addScalarData ( const VectorType & data, string dataDescr, DataSupp supp ) {
25 
26  ScalarData entry = { dataDescr, &data };
27 
28  switch ( supp ) {
29  case VERTEX_DATA:
30  if ( data.size() != static_cast<unsigned>( _mesh.getNumVertices () ) ) throw std::invalid_argument( aol::strprintf ( "Wrong size. In File %s at line %d.", __FILE__, __LINE__ ).c_str() );
31  _scalarVertexData.push_back ( entry );
32  break;
33 
34  case FACE_DATA:
35  if ( data.size() != static_cast<unsigned>( _mesh.getNumTriangs () ) ) throw std::invalid_argument( aol::strprintf ( "Wrong size. In File %s at line %d.", __FILE__, __LINE__ ).c_str() );
36  _scalarFaceData.push_back ( entry );
37  break;
38 
39  default:
40  throw std::invalid_argument( aol::strprintf ( "Unknown DataSupp. In File %s at line %d.", __FILE__, __LINE__ ).c_str() );
41  }
42  return *this;
43  }
44 
45  LegacyVtkWriter & addVectorData ( const VectorType & data, const int numComponents, string dataDescr, DataSupp supp, VectorSpec vSpec = VECTORS ) {
46 
47  VectorData entry = { dataDescr, vSpec, &data, numComponents };
48 
49  switch ( supp ) {
50  case VERTEX_DATA:
51  _vectorVertexData.push_back ( entry );
52  break;
53  case FACE_DATA:
54  _vectorFaceData.push_back ( entry );
55  break;
56  default:
57  throw std::invalid_argument( aol::strprintf ( "Unknown DataSupp. In File %s at line %d.", __FILE__, __LINE__ ).c_str() );
58  }
59  return *this;
60  }
61 
63  void setPrecisionTo( int prec ){ _precision = prec; }
64 
65  void save ( string filename ) const {
66  const int numVertices = _mesh.getNumVertices();
67 
68  std::ofstream out ( filename.c_str() ); // ctor checks if file could be opened
69 
70  out << "# vtk DataFile Version 2.0" << endl
71  << "written by method MeshWithData::saveAsLegacyVTK" << endl // paraview supports time = x.x here. useful?
72  << "ASCII" << endl
73  << "DATASET POLYDATA" << endl
74  << "POINTS " << numVertices << " float" << endl;
75 
76  // vertex coordinates
77  for ( int nodeIter = 0; nodeIter < _mesh.getNumVertices(); ++nodeIter ) {
78  const Point3DType& coords ( _mesh.getVertex(nodeIter) );
79  for ( short i = 0; i < 3; ++i )
80  out << ( i == 0 ? "" : " " ) << coords[i];
81  out << endl;
82  }
83 
84  out << "POLYGONS " << _mesh.getNumTriangs() << " " << 4 * _mesh.getNumTriangs() << endl;
85  // triangles' vertex indices
86  for ( int elementIter = 0; elementIter < _mesh.getNumTriangs(); ++elementIter ) {
87  out << "3 ";
88  for ( short i = 0; i < 3; ++i )
89  out << ( i == 0 ? "" : " " ) << _mesh.getTriangNodeIdx( elementIter, i );
90  out << endl;
91  }
92 
93  if ( _scalarVertexData.size() > 0 || _vectorVertexData.size() > 0 )
94  out << "POINT_DATA " << numVertices << endl;
95  // scalar data on vertices
96  for (size_t i = 0; i < _scalarVertexData.size(); ++i) {
97  out << "SCALARS " << _scalarVertexData[i]._descr << " float" << endl;
98  out << "LOOKUP_TABLE default" << endl;
99  for ( unsigned vx = 0; vx < (*_scalarVertexData[i]._data).size(); ++vx )
100  out << (*_scalarVertexData[i]._data)[vx] << endl;
101  }
102 
103  if ( _scalarFaceData.size() > 0 )
104  out << "CELL_DATA " << _mesh.getNumTriangs() << endl;
105  // scalar data on vertices
106  for (size_t i = 0; i < _scalarFaceData.size(); ++i) {
107  out << "SCALARS " << _scalarFaceData[i]._descr << " float 1" << endl;
108  out << "LOOKUP_TABLE default" << endl;
109  for ( unsigned vx = 0; vx < (*_scalarFaceData[i]._data).size(); ++vx )
110  out << (*_scalarFaceData[i]._data)[vx] << endl;
111  }
112 
113  // vector data on vertices
114  for (size_t i = 0; i < _vectorVertexData.size(); ++i) {
115  string spec;
116  if ( _vectorVertexData[i]._spec == VECTORS ) spec = "VECTORS";
117  if ( _vectorVertexData[i]._spec == NORMALS ) spec = "NORMALS";
118  out << spec << " " << _vectorVertexData[i]._descr << " float" << endl;
119  for ( int vx = 0; vx < numVertices; ++vx ) {
120  for ( int comp = 0; comp < _vectorVertexData[i]._numComponents; ++comp )
121  out << ( comp == 0 ? "" : " " ) << (*_vectorVertexData[i]._data)(vx + comp * numVertices); //TODO index mapper
122  out << endl;
123  }
124  }
125  }
126 
127 protected:
128 
129  struct ScalarData {
130  string _descr;
131  const VectorType * _data;
132  };
133 
134  struct VectorData {
135  string _descr;
137  const VectorType * _data;
138  const int _numComponents;
139  };
140 
141  std::vector<ScalarData> _scalarVertexData;
142  std::vector<VectorData> _vectorVertexData;
143  std::vector<ScalarData> _scalarFaceData;
144  std::vector<VectorData> _vectorFaceData;
145 
148 };
149 
150 } // end of namespace
151 
152 #endif
MeshType::RealType RealType
LegacyVtkWriter & addVectorData(const VectorType &data, const int numComponents, string dataDescr, DataSupp supp, VectorSpec vSpec=VECTORS)
const Point3DType & getVertex(const int num) const
Definition: triangMesh.h:66
MeshType::Indices3DType Indices3DType
void setPrecisionTo(int prec)
set precision in saving methods
int getNumVertices() const
Definition: triangMesh.h:42
DataTypeContainer::RealType RealType
int getNumTriangs() const
Definition: triangMesh.h:46
VectorSpec
vector-valued data can be saved (in VTK legacy format) as 3-vectors, normals or texture coordinates (...
DataTypeContainer::VectorType VectorType
std::vector< VectorData > _vectorVertexData
std::vector< ScalarData > _scalarFaceData
std::vector< VectorData > _vectorFaceData
int getTriangNodeIdx(const int num, const int localNode) const
Definition: triangMesh.h:104
LegacyVtkWriter(const MeshType &mesh, int precision)
MeshType::VectorType VectorType
string strprintf(const char *format,...)
Give back formatted string, analogously to sprintf, but save the long way &#39;round with char arrays...
Definition: aol.cpp:5
DataSupp
data can either belong to vertices or to faces
void save(string filename) const
LegacyVtkWriter & addScalarData(const VectorType &data, string dataDescr, DataSupp supp)
DataTypeContainer::Point3DType Point3DType
DataTypeContainer::Indices3DType Indices3DType
LegacyVtkWriter(const MeshType &mesh)
std::vector< ScalarData > _scalarVertexData
MeshType::Point3DType Point3DType