version 3.8
makegeometry.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_GEOMETRY_MAKE_GEOMETRY_HH
13#define DUMUX_GEOMETRY_MAKE_GEOMETRY_HH
14
15#include <vector>
16#include <array>
17#include <limits>
18#include <dune/common/fvector.hh>
19#include <dune/common/fmatrix.hh>
20#include <dune/common/exceptions.hh>
21#include <dune/geometry/multilineargeometry.hh>
22#include <dumux/common/math.hh>
24
25namespace Dumux {
26
31template<class CoordScalar>
32bool pointsAreCoplanar(const std::vector<Dune::FieldVector<CoordScalar, 3>>& points, const CoordScalar scale)
33{
34 if (points.size() != 4)
35 DUNE_THROW(Dune::InvalidStateException, "Check only works for 4 points!");
36
37 // (see "Real-Time Collision Detection" by Christer Ericson)
38 Dune::FieldMatrix<CoordScalar, 4, 4> M;
39 for (int i = 0; i < 3; ++i )
40 M[i] = {points[0][i], points[1][i], points[2][i], points[3][i]};
41 M[3] = {1.0*scale, 1.0*scale, 1.0*scale, 1.0*scale};
42
43 using std::abs;
44 return abs(M.determinant()) < 1.5e-7*scale*scale*scale*scale;
45}
46
51template<class CoordScalar>
52bool pointsAreCoplanar(const std::vector<Dune::FieldVector<CoordScalar, 3>>& points)
53{
54 Dune::FieldVector<CoordScalar, 3> bBoxMin(std::numeric_limits<CoordScalar>::max());
55 Dune::FieldVector<CoordScalar, 3> bBoxMax(std::numeric_limits<CoordScalar>::lowest());
56 for (const auto& p : points)
57 {
58 for (int i=0; i<3; i++)
59 {
60 using std::min;
61 using std::max;
62 bBoxMin[i] = min(bBoxMin[i], p[i]);
63 bBoxMax[i] = max(bBoxMax[i], p[i]);
64 }
65 }
66
67 const auto size = (bBoxMax - bBoxMin).two_norm();
68
69 return pointsAreCoplanar(points, size);
70}
71
79template<class CoordScalar>
80std::vector<Dune::FieldVector<CoordScalar, 3>> getReorderedPoints(const std::vector<Dune::FieldVector<CoordScalar, 3>>& points)
81{
82 std::array<int, 4> tmp;
83 return getReorderedPoints(points, tmp);
84}
85
93template<class CoordScalar>
94std::vector<Dune::FieldVector<CoordScalar, 3>> getReorderedPoints(const std::vector<Dune::FieldVector<CoordScalar, 3>>& points,
95 std::array<int, 4>& orientations)
96{
97 if(points.size() == 4)
98 {
99 auto& p0 = points[0];
100 auto& p1 = points[1];
101 auto& p2 = points[2];
102 auto& p3 = points[3];
103
104 // check if the points define a proper quadrilateral
105 const auto normal = crossProduct((p1 - p0), (p2 - p0));
106
107 orientations = { getOrientation(p0, p3, p2, normal),
108 getOrientation(p0, p3, p1, normal),
109 getOrientation(p2, p1, p0, normal),
110 getOrientation(p2, p1, p3, normal) };
111
112
113 // check if the points follow the dune ordering (see http://www.dcs.gla.ac.uk/~pat/52233/slides/Geometry1x1.pdf)
114 const bool diagonalsIntersect = (orientations[0] != orientations[1]) && (orientations[2] != orientations[3]);
115
116 // the points conform with the dune ordering
117 if(diagonalsIntersect)
118 return points;
119
120 // the points do not conform with the dune ordering, re-order
121 using GlobalPosition = Dune::FieldVector<CoordScalar, 3>;
122 if(!diagonalsIntersect && orientations[0] == 1)
123 return std::vector<GlobalPosition>{p1, p0, p2, p3};
124 else if(!diagonalsIntersect && orientations[0] == -1)
125 return std::vector<GlobalPosition>{p3, p1, p0, p2};
126 else
127 DUNE_THROW(Dune::InvalidStateException, "Could not reorder points");
128 }
129 else
130 DUNE_THROW(Dune::NotImplemented, "Reorder for " << points.size() << " points.");
131}
132
141template<class CoordScalar, bool enableSanityCheck = true>
142auto makeDuneQuadrilaterial(const std::vector<Dune::FieldVector<CoordScalar, 3>>& points)
143{
144 if (points.size() != 4)
145 DUNE_THROW(Dune::InvalidStateException, "A quadrilateral needs 4 corner points!");
146
147 using GlobalPosition = Dune::FieldVector<CoordScalar, 3>;
148 static constexpr auto coordDim = GlobalPosition::dimension;
149 static constexpr auto dim = coordDim-1;
150 using GeometryType = Dune::MultiLinearGeometry<CoordScalar, dim, coordDim>;
151
152 // if no sanity check if desired, use the given points directly to construct the geometry
153 if (!enableSanityCheck)
154 return GeometryType(Dune::GeometryTypes::quadrilateral, points);
155
156 // compute size
157 Dune::FieldVector<CoordScalar, 3> bBoxMin(std::numeric_limits<CoordScalar>::max());
158 Dune::FieldVector<CoordScalar, 3> bBoxMax(std::numeric_limits<CoordScalar>::lowest());
159 for (const auto& p : points)
160 {
161 for (int i = 0; i < 3; i++)
162 {
163 using std::min;
164 using std::max;
165 bBoxMin[i] = min(bBoxMin[i], p[i]);
166 bBoxMax[i] = max(bBoxMax[i], p[i]);
167 }
168 }
169
170 const auto size = (bBoxMax - bBoxMin).two_norm();
171
172 // otherwise, perform a number of checks and corrections
173 if (!pointsAreCoplanar(points, size))
174 DUNE_THROW(Dune::InvalidStateException, "Points do not lie within a plane");
175
176 auto corners = grahamConvexHull<2>(points);
177 if (corners.size() != 4)
178 DUNE_THROW(Dune::InvalidStateException, "Points do not span a strictly convex polygon!");
179
180 // make sure points conform with dune ordering
181 std::array<int, 4> orientations;
182 corners = getReorderedPoints(corners, orientations);
183
184 if (std::any_of(orientations.begin(), orientations.end(), [](auto i){ return i == 0; }))
185 DUNE_THROW(Dune::InvalidStateException, "More than two points lie on the same line.");
186
187 const auto quadrilateral = GeometryType(Dune::GeometryTypes::quadrilateral, corners);
188
189 const auto eps = 1e-7;
190 if (quadrilateral.volume() < eps*size*size)
191 DUNE_THROW(Dune::InvalidStateException, "Something went wrong, geometry has area of zero");
192
193 return quadrilateral;
194}
195
196
197
198} // end namespace Dumux
199
200#endif
A function to compute the convex hull of a point cloud.
Dune::FieldVector< Scalar, 3 > crossProduct(const Dune::FieldVector< Scalar, 3 > &vec1, const Dune::FieldVector< Scalar, 3 > &vec2)
Cross product of two vectors in three-dimensional Euclidean space.
Definition: math.hh:642
auto makeDuneQuadrilaterial(const std::vector< Dune::FieldVector< CoordScalar, 3 > > &points)
Creates a dune quadrilateral geometry given 4 corner points.
Definition: makegeometry.hh:142
int getOrientation(const Dune::FieldVector< ctype, 3 > &a, const Dune::FieldVector< ctype, 3 > &b, const Dune::FieldVector< ctype, 3 > &c, const Dune::FieldVector< ctype, 3 > &normal)
Returns the orientation of a sequence a-->b-->c in one plane (defined by normal vector)
Definition: grahamconvexhull.hh:35
Vector normal(const Vector &v)
Create a vector normal to the given one (v is expected to be non-zero)
Definition: normal.hh:26
std::vector< Dune::FieldVector< CoordScalar, 3 > > getReorderedPoints(const std::vector< Dune::FieldVector< CoordScalar, 3 > > &points)
Returns a vector of points following the dune ordering. Convenience method that creates a temporary o...
Definition: makegeometry.hh:80
bool pointsAreCoplanar(const std::vector< Dune::FieldVector< CoordScalar, 3 > > &points, const CoordScalar scale)
Checks if four points lie within the same plane.
Definition: makegeometry.hh:32
Define some often used mathematical functions.
Definition: adapt.hh:17