Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 == "element")
106  {
107  s >> word;
108  if (word == "vertex")
109  {
110  s >> nVertices;
111  }
112  else if (word == "face")
113  {
114  s >> nEntities;
115  }
116  continue;
117  }
118  else if (word == "property")
119  {
120  s >> word >> word;
121  propMap[word] = nProperties++;
122  }
123  else if (word == "end_header")
124  {
125  // Read nodes
126  vector<double> data(nProperties);
127  for (int i = 0; i < nVertices; ++i)
128  {
129  getline(mshFile, line);
130  stringstream st(line);
131 
132  for (int j = 0; j < nProperties; ++j)
133  {
134  st >> data[j];
135  }
136 
137  double x = data[propMap["x"]];
138  double y = data[propMap["y"]];
139  double z = data[propMap["z"]];
140 
141  if ((y * y) > 0.000001 && m_mesh->m_spaceDim != 3)
142  {
143  m_mesh->m_spaceDim = 2;
144  }
145  if ((z * z) > 0.000001)
146  {
147  m_mesh->m_spaceDim = 3;
148  }
149 
150  x *= scale;
151  y *= scale;
152  z *= scale;
153 
154 
155  m_mesh->m_node.push_back(
156  boost::shared_ptr<Node>(new Node(i, x, y, z)));
157 
158  // Read vertex normals.
159  if (propMap.count("nx") > 0)
160  {
161  double nx = data[propMap["nx"]];
162  double ny = data[propMap["ny"]];
163  double nz = data[propMap["nz"]];
164  m_mesh->m_vertexNormals[i] = Node(0, nx, ny, nz);
165  }
166  }
167 
168  // Read elements
169  for (int i = 0; i < nEntities; ++i)
170  {
171  getline(mshFile, line);
172  stringstream st(line);
173  int id = 0;
174 
175  // Create element tags
176  vector<int> tags;
177  tags.push_back(0); // composite
178 
179  // Read element node list
180  st >> id;
181  vector<NodeSharedPtr> nodeList;
182  for (int k = 0; k < 3; ++k)
183  {
184  int node = 0;
185  st >> node;
186  nodeList.push_back(m_mesh->m_node[node]);
187  }
188 
189  // Create element
190  ElmtConfig conf(elType,1,false,false);
192  CreateInstance(elType,conf,nodeList,tags);
193 
194  // Determine mesh expansion dimension
195  if (E->GetDim() > m_mesh->m_expDim)
196  {
197  m_mesh->m_expDim = E->GetDim();
198  }
199  m_mesh->m_element[E->GetDim()].push_back(E);
200  }
201  }
202  }
203  }
204 
205  }
206 }