Nektar++
utilities/NekMesh/OutputModules/OutputVtk.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: OutputVtk.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: VTK file format output.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
37 
38 #include <vtkXMLUnstructuredGridWriter.h>
39 #include <vtkUnstructuredGridWriter.h>
40 #include <vtkUnstructuredGrid.h>
41 #include <vtkPoints.h>
42 #include <vtkCellType.h>
43 
44 #include "OutputVtk.h"
45 
46 using namespace std;
47 using namespace Nektar::NekMeshUtils;
48 
49 namespace Nektar
50 {
51 namespace Utilities
52 {
53 
54 ModuleKey OutputVtk::className = GetModuleFactory().RegisterCreatorFunction(
55  ModuleKey(eOutputModule, "vtk"), OutputVtk::create, "Writes a VTK file.");
56 
57 OutputVtk::OutputVtk(MeshSharedPtr m) : OutputModule(m)
58 {
59  m_config["uncompress"] = ConfigOption(true, "0", "Uncompress xml sections");
60  m_config["legacy"] = ConfigOption(true, "0", "Output in legacy format");
61 }
62 
64 {
65 }
66 
67 int OutputVtk::GetVtkCellType(std::string pType)
68 {
69  if (pType == "S")
70  {
71  return VTK_LINE;
72  }
73  else if (pType == "T")
74  {
75  return VTK_TRIANGLE;
76  }
77  else if (pType == "Q")
78  {
79  return VTK_QUAD;
80  }
81  else if (pType == "A")
82  {
83  return VTK_TETRA;
84  }
85  else if (pType == "P")
86  {
87  return VTK_PYRAMID;
88  }
89  else if (pType == "R")
90  {
91  return VTK_WEDGE;
92  }
93  else if (pType == "H")
94  {
95  return VTK_HEXAHEDRON;
96  }
97  else
98  {
99  ASSERTL0(false, "Element type not supported.");
100  return 0;
101  }
102 }
103 
105 {
106  if (m_mesh->m_verbose)
107  {
108  cout << "OutputVtk: Writing file..." << endl;
109  }
110 
111  vtkUnstructuredGrid *vtkMesh = vtkUnstructuredGrid::New();
112  vtkPoints *vtkPoints = vtkPoints::New();
113 
114  std::set<NodeSharedPtr> tmp(m_mesh->m_vertexSet.begin(),
115  m_mesh->m_vertexSet.end());
116 
117  for (auto &n : tmp)
118  {
119  vtkPoints->InsertPoint(n->m_id, n->m_x, n->m_y, n->m_z);
120  }
121 
122  vtkIdType p[8];
123  vector<ElementSharedPtr> &elmt = m_mesh->m_element[m_mesh->m_expDim];
124  for (int i = 0; i < elmt.size(); ++i)
125  {
126  int vertexCount = elmt[i]->GetVertexCount();
127  for (int j = 0; j < vertexCount; ++j)
128  {
129  p[j] = elmt[i]->GetVertex(j)->m_id;
130  }
131  // Adjust vertex order to the vtk convention
132  if (elmt[i]->GetTag() == "R")
133  {
134  std::swap(p[2], p[4]);
135  }
136  vtkMesh->InsertNextCell(GetVtkCellType(elmt[i]->GetTag()),
137  vertexCount, &p[0]);
138  }
139 
140  vtkMesh->SetPoints(vtkPoints);
141 
142  // Write out the new mesh in XML or legacy format
143  if (m_config["legacy"].beenSet)
144  {
145  vtkUnstructuredGridWriter *vtkMeshWriter = vtkUnstructuredGridWriter::New();
146  vtkMeshWriter->SetFileName(m_config["outfile"].as<string>().c_str());
147 
148 #if VTK_MAJOR_VERSION <= 5
149  vtkMeshWriter->SetInput(vtkMesh);
150 #else
151  vtkMeshWriter->SetInputData(vtkMesh);
152 #endif
153  vtkMeshWriter->Update();
154  }
155  else // XML format
156  {
157  vtkXMLUnstructuredGridWriter *vtkMeshWriter = vtkXMLUnstructuredGridWriter::New();
158  vtkMeshWriter->SetFileName(m_config["outfile"].as<string>().c_str());
159 
160 #if VTK_MAJOR_VERSION <= 5
161  vtkMeshWriter->SetInput(vtkMesh);
162 #else
163  vtkMeshWriter->SetInputData(vtkMesh);
164 #endif
165  if (m_config["uncompress"].beenSet)
166  {
167  vtkMeshWriter->SetDataModeToAscii();
168  }
169  vtkMeshWriter->Update();
170  }
171 }
172 }
173 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
std::map< std::string, ConfigOption > m_config
List of configuration values.
Abstract base class for output modules.
virtual void Process()
Write mesh to output file.
std::pair< ModuleType, std::string > ModuleKey
ModuleFactory & GetModuleFactory()
std::pair< ModuleType, std::string > ModuleKey
std::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:156
Represents a command-line configuration option.