VTK-m  2.0
CellConditionMetric.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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10 // Copyright 2014 UT-Battelle, LLC.
11 // Copyright 2014 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_CellConditionMetric_h
21 #define vtk_m_worklet_cellmetrics_CellConditionMetric_h
22 
23 /*
24 * Mesh quality metric functions that compute the condition metric 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 
33 #include "TypeOfCellHexahedral.h"
35 #include "TypeOfCellTetrahedral.h"
36 #include "TypeOfCellTriangle.h"
37 #include <vtkm/CellShape.h>
38 #include <vtkm/CellTraits.h>
39 #include <vtkm/ErrorCode.h>
40 #include <vtkm/VecTraits.h>
41 #include <vtkm/VectorAnalysis.h>
42 #define UNUSED(expr) (void)(expr);
43 
44 namespace vtkm
45 {
46 namespace worklet
47 {
48 namespace cellmetrics
49 {
50 // ========================= Unsupported cells ==================================
51 
52 // By default, cells have zero shape unless the shape type template is specialized below.
53 template <typename OutType, typename PointCoordVecType, typename CellShapeType>
55  const PointCoordVecType& pts,
56  CellShapeType shape,
58 {
59  UNUSED(numPts);
60  UNUSED(pts);
61  UNUSED(shape);
62  return OutType(0);
63 }
64 
65 // =============================== Condition metric cells ==================================
66 
67 // Compute the condition quality metric of a triangular cell.
68 template <typename OutType, typename PointCoordVecType>
70  const PointCoordVecType& pts,
71  vtkm::CellShapeTagTriangle,
72  vtkm::ErrorCode& ec)
73 {
74  if (numPts != 3)
75  {
77  return OutType(0.0);
78  }
79 
80  using Scalar = OutType;
81  using CollectionOfPoints = PointCoordVecType;
82  using Vector = typename PointCoordVecType::ComponentType;
83 
84  const Scalar area = GetTriangleArea<Scalar, Vector, CollectionOfPoints>(pts);
85 
86  if (area == Scalar(0.0))
87  {
88  return vtkm::Infinity<Scalar>();
89  }
90  const Scalar two(2.0);
91  const Scalar rootThree = vtkm::Sqrt(Scalar(3.0));
92  const Vector L1 = GetTriangleL1<Scalar, Vector, CollectionOfPoints>(pts);
93  const Vector L2 = GetTriangleL2<Scalar, Vector, CollectionOfPoints>(pts);
94 
95  const Scalar q =
96  static_cast<Scalar>((vtkm::Dot(L2, L2) + vtkm::Dot(L1, L1) + vtkm::Dot(L1, L2))) /
97  (two * area * rootThree);
98  return q;
99 }
100 
101 template <typename OutType, typename PointCoordVecType>
103  const PointCoordVecType& pts,
104  vtkm::CellShapeTagQuad,
105  vtkm::ErrorCode& ec)
106 {
107  UNUSED(numPts);
108  UNUSED(ec);
109  using Scalar = OutType;
110  using CollectionOfPoints = PointCoordVecType;
111  using Vector = typename PointCoordVecType::ComponentType;
112 
113  const Scalar a0 = GetQuadAlpha0<Scalar, Vector, CollectionOfPoints>(pts);
114  const Scalar a1 = GetQuadAlpha1<Scalar, Vector, CollectionOfPoints>(pts);
115  const Scalar a2 = GetQuadAlpha2<Scalar, Vector, CollectionOfPoints>(pts);
116  const Scalar a3 = GetQuadAlpha3<Scalar, Vector, CollectionOfPoints>(pts);
117 
118  if (a0 < vtkm::NegativeInfinity<Scalar>() || a1 < vtkm::NegativeInfinity<Scalar>() ||
119  a2 < vtkm::NegativeInfinity<Scalar>() || a3 < vtkm::NegativeInfinity<Scalar>())
120  {
121  return vtkm::Infinity<Scalar>();
122  }
123 
124  const Scalar l0 = GetQuadL0Magnitude<Scalar, Vector, CollectionOfPoints>(pts);
125  const Scalar l1 = GetQuadL1Magnitude<Scalar, Vector, CollectionOfPoints>(pts);
126  const Scalar l2 = GetQuadL2Magnitude<Scalar, Vector, CollectionOfPoints>(pts);
127  const Scalar l3 = GetQuadL3Magnitude<Scalar, Vector, CollectionOfPoints>(pts);
128  const Scalar hhalf(0.5);
129 
130  const Scalar q0 = ((l0 * l0) + (l3 * l3)) / a0;
131  const Scalar q1 = ((l1 * l1) + (l0 * l0)) / a1;
132  const Scalar q2 = ((l2 * l2) + (l1 * l1)) / a2;
133  const Scalar q3 = ((l3 * l3) + (l2 * l2)) / a3;
134 
135  const Scalar q = hhalf * vtkm::Max(q0, vtkm::Max(q1, vtkm::Max(q2, q3)));
136  return q;
137 }
138 
139 // ============================= 3D volumetric cells ==================================
141 template <typename OutType, typename PointCoordVecType>
143  const PointCoordVecType& pts,
144  vtkm::CellShapeTagTetra,
145  vtkm::ErrorCode& ec)
146 {
147  if (numPts != 4)
148  {
150  return OutType(0.0);
151  }
152  using Scalar = OutType;
153  using CollectionOfPoints = PointCoordVecType;
154  using Vector = typename PointCoordVecType::ComponentType;
155 
156  const Scalar negTwo(-2.0);
157  const Scalar three(3.0);
158  const Scalar root3 = vtkm::Sqrt(three);
159  const Scalar root6 = vtkm::Sqrt(Scalar(6.0));
160  const Vector L0 = GetTetraL0<Scalar, Vector, CollectionOfPoints>(pts);
161  const Vector L2 = GetTetraL2<Scalar, Vector, CollectionOfPoints>(pts);
162  const Vector L3 = GetTetraL3<Scalar, Vector, CollectionOfPoints>(pts);
163 
164  const Vector C1 = L0;
165  const Vector C2 = ((negTwo * L2) - L0) / root3;
166  const Vector C3 = ((three * L3) + L2 - L0) / root6;
167 
168  const Scalar cDet = static_cast<Scalar>(vtkm::Dot(C1, vtkm::Cross(C2, C3)));
169 
170  if (cDet <= Scalar(0.0))
171  {
172  return vtkm::Infinity<Scalar>();
173  }
174 
175  const Vector C1xC2 = vtkm::Cross(C1, C2);
176  const Vector C2xC3 = vtkm::Cross(C2, C3);
177  const Vector C1xC3 = vtkm::Cross(C1, C3);
178 
179  const Scalar t1 = static_cast<Scalar>(vtkm::Dot(C1, C1) + vtkm::Dot(C2, C2) + vtkm::Dot(C3, C3));
180  const Scalar t2 = static_cast<Scalar>(vtkm::Dot(C1xC2, C1xC2) + vtkm::Dot(C2xC3, C2xC3) +
181  vtkm::Dot(C1xC3, C1xC3));
182 
183  const Scalar q = vtkm::Sqrt(t1 * t2) / (three * cDet);
184  return q;
185 }
186 
187 // Condition of a hex cell is a deprecated/legacy metric which is identical to the Max Aspect Frobenius metric.
188 template <typename OutType, typename PointCoordVecType>
190  const PointCoordVecType& pts,
191  vtkm::CellShapeTagHexahedron,
192  vtkm::ErrorCode& ec)
193 {
194  return CellMaxAspectFrobeniusMetric<OutType, PointCoordVecType>(
195  numPts, pts, vtkm::CellShapeTagHexahedron(), ec);
196 }
197 }
198 }
199 }
200 #endif // vtk_m_worklet_cellmetrics_CellConditionMetric_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_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
CellMaxAspectFrobeniusMetric.h
vtkm::Cross
VTKM_EXEC_CONT vtkm::Vec< typename detail::FloatingPointReturnType< T >::Type, 3 > Cross(const vtkm::Vec< T, 3 > &x, const vtkm::Vec< T, 3 > &y)
Find the cross product of two vectors.
Definition: VectorAnalysis.h:177
TypeOfCellTetrahedral.h
TypeOfCellTriangle.h
vtkm::worklet::cellmetrics::CellConditionMetric
VTKM_EXEC OutType CellConditionMetric(const vtkm::IdComponent &numPts, const PointCoordVecType &pts, CellShapeType shape, vtkm::ErrorCode &)
Definition: CellConditionMetric.h:54
TypeOfCellHexahedral.h
UNUSED
#define UNUSED(expr)
Definition: CellConditionMetric.h:42
TypeOfCellQuadrilateral.h
CellTraits.h
VecTraits.h
vtkm::ErrorCode::InvalidNumberOfPoints
@ InvalidNumberOfPoints