FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
distance_function.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/analytic/static_wrapper.hpp>
10#include <kernel/analytic/wrappers.hpp>
11#include <kernel/util/math.hpp>
12#include <kernel/geometry/cgal.hpp>
13
14namespace FEAT
15{
16 namespace Analytic
17 {
24 namespace Distance
25 {
42 template<int dim_, typename DataType_>
45 {
46 public:
47 static constexpr int domain_dim = dim_;
49 static constexpr bool can_value = true;
50 static constexpr bool can_grad = true;
51 static constexpr bool can_hess = true;
52
54
56 template<typename EvalTraits_>
57 class Evaluator :
58 public Analytic::Function::Evaluator<EvalTraits_>
59 {
60 public:
62 typedef typename EvalTraits_::DataType DataType;
64 typedef typename EvalTraits_::PointType PointType;
66 typedef typename EvalTraits_::ValueType ValueType;
68 typedef typename EvalTraits_::GradientType GradientType;
70 typedef typename EvalTraits_::HessianType HessianType;
71
72 private:
75
76 public:
78 explicit Evaluator(const DistanceFunction& function) :
79 _origin(function._origin)
80 {
81 }
82
83 ValueType value(const PointType& point) const
84 {
85 return (point - _origin).norm_euclid();
86 }
87
88 GradientType gradient(const PointType& point) const
89 {
90 DataType norm (this->value(point));
91
92 if(norm < Math::eps<DataType>())
93 return GradientType::null();
94
95 return (DataType(1) / norm) * (point - _origin);
96 }
97
98 HessianType hessian(const PointType& point) const
99 {
100 DataType norm (this->value(point));
101 if(norm < Math::eps<DataType>())
102 return HessianType::null();
103
105 norm = DataType(1)/norm;
106 DataType denom = Math::sqr(norm)*norm;
107
108 for(int i(0); i < dim_; ++i)
109 {
110 hess[i][i] = norm;
111 for(int j(0); j < dim_; ++j)
112 {
113 hess[i][j] -= ((point[i] - _origin[i]) * (point[j] - _origin[j]))*denom;
114 }
115 }
116 return hess;
117 }
118 }; // class DistanceFunction::Evaluator<...>
119
120 public:
123
124 public:
127 {
128 _origin.format();
129 }
130
132 explicit DistanceFunction(const PointType& origin) :
133 _origin(origin)
134 {
135 }
136
138 void set_point(const PointType& origin)
139 {
140 _origin = origin;
141 }
142 }; // class DistanceFunction
143
160 template<int dim_, typename DataType_>
162 public Analytic::Function
163 {
164 public:
165 static constexpr int domain_dim = dim_;
167
168 static constexpr bool can_value = true;
169 static constexpr bool can_grad = true;
170 static constexpr bool can_hess = true;
171
173
175 template<typename EvalTraits_>
176 class Evaluator :
177 public Analytic::Function::Evaluator<EvalTraits_>
178 {
179 public:
181 typedef typename EvalTraits_::DataType DataType;
183 typedef typename EvalTraits_::PointType PointType;
185 typedef typename EvalTraits_::ValueType ValueType;
187 typedef typename EvalTraits_::GradientType GradientType;
189 typedef typename EvalTraits_::HessianType HessianType;
190
191 private:
195 const DataType _a, _b;
196
197 public:
199 explicit Evaluator(const DistanceFunctionSD& function) :
200 _origin(function._origin),
201 _a(function._a),
202 _b(function._b)
203 {
204 }
205
206 ValueType value(const PointType& point) const
207 {
208 return _a + _b*(point - _origin).norm_euclid();
209 }
210
211 GradientType gradient(const PointType& point) const
212 {
213 DataType norm ((point - _origin).norm_euclid());
214 if(norm < Math::eps<DataType>())
215 return GradientType::null();
216
217 return (_b / norm) * (point - _origin);
218 }
219
220 HessianType hessian(const PointType& point) const
221 {
222 DataType norm ((point - _origin).norm_euclid());
223 if(norm <= Math::eps<DataType>())
224 return HessianType::null();
225
227
228 norm = DataType(1)/norm;
229 DataType denom = _b*Math::sqr(norm)*norm;
230
231 for(int i(0); i < dim_; ++i)
232 {
233 hess[i][i] = _b*norm;
234 for(int j(0); j < dim_; ++j)
235 {
236 hess[i][j] -= ((point[i] - _origin[i]) * (point[j] - _origin[j]))*denom;
237 }
238 }
239
240 return hess;
241 }
242 }; // class DistanceFunctionSD::Evaluator<...>
243
244 private:
248 DataType_ _a;
250 DataType_ _b;
251
252 public:
254 explicit DistanceFunctionSD(const PointType& origin, const DataType_ a_, const DataType_ b_) :
255 _origin(origin),
256 _a(a_),
257 _b(b_)
258 {
259 }
260
262 void set_point(const PointType& origin)
263 {
264 _origin = origin;
265 }
266 }; // class DistanceFunctionSD
267
284 template<int component_, int dim_, typename DataType_>
286 public Analytic::Function
287 {
288 public:
289 static constexpr int domain_dim = dim_;
291
292 static constexpr bool can_value = true;
293 static constexpr bool can_grad = true;
294 static constexpr bool can_hess = true;
295
297
299 template<typename EvalTraits_>
300 class Evaluator :
301 public Analytic::Function::Evaluator<EvalTraits_>
302 {
303 public:
305 typedef typename EvalTraits_::DataType DataType;
307 typedef typename EvalTraits_::PointType PointType;
309 typedef typename EvalTraits_::ValueType ValueType;
311 typedef typename EvalTraits_::GradientType GradientType;
313 typedef typename EvalTraits_::HessianType HessianType;
314
315 private:
319 const DataType_ _b;
320
321 public:
323 explicit Evaluator(const PlaneDistanceFunctionSD& function) :
324 _origin(function._origin),
325 _b(function._b)
326 {
327 }
328
329 ValueType value(const PointType& point) const
330 {
331 return _b * Math::abs(point[component_] - _origin[component_]);
332 }
333
334 GradientType gradient(const PointType& point) const
335 {
336 DataType norm = this->value(point);
337 if(norm < Math::eps<DataType>())
338 return GradientType::null();
339
341 grad.format();
342 grad[component_] = _b * Math::signum(point[component_] - _origin[component_]);
343 return grad;
344 }
345
346 HessianType hessian(const PointType& DOXY(point)) const
347 {
348 return HessianType::null();
349 }
350 }; // class PlaneDistanceFunctionSD::Evaluator<...>
351
352 private:
356 DataType_ _b;
357
358 public:
360 explicit PlaneDistanceFunctionSD(const PointType& origin, const DataType_ b_) :
361 _origin(origin),
362 _b(b_)
363 {
364 }
365
367 void set_point(const PointType& origin)
368 {
369 _origin = origin;
370 }
371 }; // class PlaneDistanceFunctionSD
372
389 template<int dim_, typename DataType_>
391 public Analytic::Function
392 {
393 public:
394 static constexpr int domain_dim = dim_;
396 static constexpr bool can_value = true;
397 static constexpr bool can_grad = true;
398 static constexpr bool can_hess = true;
399
401
403 template<typename EvalTraits_>
404 class Evaluator :
405 public Analytic::Function::Evaluator<EvalTraits_>
406 {
407 public:
409 typedef typename EvalTraits_::DataType DataType;
411 typedef typename EvalTraits_::PointType PointType;
413 typedef typename EvalTraits_::ValueType ValueType;
415 typedef typename EvalTraits_::GradientType GradientType;
417 typedef typename EvalTraits_::HessianType HessianType;
418
419 private:
422
423 public:
425 explicit Evaluator(const InverseDistanceFunction& function) :
426 _origin(function._origin)
427 {
428 }
429
430 ValueType value(const PointType& point) const
431 {
432 return DataType(1) / (point - _origin).norm_euclid();
433 }
434
435 GradientType gradient(const PointType& point) const
436 {
437 const DataType norm_sqr = (point - _origin).norm_euclid_sqr();
438
439 if(norm_sqr < Math::eps<DataType>())
440 return GradientType::null();
441
442 return (DataType(1) / Math::pow(norm_sqr, DataType(1.5))) * (_origin - point);
443 }
444
445 HessianType hessian(const PointType& point) const
446 {
447 const DataType norm_sqr = (point - _origin).norm_euclid_sqr();
448 if(norm_sqr < Math::eps<DataType>())
449 return HessianType::null();
450
451 const DataType denom = DataType(1) / Math::pow(norm_sqr, DataType(2.5));
453 for(int i(0); i < dim_; ++i)
454 {
455 for(int j(0); j < dim_; ++j)
456 {
457 if(i == j)
458 {
459 for(int k(0); k < dim_; ++k)
460 hess[i][j] += DataType(k == i ? 2 : -1) * Math::sqr(point[k] - _origin[k]) * denom;
461 }
462 else
463 {
464 hess[i][j] = DataType(3) * (point[i] - _origin[i]) * (point[j] - _origin[j]) * denom;
465 }
466 }
467 }
468 return hess;
469 }
470 }; // class InverseDistanceFunction::Evaluator<...>
471
472 public:
475
476 public:
479 {
480 _origin.format();
481 }
482
484 explicit InverseDistanceFunction(const PointType& origin) :
485 _origin(origin)
486 {
487 }
488
490 void set_point(const PointType& origin)
491 {
492 _origin = origin;
493 }
494 }; // class InverseDistanceFunction
495
496#ifdef FEAT_HAVE_CGAL
511 template<typename DT_>
512 class CGALDistanceFunction : public Analytic::Function
513 {
514 public:
516 typedef DT_ DataType;
518 typedef Analytic::Image::Scalar ImageType;
520 static constexpr int domain_dim = 3;
522 static constexpr bool can_value = true;
524 static constexpr bool can_grad = false;
526 static constexpr bool can_hess = false;
528 typedef Tiny::Vector<DT_, domain_dim> PointType;
529
530 private:
532 Geometry::CGALWrapper<DT_> _cgal_wrapper;
533 public:
534
544 explicit CGALDistanceFunction(std::istream& filestream_, Geometry::CGALFileMode filemode_) :
545 _cgal_wrapper(filestream_, filemode_)
546 {
547 }
548
550 template<typename EvalTraits_>
551 class Evaluator :
552 public Analytic::Function::Evaluator<EvalTraits_>
553 {
554 public:
556 typedef typename EvalTraits_::DataType DataType;
558 typedef typename EvalTraits_::PointType PointType;
560 typedef typename EvalTraits_::ValueType ValueType;
562 typedef typename EvalTraits_::GradientType GradientType;
564 typedef typename EvalTraits_::HessianType HessianType;
565
566 private:
568 typedef typename CGALDistanceFunction::DataType IDT_;
569 const Geometry::CGALWrapper<IDT_>* _cgal_wrapper;
570
571 public:
572 explicit Evaluator(const CGALDistanceFunction& function) :
573 _cgal_wrapper(&function._cgal_wrapper)
574 {
575 }
576
577 ValueType value(const PointType& point)
578 {
579 return ((_cgal_wrapper->point_inside(IDT_(point[0]), IDT_(point[1]), IDT_(point[2])) ? ValueType(1) : ValueType(-1)) *
580 ValueType(Math::sqrt(_cgal_wrapper->squared_distance(IDT_(point[0]), IDT_(point[1]), IDT_(point[2])))));
581 }
582 }; // class CGALDistFunc::Evaluator<...>
583 }; // class CGALDistFunc<...>
584#endif // FEAT_HAVE_CGAL
585 } // namespace Distance
586 } // namespace Analytic
587} // namespace FEAT
EvalTraits_::HessianType HessianType
hessian type
Evaluator(const DistanceFunction &function)
Constructor.
EvalTraits_::PointType PointType
evaluation point type
EvalTraits_::GradientType GradientType
gradient type
EvalTraits_::DataType DataType
coefficient data type
void set_point(const PointType &origin)
Sets point to x0
PointType _origin
Point to calculate the distance from.
DistanceFunction(const PointType &origin)
Constructor.
EvalTraits_::DataType DataType
coefficient data type
Evaluator(const DistanceFunctionSD &function)
Constructor.
const DataType _a
displacement and scaling factor
EvalTraits_::GradientType GradientType
gradient type
EvalTraits_::PointType PointType
evaluation point type
Scaled and displaced analytic distance function.
void set_point(const PointType &origin)
Sets point to x0
DistanceFunctionSD(const PointType &origin, const DataType_ a_, const DataType_ b_)
Constructor.
PointType _origin
The point to which the distance to is calculated.
DataType_ _a
Displacement of the function.
EvalTraits_::PointType PointType
evaluation point type
Evaluator(const InverseDistanceFunction &function)
Constructor.
void set_point(const PointType &origin)
Sets point to x0
PointType _origin
Point to calculate the distance from.
InverseDistanceFunction(const PointType &origin)
Constructor.
Evaluator(const PlaneDistanceFunctionSD &function)
Constructor.
EvalTraits_::PointType PointType
evaluation point type
Scaled and displaced analytic distance from i-th coordinate axis/plane function.
PointType _origin
The point to which the distance to is calculated.
void set_point(const PointType &origin)
Sets point to x0
PlaneDistanceFunctionSD(const PointType &origin, const DataType_ b_)
Constructor.
Analytic Function Evaluator base-class template.
Definition: function.hpp:157
Analytic Function interface.
Definition: function.hpp:112
Wrapper for the CGAL Library.
Definition: cgal.hpp:57
bool point_inside(DataType x, DataType y, DataType z) const
Check whether a point is inside the Polyhedron defined at objects' construction.
DataType squared_distance(DataType x, DataType y, DataType z) const
Returns the minimun squared distance between the query point and all input primitives defined at obje...
Tiny Vector class template.
CUDA_HOST_DEVICE void format(DataType alpha=DataType(0))
Formats the vector.
T_ abs(T_ x)
Returns the absolute value.
Definition: math.hpp:275
T_ pow(T_ x, T_ y)
Returns x raised to the power of y.
Definition: math.hpp:643
T_ sqr(T_ x)
Returns the square of a value.
Definition: math.hpp:95
T_ signum(T_ x)
Returns the sign of a value.
Definition: math.hpp:250
FEAT namespace.
Definition: adjactor.hpp:12
@ value
specifies whether the space should supply basis function values
@ hess
specifies whether the space should supply basis function hessians
@ grad
specifies whether the space should supply basis function gradients
Scalar Function Image tag class.
Definition: function.hpp:28