|
| 1 | +// The libMesh Finite Element Library. |
| 2 | +// Copyright (C) 2002-2022 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner |
| 3 | + |
| 4 | +// This library is free software; you can redistribute it and/or |
| 5 | +// modify it under the terms of the GNU Lesser General Public |
| 6 | +// License as published by the Free Software Foundation; either |
| 7 | +// version 2.1 of the License, or (at your option) any later version. |
| 8 | + |
| 9 | +// This library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | +// Lesser General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU Lesser General Public |
| 15 | +// License along with this library; if not, write to the Free Software |
| 16 | +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +#ifndef LIBMESH_ELEMEXTREMA_H |
| 21 | +#define LIBMESH_ELEMEXTREMA_H |
| 22 | + |
| 23 | +#include "libmesh/libmesh_common.h" |
| 24 | + |
| 25 | +#if LIBMESH_DIM > 1 |
| 26 | + |
| 27 | +#include "libmesh/elem.h" |
| 28 | + |
| 29 | +#include <utility> // std::pair |
| 30 | + |
| 31 | +namespace libMesh |
| 32 | +{ |
| 33 | + |
| 34 | +/** |
| 35 | + * The \p ElemExtrema is a helper class for defining whether or not |
| 36 | + * _something_ is at an element vertex or edge (an _extrema_). |
| 37 | + */ |
| 38 | +class ElemExtrema : public std::pair<unsigned short, unsigned short> |
| 39 | +{ |
| 40 | +public: |
| 41 | + /** |
| 42 | + * Default constructor: sets entires to invalid (not at vertex or edge) |
| 43 | + */ |
| 44 | + ElemExtrema() |
| 45 | + : std::pair<unsigned short, unsigned short>(Elem::invalid_vertex, |
| 46 | + Elem::invalid_vertex) { } |
| 47 | + |
| 48 | + /** |
| 49 | + * Constructor with given vertex indices \p v1 and \p v2. |
| 50 | + */ |
| 51 | + ElemExtrema(const unsigned short v1, const unsigned short v2) |
| 52 | + : std::pair<unsigned short, unsigned short>(v1, v2) { } |
| 53 | + |
| 54 | + /** |
| 55 | + * @returns true if at the extrema (edge or vertex) |
| 56 | + */ |
| 57 | + bool at_extrema() const { return first != Elem::invalid_vertex; } |
| 58 | + |
| 59 | + /** |
| 60 | + * @returns true if the vertex index data is invalid |
| 61 | + */ |
| 62 | + bool is_invalid() const |
| 63 | + { return first == Elem::invalid_vertex && second == Elem::invalid_vertex; } |
| 64 | + |
| 65 | + /** |
| 66 | + * @returns true if at a vertex |
| 67 | + */ |
| 68 | + bool at_vertex() const |
| 69 | + { return first != Elem::invalid_vertex && second == Elem::invalid_vertex; } |
| 70 | + /** |
| 71 | + * @returns true if at vertex \p v |
| 72 | + */ |
| 73 | + bool at_vertex(const unsigned short v) const |
| 74 | + { return first == v && second == Elem::invalid_vertex; } |
| 75 | + |
| 76 | + /** |
| 77 | + * Invalidates the current state |
| 78 | + */ |
| 79 | + void invalidate() |
| 80 | + { |
| 81 | + first = Elem::invalid_vertex; |
| 82 | + second = Elem::invalid_vertex; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @returns The vertex ID when at a vertex |
| 87 | + */ |
| 88 | + unsigned short vertex() const |
| 89 | + { libmesh_assert(at_vertex()); return first; } |
| 90 | + |
| 91 | + /** |
| 92 | + * Prints the current state (at edge, at vertex, not at either) |
| 93 | + */ |
| 94 | + std::string print() const; |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets the "at vertex" state |
| 98 | + */ |
| 99 | + void set_vertex(const unsigned short v) |
| 100 | + { |
| 101 | + libmesh_assert_not_equal_to(v, Elem::invalid_vertex); |
| 102 | + first = v; |
| 103 | + second = Elem::invalid_vertex; |
| 104 | + } |
| 105 | + |
| 106 | +#if LIBMESH_DIM > 2 |
| 107 | + /** |
| 108 | + * @returns true if at an edge |
| 109 | + */ |
| 110 | + bool at_edge() const |
| 111 | + { return first != Elem::invalid_vertex && second != Elem::invalid_vertex; } |
| 112 | + /** |
| 113 | + * @returns true if at the edge defined by vertices \p v1 and \p v2 |
| 114 | + */ |
| 115 | + bool at_edge(const unsigned short v1, const unsigned short v2) const |
| 116 | + { |
| 117 | + libmesh_assert_not_equal_to(v1, Elem::invalid_vertex); |
| 118 | + libmesh_assert_not_equal_to(v2, Elem::invalid_vertex); |
| 119 | + return (first == v1 && second == v2) || (first == v2 && second == v1); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @returns The vertices that contain the edge when at an edge |
| 124 | + */ |
| 125 | + const std::pair<unsigned short, unsigned short> & edge_vertices() const |
| 126 | + { libmesh_assert(at_edge()); return *this; } |
| 127 | + |
| 128 | + /** |
| 129 | + * Sets the "at edge" state |
| 130 | + */ |
| 131 | + void set_edge(const unsigned short v1, const unsigned short v2) |
| 132 | + { |
| 133 | + libmesh_assert_not_equal_to(v1, Elem::invalid_vertex); |
| 134 | + libmesh_assert_not_equal_to(v2, Elem::invalid_vertex); |
| 135 | + first = v1; |
| 136 | + second = v2; |
| 137 | + } |
| 138 | + /** |
| 139 | + * Sets the "at edge" state |
| 140 | + */ |
| 141 | + void set_edge(const std::pair<unsigned short, unsigned short> & vs) |
| 142 | + { set_edge(vs.first, vs.second); } |
| 143 | + |
| 144 | + /** |
| 145 | + * @returns The edge when at an edge |
| 146 | + */ |
| 147 | + std::unique_ptr<const Elem> build_edge(const Elem & elem) const; |
| 148 | +#endif |
| 149 | + |
| 150 | + /** |
| 151 | + * @returns The vertex point when at a vertex |
| 152 | + */ |
| 153 | + const Point & vertex_point(const Elem & elem) const |
| 154 | + { return elem.point(vertex()); } |
| 155 | + |
| 156 | + /** |
| 157 | + * @returns Whether or not the current state (at vertex/edge) is valid |
| 158 | + * for the given \p elem and \p point. \p tol is used as the tolerance |
| 159 | + * to pass to the equality checks. |
| 160 | + * |
| 161 | + * This ONLY checks for validity when at_extrema(). |
| 162 | + */ |
| 163 | + bool is_valid(const Elem & elem, const Point & point, const Real tol = TOLERANCE) const; |
| 164 | +}; |
| 165 | + |
| 166 | +} // namespace libMesh |
| 167 | + |
| 168 | +std::ostream & operator<<(std::ostream & os, const libMesh::ElemExtrema & elem_extrema); |
| 169 | + |
| 170 | +#endif // LIBMESH_DIM > 1 |
| 171 | + |
| 172 | +#endif // LIBMESH_ELEMEXTREMA_H |
0 commit comments