Nektar++
InputPly.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: InputPly.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: PLY converter.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
36 
37 #include "InputPly.h"
38 
39 using namespace std;
40 using namespace Nektar::NekMeshUtils;
41 
42 namespace Nektar
43 {
44 namespace Utilities
45 {
46 
47 ModuleKey InputPly::className = GetModuleFactory().RegisterCreatorFunction(
48  ModuleKey(eInputModule, "ply"),
49  InputPly::create,
50  "Reads ply triangulation format.");
51 
52 InputPly::InputPly(MeshSharedPtr m) : InputModule(m)
53 {
54 }
55 
57 {
58 }
59 
60 /**
61  *
62  * @param pFilename Filename of Gmsh file to read.
63  */
65 {
66 
67  // Open the file stream.
68  OpenStream();
69 
71 
72  m_mshFile.reset();
73 
75  ProcessEdges();
76  ProcessFaces();
79 }
80 
81 void InputPly::ReadPly(io::filtering_istream &mshFile, NekDouble scale)
82 {
83  m_mesh->m_expDim = 0;
84  string line;
85  int nVertices = 0;
86  int nEntities = 0;
87  int nProperties = 0;
89  map<string, int> propMap;
90 
91  if (m_mesh->m_verbose)
92  {
93  cout << "InputPly: Start reading file..." << endl;
94  }
95 
96  while (!mshFile.eof())
97  {
98  getline(mshFile, line);
99  stringstream s(line);
100  string word;
101  s >> word;
102  if (word == "format")
103  {
104  s >> word;
105  if (word != "ascii")
106  {
107  ASSERTL0(false, "InputPly file currently only set up to read "
108  "ascii formatted ply files");
109  }
110  }
111  else if (word == "element")
112  {
113  s >> word;
114  if (word == "vertex")
115  {
116  s >> nVertices;
117  }
118  else if (word == "face")
119  {
120  s >> nEntities;
121  }
122  continue;
123  }
124  else if (word == "property")
125  {
126  s >> word >> word;
127  propMap[word] = nProperties++;
128  }
129  else if (word == "end_header")
130  {
131  // Read nodes
132  vector<double> data(nProperties);
133  for (int i = 0; i < nVertices; ++i)
134  {
135  getline(mshFile, line);
136  stringstream st(line);
137 
138  for (int j = 0; j < nProperties; ++j)
139  {
140  st >> data[j];
141  }
142 
143  double x = data[propMap["x"]];
144  double y = data[propMap["y"]];
145  double z = data[propMap["z"]];
146 
147  if ((y * y) > 0.000001 && m_mesh->m_spaceDim != 3)
148  {
149  m_mesh->m_spaceDim = 2;
150  }
151  if ((z * z) > 0.000001)
152  {
153  m_mesh->m_spaceDim = 3;
154  }
155 
156  x *= scale;
157  y *= scale;
158  z *= scale;
159 
160  m_mesh->m_node.push_back(
161  std::shared_ptr<Node>(new Node(i, x, y, z)));
162 
163  // Read vertex normals.
164  if (propMap.count("nx") > 0)
165  {
166  double nx = data[propMap["nx"]];
167  double ny = data[propMap["ny"]];
168  double nz = data[propMap["nz"]];
169  m_mesh->m_vertexNormals[i] = Node(0, nx, ny, nz);
170  }
171  }
172 
173  // Read elements
174  for (int i = 0; i < nEntities; ++i)
175  {
176  getline(mshFile, line);
177  stringstream st(line);
178  int id = 0;
179 
180  // Create element tags
181  vector<int> tags;
182  tags.push_back(0); // composite
183 
184  // Read element node list
185  st >> id;
186  vector<NodeSharedPtr> nodeList;
187  for (int k = 0; k < 3; ++k)
188  {
189  int node = 0;
190  st >> node;
191  nodeList.push_back(m_mesh->m_node[node]);
192  }
193 
194  // Create element
195  ElmtConfig conf(elType, 1, false, false);
197  elType, conf, nodeList, tags);
198 
199  // Determine mesh expansion dimension
200  if (E->GetDim() > m_mesh->m_expDim)
201  {
202  m_mesh->m_expDim = E->GetDim();
203  }
204  m_mesh->m_element[E->GetDim()].push_back(E);
205  }
206  }
207  }
208 }
209 }
210 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
Basic information about an element.
Definition: ElementConfig.h:49
io::filtering_istream m_mshFile
Input stream.
STL namespace.
std::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:156
ElementFactory & GetElementFactory()
Definition: Element.cpp:44
NEKMESHUTILS_EXPORT void OpenStream()
Open a file for input.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144
std::pair< ModuleType, std::string > ModuleKey
virtual NEKMESHUTILS_EXPORT void ProcessFaces(bool ReprocessFaces=true)
Extract element faces.
std::shared_ptr< Element > ElementSharedPtr
Definition: Edge.h:49
virtual NEKMESHUTILS_EXPORT void ProcessElements()
Generate element IDs.
double NekDouble
Abstract base class for input modules.
virtual NEKMESHUTILS_EXPORT void ProcessVertices()
Extract element vertices.
virtual NEKMESHUTILS_EXPORT void ProcessEdges(bool ReprocessEdges=true)
Extract element edges.
void ReadPly(io::filtering_istream &mshFile, NekDouble scale=1.0)
Definition: InputPly.cpp:81
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
virtual void Process()
Populate and validate required data structures.
Definition: InputPly.cpp:64
std::pair< ModuleType, std::string > ModuleKey
virtual NEKMESHUTILS_EXPORT void ProcessComposites()
Generate composites.
ModuleFactory & GetModuleFactory()