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