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 +
9#include "MantidKernel/VMD.h"
10
11#include <algorithm>
12
14
15namespace Mantid::Geometry {
16
17//----------------------------------------------------------------------------------------------
27MDBoxImplicitFunction::MDBoxImplicitFunction(const std::vector<coord_t> &min, const std::vector<coord_t> &max)
28 : m_max(max), m_min(min) {
29 construct(VMD(min), VMD(max));
30}
31
32//----------------------------------------------------------------------------------------------
46
47//----------------------------------------------------------------------------------------------
54 size_t nd = min.size();
55 if (max.size() != nd)
56 throw std::invalid_argument("MDBoxImplicitFunction::ctor(): Min and max vector sizes must match!");
57 if (nd == 0 || nd > 100)
58 throw std::invalid_argument("MDBoxImplicitFunction::ctor(): Invalid number of dimensions!");
59
60 double boxVolume = 1;
61 for (size_t d = 0; d < nd; d++) {
62 boxVolume *= (max[d] - min[d]);
63
64 // Make two parallel planes per dimension
65
66 // Normal on the min side, so it faces towards +X
67 std::vector<coord_t> normal_min(nd, 0);
68 normal_min[d] = +1.0;
69 // Origin just needs to have its X set to the value. Other coords are
70 // irrelevant
71 std::vector<coord_t> origin_min(nd, 0);
72 origin_min[d] = static_cast<coord_t>(min[d]);
73 // Build the plane
74 MDPlane p_min(normal_min, origin_min);
75 this->addPlane(p_min);
76
77 // Normal on the max side, so it faces towards -X
78 std::vector<coord_t> normal_max(nd, 0);
79 normal_max[d] = -1.0;
80 // Origin just needs to have its X set to the value. Other coords are
81 // irrelevant
82 std::vector<coord_t> origin_max(nd, 0);
83 origin_max[d] = static_cast<coord_t>(max[d]);
84 // Build the plane
85 MDPlane p_max(normal_max, origin_max);
86 this->addPlane(p_max);
87 }
88 m_volume = boxVolume;
89}
90
95double MDBoxImplicitFunction::volume() const { return m_volume; }
96
102double
103MDBoxImplicitFunction::fraction(const std::vector<boost::tuple<Mantid::coord_t, Mantid::coord_t>> &boxExtents) const {
104
105 size_t nd = m_min.size();
106 coord_t frac = 1;
107
108 for (size_t d = 0; d < nd; ++d) {
109
110 const coord_t min = boxExtents[d].get<0>();
111 const coord_t max = boxExtents[d].get<1>();
112
113 // Check that there is overlap at all. There must be overlap in ALL
114 // dimensions for the fraction to be > 0, so abort early if not.
115 if (max < m_min[d] || min > m_max[d]) {
116 frac = 0;
117 break;
118 }
119
120 const coord_t dBoxRange = (max - min); // max-min
121 const coord_t dInnerMin = std::max(m_min[d], min);
122 const coord_t dInnerMax = std::min(m_max[d], max);
123 const coord_t dOverlap = dInnerMax - dInnerMin;
124
125 frac *= dOverlap / dBoxRange;
126 }
127
128 return frac;
129}
130
131} // 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 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