Mantid
Loading...
Searching...
No Matches
LoadOff.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 +
11#include "MantidKernel/V3D.h"
12
13#include <boost/algorithm/string.hpp>
14
15namespace Mantid::DataHandling {
16
17LoadOff::LoadOff(const std::string &filename, ScaleUnits scaleType)
18 : LoadSingleMesh(filename, std::ios_base::in, scaleType) {}
19
20bool LoadOff::getOFFline(std::string &line) {
21 // Get line from OFF file ignoring blank lines and comments
22 // The line output is ready trimmed
23 if (!getline(m_file, line)) {
24 return false;
25 }
26 boost::trim(line);
27 while (line.empty() || line.substr(0, 1) == "#") {
28 if (!getline(m_file, line)) {
29 return false;
30 }
31 boost::trim(line);
32 }
33 return true;
34}
35
37 std::string line;
38 for (uint32_t i = 0; i < m_nVertices; i++) {
39 if (getOFFline(line)) {
40 std::vector<std::string> tokens;
41 boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
42 if (tokens.size() == 3) {
43 auto vertex = createScaledV3D(boost::lexical_cast<double>(tokens[0]), // x
44 boost::lexical_cast<double>(tokens[1]), // y
45 boost::lexical_cast<double>(tokens[2])); // z
46 m_vertices.emplace_back(vertex);
47 } else {
48 throw std::runtime_error("Error on reading OFF vertex");
49 }
50 } else {
51 throw std::runtime_error("Unexpected end of file, while reading OFF m_vertices");
52 }
53 }
54}
55
57 std::string line;
58 uint32_t t1, t2, t3;
59 size_t nFaceVertices;
60 for (uint32_t i = 0; i < m_nTriangles; i++) {
61 if (getOFFline(line)) {
62 std::vector<std::string> tokens;
63 boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
64 if (tokens.size() >= 4) {
65 nFaceVertices = boost::lexical_cast<size_t>(tokens[0]);
66 if (nFaceVertices == 3) {
67 t1 = boost::lexical_cast<uint32_t>(tokens[1]);
68 t2 = boost::lexical_cast<uint32_t>(tokens[2]);
69 t3 = boost::lexical_cast<uint32_t>(tokens[3]);
70 } else {
71 throw std::runtime_error("OFF face is not a triangle.");
72 }
73 m_triangle.emplace_back(t1);
74 m_triangle.emplace_back(t2);
75 m_triangle.emplace_back(t3);
76 } else {
77 throw std::runtime_error("Error on reading OFF triangle");
78 }
79 } else {
80 throw std::runtime_error("Unexpected end of file, while reading OFF triangles");
81 }
82 }
83}
84
85std::unique_ptr<Geometry::MeshObject> LoadOff::readOFFMeshObject() {
86
87 std::string line;
88 // Get number of vetrtices and faces
89 if (getOFFline(line)) {
90 std::vector<std::string> tokens;
91 boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
92 if (tokens.size() == 3) {
93 try {
94 m_nVertices = boost::lexical_cast<uint32_t>(tokens[0]);
95 m_nTriangles = boost::lexical_cast<uint32_t>(tokens[1]);
96 } catch (...) {
97 throw std::runtime_error("Error in reading numbers of OFF m_vertices and "
98 "triangles, which may be too large");
99 }
100 m_vertices.reserve(m_nVertices);
101 m_triangle.reserve(3 * m_nTriangles);
102 } else {
103 throw std::runtime_error("Error on reading OFF number of m_vertices, faces & edges");
104 }
105 } else {
106 throw std::runtime_error("Unexpected end of OFF file");
107 }
110
111 // Use efficient constructor of MeshObject
112 std::unique_ptr<Geometry::MeshObject> retVal =
113 std::make_unique<Geometry::MeshObject>(std::move(m_triangle), std::move(m_vertices), Mantid::Kernel::Material());
114 return retVal;
115}
116
117std::unique_ptr<Geometry::MeshObject> LoadOff::readShape() {
118 std::string line;
119 if (getOFFline(line)) {
120 if (line != "OFF") {
121 throw std::runtime_error("Expected first line to be 'OFF' keyword");
122 }
123 // Read OFF shape
124 return readOFFMeshObject();
125 }
126 return nullptr;
127}
128} // namespace Mantid::DataHandling
std::unique_ptr< Geometry::MeshObject > readOFFMeshObject()
Definition: LoadOff.cpp:85
std::unique_ptr< Geometry::MeshObject > readShape() override
Definition: LoadOff.cpp:117
LoadOff(const std::string &filename, ScaleUnits scaleType)
Definition: LoadOff.cpp:17
bool getOFFline(std::string &line)
Definition: LoadOff.cpp:20
std::vector< uint32_t > m_triangle
Definition: MeshFileIO.h:64
Kernel::V3D createScaledV3D(double xVal, double yVal, double zVal)
scales a 3D point according the units defined in the MeshFileIO class
Definition: MeshFileIO.cpp:53
std::vector< Kernel::V3D > m_vertices
Definition: MeshFileIO.h:65
A material is defined as being composed of a given element, defined as a PhysicalConstants::NeutronAt...
Definition: Material.h:50
STL namespace.