Mantid
Loading...
Searching...
No Matches
Torus.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 <algorithm>
8#include <cmath>
9#include <fstream>
10#include <list>
11#include <map>
12#include <sstream>
13#include <stack>
14#include <vector>
15
20#include "MantidKernel/Logger.h"
21#include "MantidKernel/Matrix.h"
24#include "MantidKernel/V3D.h"
25
26#ifdef ENABLE_OPENCASCADE
27// Opencascade defines _USE_MATH_DEFINES without checking whether it is already
28// used.
29// Undefine it here before we include the headers to avoid a warning
30#ifdef _MSC_VER
31#undef _USE_MATH_DEFINES
32#ifdef M_SQRT1_2
33#undef M_SQRT1_2
34#endif
35#endif
36
38GNU_DIAG_OFF("conversion")
39GNU_DIAG_OFF("cast-qual")
40#include <TopoDS_Shape.hxx>
41GNU_DIAG_ON("conversion")
42GNU_DIAG_ON("cast-qual")
43#endif
44
45namespace Mantid::Geometry {
47using Kernel::V3D;
48
50 : Surface(), Centre(), Normal(1, 0, 0), Iradius(0), Dradius(0), Displacement(0)
55{
56 throw Kernel::Exception::NotImplementedError("Torus is not implemented.");
57}
58
64{
65 return new Torus(*this);
66}
67
68std::unique_ptr<Torus> Torus::clone() const
73{
74 return std::unique_ptr<Torus>(doClone());
75}
76
77int Torus::operator==(const Torus &A) const
84{
85 if (this == &A)
86 return 1;
87
88 if ((fabs(Displacement - A.Displacement) > Tolerance) || (fabs(Iradius - A.Iradius) > Tolerance) ||
89 (fabs(Dradius - A.Dradius) > Tolerance))
90 return 0;
91
92 if (Centre.distance(A.Centre) > Tolerance)
93 return 0;
94 if (Normal.distance(A.Normal) > Tolerance)
95 return 0;
96
97 return 1;
98}
99
100/*
101 takes a character string and evaluates
102 the first <T> object. The string is then filled with
103 spaces upto the end of the <T> object
104 @param out :: place for output
105 @param A :: string for input and output.
106 @return 1 on success 0 on failure
107*/
108int sectionV3D(std::string &A, Mantid::Kernel::V3D &out) {
109 if (A.empty())
110 return 0;
111 std::istringstream cx;
112 Mantid::Kernel::V3D retval;
113 cx.str(A);
114 cx.clear();
115 cx >> retval;
116 if (cx.fail())
117 return 0;
118 const std::streamoff xpt = cx.tellg();
119 const auto xc = static_cast<char>(cx.get());
120 if (!cx.fail() && !isspace(xc))
121 return 0;
122 A.erase(0, static_cast<unsigned int>(xpt));
123 out = retval;
124 return 1;
125}
126
127int Torus::setSurface(const std::string &Pstr)
136{
137 enum { errDesc = -1, errAxis = -2, errCent = -3, errNormal = -4 };
138
139 std::string Line = Pstr;
140
141 std::string item;
142 if (!Mantid::Kernel::Strings::section(Line, item) || tolower(item[0]) != 't' || item.length() != 3)
143 return errDesc;
144
145 // Torus on X/Y/Z axis
146 const auto ptype = static_cast<std::size_t>(tolower(item[2]) - 'x');
147 if (ptype >= 3)
148 return errAxis;
149
150 Kernel::V3D PtVec;
151
152 // Torus on X/Y/Z axis
153 if (!sectionV3D(Line, Centre))
154 return errCent;
155 if (!sectionV3D(Line, PtVec))
156 return errNormal;
157
158 Iradius = PtVec[1];
159 Dradius = PtVec[2];
160 Displacement = PtVec[0];
161 return 0;
162}
163
169{
170 Centre.rotate(R);
171 Normal.rotate(R);
172}
173
180{
181 Centre += A;
182}
183
189{
190 Centre = A;
191}
192
198{
199 const auto length = A.norm();
200 if (length > Tolerance) {
201 Normal = A / length;
202 }
203}
204
212{
213 UNUSED_ARG(Pt);
214 return Normal;
215}
216
217double Torus::distance(const Kernel::V3D &Pt) const
228{
229 const Kernel::V3D Px = Pt - Centre;
230 // test is the centre to point distance is zero
231 if (Px.norm() < Tolerance)
232 return 0;
233 return Px.norm();
234}
235
236int Torus::side(const Kernel::V3D &R) const
237
248{
249 UNUSED_ARG(R);
250 return -1;
251}
252
253bool Torus::onSurface(const Kernel::V3D &R) const {
261 UNUSED_ARG(R);
262 // was formerly alwasy returning -1 which bool(-1) == true
263 // which appears to be an accidental bug
264
265 // the previous doxygen comment was
266 // return 1 if on surface and -1 if not no surface
267 return false;
268}
269
270void Torus::write(std::ostream &OX) const
276{
277 // -3 -2 -1 0 1 2 3
278 const char Tailends[] = "zyx xyz";
279 const int Ndir = Normal.masterDir(Tolerance);
280 if (Ndir == 0) {
281 Surface::write(OX);
282 return;
283 }
284 std::ostringstream cx;
286 cx << "t" << Tailends[Ndir + 3] << " ";
287 cx.precision(Surface::Nprecision);
288 // Name and transform
289
290 cx << Centre << " " << Displacement << " " << Iradius << " " << Dradius;
292}
293
302void Torus::getBoundingBox(double &xmax, double &ymax, double &zmax, double &xmin, double &ymin, double &zmin) {
303 UNUSED_ARG(xmax);
304 UNUSED_ARG(ymax);
305 UNUSED_ARG(zmax);
306 UNUSED_ARG(xmin);
307 UNUSED_ARG(ymin);
308 UNUSED_ARG(zmin);
310 Kernel::Logger log("Torus");
311 log.warning("Torus::getBoundingBox is not implemented.");
312}
313
318void Torus::setDistanceFromCentreToTube(double dist) { Iradius = dist; }
319
323void Torus::setTubeRadius(double dist) { Dradius = dist; }
324
325#ifdef ENABLE_OPENCASCADE
326TopoDS_Shape Torus::createShape() {
327 // NOTE:: Not yet implemented
328 return TopoDS_Shape();
329}
330#endif
331
332} // namespace Mantid::Geometry
#define fabs(x)
Definition: Matrix.cpp:22
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
Impliments a line.
Definition: Line.h:43
Holds a basic quadratic surface.
Definition: Surface.h:33
void writeHeader(std::ostream &) const
Writes out the start of an MCNPX surface description .
Definition: Surface.cpp:71
virtual void write(std::ostream &) const
The writes the data to the output stream.
Definition: Surface.cpp:81
static const int Nprecision
Precision of the output.
Definition: Surface.h:41
Holds a torus in vector form.
Definition: Torus.h:30
Kernel::V3D Normal
Normal.
Definition: Torus.h:33
double Displacement
Displacement.
Definition: Torus.h:36
void setDistanceFromCentreToTube(double dist)
Suppose to set the distance from centre of the torus to the centre of tube.
Definition: Torus.cpp:318
double Dradius
Inner radius.
Definition: Torus.h:35
std::unique_ptr< Torus > clone() const
Makes a clone (implicit virtual copy constructor)
Definition: Torus.cpp:68
double distance(const Kernel::V3D &Pt) const override
Calculates the distance from the point to the Torus does not calculate the point on the Torus that is...
Definition: Torus.cpp:217
int side(const Kernel::V3D &R) const override
Calculate if the point R is within the torus (return -1) or outside, (return 1)
Definition: Torus.cpp:236
void setTubeRadius(double dist)
Suppose to set the radius of the tube which makes up the torus TODO:
Definition: Torus.cpp:323
Kernel::V3D Centre
Geometry::Vec3D for centre.
Definition: Torus.h:32
int operator==(const Torus &) const
Equality operator.
Definition: Torus.cpp:77
void getBoundingBox(double &xmax, double &ymax, double &zmax, double &xmin, double &ymin, double &zmin) override
SGenerates a bounding box for the Torus.
Definition: Torus.cpp:302
void setNorm(const Kernel::V3D &A)
Sets the Normal and the Base Equation.
Definition: Torus.cpp:193
double Iradius
Inner radius.
Definition: Torus.h:34
void rotate(const Kernel::Matrix< double > &) override
Rotate both the centre and the normal direction.
Definition: Torus.cpp:164
int setSurface(const std::string &Pstr) override
Processes a standard MCNPX cone string Recall that cones can only be specified on an axis Valid input...
Definition: Torus.cpp:127
bool onSurface(const Kernel::V3D &R) const override
is point valid on surface
Definition: Torus.cpp:253
Torus * doClone() const override
Makes a clone (implicit virtual copy constructor)
Definition: Torus.cpp:59
Torus()
Constructor with centre line along X axis and centre on origin.
Definition: Torus.cpp:49
void displace(const Kernel::V3D &) override
Displace the centre Only need to update the centre position.
Definition: Torus.cpp:174
void write(std::ostream &OX) const override
Write out the cone class in an mcnpx format.
Definition: Torus.cpp:270
Kernel::V3D surfaceNormal(const Kernel::V3D &Pt) const override
Get the normal at a point.
Definition: Torus.cpp:205
void setCentre(const Kernel::V3D &A)
Sets the central point and the Base Equation.
Definition: Torus.cpp:184
Marks code as not implemented yet.
Definition: Exception.h:138
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
Numerical Matrix class.
Definition: Matrix.h:42
Class for 3D vectors.
Definition: V3D.h:34
void rotate(const Matrix< double > &) noexcept
Rotate a point by a matrix.
Definition: V3D.cpp:217
double norm() const noexcept
Definition: V3D.h:263
int sectionV3D(std::string &A, Mantid::Kernel::V3D &out)
Definition: Torus.cpp:108
int section(std::string &A, T &out)
Convert and cut a string.
Definition: Strings.cpp:573
MANTID_KERNEL_DLL void writeMCNPX(const std::string &Line, std::ostream &OX)
Write file in standard MCNPX input form.
Definition: Strings.cpp:421
constexpr double Tolerance
Standard tolerance value.
Definition: Tolerance.h:12