OpenLB 1.8.1
Loading...
Searching...
No Matches
sdf.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2019 Adrian Kummerlaender
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef SDF_H
25#define SDF_H
26
27#include "core/vector.h"
28
29#include <algorithm>
30#include <functional>
31
32#ifndef M_PI
33#define M_PI 3.14159265358979323846
34#endif
35#ifndef M_PI2
36#define M_PI2 1.57079632679489661923
37#endif
38
39namespace olb {
40
41namespace sdf {
42
43template <typename T>
44T mix(T a, T b, T h) any_platform
45{
46 return b * (1.0 - h) + a * h;
47}
48
49template <typename T0, typename T1, typename T2>
50decltype(T0 {} * T1 {} * T2 {}) clamp(T0 x, T1 a, T2 b) any_platform
51{
52 if (x < a) {
53 return a;
54 }
55 else if (x > b) {
56 return b;
57 }
58 else {
59 return x;
60 }
61}
62
64template <typename T>
65Vector<bool, 3> isSymmetric(std::function<T(const Vector<T, 3>&)> sdf,
67{
69
70 isSymmetric[0] = sdf(Vector<T, 3> {1., 1., 1.} + center) ==
71 sdf(Vector<T, 3> {-1., 1., 1.} + center) &&
72 sdf(Vector<T, 3> {1., -1., 1.} + center) ==
73 sdf(Vector<T, 3> {-1., -1., 1.} + center) &&
74 sdf(Vector<T, 3> {1., 1., -1.} + center) ==
75 sdf(Vector<T, 3> {-1., 1., -1.} + center) &&
76 sdf(Vector<T, 3> {1., -1., -1.} + center) ==
77 sdf(Vector<T, 3> {-1., -1., -1.} + center);
78 isSymmetric[1] = sdf(Vector<T, 3> {1., 1., 1.} + center) ==
79 sdf(Vector<T, 3> {1., -1., 1.} + center) &&
80 sdf(Vector<T, 3> {-1., 1., 1.} + center) ==
81 sdf(Vector<T, 3> {-1., -1., 1.} + center) &&
82 sdf(Vector<T, 3> {1., 1., -1.} + center) ==
83 sdf(Vector<T, 3> {1., -1., -1.} + center) &&
84 sdf(Vector<T, 3> {-1., 1., -1.} + center) ==
85 sdf(Vector<T, 3> {-1., -1., -1.} + center);
86 isSymmetric[2] = sdf(Vector<T, 3> {1., 1., 1.} + center) ==
87 sdf(Vector<T, 3> {1., 1., -1.} + center) &&
88 sdf(Vector<T, 3> {-1., 1., 1.} + center) ==
89 sdf(Vector<T, 3> {-1., 1., -1.} + center) &&
90 sdf(Vector<T, 3> {1., -1., 1.} + center) ==
91 sdf(Vector<T, 3> {1., -1., -1.} + center) &&
92 sdf(Vector<T, 3> {-1., -1., 1.} + center) ==
93 sdf(Vector<T, 3> {-1., -1., -1.} + center);
94
95 return isSymmetric;
96}
97
103template <typename T, unsigned D>
105{
106 return norm(p) - r;
107}
108
114template <typename T>
116{
117 Vector<T, 2> q = abs(p) - b;
118 return norm(maxv(q, Vector<T, 2>(0.0))) +
119 util::min(util::max({q[0], q[1]}), T());
120}
121
127template <typename T>
129{
130 Vector<T, 3> q = abs(p) - b;
131 return norm(maxv(q, Vector<T, 3>(0.0))) +
132 util::min(util::max({q[0], q[1], q[2]}), T());
133}
134
135template <typename T, unsigned D>
137{
138 const auto pa = p - a;
139 const auto ba = b - a;
140 const auto h = clamp((pa*ba)/(ba*ba), T{0}, T{1});
141 return norm(pa - ba*h) - r;
142}
143
151template <typename T>
154{
155 Vector<T, 2> e0 = b - a;
156 Vector<T, 2> v0 = p - a;
157 Vector<T, 2> e1 = c - b;
158 Vector<T, 2> v1 = p - b;
159 Vector<T, 2> e2 = a - c;
160 Vector<T, 2> v2 = p - c;
161 Vector<T, 2> pq0 = v0 - e0 * clamp((v0 * e0) / (e0 * e0), 0., 1.);
162 Vector<T, 2> pq1 = v1 - e1 * clamp((v1 * e1) / (e1 * e1), 0., 1.);
163 Vector<T, 2> pq2 = v2 - e2 * clamp((v2 * e2) / (e2 * e2), 0., 1.);
164 T s = util::sign(e0[0] * e2[1] - e0[1] * e2[0]);
165
166 T dx = util::min(util::min((pq0 * pq0), (pq1 * pq1)), (pq2 * pq2));
167 T dy = util::min(util::min(s * (v0[0] * e0[1] - v0[1] * e0[0]),
168 s * (v1[0] * e1[1] - v1[1] * e1[0])),
169 s * (v2[0] * e2[1] - v2[1] * e2[0]));
170 return -util::sqrt(dx) * util::sign(dy);
171}
172
180template <typename T>
182{
183 auto ba = b - a; auto pa = p - a;
184 auto cb = c - b; auto pb = p - b;
185 auto ac = a - c; auto pc = p - c;
186 auto nor = crossProduct(ba, ac);
187
188 auto x1 = ba*clamp((ba*pa)/(ba*ba), T{0}, T{1}) - pa;
189 auto x2 = cb*clamp((cb*pb)/(cb*cb), T{0}, T{1}) - pb;
190 auto x3 = ac*clamp((ac*pc)/(ac*ac), T{0}, T{1}) - pc;
191
192 return util::sqrt(
193 (util::sign(crossProduct(ba,nor) * pa) +
194 util::sign(crossProduct(cb,nor) * pb) +
195 util::sign(crossProduct(ac,nor) * pc) < T{2})
196 ?
197 util::min(util::min((x1*x1),
198 (x2*x2)),
199 (x3*x3))
200 :
201 (nor*pa)*(nor*pa)/(nor*nor));
202}
203
212template <typename T>
214{
215 const Vector<T, 3> pa = p - a;
216 const T paba = pa * ba;
217 const T x = norm(pa * baba - ba * paba) - r * baba;
218 const T y = util::abs(paba - baba * T {0.5}) - baba * T {0.5};
219 const T x2 = x * x;
220 const T y2 = y * y * baba;
221 const T d = (util::max(x, y) < T {0})
222 ? -util::min(x2, y2)
223 : (((x > T {0}) ? x2 : T {0}) + ((y > T {0}) ? y2 : T {0}));
224 return util::sign(d) * util::sqrt(util::abs(d)) / baba;
225}
226
234template <typename T>
236{
237 Vector<T, 3> ba = b - a;
238 T baba = ba * ba;
239 return sdf::cylinder(p, a, ba, baba, r);
240}
241
249template <typename T>
251 T rb) any_platform
252{
253 T rba = rb - ra;
254 T papa = (p - a) * (p - a);
255 T paba = (p - a) * (ba) / baba;
256 // x distance to the axis of the cone
257 // note: abs() prevents from negative values, e.g. caused by rounding errors
258 T x = util::sqrt(util::abs(papa - paba * paba * baba));
259 // cax and cay for the distances to the caps
260 T cax = util::max(0.0, x - ((paba < 0.5) ? ra : rb));
261 T cay = util::abs(paba - 0.5) - 0.5;
262 // cbx and cby for the distance to the side wall
263 T k = rba * rba + baba;
264 T f = clamp((rba * (x - ra) + paba * baba) / k, 0.0, 1.0);
265 T cbx = x - ra - f * rba;
266 T cby = paba - f;
267 T s = (cbx < 0.0 && cay < 0.0) ? -1.0 : 1.0;
268 return s * util::sqrt(util::min(cax * cax + cay * cay * baba,
269 cbx * cbx + cby * cby * baba));
270}
271
280template <typename T>
282{
283 Vector<T, 3> ba = b - a;
284 T baba = ba * ba;
285 return sdf::cone(p, a, ba, baba, ra, rb);
286}
287
292template <typename T>
294{
295 const Vector<T, 3> a(p[0] / r[0], p[1] / r[1], p[2] / r[2]);
296 T k0 = norm(a);
297 const Vector<T, 3> r2(r[0] * r[0], r[1] * r[1], r[2] * r[2]);
298 const Vector<T, 3> b(p[0] / r2[0], p[1] / r2[1], p[2] / r2[2]);
299 T k1 = norm(b);
300 return (k0 < 1.0) ? (k0 - 1.0) * util::min(util::min(r[0], r[1]), r[2])
301 : k0 * (k0 - 1.0) / k1;
302}
303
310template <typename T>
312{
313 Vector<T, 2> b {p[0], p[2]};
314 Vector<T, 2> q {norm(b) - t[0], p[1]};
315 return norm(q) - t[1];
316}
317
325template <typename T>
327{
328 Vector<T, 2> q {norm(Vector<T, 2> {p[0], p[2]}), p[1]};
329 T l = norm(q) - r;
330 T m = norm(q - c * clamp(q * c, 0.0, r));
331 return util::max(l, m * util::sign(c[1] * q[0] - c[0] * q[1]));
332}
333
338template <typename T, unsigned D>
340{
341 return p - origin;
342}
343
344template <typename T>
346{
347 return {p[1], p[0], p[2]};
348}
349
356template <typename T>
358{
359 return util::max(-a, b);
360}
361
368template <typename T>
370{
371 return util::min(a, b);
372}
373
380template <typename T>
382{
383 return util::max(a, b);
384}
385
386template <typename T>
388{
389 T h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
390 return mix(a, b, h) - k * h * (1.0 - h);
391}
392
393template <typename T>
395{
396 T h = clamp(0.5 - 0.5 * (b + a) / k, 0.0, 1.0);
397 return mix(b, -a, h) + k * h * (1.0 - h);
398}
399
400template <typename T>
402{
403 T h = clamp(0.5 - 0.5 * (b - a) / k, 0.0, 1.0);
404 return mix(b, a, h) + k * h * (1.0 - h);
405}
406
411template <typename T>
413{
414 return a - r;
415}
416
425template <typename T, bool symmetryCheck = true>
426T elongation(std::function<T(const Vector<T, 3>&)> sdf, const Vector<T, 3>& p,
427 const Vector<T, 3>& h,
428 const Vector<T, 3>& center = (T(0))) any_platform
429{
430 Vector<T, 3> q = abs(p - center) - h;
431 Vector<T, 3> p_elong = p - center;
432 // elongation requires symmetry e.g. for elongation in x --> plane of symmetry = YZ-plane
433 Vector<bool, 3> sdfIsSymmetric;
434 if constexpr (symmetryCheck) {
435 sdfIsSymmetric = isSymmetric(sdf, center);
436 }
437
438 if (h[0] != 0) {
439 p_elong[0] = util::max(q[0], 0.);
440 if constexpr (symmetryCheck) {
441 if (!sdfIsSymmetric[0]) {
442 std::cout << "Warning: symmetry in x is not met" << std::endl;
443 }
444 }
445 }
446
447 if (h[1] != 0) {
448 p_elong[1] = util::max(q[1], 0.);
449 if constexpr (symmetryCheck) {
450 if (!sdfIsSymmetric[1]) {
451 std::cout << "Warning: symmetry in y is not met" << std::endl;
452 }
453 }
454 }
455
456 if (h[2] != 0) {
457 p_elong[2] = util::max(q[2], 0.);
458 if constexpr (symmetryCheck) {
459 if (!sdfIsSymmetric[2]) {
460 std::cout << "Warning: symmetry in z is not met" << std::endl;
461 }
462 }
463 }
464
465 // Second term needed in case of 3D-Elongation if all parts of Vector q are negative
466 return sdf(p_elong + center) + util::min(util::max({q[0], q[1], q[2]}), 0.);
467}
468
476template <typename T, unsigned D>
477T scale(std::function<T(const Vector<T, D>&)> sdf, const Vector<T, D>& p, T s,
478 const Vector<T, D>& center = (T(0))) any_platform
479{
480 return sdf((p - center) / s) * s;
481}
482
484// TODO: Rename function
485template <typename T>
487{
488 const T d = signedDist + .5 * eps;
489 return util::pow(util::cos(M_PI2 * d / eps), 2);
490}
491
492template <typename T>
493bool evalSolidVolumeFraction(T output[], T signedDist, T eps) any_platform
494{
495 T const halfEps = .5 * eps;
496 if (signedDist <= -halfEps) {
497 output[0] = 1.;
498 return true;
499 }
500 else if (signedDist < halfEps) {
501 output[0] = signedDistanceToPorosity(signedDist, eps);
502 return true;
503 }
504 output[0] = 0.;
505 return false;
506}
507
508} // namespace sdf
509
510} // namespace olb
511
512#endif
Plain old scalar vector.
#define M_PI2
T rounding(T a, T r) any_platform
Computes a layer of a constant thickness around the surface.
Definition sdf.h:412
T torus(Vector< T, 3 > p, Vector< T, 2 > t) any_platform
Exact signed distance to the surface of a torus placed in the XZ-plane of the coordinate system.
Definition sdf.h:311
Vector< T, 3 > flip(Vector< T, 3 > p) any_platform
Definition sdf.h:345
T signedDistanceToPorosity(T signedDist, T eps) any_platform
Converts signed distance to values for the smooth epsilon boundary.
Definition sdf.h:486
T intersection(T a, T b) any_platform
Volume which is shared by a and b creates a new object.
Definition sdf.h:381
T triangle(Vector< T, 2 > p, Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c) any_platform
Exact signed distance to the surface of two-dimensional triangle.
Definition sdf.h:152
T sphere(Vector< T, D > p, T r) any_platform
Exact signed distance to the surface of circle (in 2D) or sphere (in 3D).
Definition sdf.h:104
T elongation(std::function< T(const Vector< T, 3 > &)> sdf, const Vector< T, 3 > &p, const Vector< T, 3 > &h, const Vector< T, 3 > &center=(T(0))) any_platform
Elongation splits the object in 2 (4 or 8) parts, moves them apart and connects them again The object...
Definition sdf.h:426
T scale(std::function< T(const Vector< T, D > &)> sdf, const Vector< T, D > &p, T s, const Vector< T, D > &center=(T(0))) any_platform
Function to scale a geometry The object has to be placed in the origin of the coodinate system.
Definition sdf.h:477
T unify(T a, T b) any_platform
Volume of a and volume of b are combined as a new object.
Definition sdf.h:369
Vector< T, D > translate(Vector< T, D > p, Vector< T, D > origin) any_platform
Translation: The output of this function is used for the calculation of the signed distance to transl...
Definition sdf.h:339
T mix(T a, T b, T h) any_platform
Definition sdf.h:44
bool evalSolidVolumeFraction(T output[], T signedDist, T eps) any_platform
Definition sdf.h:493
T smooth_subtraction(T a, T b, T k) any_platform
Definition sdf.h:394
T line(Vector< T, D > p, Vector< T, D > a, Vector< T, D > b, T r) any_platform
Definition sdf.h:136
T cone(Vector< T, 3 > p, Vector< T, 3 > a, Vector< T, 3 > ba, T baba, T ra, T rb) any_platform
Exact signed distance to the surface of three-dimensional (capped) cone.
Definition sdf.h:250
T smooth_intersection(T a, T b, T k) any_platform
Definition sdf.h:401
T box(Vector< T, 2 > p, Vector< T, 2 > b) any_platform
Exact signed distance to the surface of two-dimensional cuboid.
Definition sdf.h:115
T ellipsoid(Vector< T, 3 > p, Vector< T, 3 > r) any_platform
Calculate the NOT EXACT (!) signed distance to the surface of three-dimensional ellipsoid.
Definition sdf.h:293
T cylinder(Vector< T, 3 > p, Vector< T, 3 > a, Vector< T, 3 > ba, T baba, T r) any_platform
Exact signed distance to the surface of three-dimensional cylinder.
Definition sdf.h:213
Vector< bool, 3 > isSymmetric(std::function< T(const Vector< T, 3 > &)> sdf, Vector< T, 3 > center) any_platform
A rough test for symmetry.
Definition sdf.h:65
T solidAngle(Vector< T, 3 > p, Vector< T, 2 > c, T r) any_platform
Exact signed distance to the surface of a solid angle which represents a combination of a cone (axis ...
Definition sdf.h:326
T subtraction(T a, T b) any_platform
Volume of a is subtracted from b.
Definition sdf.h:357
decltype(T0 {} *T1 {} *T2 {}) clamp(T0 x, T1 a, T2 b) any_platform
Definition sdf.h:50
T smooth_union(T a, T b, T k) any_platform
Definition sdf.h:387
ADf< T, DIM > abs(const ADf< T, DIM > &a)
Definition aDiff.h:1019
Expr sqrt(Expr x)
Definition expr.cpp:225
Expr min(Expr a, Expr b)
Definition expr.cpp:249
Expr max(Expr a, Expr b)
Definition expr.cpp:245
int sign(T val) any_platform
Definition util.h:53
Expr pow(Expr base, Expr exp)
Definition expr.cpp:235
ADf< T, DIM > cos(const ADf< T, DIM > &a)
Definition aDiff.h:578
Top level namespace for all of OpenLB.
constexpr Vector< T, D > maxv(const ScalarVector< T, D, IMPL > &v, const ScalarVector< T, D, IMPL_ > &w)
Definition vector.h:457
std::enable_if_t< std::is_arithmetic< T >::type::value, T > abs(T x) any_platform
Definition util.h:464
auto crossProduct(const ScalarVector< T, D, IMPL > &a, const ScalarVector< T, D, IMPL_ > &b) any_platform
Definition vector.h:274
constexpr T norm(const ScalarVector< T, D, IMPL > &a) any_platform
Euclidean vector norm.
#define any_platform
Define preprocessor macros for device-side functions, constant storage.
Definition platform.h:77
efficient implementation of a vector class