-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadSampleShape.cpp
225 lines (197 loc) · 7.13 KB
/
LoadSampleShape.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/LoadSampleShape.h"
#include "MantidDataHandling/LoadAsciiStl.h"
#include "MantidDataHandling/LoadBinaryStl.h"
#include "MantidGeometry/Objects/MeshObject.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Sample.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/Exception.h"
#include <Poco/File.h>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <stdio.h>
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadSampleShape)
using namespace Kernel;
using namespace API;
using namespace Geometry;
namespace {
bool getOFFline(std::ifstream &file, std::string &line) {
// Get line from OFF file ignoring blank lines and comments
// The line output is ready trimmed
if (!getline(file, line)) {
return false;
}
boost::trim(line);
while (line.empty() || line.substr(0, 1) == "#") {
if (!getline(file, line)) {
return false;
}
boost::trim(line);
}
return true;
}
void readOFFVertices(std::ifstream &file, uint32_t nVertices,
std::vector<V3D> &vertices) {
std::string line;
for (uint32_t i = 0; i < nVertices; i++) {
if (getOFFline(file, line)) {
std::vector<std::string> tokens;
boost::split(tokens, line, boost::is_any_of(" "),
boost::token_compress_on);
if (tokens.size() == 3) {
vertices.emplace_back(boost::lexical_cast<double>(tokens[0]), // x
boost::lexical_cast<double>(tokens[1]), // y
boost::lexical_cast<double>(tokens[2])); // z
} else {
throw std::runtime_error("Error on reading OFF vertex");
}
} else {
throw std::runtime_error(
"Unexpected end of file, while reading OFF vertices");
}
}
}
void readOFFTriangles(std::ifstream &file, uint32_t nTriangles,
std::vector<uint32_t> &triangleIndices) {
std::string line;
uint32_t t1, t2, t3;
size_t nFaceVertices;
for (uint32_t i = 0; i < nTriangles; i++) {
if (getOFFline(file, line)) {
std::vector<std::string> tokens;
boost::split(tokens, line, boost::is_any_of(" "),
boost::token_compress_on);
if (tokens.size() >= 4) {
nFaceVertices = boost::lexical_cast<size_t>(tokens[0]);
if (nFaceVertices == 3) {
t1 = boost::lexical_cast<uint32_t>(tokens[1]);
t2 = boost::lexical_cast<uint32_t>(tokens[2]);
t3 = boost::lexical_cast<uint32_t>(tokens[3]);
} else {
throw std::runtime_error("OFF face is not a triangle.");
}
triangleIndices.emplace_back(t1);
triangleIndices.emplace_back(t2);
triangleIndices.emplace_back(t3);
} else {
throw std::runtime_error("Error on reading OFF triangle");
}
} else {
throw std::runtime_error(
"Unexpected end of file, while reading OFF triangles");
}
}
}
std::unique_ptr<MeshObject> readOFFMeshObject(std::ifstream &file) {
std::vector<uint32_t> triangleIndices;
std::vector<V3D> vertices;
uint32_t nVertices;
uint32_t nTriangles;
std::string line;
// Get number of vetrtices and faces
if (getOFFline(file, line)) {
std::vector<std::string> tokens;
boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
if (tokens.size() == 3) {
try {
nVertices = boost::lexical_cast<uint32_t>(tokens[0]);
nTriangles = boost::lexical_cast<uint32_t>(tokens[1]);
} catch (...) {
throw std::runtime_error("Error in reading numbers of OFF vertices and "
"triangles, which may be too large");
}
vertices.reserve(nVertices);
triangleIndices.reserve(3 * nTriangles);
} else {
throw std::runtime_error(
"Error on reading OFF number of vertices, faces & edges");
}
} else {
throw std::runtime_error("Unexpected end of OFF file");
}
readOFFVertices(file, nVertices, vertices);
readOFFTriangles(file, nTriangles, triangleIndices);
// Use efficient constructor of MeshObject
std::unique_ptr<MeshObject> retVal = std::unique_ptr<MeshObject>(
new MeshObject(std::move(triangleIndices), std::move(vertices),
Mantid::Kernel::Material()));
return retVal;
}
std::unique_ptr<Geometry::MeshObject> readOFFshape(std::ifstream &file) {
std::string line;
if (getOFFline(file, line)) {
if (line != "OFF") {
throw std::runtime_error("Expected first line to be 'OFF' keyword");
}
// Read OFF shape
return readOFFMeshObject(file);
}
return nullptr;
}
} // end anonymous namespace
void LoadSampleShape::init() {
auto wsValidator = boost::make_shared<API::InstrumentValidator>();
;
// input workspace
declareProperty(
make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input,
wsValidator),
"The name of the workspace containing the instrument to add the shape");
// shape file
const std::vector<std::string> extensions{".stl", ".off"};
declareProperty(
make_unique<FileProperty>("Filename", "", FileProperty::Load, extensions),
"The path name of the file containing the shape");
// Output workspace
declareProperty(make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output),
"The name of the workspace that will contain the loaded "
"shape of the sample");
}
void LoadSampleShape::exec() {
MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
if (inputWS != outputWS) {
outputWS = inputWS->clone();
}
const std::string filename = getProperty("Filename");
std::ifstream file(filename.c_str());
if (!file) {
g_log.error("Unable to open file: " + filename);
throw Exception::FileError("Unable to open file: ", filename);
}
const std::string filetype = filename.substr(filename.size() - 3);
boost::shared_ptr<MeshObject> shape = nullptr;
if (filetype == "off") {
shape = readOFFshape(file);
} else /* stl */ {
auto asciiStlReader = LoadAsciiStl(filename);
auto binaryStlReader = LoadBinaryStl(filename);
if (binaryStlReader.isBinarySTL(filename)) {
shape = binaryStlReader.readStl();
} else if (asciiStlReader.isAsciiSTL(filename)) {
shape = asciiStlReader.readStl();
} else {
throw Kernel::Exception::ParseError(
"Could not read file, did not match either STL Format", filename, 0);
}
}
// Put shape into sample.
Sample &sample = outputWS->mutableSample();
sample.setShape(shape);
// Set output workspace
setProperty("OutputWorkspace", outputWS);
}
} // namespace DataHandling
} // namespace Mantid