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