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