Mantid
Loading...
Searching...
No Matches
Container.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 +
10
11#include "Poco/DOM/AutoPtr.h"
12#include "Poco/DOM/DOMParser.h"
13#include "Poco/DOM/Document.h"
14#include "Poco/DOM/NodeFilter.h"
15#include "Poco/DOM/NodeIterator.h"
16#include "Poco/SAX/InputSource.h"
17#include "Poco/SAX/SAXException.h"
18#include <memory>
19#include <utility>
20
21namespace Mantid::Geometry {
22
23namespace {
24constexpr const char *SAMPLEGEOMETRY_TAG = "samplegeometry";
25constexpr const char *VAL_ATT = "val";
26
27//------------------------------------------------------------------------------
28// Anonymous methods
29//------------------------------------------------------------------------------
37void updateTreeValues(Poco::XML::Element *root, const Container::ShapeArgs &args) {
38 using namespace Poco::XML;
39 NodeIterator nodeIter(root, NodeFilter::SHOW_ELEMENT);
40 Node *node = nodeIter.nextNode();
41 while (node) {
42 auto *element = static_cast<Element *>(node);
43 auto argIter = args.find(node->nodeName());
44 if (argIter != args.end()) {
45 element->setAttribute(VAL_ATT, std::to_string(argIter->second));
46 }
47 node = nodeIter.nextNode();
48 }
49}
50} // namespace
51
52//------------------------------------------------------------------------------
53// Public methods
54//------------------------------------------------------------------------------
55Container::Container() : m_shape(std::make_shared<CSGObject>()) {}
56
57Container::Container(IObject_sptr shape) : m_shape(std::move(shape)) {}
58
59Container::Container(const Container &container) { *this = container; }
60
62 m_shape = IObject_sptr(container.m_shape->clone());
64 m_sampleShape = container.m_sampleShape;
65 return *this;
66}
67
72Container::Container(const std::string &xml) : m_shape(std::make_shared<CSGObject>(xml)) {}
73
79
84bool Container::hasFixedSampleShape() const { return m_sampleShape != nullptr; }
85
94 using namespace Poco::XML;
96 throw std::runtime_error("Can::createSampleShape() - No definition found "
97 "for the sample geometry.");
98 }
99 // Parse XML
100 std::istringstream instrm(m_sampleShapeXML);
101 InputSource src(instrm);
102 DOMParser parser;
103 AutoPtr<Document> doc;
104 try {
105 doc = parser.parse(&src);
106 } catch (SAXParseException &exc) {
107 std::ostringstream os;
108 os << "Can::createSampleShape() - Error parsing XML: " << exc.what();
109 throw std::invalid_argument(os.str());
110 }
111 Element *root = doc->documentElement();
112 if (!args.empty())
113 updateTreeValues(root, args);
114
115 ShapeFactory factory;
116 return factory.createShape(root);
117}
118
120
125void Container::setSampleShape(const std::string &sampleShapeXML) {
126 using namespace Poco::XML;
127 std::istringstream instrm(sampleShapeXML);
128 InputSource src(instrm);
129 DOMParser parser;
130 AutoPtr<Document> doc = parser.parse(&src);
131 if (doc->documentElement()->nodeName() != SAMPLEGEOMETRY_TAG) {
132 std::ostringstream msg;
133 msg << "Can::setSampleShape() - XML definition "
134 "expected to be contained within a <"
135 << SAMPLEGEOMETRY_TAG << "> tag. Found " << doc->documentElement()->nodeName() << "instead.";
136 throw std::invalid_argument(msg.str());
137 }
138 m_sampleShapeXML = sampleShapeXML;
139}
140
145void Container::setID(const std::string &id) {
146 // We only do anything if the contained shape is a CSGObject
147 if (auto csgObj = std::dynamic_pointer_cast<CSGObject>(m_shape)) {
148 csgObj->setID(id);
149 }
150}
151
152} // namespace Mantid::Geometry
Constructive Solid Geometry object.
Definition: CSGObject.h:51
Models a Container is used to hold a sample in the beam.
Definition: Container.h:24
bool hasFixedSampleShape() const
Definition: Container.cpp:84
IObject_sptr getSampleShape() const
Definition: Container.cpp:119
std::unordered_map< std::string, double > ShapeArgs
Definition: Container.h:26
Container & operator=(const Container &container)
Definition: Container.cpp:61
bool hasCustomizableSampleShape() const
Definition: Container.cpp:78
void setSampleShape(const std::string &sampleShapeXML)
Set the definition of the sample shape for this can.
Definition: Container.cpp:125
IObject_sptr m_sampleShape
Definition: Container.h:101
std::string m_sampleShapeXML
Definition: Container.h:100
IObject_sptr createSampleShape(const ShapeArgs &args) const
Return an object that represents the sample shape from the current definition but override the defaul...
Definition: Container.cpp:93
void setID(const std::string &id) override
Set the ID of the shape, if it is a CSG Object.
Definition: Container.cpp:145
Class originally intended to be used with the DataHandling 'LoadInstrument' algorithm.
Definition: ShapeFactory.h:89
std::shared_ptr< CSGObject > createShape(Poco::XML::Element *pElem)
Creates a geometric object from a DOM-element-node pointing to an element whose child nodes contain t...
std::shared_ptr< IObject > IObject_sptr
Typdef for a shared pointer.
Definition: IObject.h:92
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)