containedmagnitude.hh 4.82 KB
Newer Older
1
2
3
4
5
6
7
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*****************************************************************************
 *   See the file COPYING for full copying permissions.                      *
 *                                                                           *
 *   This program is free software: you can redistribute it and/or modify    *
 *   it under the terms of the GNU General Public License as published by    *
Dennis Gläser's avatar
Dennis Gläser committed
8
 *   the Free Software Foundation, either version 3 of the License, or       *
9
10
11
12
13
14
15
16
17
18
19
20
 *   (at your option) any later version.                                     *
 *                                                                           *
 *   This program is distributed in the hope that it will be useful,         *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 *****************************************************************************/
/*!
 * \file
21
 * \ingroup Magnitude
22
23
24
25
26
27
28
29
30
31
 * \brief Contains functionality for computing the
 *        magnitude (length/area/volume) of the part
 *        of a geometry contained within another geometry.
 */
#ifndef FRACKIT_CONTAINED_MAGNITUDE_HH
#define FRACKIT_CONTAINED_MAGNITUDE_HH

#include <type_traits>

#include <frackit/occ/breputilities.hh>
32
33
34
35
36
37
#include <frackit/common/extractctype.hh>
#include <frackit/precision/defaultepsilon.hh>

#include <frackit/geometry/geometry.hh>
#include <frackit/geometryutilities/applyongeometry.hh>

38
39
40
41
42
#include "magnitude.hh"

namespace Frackit {

/*!
43
 * \ingroup Magnitude
44
45
46
47
48
 * \brief Returns the magnitude of zero-dimensional geometries (points)
 * \note The magnitude of points is always zero independent on if they
 *       are contained in the geometry or not.
 */
template<class Geometry, class Domain, std::enable_if_t<Geometry::myDimension() == 0, int> = 0>
49
50
51
typename CoordinateTypeTraits<Geometry>::type
computeContainedMagnitude(const Geometry& geometry,
                          const Domain& domain)
52
53
54
{ return 0.0; }

/*!
55
 * \ingroup Magnitude
56
57
58
59
 * \brief Returns the length of the part of a one-dimensional
 *        geometry that is contained in a domain geometry.
 */
template<class Geometry, class Domain, std::enable_if_t<Geometry::myDimension() == 1, int> = 0>
60
61
62
typename CoordinateTypeTraits<Geometry>::type
computeContainedMagnitude(const Geometry& geometry,
                          const Domain& domain)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{
    const auto geomShape = OCCUtilities::getShape(geometry);
    const auto domainShape = OCCUtilities::getShape(domain);
    const auto is = OCCUtilities::intersect(geomShape, domainShape, defaultEpsilon(domain));
    const auto isEdges = OCCUtilities::getEdges(is);

    if (isEdges.empty())
        return 0.0;

    typename Geometry::ctype size = 0.0;
    for (const auto& edge : isEdges)
        size += computeMagnitude(edge);
    return size;
}

/*!
79
 * \ingroup Magnitude
80
81
82
83
 * \brief Returns the surface area of the part of a two-dimensional
 *        geometry that is contained in a domain geometry.
 */
template<class Geometry, class Domain, std::enable_if_t<Geometry::myDimension() == 2, int> = 0>
84
85
86
typename CoordinateTypeTraits<Geometry>::type
computeContainedMagnitude(const Geometry& geometry,
                          const Domain& domain)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{
    const auto geomShape = OCCUtilities::getShape(geometry);
    const auto domainShape = OCCUtilities::getShape(domain);
    const auto is = OCCUtilities::intersect(geomShape, domainShape, defaultEpsilon(domain));
    const auto isFaces = OCCUtilities::getFaces(is);

    if (isFaces.empty())
        return 0.0;

    typename Geometry::ctype size = 0.0;
    for (const auto& face : isFaces)
        size += computeMagnitude(face,
                                 defaultEpsilon(domain),
                                 OCCUtilities::point(geometry.center()));
    return size;
}

104
/*!
105
106
107
 * \ingroup Magnitude
 * \brief Returns the magnitude of that part of a
 *        geometry that is contained in a domamin.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
 */
template<class Domain>
typename CoordinateTypeTraits<Domain>::type
computeContainedMagnitude(std::shared_ptr<Geometry> geometry,
                          const Domain& domain)
{
    using ctype = typename CoordinateTypeTraits<Domain>::type;

    // lambda to evaluate the contained magnitude
    auto doComputation = [&] (const auto& geometry) -> ctype
    { return computeContainedMagnitude(geometry, domain); };

    return applyOnGeometry(doComputation, geometry);
}

123
124
125
} // end namespace Frackit

#endif // FRACKIT_CONTAINED_MAGNITUDE_HH