Mantid
Loading...
Searching...
No Matches
SaveStl.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10
11#include <Poco/File.h>
12#include <fstream>
13#include <vector>
14namespace Mantid::DataHandling {
15
16namespace {
17
24void writeNumberTriangles(Kernel::BinaryStreamWriter streamWriter, uint32_t numberTrianglesLong) {
25
26 // Write the number of triangles.
27 streamWriter << numberTrianglesLong;
28}
29
36void writeNormal(Kernel::BinaryStreamWriter StreamWriter) {
37 float normal = 0;
38 StreamWriter << normal;
39 StreamWriter << normal;
40 StreamWriter << normal;
41}
42
43} // namespace
44
53 const std::string headerStart = "Binary STL File created using Mantid Environment:";
54 const auto timeString = Types::Core::DateAndTime::getCurrentTime().toFormattedString("%Y-%b-%dT%H:%M:%S") + ":";
55 std::string unitString;
56 switch (m_scaleType) {
58 unitString = "cm:";
59 break;
61 unitString = "mm:";
62 break;
64 unitString = "m:";
65 break;
66 default:
67 // not mandatory to have units in header so just output blank
68 unitString = ":";
69 }
70 const size_t emptySize = 80 - size_t(headerStart.size() + timeString.size() + 4 + unitString.size());
71 streamWriter << headerStart + timeString + unitString + std::string(emptySize, ' ');
72}
73
80 if (m_triangle.size() % 3 != 0) {
81 throw std::runtime_error("Invalid mesh, could not save.");
82 }
83 std::ofstream myFile(m_filename.c_str(), std::ios::out | std::ios::binary);
84 const auto numberOfTriangles = uint32_t(m_triangle.size() / 3);
86 writeHeader(streamWriter);
87 writeNumberTriangles(streamWriter, numberOfTriangles);
88 const uint16_t attributeByte = 0;
89 for (uint32_t i = 0; i < numberOfTriangles; ++i) {
90 writeNormal(streamWriter);
91 writeTriangle(streamWriter, i * 3);
92 // write out attribute, currently always zero
93 streamWriter << attributeByte;
94 }
95 myFile.close();
96}
97
104void SaveStl::writeTriangle(Kernel::BinaryStreamWriter streamWriter, uint32_t triangle) {
105 // for each vertex
106 for (int i = 0; i < 3; ++i) {
107 // write out the coords
108 uint32_t index = m_triangle[triangle + i];
109 Kernel::V3D vertex = m_vertices[index];
110 float xVal = removeScale(vertex.X());
111 float yVal = removeScale(vertex.Y());
112 float zVal = removeScale(vertex.Z());
113 streamWriter << xVal;
114 streamWriter << yVal;
115 streamWriter << zVal;
116 }
117}
118
119} // namespace Mantid::DataHandling
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
float removeScale(double value)
Definition: MeshFileIO.h:50
std::vector< uint32_t > m_triangle
Definition: MeshFileIO.h:64
std::vector< Kernel::V3D > m_vertices
Definition: MeshFileIO.h:65
void writeStl()
Function to write out the full mesh to an stl binary file.
Definition: SaveStl.cpp:79
void writeHeader(Kernel::BinaryStreamWriter streamWriter)
Function to write the header of the STL file, the header has no requirements other than it being 80 b...
Definition: SaveStl.cpp:52
void writeTriangle(Kernel::BinaryStreamWriter streamWriter, uint32_t triangle)
Function to write out an individual triangle with the scale removed.
Definition: SaveStl.cpp:104
const std::string m_filename
Definition: SaveStl.h:39
Assists with writing a binary file by providing standard overloads for the ostream operators (<<) to ...
Class for 3D vectors.
Definition: V3D.h:34
constexpr double X() const noexcept
Get x.
Definition: V3D.h:232
constexpr double Y() const noexcept
Get y.
Definition: V3D.h:233
constexpr double Z() const noexcept
Get z.
Definition: V3D.h:234