deal.II version GIT relicensing-6960-g70e9c5b7be 2026-10-22 20:10:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
function_signed_distance.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2021 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
16
17#include <algorithm>
18
20
21namespace Functions
22{
23 namespace SignedDistance
24 {
25 template <int dim>
26 Sphere<dim>::Sphere(const Point<dim> &center, const double radius)
27 : center(center)
28 , radius(radius)
29 {
30 Assert(radius > 0, ExcMessage("Radius must be positive."));
31 }
32
33
34
35 template <int dim>
36 double
38 const unsigned int component) const
39 {
40 AssertIndexRange(component, this->n_components);
41
42 return point.distance(center) - radius;
43 }
44
45
46
47 template <int dim>
50 const unsigned int component) const
51 {
52 AssertIndexRange(component, this->n_components);
53
54 const Tensor<1, dim> center_to_point = point - center;
55 const Tensor<1, dim> grad = center_to_point / center_to_point.norm();
56 return grad;
57 }
58
59
60
61 template <int dim>
64 const unsigned int component) const
65 {
66 AssertIndexRange(component, this->n_components);
67
68 const Tensor<1, dim> center_to_point = point - center;
69 const double distance = center_to_point.norm();
70
71 const SymmetricTensor<2, dim> hess =
72 unit_symmetric_tensor<dim>() / distance -
73 symmetrize(outer_product(center_to_point, center_to_point)) /
74 Utilities::fixed_power<3>(distance);
75
76 return hess;
77 }
78
79
80
81 template <int dim>
82 Plane<dim>::Plane(const Point<dim> &point, const Tensor<1, dim> &normal)
83 : point_in_plane(point)
84 , normal(normal)
85 {
86 Assert(normal.norm() > 0, ExcMessage("Plane normal must not be 0."));
87 }
88
89
90
91 template <int dim>
92 double
94 const unsigned int component) const
95 {
96 AssertIndexRange(component, this->n_components);
97
98 return normal * (point - point_in_plane);
99 }
100
101
102
103 template <int dim>
105 Plane<dim>::gradient(const Point<dim> &, const unsigned int component) const
106 {
107 AssertIndexRange(component, this->n_components);
108
109 return normal;
110 }
111
112
113
114 template <int dim>
116 Plane<dim>::hessian(const Point<dim> &, const unsigned int component) const
117 {
118 AssertIndexRange(component, this->n_components);
119
121 }
122
123
124
125 template <int dim>
127 const std::array<double, dim> &radii,
128 const double tolerance,
129 const unsigned int max_iter)
130 : center(center)
131 , radii(radii)
132 , tolerance(tolerance)
133 , max_iter(max_iter)
134 {
135 for (unsigned int d = 0; d < dim; ++d)
136 Assert(radii[d] > 0, ExcMessage("All radii must be positive."));
137 }
138
139
140
141 template <int dim>
142 double
144 const unsigned int component) const
145 {
146 AssertIndexRange(component, this->n_components);
147
148 if (dim == 1)
149 return point.distance(center) - radii[0];
150 else if (dim == 2)
151 return compute_signed_distance_ellipse(point);
152 else
154
155 return 0.0;
156 }
157
158
159
160 template <int dim>
163 const unsigned int component) const
164 {
165 AssertIndexRange(component, this->n_components);
166
167 Tensor<1, dim> grad;
168 if (dim == 1)
169 grad = point - center;
170 else if (dim == 2)
171 {
172 const Point<dim> point_in_centered_coordinate_system =
173 Point<dim>(compute_closest_point_ellipse(point) - center);
174 grad = compute_analyical_normal_vector_on_ellipse(
175 point_in_centered_coordinate_system);
176 }
177 else
179
180 if (grad.norm() > 1e-12)
181 return grad / grad.norm();
182 else
183 return grad;
184 }
185
186
187
188 template <int dim>
189 double
191 {
192 double val = 0.0;
193 for (unsigned int d = 0; d < dim; ++d)
194 val += Utilities::fixed_power<2>((point[d] - center[d]) / radii[d]);
195 return val - 1.0;
196 }
197
198
199
200 template <int dim>
203 {
204 AssertDimension(dim, 2);
205
206 /*
207 * Function to compute the closest point on an ellipse (adopted from
208 * https://wet-robots.ghost.io/simple-method-for-distance-to-ellipse/ and
209 * https://github.com/0xfaded/ellipse_demo):
210 *
211 * Since the ellipse is symmetric to the two major axes through its
212 * center, the point is moved so the center coincides with the origin and
213 * into the first quadrant.
214 * 1. Choose a point on the ellipse (x), here x = a*cos(pi/4) and y =
215 * b*sin(pi/4).
216 * 2. Find second point on the ellipse, that has the same distance.
217 * 3. Find midpoint on the ellipse (must be closer).
218 * 4. Repeat 2.-4. until convergence.
219 */
220 // get equivalent point in first quadrant of centered ellipse
221 const double px = std::abs(point[0] - center[0]);
222 const double py = std::abs(point[1] - center[1]);
223 const double sign_px = std::copysign(1.0, point[0] - center[0]);
224 const double sign_py = std::copysign(1.0, point[1] - center[1]);
225 // get semi axes radii
226 const double &a = radii[0];
227 const double &b = radii[1];
228 // initial guess (t = angle from x-axis)
229 double t = numbers::PI_4;
230 double x = a * std::cos(t);
231 double y = b * std::sin(t);
232
233 unsigned int iter = 0;
234 double delta_t;
235 do
236 {
237 // compute the ellipse evolute (center of curvature) for the current t
238 const double ex =
239 (a * a - b * b) * Utilities::fixed_power<3>(std::cos(t)) / a;
240 const double ey =
241 (b * b - a * a) * Utilities::fixed_power<3>(std::sin(t)) / b;
242 // compute distances from current point on ellipse to its evolute
243 const double rx = x - ex;
244 const double ry = y - ey;
245 // compute distances from point to the current evolute
246 const double qx = px - ex;
247 const double qy = py - ey;
248 // compute the curvature radius at the current point on the ellipse
249 const double r = std::hypot(rx, ry);
250 // compute the distance from evolute to the point
251 const double q = std::hypot(qx, qy);
252 // compute step size on ellipse
253 const double delta_c = r * std::asin((rx * qy - ry * qx) / (r * q));
254 // compute approximate angle step
255 delta_t = delta_c / std::sqrt(a * a + b * b - x * x - y * y);
256 t += delta_t;
257 // make sure the angle stays in first quadrant
258 t = std::clamp(t, 0.0, numbers::PI_2);
259 x = a * std::cos(t);
260 y = b * std::sin(t);
261 ++iter;
262 }
263 while (std::abs(delta_t) > tolerance && iter < max_iter);
264 AssertIndexRange(iter, max_iter);
265
268
269 return center + Point<dim>(sign_px * x, sign_py * y);
270 }
271
272
273
274 template <int dim>
282
283
284
285 template <>
288 const Point<2> &point) const
289 {
290 const auto &a = radii[0];
291 const auto &b = radii[1];
292 const auto &x = point[0];
293 const auto &y = point[1];
294 return Tensor<1, 2, double>({b * x / a, a * y / b});
295 }
296
297
298
299 template <int dim>
300 double
302 {
304 return 0;
305 }
306
307
308
309 template <>
310 double
312 {
313 // point corresponds to center
314 if (point.distance(center) < tolerance)
315 return *std::min_element(radii.begin(), radii.end()) * -1.;
316
317 const Point<2> &closest_point = compute_closest_point_ellipse(point);
318
319 const double distance =
320 std::hypot(closest_point[0] - point[0], closest_point[1] - point[1]);
321
322 return evaluate_ellipsoid(point) < 0.0 ? -distance : distance;
323 }
324
325
326
327 template <int dim>
329 const Point<dim> &top_right)
330 : bounding_box({bottom_left, top_right})
331 {}
332
333
334
335 template <int dim>
337 : bounding_box(bounding_box)
338 {}
339
340
341
342 template <int dim>
343 double
345 const unsigned int component) const
346 {
347 AssertDimension(component, 0);
348
349 return bounding_box.signed_distance(p);
350 }
351
352
353
354 template <int dim>
356 const double radius,
357 const double notch_width,
358 const double notch_height)
359 : sphere(center, radius)
360 , notch(// bottom left
361 (dim == 1) ?
362 Point<dim>(center[0] - 0.5 * notch_width) :
363 (dim == 2) ?
364 Point<dim>(center[0] - 0.5 * notch_width,
365 std::numeric_limits<double>::lowest()) : /* notch is open in negative y-direction*/
366 Point<dim>(center[0] - 0.5 * notch_width,
367 std::numeric_limits<
368 double>::lowest() /* notch is open in negative y-direction*/,
369 std::numeric_limits<double>::lowest()), /* notch is open in negative z-direction*/
370 // top right
371 (dim == 1) ?
372 Point<dim>(center[0] + 0.5 * notch_width) :
373 (dim == 2) ?
374 Point<dim>(center[0] + 0.5 * notch_width,
375 center[1] + notch_height - radius) :
376 Point<dim>(center[0] + 0.5 * notch_width,
377 std::numeric_limits<
378 double>::max() /* notch is open in y-direction*/,
379 center[2] + notch_height - radius))
380 {
381 Assert(
382 notch_width <= 2 * radius,
384 "The width of the notch must be less than the circle diameter."));
385 Assert(
386 notch_height <= 2 * radius,
388 "The height of the notch must be less than the circle diameter."));
389 }
390
391
392
393 template <int dim>
394 double
396 const unsigned int component) const
397 {
398 AssertDimension(component, 0);
399
400 // calculate the set difference between the level set functions of the
401 // sphere and the notch
402 return std::max(sphere.value(p), -notch.value(p));
403 }
404
405
406 template <int dim>
408 const double radius,
409 const Tensor<1, dim> &axis_direction,
410 const Point<dim> &axis_point)
411 : Function<dim>()
412 , x0(axis_point)
413 , axis(axis_direction / axis_direction.norm())
414 , R(radius)
415 {
416 AssertThrow(radius > 0.0,
417 ExcMessage("Cylinder radius must be positive."));
418
419 const double norm = axis.norm();
420
421 AssertThrow(std::abs(norm - 1.0) < 1e-12,
422 ExcMessage("Axis vector must be normalized (||axis|| = 1)."));
423 }
424
425
426 template <int dim>
427 double
429 const unsigned int) const
430 {
431 const Tensor<1, dim> q = point - x0;
432 const double s = q * axis;
433 const Tensor<1, dim> q_perp = q - s * axis;
434
435 return q_perp.norm() - R;
436 }
437
438
439 template <int dim>
442 const unsigned int) const
443 {
444 const Tensor<1, dim> q = point - x0;
445 const double s = q * axis;
446 const Tensor<1, dim> q_perp = q - s * axis;
447
448 const double r = q_perp.norm();
449
450 return q_perp / r;
451 }
452
453 } // namespace SignedDistance
454} // namespace Functions
455
456#include "base/function_signed_distance.inst"
457
*  const Number radius
double value(const Point< dim > &point, const unsigned int component=0) const override
Tensor< 1, dim > gradient(const Point< dim > &, const unsigned int component=0) const override
double compute_signed_distance_ellipse(const Point< dim > &point) const
Point< dim > compute_closest_point_ellipse(const Point< dim > &point) const
Ellipsoid(const Point< dim > &center, const std::array< double, dim > &radii, const double tolerance=1e-14, const unsigned int max_iter=10)
double evaluate_ellipsoid(const Point< dim > &point) const
Tensor< 1, dim, double > compute_analyical_normal_vector_on_ellipse(const Point< dim > &point) const
Tensor< 1, dim > gradient(const Point< dim > &point, const unsigned int component=0) const override
double value(const Point< dim > &point, const unsigned int component=0) const override
InfiniteCylinder(const double radius, const Tensor< 1, dim > &axis_direction=[] { Tensor< 1, dim > a;a[dim - 1]=1.0;return a;}(), const Point< dim > &axis_point=Point< dim >())
Plane(const Point< dim > &point, const Tensor< 1, dim > &normal)
double value(const Point< dim > &point, const unsigned int component=0) const override
SymmetricTensor< 2, dim > hessian(const Point< dim > &, const unsigned int component=0) const override
Tensor< 1, dim > gradient(const Point< dim > &, const unsigned int component=0) const override
double value(const Point< dim > &p, const unsigned int component=0) const override
Rectangle(const Point< dim > &bottom_left, const Point< dim > &top_right)
Sphere(const Point< dim > &center=Point< dim >(), const double radius=1)
SymmetricTensor< 2, dim > hessian(const Point< dim > &point, const unsigned int component=0) const override
double value(const Point< dim > &point, const unsigned int component=0) const override
Tensor< 1, dim > gradient(const Point< dim > &point, const unsigned int component=0) const override
ZalesakDisk(const Point< dim > &center, const double radius, const double notch_width, const double notch_height)
double value(const Point< dim > &p, const unsigned int component=0) const override
Definition point.h:111
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
numbers::NumberTraits< Number >::real_type norm() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_NOT_IMPLEMENTED()
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
constexpr double PI_2
Definition numbers.h:245
constexpr double PI_4
Definition numbers.h:250
STL namespace.
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > cos(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
inline ::VectorizedArray< Number, width > asin(const ::VectorizedArray< Number, width > &x)
constexpr SymmetricTensor< 2, dim, Number > symmetrize(const Tensor< 2, dim, Number > &t)
constexpr SymmetricTensor< 4, dim, Number > outer_product(const SymmetricTensor< 2, dim, Number > &t1, const SymmetricTensor< 2, dim, Number > &t2)