VTK-m  2.0
CellAspectRatioMetric.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 // This software is distributed WITHOUT ANY WARRANTY; without even
6 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
7 // PURPOSE. See the above copyright notice for more information.
8 //
9 // Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 // Copyright 2018 UT-Battelle, LLC.
11 // Copyright 2018 Los Alamos National Security.
12 //
13 // Under the terms of Contract DE-NA0003525 with NTESS,
14 // the U.S. Government retains certain rights in this software.
15 //
16 // Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
17 // Laboratory (LANL), the U.S. Government retains certain rights in
18 // this software.
19 //============================================================================
20 #ifndef vtk_m_worklet_cellmetrics_CellAspectRatioMetric_h
21 #define vtk_m_worklet_cellmetrics_CellAspectRatioMetric_h
22 
23 /*
24 * Mesh quality metric functions that compute the aspect ratio of mesh cells.
25 ** These metric computations are adapted from the VTK implementation of the Verdict library,
26 * which provides a set of mesh/cell metrics for evaluating the geometric qualities of regions
27 * of mesh spaces.
28 ** See: The Verdict Library Reference Manual (for per-cell-type metric formulae)
29 * See: vtk/ThirdParty/verdict/vtkverdict (for VTK code implementation of this metric)
30 */
31 
32 #include "TypeOfCellHexahedral.h"
34 #include "TypeOfCellTetrahedral.h"
35 #include "TypeOfCellTriangle.h"
36 #include <vtkm/CellShape.h>
37 #include <vtkm/CellTraits.h>
38 #include <vtkm/ErrorCode.h>
39 #include <vtkm/VecTraits.h>
40 #include <vtkm/VectorAnalysis.h>
41 #define UNUSED(expr) (void)(expr);
42 
43 namespace vtkm
44 {
45 namespace worklet
46 {
47 namespace cellmetrics
48 {
49 // The Verdict Manual and the Implementation have conflicting definitions.
50 // This duplicates the Verdict implementation in the VTKm Paradigm, with prior Manual
51 // definitions commented out when formerly coded.
52 // ========================= Unsupported cells ==================================
53 
54 // By default, cells have zero shape unless the shape type template is specialized below.
55 template <typename OutType, typename PointCoordVecType, typename CellShapeType>
57  const PointCoordVecType& pts,
58  CellShapeType shape,
60 {
61  UNUSED(numPts);
62  UNUSED(pts);
63  UNUSED(shape);
64  return OutType(0);
65 }
66 
67 // ========================= 2D cells ==================================
68 
69 // Compute the diagonal ratio of a triangle.
70 template <typename OutType, typename PointCoordVecType>
72  const PointCoordVecType& pts,
73  vtkm::CellShapeTagTriangle,
74  vtkm::ErrorCode& ec)
75 {
76  if (numPts != 3)
77  {
79  return OutType(0.0);
80  }
81 
82  using Scalar = OutType;
83  using CollectionOfPoints = PointCoordVecType;
84  using Vector = typename PointCoordVecType::ComponentType;
85 
86  const Scalar lmax = GetTriangleLMax<Scalar, Vector, CollectionOfPoints>(pts);
87  const Scalar r = GetTriangleInradius<Scalar, Vector, CollectionOfPoints>(pts);
88  const Scalar hhalf(0.5);
89  const Scalar three(3.0);
90  const Scalar q = (lmax * hhalf * vtkm::RSqrt(three)) / r;
91  return q;
92 }
93 
94 template <typename OutType, typename PointCoordVecType>
96  const PointCoordVecType& pts,
97  vtkm::CellShapeTagQuad,
98  vtkm::ErrorCode& ec)
99 {
100  if (numPts != 4)
101  {
103  return OutType(0.0);
104  }
105 
106  using Scalar = OutType;
107  using CollectionOfPoints = PointCoordVecType;
108  using Vector = typename PointCoordVecType::ComponentType;
109 
110  const Vector X1 = GetQuadX0<Scalar, Vector, CollectionOfPoints>(pts);
111  const Vector X2 = GetQuadX1<Scalar, Vector, CollectionOfPoints>(pts);
112 
113  const Scalar x1 = static_cast<Scalar>(vtkm::Sqrt(vtkm::MagnitudeSquared(X1)));
114  const Scalar x2 = static_cast<Scalar>(vtkm::Sqrt(vtkm::MagnitudeSquared(X2)));
115  if (x1 <= Scalar(0.0) || x2 <= Scalar(0.0))
116  {
117  return vtkm::Infinity<Scalar>();
118  }
119 
120  const Scalar q = vtkm::Max(x1 / x2, x2 / x1);
121  return q;
122 }
123 
124 // ========================= 3D cells ==================================
125 template <typename OutType, typename PointCoordVecType>
127  const PointCoordVecType& pts,
128  vtkm::CellShapeTagHexahedron,
129  vtkm::ErrorCode& ec)
130 {
131  if (numPts != 8)
132  {
134  return OutType(0.0);
135  }
136 
137  using Scalar = OutType;
138  using CollectionOfPoints = PointCoordVecType;
139  using Vector = typename PointCoordVecType::ComponentType;
140 
141  const Vector X1 = GetHexX1<Scalar, Vector, CollectionOfPoints>(pts);
142  const Vector X2 = GetHexX2<Scalar, Vector, CollectionOfPoints>(pts);
143  const Vector X3 = GetHexX3<Scalar, Vector, CollectionOfPoints>(pts);
144 
145  const Scalar x1 = static_cast<Scalar>(vtkm::Sqrt(vtkm::MagnitudeSquared(X1)));
146  const Scalar x2 = static_cast<Scalar>(vtkm::Sqrt(vtkm::MagnitudeSquared(X2)));
147  const Scalar x3 = static_cast<Scalar>(vtkm::Sqrt(vtkm::MagnitudeSquared(X3)));
148 
149  if (x1 <= Scalar(0.0) || x2 <= Scalar(0.0) || x3 <= Scalar(0.0))
150  {
151  return vtkm::Infinity<Scalar>();
152  }
153 
154  const Scalar q = vtkm::Max(
155  x1 / x2,
156  vtkm::Max(x2 / x1, vtkm::Max(x1 / x3, vtkm::Max(x3 / x1, vtkm::Max(x3 / x2, x3 / x2)))));
157  return q;
158 }
159 
160 // Compute the aspect ratio of a tetrahedron.
161 template <typename OutType, typename PointCoordVecType>
163  const PointCoordVecType& pts,
164  vtkm::CellShapeTagTetra,
165  vtkm::ErrorCode& ec)
166 {
167  if (numPts != 4)
168  {
170  return OutType(0.0);
171  }
172 
173  using Scalar = OutType;
174  using CollectionOfPoints = PointCoordVecType;
175  using Vector = typename PointCoordVecType::ComponentType;
176 
177  const Scalar rootSixInvert = vtkm::RSqrt(Scalar(6.0));
178  const Scalar hhalf(0.5);
179  const Scalar lmax = GetTetraLMax<Scalar, Vector, CollectionOfPoints>(pts);
180  const Scalar r = GetTetraInradius<Scalar, Vector, CollectionOfPoints>(pts);
181  const Scalar q = (hhalf * rootSixInvert * lmax) / r;
182  return q;
183 }
184 } // namespace cellmetrics
185 } // namespace worklet
186 } // namespace vtkm
187 #endif // vtk_m_worklet_cellmetrics_CellAspectRatioMetric_h
vtkm::Sqrt
VTKM_EXEC_CONT vtkm::Float32 Sqrt(vtkm::Float32 x)
Compute the square root of x.
Definition: Math.h:958
vtkm::ErrorCode
ErrorCode
Definition: ErrorCode.h:19
vtkm::MagnitudeSquared
VTKM_EXEC_CONT detail::FloatingPointReturnType< T >::Type MagnitudeSquared(const T &x)
Returns the square of the magnitude of a vector.
Definition: VectorAnalysis.h:64
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
CellShape.h
ErrorCode.h
VectorAnalysis.h
vtkm::worklet::cellmetrics::CellAspectRatioMetric
VTKM_EXEC OutType CellAspectRatioMetric(const vtkm::IdComponent &numPts, const PointCoordVecType &pts, CellShapeType shape, vtkm::ErrorCode &)
Definition: CellAspectRatioMetric.h:56
TypeOfCellTetrahedral.h
TypeOfCellTriangle.h
TypeOfCellHexahedral.h
UNUSED
#define UNUSED(expr)
Definition: CellAspectRatioMetric.h:41
TypeOfCellQuadrilateral.h
CellTraits.h
VecTraits.h
vtkm::ErrorCode::InvalidNumberOfPoints
@ InvalidNumberOfPoints