Mantid
Loading...
Searching...
No Matches
vtkGeometryCacheWriter.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 +
8
11#include "MantidKernel/Logger.h"
12
13#include <Poco/DOM/AutoPtr.h>
14#include <Poco/DOM/DOMWriter.h>
15#include <Poco/DOM/Document.h>
16#include <Poco/DOM/Element.h>
17#include <Poco/DOM/Text.h>
18#include <Poco/File.h>
19#include <Poco/FileStream.h>
20#include <Poco/Path.h>
21#include <Poco/XML/XMLWriter.h>
22
23#include <fstream>
24#include <sstream>
25#include <utility>
26
27using Poco::XML::AutoPtr;
28using Poco::XML::Document;
29using Poco::XML::DOMWriter;
30using Poco::XML::Element;
31using Poco::XML::Text;
32using Poco::XML::XMLWriter;
33
34namespace Mantid::Geometry {
35namespace {
37Kernel::Logger g_log("vtkGeometryCacheWriter");
38} // namespace
39
44 : m_doc(new Document()), m_filename(std::move(filename)) {
45 Init();
46}
47
52 m_root->release();
53 m_doc->release();
54}
55
60 m_root = m_doc->createElement("PolyData");
62}
71 AutoPtr<Element> pRoot = m_doc->createElement("VTKFile");
72 pRoot->setAttribute("type", "PolyData");
73 pRoot->setAttribute("version", "1.0");
74 pRoot->setAttribute("byte_order", "LittleEndian");
75 m_doc->appendChild(pRoot);
76 pRoot->appendChild(m_root);
77}
78
84 // First check whether Object can be written to the file
85 std::shared_ptr<GeometryHandler> handle = obj->getGeometryHandler();
86 if (!(handle->canTriangulate()))
87 return; // Cannot add the object to the file
88 std::stringstream buf;
89 // get the name of the Object
90 int name = obj->getName();
91 // get number of point
92 auto noPts = handle->numberOfPoints();
93 // get number of triangles
94 auto noTris = handle->numberOfTriangles();
95 // Add Piece
96 AutoPtr<Element> pPiece = m_doc->createElement("Piece");
97 // Add attribute name
98 buf << name;
99 pPiece->setAttribute("name", buf.str());
100 // Add Number of Points
101 buf.str("");
102 buf << noPts;
103 pPiece->setAttribute("NumberOfPoints", buf.str());
104 // Add Number of Triangles/Polys
105 buf.str("");
106 buf << noTris;
107 pPiece->setAttribute("NumberOfPolys", buf.str());
108 // write the points
109 AutoPtr<Element> pPoints = m_doc->createElement("Points");
110 AutoPtr<Element> pPtsDataArray = m_doc->createElement("DataArray");
111 // Add attributes to data array
112 pPtsDataArray->setAttribute("type", "Float32");
113 pPtsDataArray->setAttribute("NumberOfComponents", "3");
114 pPtsDataArray->setAttribute("format", "ascii");
115 buf.str("");
116 // get the triangles info
117 const auto &points = handle->getTriangleVertices();
118 size_t i;
119 for (i = 0; i < points.size(); i++) {
120 buf << points[i] << " ";
121 }
122 AutoPtr<Text> pPointText = m_doc->createTextNode(buf.str());
123 pPtsDataArray->appendChild(pPointText);
124 pPoints->appendChild(pPtsDataArray);
125 // get triangles faces info
126 AutoPtr<Element> pFaces = m_doc->createElement("Polys");
127 AutoPtr<Element> pTrisDataArray = m_doc->createElement("DataArray");
128 // add attribute
129 pTrisDataArray->setAttribute("type", "UInt32");
130 pTrisDataArray->setAttribute("Name", "connectivity");
131 pTrisDataArray->setAttribute("format", "ascii");
132
133 buf.str("");
134 const auto &faces = handle->getTriangleFaces();
135 for (i = 0; i < faces.size(); i++) {
136 buf << faces[i] << " ";
137 }
138 AutoPtr<Text> pTrisDataText = m_doc->createTextNode(buf.str());
139 pTrisDataArray->appendChild(pTrisDataText);
140 pFaces->appendChild(pTrisDataArray);
141 // set the offsets
142 AutoPtr<Element> pTrisOffsetDataArray = m_doc->createElement("DataArray");
143 // add attribute
144 pTrisOffsetDataArray->setAttribute("type", "UInt32");
145 pTrisOffsetDataArray->setAttribute("Name", "offsets");
146 pTrisOffsetDataArray->setAttribute("format", "ascii");
147 buf.str("");
148 for (i = 1; i < noTris + 1; i++) {
149 buf << i * 3 << " ";
150 }
151 AutoPtr<Text> pTrisOffsetDataText = m_doc->createTextNode(buf.str());
152 pTrisOffsetDataArray->appendChild(pTrisOffsetDataText);
153 pFaces->appendChild(pTrisOffsetDataArray);
154
155 // append all
156 pPiece->appendChild(pPoints);
157 pPiece->appendChild(pFaces);
158
159 // add this piece to root
160 m_root->appendChild(pPiece);
161}
166 DOMWriter writer;
167 writer.setNewLine("\n");
168 writer.setOptions(XMLWriter::PRETTY_PRINT);
169 std::ofstream file;
170 try {
171 g_log.information("Writing Geometry Cache file to " + m_filename);
172 file.open(m_filename.c_str(), std::ios::trunc);
173 writer.writeNode(file, m_doc);
174 file.close();
175 } catch (...) {
176 g_log.error("Geometry Cache file writing exception");
177 }
178}
179} // namespace Mantid::Geometry
double obj
the value of the quadratic function
Constructive Solid Geometry object.
Definition: CSGObject.h:51
void createVTKFileHeader()
creates VTK XML header <VTKFile type="PolyData" version="1.0" byte_order="LittleEndian"> <PolyData> <...
Poco::XML::Element * m_root
The root XML element.
void addObject(CSGObject *obj)
Adds the geometry of the Object to the document.
Poco::XML::Document * m_doc
The XML document.
void Init()
Initialises the XML Document with the required vtk XML Headings.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
Mantid::Kernel::Logger g_log("Goniometer")
STL namespace.