Monte Carlo Integration Library 1.0
High-performance Monte Carlo methods for numerical integration and optimization
polytope.tpp
Go to the documentation of this file.
1/**
2 * @file polytope.tpp
3 * @brief PolyTope template implementation.
4 * @details Contains the inline implementations of `PolyTope` methods for
5 * half-space intersection bounding box and point containment testing.
6 *
7 * @note Qhull usage: `qhull Qt Qx Fn < points.txt > hull.txt`
8 * Points file format: First line: <num_points> <dim>
9 * Following lines: coordinates of each vertex
10 */
11
12#ifndef MONTECARLO_1_POLYTOPE_TPP
13#define MONTECARLO_1_POLYTOPE_TPP
14
15#include <cmath>
16#include <algorithm> // For std::pow
17#include "../geometry.hpp"
18#include "integration_domain.hpp"
19#include <vector>
20
21namespace mc::domains {
22
23template <size_t dim>
24PolyTope<dim>::PolyTope(const std::vector<mc::geom::Point<dim>>& vertices,
25 const std::vector<std::array<double, dim>>& norms,
26 const std::vector<double>& offs)
27 : vec(vertices)
28 , normals(norms)
29 , offsets(offs)
30{
31 if (normals.size() != offsets.size()) {
32 throw std::runtime_error(
33 "PolyTope: normals and offsets must have the same size.");
34 }
35
36 if (vec.empty()) {
37 throw std::runtime_error(
38 "PolyTope: vertices list cannot be empty.");
39 }
40}
41
42template<size_t dim>
43mc::geom::Bounds<dim> PolyTope<dim>::getBounds() const{
44 mc::geom::Bounds<dim> bounds;
45 for (int i = 0; i < dim; ++i) {
46 double max = vec[0][i];
47 double min = vec[0][i];
48 for (mc::geom::Point<dim> p: vec) {
49 if (p[i] > max) max = p[i];
50 if (p[i] < min) min = p[i];
51 }
52 bounds[i] = std::make_pair(min, max);
53 }
54 return bounds;
55}
56
57template<size_t dim>
58double PolyTope<dim>::getBoxVolume() const {
59 mc::geom::Bounds<dim> bou = this->getBounds();
60 double vol = 1;
61 for (int i = 0; i <dim; ++i) {
62 vol *= (bou[i].second - bou[i].first);
63 }
64 return vol;
65}
66
67template<size_t dim>
68bool PolyTope<dim>::isInside(const mc::geom::Point<dim> &point) const {
69 const double tol = 1e-12; // Numerical tolerance for robust testing
70 for (size_t i = 0; i < normals.size(); ++i) {
71 double s = 0.0;
72 // Compute dot product: normal_i · point
73 for (size_t k = 0; k < dim; ++k)
74 s += normals[i][k] * point[k];
75
76 // Check half-space inequality: n_i · x + offset_i ≤ 0
77 if (s > offsets[i] + tol)
78 return false; // Point violates this facet constraint
79 }
80 return true;
81}
82
83} // namespace mc::domains
84
85#endif //MONTECARLO_1_POLYTOPE_TPP