ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
shapes/include/shapes/Union.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "Shape.hpp"
23
24#include <algorithm>
25#include <limits>
26#include <memory>
27#include <stdexcept>
28#include <utility>
29#include <vector>
30
31namespace Shapes {
32
33class Union : public Shape {
34public:
35 bool contains(std::shared_ptr<Shapes::Shape> const &shape) const noexcept {
36 return std::ranges::find(m_shapes, shape) != m_shapes.end();
37 }
38
39 void add(std::shared_ptr<Shapes::Shape> const &shape) {
40 m_shapes.emplace_back(shape);
41 }
42
43 void remove(std::shared_ptr<Shapes::Shape> const &shape) {
44 std::erase(m_shapes, shape);
45 }
46
47 /**
48 * @brief Calculate the minimum of all distances and the corresponding
49 * distance vector for a given position and any contained shape.
50 * @param[in] pos Position from which to get the nearest distance.
51 * @param[out] dist Nearest distance to the shape. Negative if inside the
52 * shape, zero if in direct contact with the shape.
53 * @param[out] vec Vector to nearest point on the shape.
54 */
55 void calculate_dist(Utils::Vector3d const &pos, double &dist,
56 Utils::Vector3d &vec) const override {
57 auto dist_compare = [&pos](std::pair<double, Utils::Vector3d> const &res,
58 std::shared_ptr<Shapes::Shape> const &shape) {
59 auto const &old_dist = res.first;
60 double new_dist;
62 shape->calculate_dist(pos, new_dist, new_vec);
63 if (new_dist < 0.0)
64 throw std::domain_error(
65 "Distance to Union not well-defined for given position!");
66 if (new_dist < old_dist) {
67 return std::make_pair(new_dist, new_vec);
68 }
69 return res;
70 };
71 std::tie(dist, vec) =
72 std::accumulate(m_shapes.begin(), m_shapes.end(),
73 std::make_pair(std::numeric_limits<double>::infinity(),
76 }
77
78 bool is_inside(Utils::Vector3d const &pos) const override {
79 return std::ranges::any_of(
80 m_shapes, [&pos](auto const &shape) { return shape->is_inside(pos); });
81 }
82
83private:
84 std::vector<std::shared_ptr<Shapes::Shape>> m_shapes;
85};
86
87} // namespace Shapes
void calculate_dist(Utils::Vector3d const &pos, double &dist, Utils::Vector3d &vec) const override
Calculate the minimum of all distances and the corresponding distance vector for a given position and...
void add(std::shared_ptr< Shapes::Shape > const &shape)
bool contains(std::shared_ptr< Shapes::Shape > const &shape) const noexcept
void remove(std::shared_ptr< Shapes::Shape > const &shape)
bool is_inside(Utils::Vector3d const &pos) const override
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.