Mantid
Loading...
Searching...
No Matches
vtkGeometryCacheReader.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 +
7#include <Poco/DOM/AutoPtr.h>
8#include <Poco/DOM/DOMParser.h>
9#include <Poco/DOM/Document.h>
10#include <Poco/DOM/Element.h>
11#include <Poco/DOM/NodeFilter.h>
12#include <Poco/DOM/NodeIterator.h>
13#include <Poco/Exception.h>
14#include <Poco/SAX/InputSource.h>
15
16#include <utility>
17
22#include "MantidKernel/Logger.h"
23
24using Poco::XML::DOMParser;
25using Poco::XML::Element;
26
27namespace Mantid::Geometry {
28namespace {
30Kernel::Logger g_log("vtkGeometryCacheReader");
31} // namespace
32
36vtkGeometryCacheReader::vtkGeometryCacheReader(std::string filename) : m_doc(nullptr), m_filename(std::move(filename)) {
37 Init();
38}
39
44 m_doc->release();
45 delete m_pParser;
46}
47
52 // Set up the DOM parser and parse xml file
53 m_pParser = new DOMParser();
54 try {
55 m_doc = m_pParser->parse(m_filename);
56 } catch (...) {
57 throw Kernel::Exception::FileError("Unable to parse File:", m_filename);
58 }
59}
60
65 // Get the element corresponding to the name of the object
66 std::stringstream objName;
67 objName << obj->getName();
68 Poco::XML::Element *pEle = getElementByObjectName(objName.str());
69 if (pEle == nullptr) // Element not found
70 {
71 g_log.debug("Cache not found for Object with name " + objName.str());
72 return;
73 }
74 // Read the cache from the element
75 int noOfTriangles = 0, noOfPoints = 0;
76 std::vector<double> Points;
77 std::vector<uint32_t> Faces;
78 std::stringstream buff;
79 // Read number of points
80 buff << pEle->getAttribute("NumberOfPoints");
81 buff >> noOfPoints;
82 buff.clear();
83 // Read number of triangles
84 buff << pEle->getAttribute("NumberOfPolys");
85 buff >> noOfTriangles;
86 buff.clear();
87
88 // Read Points
89 Element *pPts = pEle->getChildElement("Points")->getChildElement("DataArray");
90 readPoints(pPts, noOfPoints, Points);
91
92 // Read Triangles
93 Element *pTris = pEle->getChildElement("Polys")->getChildElement("DataArray");
94 readTriangles(pTris, noOfTriangles, Faces);
95
96 // First check whether Object can be written to the file
97 std::shared_ptr<GeometryHandler> handle = obj->getGeometryHandler();
98 handle->setGeometryCache(noOfPoints, noOfTriangles, std::move(Points), std::move(Faces));
99}
100
104Poco::XML::Element *vtkGeometryCacheReader::getElementByObjectName(const std::string &name) {
105 Element *pRoot = m_doc->documentElement();
106 if (pRoot == nullptr || pRoot->nodeName() != "VTKFile")
107 return nullptr;
108 Element *pPolyData = pRoot->getChildElement("PolyData");
109 if (pPolyData == nullptr)
110 return nullptr;
111 return pPolyData->getElementById(name, "name");
112}
113
117void vtkGeometryCacheReader::readPoints(Poco::XML::Element *pEle, int noOfPoints, std::vector<double> &points) {
118 if (pEle == nullptr) {
119 return;
120 }
121 // Allocate memory
122 points.resize(noOfPoints * 3);
123 if (points.size() != static_cast<size_t>(noOfPoints * 3)) // Out of memory
124 {
125 g_log.error("Cannot allocate memory for triangle cache of Object ");
126 return;
127 }
128 if (pEle->getAttribute("format") == "ascii") { // Read from Ascii
129 std::stringstream buf;
130 buf << pEle->innerText();
131 for (double &point : points) {
132 buf >> point;
133 }
134 }
135 // Read from binary otherwise
136}
137
141void vtkGeometryCacheReader::readTriangles(Poco::XML::Element *pEle, int noOfTriangles, std::vector<uint32_t> &faces) {
142 if (pEle == nullptr) {
143 return;
144 }
145 // Allocate memory
146 faces.resize(noOfTriangles * 3);
147 if (faces.size() != static_cast<size_t>(noOfTriangles * 3)) // Out of memory
148 {
149 g_log.error("Cannot allocate memory for triangle cache of Object ");
150 return;
151 }
152 if (pEle->getAttribute("format") == "ascii") { // Read from Ascii
153 std::stringstream buf;
154 buf << pEle->innerText();
155 for (unsigned int &face : faces) {
156 buf >> face;
157 }
158 }
159 // Read from binary otherwise
160}
161} // namespace Mantid::Geometry
double obj
the value of the quadratic function
IObject : Interface for geometry objects.
Definition: IObject.h:41
Poco::XML::DOMParser * m_pParser
The XML parser.
void readTriangles(Poco::XML::Element *pEle, int noOfTriangles, std::vector< uint32_t > &faces)
Read triangle face indexs.
void readPoints(Poco::XML::Element *pEle, int noOfPoints, std::vector< double > &points)
Read the points from the element.
Poco::XML::Document * m_doc
The XML document.
Poco::XML::Element * getElementByObjectName(const std::string &name)
Get the Element by using the object name.
void Init()
Initialise Reading of the cached file.
vtkGeometryCacheReader(std::string filename)
Constructor.
void readCacheForObject(IObject *obj)
Set the geometry for the object.
Records the filename and the description of failure.
Definition: Exception.h:98
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
Mantid::Kernel::Logger g_log("Goniometer")
STL namespace.