Nektar++
OutputSTL.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Outputstl.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: STL surface writer.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
36 
37 #include "OutputSTL.h"
38 
39 using namespace std;
40 using namespace Nektar::NekMeshUtils;
41 
42 namespace Nektar
43 {
44 namespace Utilities
45 {
46 
47 ModuleKey OutputSTL::className = GetModuleFactory().RegisterCreatorFunction(
48  ModuleKey(eOutputModule, "stl"), OutputSTL::create, "Writes STL file.");
49 
50 OutputSTL::OutputSTL(MeshSharedPtr m) : OutputModule(m)
51 {
52 }
53 
55 {
56 }
57 
59 {
60  if (m_mesh->m_verbose)
61  {
62  cout << "Outputstl: Writing file..." << endl;
63  }
64 
65  ASSERTL0(m_mesh->m_expDim == 3, "3d meshes only");
66 
67  OpenStream();
68 
69  m_mshFile << std::scientific << setprecision(8);
70 
71  for (auto &it : m_mesh->m_composite)
72  {
73  if (it.second->m_tag != "F")
74  {
75  continue;
76  }
77 
78  m_mshFile << "solid comp:" << it.second->m_id << endl;
79 
80  vector<ElementSharedPtr> el = it.second->m_items;
81 
82  for (int i = 0; i < el.size(); i++)
83  {
84  vector<NodeSharedPtr> ns = el[i]->GetVertexList();
85 
86  Array<OneD, NekDouble> tmp(3, 0.0);
87  tmp[0] = (ns[1]->m_y - ns[0]->m_y) * (ns[2]->m_z - ns[0]->m_z) -
88  (ns[1]->m_z - ns[0]->m_z) * (ns[2]->m_y - ns[0]->m_y);
89  tmp[1] = (ns[1]->m_z - ns[0]->m_z) * (ns[2]->m_x - ns[0]->m_x) -
90  (ns[1]->m_x - ns[0]->m_x) * (ns[2]->m_z - ns[0]->m_z);
91  tmp[2] = (ns[1]->m_x - ns[0]->m_x) * (ns[2]->m_y - ns[0]->m_y) -
92  (ns[1]->m_y - ns[0]->m_y) * (ns[2]->m_x - ns[0]->m_x);
93 
94  NekDouble mt = tmp[0] * tmp[0] + tmp[1] * tmp[1] + tmp[2] * tmp[2];
95  mt = sqrt(mt);
96  tmp[0] /= mt;
97  tmp[1] /= mt;
98  tmp[2] /= mt;
99 
100  m_mshFile << "facet normal " << tmp[0] << " " << tmp[1] << " "
101  << endl;
102  m_mshFile << "outer loop" << endl;
103  for (int j = 0; j < ns.size(); j++)
104  {
105  m_mshFile << "vertex " << ns[j]->m_x << " " << ns[j]->m_y << " "
106  << ns[j]->m_z << endl;
107  }
108  m_mshFile << "endloop" << endl << "endfacet" << endl;
109  }
110  m_mshFile << "endsolid" << endl;
111  }
112 }
113 }
114 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
io::filtering_ostream m_mshFile
Output stream.
NEKMESHUTILS_EXPORT void OpenStream()
Open a file for output.
Abstract base class for output modules.
STL namespace.
std::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:156
std::pair< ModuleType, std::string > ModuleKey
double NekDouble
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
std::pair< ModuleType, std::string > ModuleKey
virtual void Process()
Write mesh to output file.
Definition: OutputSTL.cpp:58
ModuleFactory & GetModuleFactory()