distancetoboundary.hh 10.4 KB
Newer Older
Dennis Gläser's avatar
Dennis Gläser committed
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       *
Dennis Gläser's avatar
Dennis Gläser committed
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 Distance
Dennis Gläser's avatar
Dennis Gläser committed
22
23
24
25
26
27
 * \brief Contains functionality for computing the distance
 *        of a geometry to the boundary of another geometry.
 */
#ifndef FRACKIT_DISTANCE_TO_BOUNDARY_HH
#define FRACKIT_DISTANCE_TO_BOUNDARY_HH

28
29
#include <cmath>
#include <limits>
30
#include <algorithm>
31

Dennis Gläser's avatar
Dennis Gläser committed
32
33
#include <Extrema_ExtAlgo.hxx>
#include <Extrema_ExtFlag.hxx>
34
#include <BRepTools.hxx>
Dennis Gläser's avatar
Dennis Gläser committed
35
36
37

#include <frackit/precision/precision.hh>
#include <frackit/geometry/disk.hh>
38
#include <frackit/geometry/quadrilateral.hh>
39
#include <frackit/geometry/cylindersurface.hh>
40
41

#include <frackit/geometryutilities/name.hh>
Dennis Gläser's avatar
Dennis Gläser committed
42
43
44
45
46
47

#include "distance.hh"

namespace Frackit {

/*!
48
 * \ingroup Distance
Dennis Gläser's avatar
Dennis Gläser committed
49
50
51
52
53
54
55
 * \brief Compute the distance of a geometry
 *        to the boundary of another geometry.
 * \param geo1 The first geometry
 * \param geo2 The second geometry
 * \param deflection The epsilon used in the BrepExtrema command
 * \param extFlag The flag passed to the BrepExtrema command (MIN/MAX/MINMAX)
 * \param extAlgo The algorithm passed to the BrepExtrema command (TREE/GRAD)
56
57
 * \note This is the default overload throwing an error. Overloads have to be
 *       implemented for pairs of geometries.
Dennis Gläser's avatar
Dennis Gläser committed
58
59
60
61
62
63
64
65
66
67
 */
template<class Geo1, class Geo2>
Impl::PCT<Geo1, Geo2>
computeDistanceToBoundary(const Geo1& geo1,
                          const Geo2& geo2,
                          Impl::PCT<Geo1, Geo2> deflection = Precision<Impl::PCT<Geo1, Geo2>>::confusion(),
                          Extrema_ExtFlag extFlag = Extrema_ExtFlag_MINMAX,
                          Extrema_ExtAlgo extAlgo = Extrema_ExtAlgo_Grad)
{
    std::string msg = "Distance to boundary computation not implemented for ";
68
    msg += "\"" + geometryName(geo1) + "\"";
Dennis Gläser's avatar
Dennis Gläser committed
69
    msg += " and ";
70
71
    msg += "\"" + geometryName(geo2)  + "\"";
    throw std::runtime_error(msg);
Dennis Gläser's avatar
Dennis Gläser committed
72
73
74
}

/*!
75
 * \ingroup Distance
Dennis Gläser's avatar
Dennis Gläser committed
76
77
78
79
80
81
82
83
84
85
86
87
 * \brief Compute the distance of a geometry
 *        to the bounding ellipse of a disk.
 * \param geo The geometry
 * \param disk The disk
 * \param deflection The epsilon used in the BrepExtrema command
 * \param extFlag The flag passed to the BrepExtrema command (MIN/MAX/MINMAX)
 * \param extAlgo The algorithm passed to the BrepExtrema command (TREE/GRAD)
 */
template<class Geo, class ctype>
Impl::PCT<Geo, Disk<ctype>>
computeDistanceToBoundary(const Geo& geo,
                          const Disk<ctype>& disk,
88
89
                          Impl::PCT<Geo, Disk<ctype>> deflection
                            = Precision<Impl::PCT<Geo, Disk<ctype>>>::confusion(),
Dennis Gläser's avatar
Dennis Gläser committed
90
91
                          Extrema_ExtFlag extFlag = Extrema_ExtFlag_MINMAX,
                          Extrema_ExtAlgo extAlgo = Extrema_ExtAlgo_Grad)
92
93
94
95
96
97
98
{
    return computeDistance(OCCUtilities::getShape(geo),
                           OCCUtilities::getShape(disk.boundingEllipse()),
                           deflection,
                           extFlag,
                           extAlgo);
}
Dennis Gläser's avatar
Dennis Gläser committed
99

100
/*!
101
 * \ingroup Distance
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
 * \brief Compute the distance of a point
 *        to the bounding of a quadrilateral.
 * \param p The point
 * \param quad The quadrilateral
 */
template<class ctype1, class ctype2>
PromotedType<ctype1, ctype2>
computeDistanceToBoundary(const Point<ctype1, 3>& p,
                          const Quadrilateral<ctype2, 3>& quad)
{
    using std::min;
    using ctype = PromotedType<ctype1, ctype2>;

    ctype distance = std::numeric_limits<ctype>::max();
    for (unsigned int i = 0; i < quad.numEdges(); ++i)
        distance = min(distance, computeDistance(quad.edge(i), p));
    return distance;
}

121
/*!
122
 * \ingroup Distance
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 * \brief Compute the distance of a geometry
 *        to the bounding circles of a cylinder surface.
 * \param geo The geometry
 * \param cylSurface The cylinder surface
 * \param deflection The epsilon used in the BrepExtrema command
 * \param extFlag The flag passed to the BrepExtrema command (MIN/MAX/MINMAX)
 * \param extAlgo The algorithm passed to the BrepExtrema command (TREE/GRAD)
 */
template<class Geo, class ctype>
Impl::PCT<Geo, CylinderSurface<ctype>>
computeDistanceToBoundary(const Geo& geo,
                          const CylinderSurface<ctype>& cylSurface,
                          Impl::PCT<Geo, CylinderSurface<ctype>> deflection
                            = Precision<Impl::PCT<Geo, CylinderSurface<ctype>>>::confusion(),
                          Extrema_ExtFlag extFlag = Extrema_ExtFlag_MINMAX,
                          Extrema_ExtAlgo extAlgo = Extrema_ExtAlgo_Grad)
{
    using std::min;
    return min(computeDistance(OCCUtilities::getShape(geo),
                               OCCUtilities::getShape(cylSurface.lowerBoundingCircle()),
                               deflection,
                               extFlag,
                               extAlgo),
               computeDistance(OCCUtilities::getShape(geo),
                               OCCUtilities::getShape(cylSurface.upperBoundingCircle()),
                               deflection,
                               extFlag,
                               extAlgo));
}

153
/*!
154
 * \ingroup Distance
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 * \brief Compute the distance of a shape
 *        to the bounding ellipse of a disk.
 * \param shape The shape
 * \param disk The disk
 * \param deflection The epsilon used in the BrepExtrema command
 * \param extFlag The flag passed to the BrepExtrema command (MIN/MAX/MINMAX)
 * \param extAlgo The algorithm passed to the BrepExtrema command (TREE/GRAD)
 */
template<class ctype>
ctype computeDistanceToBoundary(const TopoDS_Shape& shape,
                                const Disk<ctype>& disk,
                                ctype deflection = Precision<ctype>::confusion(),
                                Extrema_ExtFlag extFlag = Extrema_ExtFlag_MINMAX,
                                Extrema_ExtAlgo extAlgo = Extrema_ExtAlgo_Grad)
{
    return computeDistance(shape,
                           OCCUtilities::getShape(disk.boundingEllipse()),
                           deflection,
                           extFlag,
                           extAlgo);
}

/*!
178
 * \ingroup Distance
179
180
181
182
183
184
185
 * \brief Compute the distance of a shape
 *        to the boundary of a TopoDS_Face.
 * \param shape The shape
 * \param face The TopoDS_Face
 * \param deflection The epsilon used in the BrepExtrema command
 * \param extFlag The flag passed to the BrepExtrema command (MIN/MAX/MINMAX)
 * \param extAlgo The algorithm passed to the BrepExtrema command (TREE/GRAD)
Dennis Gläser's avatar
Dennis Gläser committed
186
187
 * \note This only computes the distance to the outer wire of the face.
 *       Thus, this assumes faces that do not have a whole inside.
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
 */
template<class ctype = double>
ctype computeDistanceToBoundary(const TopoDS_Shape& shape,
                                const TopoDS_Face& face,
                                ctype deflection = Precision<ctype>::confusion(),
                                Extrema_ExtFlag extFlag = Extrema_ExtFlag_MINMAX,
                                Extrema_ExtAlgo extAlgo = Extrema_ExtAlgo_Grad)
{
    return computeDistance(shape,
                           BRepTools::OuterWire(face),
                           deflection,
                           extFlag,
                           extAlgo);
}

203
/*!
204
 * \ingroup Distance
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
 * \brief Compute the distance of a shape
 *        to the bounding wire of a quadrilateral.
 * \param shape The shape
 * \param quad The quadrilateral
 * \param deflection The epsilon used in the BrepExtrema command
 * \param extFlag The flag passed to the BrepExtrema command (MIN/MAX/MINMAX)
 * \param extAlgo The algorithm passed to the BrepExtrema command (TREE/GRAD)
 */
template<class ctype>
ctype computeDistanceToBoundary(const TopoDS_Shape& shape,
                                const Quadrilateral<ctype, 3>& quad,
                                ctype deflection = Precision<ctype>::confusion(),
                                Extrema_ExtFlag extFlag = Extrema_ExtFlag_MINMAX,
                                Extrema_ExtAlgo extAlgo = Extrema_ExtAlgo_Grad)
{
    return computeDistance(shape,
                           OCCUtilities::getShape(quad),
                           deflection,
                           extFlag,
                           extAlgo);
}

227
/*!
228
 * \ingroup Distance
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
 * \brief Compute the distance of a geometry
 *        to the boundary of a TopoDS_Face.
 * \param geo The geometry
 * \param face The TopoDS_Face
 * \param deflection The epsilon used in the BrepExtrema command
 * \param extFlag The flag passed to the BrepExtrema command (MIN/MAX/MINMAX)
 * \param extAlgo The algorithm passed to the BrepExtrema command (TREE/GRAD)
 */
template<class Geo>
typename Geo::ctype
computeDistanceToBoundary(const Geo& geo,
                          const TopoDS_Face& face,
                          typename Geo::ctype deflection
                          = Precision<typename Geo::ctype>::confusion(),
                          Extrema_ExtFlag extFlag = Extrema_ExtFlag_MINMAX,
                          Extrema_ExtAlgo extAlgo = Extrema_ExtAlgo_Grad)
{
    return computeDistance(OCCUtilities::getShape(geo),
                           BRepTools::OuterWire(face),
                           deflection,
                           extFlag,
                           extAlgo);
}

Dennis Gläser's avatar
Dennis Gläser committed
253
254
255
} // end namespace Frackit

#endif // FRACKIT_DISTANCE_TO_BOUNDARY_HH