Mantid
Loading...
Searching...
No Matches
LoadSampleShape.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
13
16#include "MantidAPI/Run.h"
17#include "MantidAPI/Sample.h"
18#include "MantidAPI/Workspace.h"
19
21
23
24namespace Mantid::DataHandling {
25
26namespace {
27double DegreesToRadians(double angle) { return angle * M_PI / 180; }
28} // namespace
29
30// Register the algorithm into the algorithm factory
31DECLARE_ALGORITHM(LoadSampleShape)
32
33using namespace Kernel;
34using namespace API;
35using namespace Geometry;
36
38 auto wsValidator = std::make_shared<API::InstrumentValidator>();
39 ;
40
41 // input workspace
42 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::Input, wsValidator),
43 "The name of the workspace containing the instrument to add the shape");
44
45 // shape file
46 const std::vector<std::string> extensions{".stl", ".off"};
47 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, extensions),
48 "The path name of the file containing the shape");
49
50 // scale to use for stl
51 declareProperty("Scale", "cm", "The scale of the stl: m, cm, or mm");
52
53 // Rotation angles
54 declareProperty("XDegrees", 0.0, "The degrees to rotate on the x axis by");
55 declareProperty("YDegrees", 0.0, "The degrees to rotate on the y axis by");
56 declareProperty("ZDegrees", 0.0, "The degrees to rotate on the z axis by");
57
58 // Vector to translate mesh
59 declareProperty(std::make_unique<ArrayProperty<double>>("TranslationVector", "0,0,0"),
60 "Vector by which to translate the loaded sample shape");
61
62 // Output workspace
63 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
64 "The name of the workspace that will contain the loaded "
65 "shape of the sample");
66}
67
69
70 Workspace_const_sptr inputWS = getProperty("InputWorkspace");
71 Workspace_sptr outputWS = getProperty("OutputWorkspace");
72
73 if (inputWS != outputWS) {
74 outputWS = inputWS->clone();
75 }
76
77 auto ei = std::dynamic_pointer_cast<ExperimentInfo>(outputWS);
78 if (!ei)
79 throw std::invalid_argument("Wrong type of input workspace");
80
81 const std::string filename = getProperty("Filename");
82
83 const std::string filetype = filename.substr(filename.size() - 3);
84
85 std::shared_ptr<MeshObject> shape = nullptr;
86 const std::string scaleProperty = getPropertyValue("Scale");
87 const ScaleUnits scaleType = getScaleTypeFromStr(scaleProperty);
88
89 std::unique_ptr<LoadSingleMesh> reader = nullptr;
90 if (filetype == "off") {
91 reader = std::make_unique<LoadOff>(filename, scaleType);
92 shape = reader->readShape();
93 } else {
94 reader = LoadStlFactory::createReader(filename, scaleType);
95 shape = reader->readShape();
96 }
97
98 const double xRotation = DegreesToRadians(getProperty("xDegrees"));
99 const double yRotation = DegreesToRadians(getProperty("yDegrees"));
100 const double zRotation = DegreesToRadians(getProperty("zDegrees"));
101 shape = reader->rotate(shape, xRotation, yRotation, zRotation);
102
103 const std::vector<double> translationVector = getProperty("TranslationVector");
104 shape = reader->translate(shape, translationVector);
105
106 // rotate shape according to goniometer
107 shape->rotate(ei->run().getGoniometer().getR());
108
109 // Put shape into sample.
110 Sample &sample = ei->mutableSample();
111 sample.setShape(shape);
112
113 // Set output workspace
114 setProperty("OutputWorkspace", outputWS);
115}
116
117} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
This class stores information about the sample used in particular run.
Definition: Sample.h:33
void setShape(const Geometry::IObject_sptr &shape)
Update the shape of the object.
Definition: Sample.cpp:116
A property class for workspaces.
void exec() override
Virtual method - must be overridden by concrete algorithm.
void init() override
Virtual method - must be overridden by concrete algorithm.
static std::unique_ptr< LoadStl > createReader(const std::string &filename, ScaleUnits scaleType)
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< const Workspace > Workspace_const_sptr
shared pointer to Mantid::API::Workspace (const version)
Definition: Workspace_fwd.h:22
ScaleUnits getScaleTypeFromStr(const std::string &scaleProperty)
Definition: MeshFileIO.h:73
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54