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-2022 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#ifndef SHAPES_UNION
21#define SHAPES_UNION
22
23#include "Shape.hpp"
24
25#include <algorithm>
26#include <limits>
27#include <memory>
28#include <stdexcept>
29#include <utility>
30#include <vector>
31
32namespace Shapes {
33
34class Union : public Shape {
35public:
36 bool contains(std::shared_ptr<Shapes::Shape> const &shape) const noexcept {
37 return std::ranges::find(m_shapes, shape) != m_shapes.end();
38 }
39
40 void add(std::shared_ptr<Shapes::Shape> const &shape) {
41 m_shapes.emplace_back(shape);
42 }
43
44 void remove(std::shared_ptr<Shapes::Shape> const &shape) {
45 std::erase(m_shapes, shape);
46 }
47
48 /**
49 * @brief Calculate the minimum of all distances and the corresponding
50 * distance vector for a given position and any contained shape.
51 * @param[in] pos Position from which to get the nearest distance.
52 * @param[out] dist Nearest distance to the shape. Negative if inside the
53 * shape, zero if in direct contact with the shape.
54 * @param[out] vec Vector to nearest point on the shape.
55 */
56 void calculate_dist(Utils::Vector3d const &pos, double &dist,
57 Utils::Vector3d &vec) const override {
58 auto dist_compare = [&pos](std::pair<double, Utils::Vector3d> const &res,
59 std::shared_ptr<Shapes::Shape> const &shape) {
60 auto const &old_dist = res.first;
61 double new_dist;
63 shape->calculate_dist(pos, new_dist, new_vec);
64 if (new_dist < 0.0)
65 throw std::domain_error(
66 "Distance to Union not well-defined for given position!");
67 if (new_dist < old_dist) {
68 return std::make_pair(new_dist, new_vec);
69 }
70 return res;
71 };
72 std::tie(dist, vec) =
73 std::accumulate(m_shapes.begin(), m_shapes.end(),
74 std::make_pair(std::numeric_limits<double>::infinity(),
77 }
78
79 bool is_inside(Utils::Vector3d const &pos) const override {
80 return std::ranges::any_of(
81 m_shapes, [&pos](auto const &shape) { return shape->is_inside(pos); });
82 }
83
84private:
85 std::vector<std::shared_ptr<Shapes::Shape>> m_shapes;
86};
87
88} // namespace Shapes
89
90#endif
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.