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 <fstream>
12#include <vector>
13namespace Mantid::DataHandling {
14
15namespace {
16
23void writeNumberTriangles(Kernel::BinaryStreamWriter streamWriter, uint32_t numberTrianglesLong) {
24
25 // Write the number of triangles.
26 streamWriter << numberTrianglesLong;
27}
28
35void writeNormal(Kernel::BinaryStreamWriter StreamWriter) {
36 float normal = 0;
37 StreamWriter << normal;
38 StreamWriter << normal;
39 StreamWriter << normal;
40}
41
42} // namespace
43
52 const std::string headerStart = "Binary STL File created using Mantid Environment:";
53 const auto timeString = Types::Core::DateAndTime::getCurrentTime().toFormattedString("%Y-%b-%dT%H:%M:%S") + ":";
54 std::string unitString;
55 switch (m_scaleType) {
57 unitString = "cm:";
58 break;
60 unitString = "mm:";
61 break;
63 unitString = "m:";
64 break;
65 default:
66 // not mandatory to have units in header so just output blank
67 unitString = ":";
68 }
69 const size_t emptySize = 80 - size_t(headerStart.size() + timeString.size() + 4 + unitString.size());
70 streamWriter << headerStart + timeString + unitString + std::string(emptySize, ' ');
71}
72
79 if (m_triangle.size() % 3 != 0) {
80 throw std::runtime_error("Invalid mesh, could not save.");
81 }
82 std::ofstream myFile(m_filename.c_str(), std::ios::out | std::ios::binary);
83 const auto numberOfTriangles = uint32_t(m_triangle.size() / 3);
85 writeHeader(streamWriter);
86 writeNumberTriangles(streamWriter, numberOfTriangles);
87 const uint16_t attributeByte = 0;
88 for (uint32_t i = 0; i < numberOfTriangles; ++i) {
89 writeNormal(streamWriter);
90 writeTriangle(streamWriter, i * 3);
91 // write out attribute, currently always zero
92 streamWriter << attributeByte;
93 }
94 myFile.close();
95}
96
103void SaveStl::writeTriangle(Kernel::BinaryStreamWriter streamWriter, uint32_t triangle) {
104 // for each vertex
105 for (int i = 0; i < 3; ++i) {
106 // write out the coords
107 uint32_t index = m_triangle[triangle + i];
108 Kernel::V3D vertex = m_vertices[index];
109 float xVal = removeScale(vertex.X());
110 float yVal = removeScale(vertex.Y());
111 float zVal = removeScale(vertex.Z());
112 streamWriter << xVal;
113 streamWriter << yVal;
114 streamWriter << zVal;
115 }
116}
117
118} // namespace Mantid::DataHandling
std::map< DeltaEMode::Type, std::string > index
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:78
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:51
void writeTriangle(Kernel::BinaryStreamWriter streamWriter, uint32_t triangle)
Function to write out an individual triangle with the scale removed.
Definition SaveStl.cpp:103
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:238
constexpr double Y() const noexcept
Get y.
Definition V3D.h:239
constexpr double Z() const noexcept
Get z.
Definition V3D.h:240