4#ifndef DUNE_FUFEM_GEOMETRY_POLYHEDRONDISTANCE_HH
5#define DUNE_FUFEM_GEOMETRY_POLYHEDRONDISTANCE_HH
14template <
class Coordinate,
class Matrix>
18 for (
size_t i = 0; i < nCorners; i++)
19 for (
size_t j = 0; j <= i; j++)
22 for (
size_t i = 0; i < nCorners; i++)
23 for (
size_t j = i + 1; j < nCorners; j++)
24 matrix[i][j] = matrix[j][i];
42template <
class Coordinate>
52 for (
size_t k = 0; k < nCorners; k++)
53 for (
size_t j = 0; j < nCorners; j++)
54 rhs[k] -= A[k][j] * sol[j];
58 for (
size_t j1 = 0; j1 < nCorners - 1; ++j1) {
59 for (
size_t j2 = j1 + 1; j2 < nCorners; ++j2) {
61 double a = A[j1][j1] - A[j1][j2] - A[j2][j1] + A[j2][j2];
62 double b =
rhs[j1] -
rhs[j2];
68 double const largestAlpha = sol[j2];
69 double const smallestAlpha = -sol[j1];
72 if (
alpha > largestAlpha)
74 else if (
alpha < smallestAlpha)
75 alpha = smallestAlpha;
79 double sum = sol[j1] + sol[j2];
81 double lValue = 0.5 * A[j1][j1] * sum * sum -
rhs[j1] * sum;
82 double uValue = 0.5 * A[j2][j2] * sum * sum -
rhs[j2] * sum;
84 alpha = lValue < uValue ? sol[j2] : -sol[j1];
91 for (
size_t p = 0; p < nCorners; p++)
92 rhs[p] -= (A[p][j1] - A[p][j2]) *
alpha;
98template <
class Coordinate>
101 double correctionTolerance,
102 size_t maxIterations = 1000) {
106 double const barycentricTolerance = 1e-14;
112 for (
size_t i = 0; i < nCorners; i++)
113 l[i] = target * segment.
vertices[i];
117 for (
size_t i = 0; i < nCorners; i++)
118 sol[i] = 1.0 / nCorners;
121 Coordinate newCoordinates;
123 for (
size_t iterations = 0; iterations < maxIterations; ++iterations) {
127 if ((oldCoordinates - newCoordinates).
two_norm() < correctionTolerance)
130 oldCoordinates = newCoordinates;
133 return (target - newCoordinates).two_norm();
137template <
class Coordinate>
140 double valueCorrectionTolerance,
141 size_t maxIterations = 1000) {
144 if (nCorners1 == 0 or nCorners2 == 0)
146 double const barycentricTolerance = 1e-14;
151 sol1[i] = 1.0 / nCorners1;
155 for (
size_t i = 0; i < nCorners2; i++)
156 sol2[i] = 1.0 / nCorners2;
159 double oldDistance = (c2 - c1).
two_norm();
167 for (
size_t iterations = 0; iterations < maxIterations; ++iterations) {
169 for (
size_t i = 0; i < nCorners2; i++)
176 for (
size_t i = 0; i < nCorners1; i++)
181 double const newDistance = (c2 - c1).
two_norm();
182 if (oldDistance - newDistance <= valueCorrectionTolerance) {
188 oldDistance = newDistance;
194template <
class Geometry>
195double distance(
typename Geometry::GlobalCoordinate
const &point,
196 Geometry
const &geo,
double valueCorrectionTolerance) {
197 using Coordinate =
typename Geometry::GlobalCoordinate;
200 polyhedron.vertices.resize(geo.corners());
201 for (
size_t i = 0; i < polyhedron.vertices.size(); ++i)
202 polyhedron.vertices[i] = geo.corner(i);
204 return distance(point, polyhedron, valueCorrectionTolerance);
208template <
class Geometry>
210 Geometry
const &geo,
double valueCorrectionTolerance,
211 size_t maxIterations = 1000) {
212 using Coordinate =
typename Geometry::GlobalCoordinate;
219 return distance(p2, p1, valueCorrectionTolerance, maxIterations);
real_type two_norm() const
void approximate(Coordinate const &target, Dune::DynamicMatrix< double > const &A, Dune::DynamicVector< double > const &l, ConvexPolyhedron< Coordinate > const &segment, Dune::DynamicVector< double > &sol)
Definition polyhedrondistance.hh:43
void populateMatrix(ConvexPolyhedron< Coordinate > const &segment, Matrix &matrix)
Definition polyhedrondistance.hh:15
double distance(const Coordinate &target, const ConvexPolyhedron< Coordinate > &segment, double correctionTolerance, size_t maxIterations=1000)
Definition polyhedrondistance.hh:99
Definition convexpolyhedron.hh:10
void sanityCheck(DynamicVector const &b, double tol) const
Definition convexpolyhedron.hh:24
std::vector< Coordinate > vertices
Definition convexpolyhedron.hh:11
Coordinate barycentricToGlobal(DynamicVector const &b) const
Definition convexpolyhedron.hh:14