Mantid
Loading...
Searching...
No Matches
MDBoxImplicitFunction.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#include "MantidKernel/VMD.h"
11
12#include <algorithm>
13
15
16namespace Mantid::Geometry {
17
18//----------------------------------------------------------------------------------------------
28MDBoxImplicitFunction::MDBoxImplicitFunction(const std::vector<coord_t> &min, const std::vector<coord_t> &max)
29 : m_max(max), m_min(min) {
30 construct(VMD(min), VMD(max));
31}
32
33//----------------------------------------------------------------------------------------------
44 : m_max(max), m_min(min) {
45 construct(min, max);
46}
47
48//----------------------------------------------------------------------------------------------
55 size_t nd = min.size();
56 if (max.size() != nd)
57 throw std::invalid_argument("MDBoxImplicitFunction::ctor(): Min and max vector sizes must match!");
58 if (nd == 0 || nd > 100)
59 throw std::invalid_argument("MDBoxImplicitFunction::ctor(): Invalid number of dimensions!");
60
61 double volume = 1;
62 for (size_t d = 0; d < nd; d++) {
63 volume *= (max[d] - min[d]);
64
65 // Make two parallel planes per dimension
66
67 // Normal on the min side, so it faces towards +X
68 std::vector<coord_t> normal_min(nd, 0);
69 normal_min[d] = +1.0;
70 // Origin just needs to have its X set to the value. Other coords are
71 // irrelevant
72 std::vector<coord_t> origin_min(nd, 0);
73 origin_min[d] = static_cast<coord_t>(min[d]);
74 // Build the plane
75 MDPlane p_min(normal_min, origin_min);
76 this->addPlane(p_min);
77
78 // Normal on the max side, so it faces towards -X
79 std::vector<coord_t> normal_max(nd, 0);
80 normal_max[d] = -1.0;
81 // Origin just needs to have its X set to the value. Other coords are
82 // irrelevant
83 std::vector<coord_t> origin_max(nd, 0);
84 origin_max[d] = static_cast<coord_t>(max[d]);
85 // Build the plane
86 MDPlane p_max(normal_max, origin_max);
87 this->addPlane(p_max);
88 }
90}
91
96double MDBoxImplicitFunction::volume() const { return m_volume; }
97
103double
104MDBoxImplicitFunction::fraction(const std::vector<boost::tuple<Mantid::coord_t, Mantid::coord_t>> &boxExtents) const {
105
106 size_t nd = m_min.size();
107 coord_t frac = 1;
108
109 for (size_t d = 0; d < nd; ++d) {
110
111 const coord_t min = boxExtents[d].get<0>();
112 const coord_t max = boxExtents[d].get<1>();
113
114 // Check that there is overlap at all. There must be overlap in ALL
115 // dimensions for the fraction to be > 0, so abort early if not.
116 if (max < m_min[d] || min > m_max[d]) {
117 frac = 0;
118 break;
119 }
120
121 const coord_t dBoxRange = (max - min); // max-min
122 const coord_t dInnerMin = std::max(m_min[d], min);
123 const coord_t dInnerMax = std::min(m_max[d], max);
124 const coord_t dOverlap = dInnerMax - dInnerMin;
125
126 frac *= dOverlap / dBoxRange;
127 }
128
129 return frac;
130}
131
132} // namespace Mantid::Geometry
specnum_t m_min
specnum_t m_max
MDBoxImplicitFunction(const Mantid::Kernel::VMD &min, const Mantid::Kernel::VMD &max)
Constructor with min/max dimensions.
void construct(const Mantid::Kernel::VMD &min, const Mantid::Kernel::VMD &max)
Constructor helper method.
const Mantid::Kernel::VMD m_max
Maximum extents of MDBox.
const Mantid::Kernel::VMD m_min
Minimum extents of MDBox.
double volume() const
Calculate volume.
double fraction(const std::vector< boost::tuple< Mantid::coord_t, Mantid::coord_t > > &boxExtents) const
Calculate the fraction of a box residing inside this implicit function.
void addPlane(const MDPlane &plane)
Add a bounded plane to this implicit function.
A generalized description of a N-dimensional hyperplane.
Definition: MDPlane.h:41
size_t size() const
Definition: VMD.cpp:239
VMDBase< VMD_t > VMD
Define the VMD as using the double or float data type.
Definition: VMD.h:86
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27