Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InputVtk.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: InputVtk.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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: VTK converter.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
37 
38 #include <vtkPolyDataReader.h>
39 #include <vtkPolyData.h>
40 #include <vtkPoints.h>
41 #include <vtkCellArray.h>
42 
43 #include "InputVtk.h"
44 
45 using namespace std;
46 using namespace Nektar::NekMeshUtils;
47 
48 namespace Nektar
49 {
50 namespace Utilities
51 {
52 
53 ModuleKey InputVtk::className = GetModuleFactory().RegisterCreatorFunction(
54  ModuleKey(eInputModule, "vtk"), InputVtk::create, "Reads VTK format.");
55 
56 InputVtk::InputVtk(MeshSharedPtr m) : InputModule(m)
57 {
58 }
59 
61 {
62 }
63 
64 /**
65  * Gmsh file contains a list of nodes and their coordinates, along with
66  * a list of elements and those nodes which define them. We read in and
67  * store the list of nodes in #m_node and store the list of elements in
68  * #m_element. Each new element is supplied with a list of entries from
69  * #m_node which defines the element. Finally some mesh statistics are
70  * printed.
71  *
72  * @param pFilename Filename of Gmsh file to read.
73  */
75 {
76  if (m_mesh->m_verbose)
77  {
78  cout << "InputVtk: Start reading file..." << endl;
79  }
80 
81  vtkPolyDataReader *vtkMeshReader = vtkPolyDataReader::New();
82  vtkMeshReader->SetFileName(m_config["infile"].as<string>().c_str());
83  vtkMeshReader->Update();
84  vtkPolyData *vtkMesh = vtkMeshReader->GetOutput();
85 
86  vtkPoints *vtkPoints = vtkMesh->GetPoints();
87 
88  const int numCellTypes = 3;
89  vtkCellArray *vtkCells[numCellTypes];
90  LibUtilities::ShapeType vtkCellTypes[numCellTypes];
91  int vtkNumPoints[numCellTypes];
92  vtkCells[0] = vtkMesh->GetPolys();
93  vtkCells[1] = vtkMesh->GetStrips();
94  vtkCells[2] = vtkMesh->GetLines();
95  vtkCellTypes[0] = LibUtilities::eTriangle;
96  vtkCellTypes[1] = LibUtilities::eTriangle;
97  vtkCellTypes[2] = LibUtilities::eSegment;
98  vtkNumPoints[0] = 3;
99  vtkNumPoints[1] = 3;
100  vtkNumPoints[2] = 2;
101 
102  vtkIdType npts;
103  vtkIdType *pts = 0;
104  double p[3];
105 
106  for (int i = 0; i < vtkPoints->GetNumberOfPoints(); ++i)
107  {
108  vtkPoints->GetPoint(i, p);
109 
110  if ((p[0] * p[0]) > 0.000001 && m_mesh->m_spaceDim < 1)
111  {
112  m_mesh->m_spaceDim = 1;
113  }
114  if ((p[1] * p[1]) > 0.000001 && m_mesh->m_spaceDim < 2)
115  {
116  m_mesh->m_spaceDim = 2;
117  }
118  if ((p[2] * p[2]) > 0.000001 && m_mesh->m_spaceDim < 3)
119  {
120  m_mesh->m_spaceDim = 3;
121  }
122 
123  m_mesh->m_node.push_back(
124  boost::shared_ptr<Node>(new Node(i, p[0], p[1], p[2])));
125  }
126 
127  for (int c = 0; c < numCellTypes; ++c)
128  {
129  vtkCells[c]->InitTraversal();
130  for (int i = 0; vtkCells[c]->GetNextCell(npts, pts); ++i)
131  {
132  for (int j = 0; j < npts - vtkNumPoints[c] + 1; ++j)
133  {
134  // Create element tags
135  vector<int> tags;
136  tags.push_back(0); // composite
137  tags.push_back(vtkCellTypes[c]); // element type
138 
139  // Read element node list
140  vector<NodeSharedPtr> nodeList;
141  for (int k = j; k < j + vtkNumPoints[c]; ++k)
142  {
143  nodeList.push_back(m_mesh->m_node[pts[k]]);
144  }
145 
146  // Create element
147  ElmtConfig conf(vtkCellTypes[c], 1, false, false);
149  vtkCellTypes[c], conf, nodeList, tags);
150 
151  // Determine mesh expansion dimension
152  if (E->GetDim() > m_mesh->m_expDim)
153  {
154  m_mesh->m_expDim = E->GetDim();
155  }
156  m_mesh->m_element[E->GetDim()].push_back(E);
157  }
158  }
159  }
160 
161  ProcessVertices();
162  ProcessEdges();
163  ProcessFaces();
164  ProcessElements();
166 }
167 }
168 }
Basic information about an element.
Definition: Element.h:58
pair< ModuleType, string > ModuleKey
tBaseSharedPtr CreateInstance(tKey idKey BOOST_PP_COMMA_IF(MAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x))
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:162
virtual void Process()
Populate and validate required data structures.
Definition: InputVtk.cpp:74
map< string, ConfigOption > m_config
List of configuration values.
STL namespace.
MeshSharedPtr m_mesh
Mesh object.
ElementFactory & GetElementFactory()
Definition: Element.cpp:47
Represents a point in the domain.
Definition: Node.h:60
virtual void ProcessEdges(bool ReprocessEdges=true)
Extract element edges.
virtual void ProcessVertices()
Extract element vertices.
virtual void ProcessElements()
Generate element IDs.
static std::string npts
Definition: InputFld.cpp:43
virtual void ProcessComposites()
Generate composites.
boost::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:137
virtual void ProcessFaces(bool ReprocessFaces=true)
Extract element faces.
boost::shared_ptr< Element > ElementSharedPtr
Definition: Edge.h:52
ModuleFactory & GetModuleFactory()
Abstract base class for input modules.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215