Monte Carlo Integration Library 1.0
High-performance Monte Carlo methods for numerical integration and optimization
hyperrectangle.tpp
Go to the documentation of this file.
1/**
2 * @file hyperrectangle.tpp
3 * @brief HyperRectangle template implementation.
4 * @details Contains the inline implementations of `HyperRectangle` methods
5 * for axis-aligned box volume and containment testing.
6 */
7
8#include <cmath>
9#include "../geometry.hpp"
10#include "integration_domain.hpp"
11#include <array>
12#include <utility>
13
14namespace mc::domains {
15
16/**
17 * @brief Construct an axis-aligned hyperrectangle from dimension extents.
18 * @tparam dim Dimensionality of the space.
19 * @param dims Array of dimension sizes. Box is centered at origin with ±dims[i]/2 bounds.
20 *
21 * @details The resulting box spans [-d₁/2, d₁/2] × ... × [-dₙ/2, dₙ/2]
22 * where dᵢ = dims[i].
23 */
24template <size_t dim>
25HyperRectangle<dim>::HyperRectangle(std::array<double, dim> &dims):
26 dimensions(dims)
27{};
28
29/**
30 * @brief Get the axis-aligned bounding box (which coincides with the domain).
31 * @tparam dim Dimensionality parameter.
32 * @return Bounds [-dims[i]/2, dims[i]/2] for each dimension i.
33 *
34 * @details For a hyperrectangle, the bounding box is exact (not approximation).
35 */
36template <size_t dim>
37auto HyperRectangle<dim>::getBounds() const -> mc::geom::Bounds<dim> {
38 mc::geom::Bounds<dim> bounds;
39 for(size_t i = 0; i < dim; ++i)
40 bounds[i] = std::make_pair(-dimensions[i]/2, dimensions[i]/2);
41 return bounds;
42}
43
44/**
45 * @brief Compute the volume of the hyperrectangle.
46 * @tparam dim Dimensionality parameter.
47 * @return Product of all dimension extents: ∏ dims[i].
48 *
49 * @details Uses straightforward multiplication across all dimensions.
50 * Time complexity: O(dim).
51 */
52template <size_t dim>
53double HyperRectangle<dim>::getBoxVolume() const{
54 double volume = 1;
55 for(size_t i = 0; i < dim; ++i) {
56 volume = volume * dimensions[i];
57 }
58 return volume;
59}
60
61/**
62 * @brief Test whether a point lies inside the hyperrectangle.
63 * @tparam dim Dimensionality parameter.
64 * @param point Point to test.
65 * @return true if |point[i]| ≤ dims[i]/2 for all dimensions; false otherwise.
66 *
67 * @details Simple componentwise range checking. Returns true only if
68 * the point is within the box on all dimensions.
69 * Time complexity: O(dim).
70 */
71template <size_t dim>
72bool HyperRectangle<dim>::isInside(const mc::geom::Point<dim> &point) const{
73 bool inside = true;
74 for(size_t i = 0; i < dim; ++i) {
75 if (point[i] < -dimensions[i]/2 || point[i] > dimensions[i]/2) {
76 inside = false;
77 }
78 }
79 return inside;
80}
81
82} // namespace mc::domains