Nektar++
Loading...
Searching...
No Matches
GeometryLocator.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: GeometryLocator.h
4//
5// For more information, please see: http://www.nektar.info/
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Derived state used only for inverse mapping (x -> xi) and
32// geometry classification.
33//
34////////////////////////////////////////////////////////////////////////////////
35
36#ifndef NEKTAR_SPATIALDOMAINS_GEOMETRYLOCATOR_H
37#define NEKTAR_SPATIALDOMAINS_GEOMETRYLOCATOR_H
38
42
43#include <array>
44#include <limits>
45#include <memory>
46
48{
49
50class Geometry;
51
52/// Extents bounded across every supported shape: three coordinate directions,
53/// eight isoparametric coefficients (the trilinear hexahedron) and four
54/// vertices (a quadrilateral). Nothing here needs a runtime-sized container.
55constexpr int kMaxGeomDim = 3;
56constexpr int kMaxGeomIsoCoeff = 8;
57constexpr int kMaxGeomVerts = 4;
58
59/**
60 * @brief The isoparametric description of a shape's \f$\chi\f$ mapping.
61 *
62 * This is precisely what a Geometry knows how to compute about itself, and
63 * everything the inverse mapping needs from it. Keeping it separate from
64 * GeometryLocator means classification -- which is exactly the test of whether
65 * the nonlinear coefficients vanish -- can be answered with a stack-resident
66 * temporary and no dependency on the locator at all.
67 *
68 * @see Geometry::CalcGeomType
69 */
71{
72 /// Coefficients per coordinate direction. Only the leading @p m_nCoeff
73 /// entries of each row are meaningful, and that count identifies the
74 /// shape: 3 triangle, 4 quadrilateral or tetrahedron, 5 pyramid, 6 prism,
75 /// 8 hexahedron.
76 int m_nCoeff = 0;
77 std::array<std::array<NekDouble, kMaxGeomIsoCoeff>, kMaxGeomDim> m_coeff{};
78
79 /// 2D only: coordinate directions spanning the manifold.
80 std::array<int, kMaxGeomDim> m_manifold{};
81};
82
83/**
84 * @brief Inverts the mapping \f$\chi:\xi\mapsto x\f$ for one geometry.
85 *
86 * Point location -- deciding whether an element contains a Cartesian
87 * coordinate, recovering the local coordinate that maps to it, or measuring
88 * the distance to it -- is an algorithm applied to a geometry, not a property
89 * of one. For anything but an affine element it is a nonlinear solve, and it
90 * needs a cache of derived quantities to be quick. Neither the algorithm nor
91 * the cache belongs on Geometry, so both live here and are owned by whoever
92 * does the searching.
93 *
94 * Build one with GeometryLocator::Create() and keep it for as long as the
95 * geometry is unchanged; it must not outlive the geometry it refers to. A
96 * locator is invalidated by anything that moves or re-curves the geometry,
97 * which is to say by any of the Geometry Reset entry points.
98 *
99 * The concrete algorithms differ by the dimension of the shape rather than by
100 * the shape itself, which is why the subclasses are per-dimension.
101 */
103{
104public:
107
108 /// Build the locator appropriate to @p geom's dimension, with its cache
109 /// already populated.
110 SPATIAL_DOMAINS_EXPORT static std::unique_ptr<GeometryLocator> Create(
111 Geometry *geom);
112
113 /// Populate the cache. Called by Create() once the concrete type exists,
114 /// since it dispatches virtually.
116
118 {
119 return m_geom;
120 }
121
122 //---------------------------------------
123 // Point lookups
124 //---------------------------------------
125 SPATIAL_DOMAINS_EXPORT std::array<NekDouble, 6> GetBoundingBox();
127
129 const Array<OneD, const NekDouble> &gloCoord, NekDouble tol = 0.0);
131 const Array<OneD, const NekDouble> &gloCoord,
132 Array<OneD, NekDouble> &locCoord, NekDouble tol);
134 const Array<OneD, const NekDouble> &gloCoord,
135 Array<OneD, NekDouble> &locCoord, NekDouble tol, NekDouble &dist);
136
139 Array<OneD, NekDouble> &Lcoords);
142
144 const Array<OneD, const NekDouble> &gloCoord, NekDouble &edgeDist);
146 const Array<OneD, const NekDouble> &gloCoord);
147 /// Clamp local coordinates into the reference element
148 /// \f$[-1-tol,1+tol]^{dim}\f$, returning whether anything was clamped.
149 /// Depends only on the reference element, so it needs no locator.
151 Array<OneD, NekDouble> &locCoord, int dim,
152 NekDouble tol = std::numeric_limits<NekDouble>::epsilon());
153
154protected:
155 /// The geometry being searched. Not owned.
157
158 /// The shape's isoparametric coefficients. The straight-edged 2D solve
159 /// rewrites them into a factored form, so the locator keeps its own copy
160 /// rather than sharing the geometry's.
162
163 /// Non-zero if every \f$\chi\f$ basis is linear. Bits 1 and 2 are used
164 /// by the 2D straight-edged solve to record which axes were swapped.
166
167 /// Inverse of the linear part of @p m_iso, for regular elements.
168 std::array<std::array<NekDouble, kMaxGeomDim>, kMaxGeomDim> m_invIsoParam{};
169
170 /// Whether the Newton iteration of the last GetLocCoords call converged,
171 /// i.e. whether its local coordinates map to the point. Set only by
172 /// elements located by Newton iteration; read by ContainsPoint.
173 bool m_newtonConverged = false;
174
175 /// Element bounding box as (xmin, ymin, zmin, xmax, ymax, zmax).
176 bool m_hasBoundingBox = false;
177 std::array<NekDouble, 6> m_boundingBox{};
178
179 /// 2D only: inward edge normals used to reject points outside
180 /// straight-sided elements. A curved edge gets no normal, so
181 /// @p m_hasEdgeNormal records which entries are usable.
182 bool m_edgeNormalBuilt = false;
183 std::array<bool, kMaxGeomVerts> m_hasEdgeNormal{};
184 std::array<std::array<NekDouble, 2>, kMaxGeomVerts> m_edgeNormal{};
185
187 Array<OneD, NekDouble> &Lcoords);
190 virtual int v_AllLeftCheck(const Array<OneD, const NekDouble> &gloCoord,
191 NekDouble &edgeDist);
192 virtual void v_CalculateInverseIsoParam();
193 virtual void v_PreSolveStraightEdge();
194};
195
196typedef std::unique_ptr<GeometryLocator> GeometryLocatorUniquePtr;
197
198/// Inverse mapping for a segment.
200{
201public:
203
204protected:
206 Array<OneD, NekDouble> &Lcoords) override;
208 Array<OneD, NekDouble> &xi) override;
209};
210
211/// Inverse mapping for a triangle or quadrilateral, including the closed-form
212/// solve available when every edge is straight.
214{
215public:
217
218protected:
220 Array<OneD, NekDouble> &Lcoords) override;
222 Array<OneD, NekDouble> &xi) override;
224 NekDouble &edgeDist) override;
225 void v_CalculateInverseIsoParam() override;
226 void v_PreSolveStraightEdge() override;
227
228private:
230 Array<OneD, NekDouble> &Lcoords);
234 Array<OneD, NekDouble> &Lcoords,
235 NekDouble &dist);
236};
237
238/// Inverse mapping for a tetrahedron, pyramid, prism or hexahedron.
240{
241public:
243
244protected:
246 Array<OneD, NekDouble> &Lcoords) override;
247 void v_CalculateInverseIsoParam() override;
248
249private:
251 Array<OneD, NekDouble> &Lcoords,
252 NekDouble &dist);
257 Array<OneD, NekDouble> &Lcoords,
258 NekDouble &dist);
259};
260
261} // namespace Nektar::SpatialDomains
262
263#endif
#define SPATIAL_DOMAINS_EXPORT
NekDouble v_FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi) override
NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords) override
Inverse mapping for a triangle or quadrilateral, including the closed-form solve available when every...
int v_AllLeftCheck(const Array< OneD, const NekDouble > &gloCoord, NekDouble &edgeDist) override
NekDouble v_FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi) override
void NewtonIterationForLocCoord(const Array< OneD, const NekDouble > &coords, const Array< OneD, const NekDouble > &ptsx, const Array< OneD, const NekDouble > &ptsy, Array< OneD, NekDouble > &Lcoords, NekDouble &dist)
NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords) override
void SolveStraightEdgeQuad(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
Inverse mapping for a tetrahedron, pyramid, prism or hexahedron.
void NewtonIterationForLocCoord(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords, NekDouble &dist)
NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords) override
Inverts the mapping for one geometry.
int PreliminaryCheck(const Array< OneD, const NekDouble > &gloCoord, NekDouble &edgeDist)
A fast and robust check if a given global coord is outside of a deformed element. For regular element...
bool m_edgeNormalBuilt
2D only: inward edge normals used to reject points outside straight-sided elements....
virtual NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
IsoParam m_iso
The shape's isoparametric coefficients. The straight-edged 2D solve rewrites them into a factored for...
NekDouble FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi)
Geometry * m_geom
The geometry being searched. Not owned.
std::array< std::array< NekDouble, 2 >, kMaxGeomVerts > m_edgeNormal
static std::unique_ptr< GeometryLocator > Create(Geometry *geom)
Build the locator appropriate to geom's dimension, with its cache already populated.
std::array< NekDouble, 6 > GetBoundingBox()
Generates the bounding box for the element.
bool ContainsPoint(const Array< OneD, const NekDouble > &gloCoord, NekDouble tol=0.0)
Determine whether the element contains a Cartesian coordinate.
NekDouble GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
Determine the local collapsed coordinates corresponding to a given Cartesian coordinate.
bool MinMaxCheck(const Array< OneD, const NekDouble > &gloCoord)
Check if given global coord is within the BoundingBox of the element.
void Build()
Populate the cache. Called by Create() once the concrete type exists, since it dispatches virtually.
std::array< NekDouble, 6 > m_boundingBox
int m_straightEdge
Non-zero if every basis is linear. Bits 1 and 2 are used by the 2D straight-edged solve to record wh...
std::array< std::array< NekDouble, kMaxGeomDim >, kMaxGeomDim > m_invIsoParam
Inverse of the linear part of m_iso, for regular elements.
virtual NekDouble v_FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi)
static bool ClampLocCoords(Array< OneD, NekDouble > &locCoord, int dim, NekDouble tol=std::numeric_limits< NekDouble >::epsilon())
Clamp local coordinates into the reference element , returning whether anything was clamped....
bool m_hasBoundingBox
Element bounding box as (xmin, ymin, zmin, xmax, ymax, zmax).
std::array< bool, kMaxGeomVerts > m_hasEdgeNormal
virtual int v_AllLeftCheck(const Array< OneD, const NekDouble > &gloCoord, NekDouble &edgeDist)
bool m_newtonConverged
Whether the Newton iteration of the last GetLocCoords call converged, i.e. whether its local coordina...
constexpr int kMaxGeomIsoCoeff
constexpr int kMaxGeomVerts
constexpr int kMaxGeomDim
Extents bounded across every supported shape: three coordinate directions, eight isoparametric coeffi...
std::unique_ptr< GeometryLocator > GeometryLocatorUniquePtr
The isoparametric description of a shape's mapping.
std::array< int, kMaxGeomDim > m_manifold
2D only: coordinate directions spanning the manifold.
int m_nCoeff
Coefficients per coordinate direction. Only the leading m_nCoeff entries of each row are meaningful,...
std::array< std::array< NekDouble, kMaxGeomIsoCoeff >, kMaxGeomDim > m_coeff