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::find(m_shapes.begin(), m_shapes.end(), 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 m_shapes.erase(std::remove(m_shapes.begin(), m_shapes.end(), shape),
46 m_shapes.end());
47 }
48
49 /**
50 * @brief Calculate the minimum of all distances and the corresponding
51 * distance vector for a given position and any contained shape.
52 * @param[in] pos Position from which to get the nearest distance.
53 * @param[out] dist Nearest distance to the shape. Negative if inside the
54 * shape, zero if in direct contact with the shape.
55 * @param[out] vec Vector to nearest point on the shape.
56 */
57 void calculate_dist(Utils::Vector3d const &pos, double &dist,
58 Utils::Vector3d &vec) const override {
59 auto dist_compare = [&pos](std::pair<double, Utils::Vector3d> const &res,
60 std::shared_ptr<Shapes::Shape> const &shape) {
61 double d;
63 shape->calculate_dist(pos, d, vec);
64 if (d < 0.0)
65 throw std::domain_error(
66 "Distance to Union not well-defined for given position!");
67 if (d < res.first) {
68 return std::make_pair(d, 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(),
76 dist_compare);
77 }
78
79 bool is_inside(Utils::Vector3d const &pos) const override {
80 return std::any_of(
81 m_shapes.begin(), m_shapes.end(),
82 [&pos](auto const &shape) { return shape->is_inside(pos); });
83 }
84
85private:
86 std::vector<std::shared_ptr<Shapes::Shape>> m_shapes;
87};
88
89} // namespace Shapes
90
91#endif
__shared__ int pos[MAXDEPTH *THREADS5/WARPSIZE]
__shared__ float res[]
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