VTK-m  2.0
CellDiagonalRatioMetric.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_CellDiagonalRatioMetric_h
21 #define vtk_m_worklet_cellmetrics_CellDiagonalRatioMetric_h
22 
23 /*
24 * Mesh quality metric functions that compute the diagonal ratio of mesh cells.
25 * The diagonal ratio of a cell is defined as the length (magnitude) of the longest
26 * cell diagonal length divided by the length of the shortest cell diagonal length.
27 ** These metric computations are adapted from the VTK implementation of the Verdict library,
28 * which provides a set of mesh/cell metrics for evaluating the geometric qualities of regions
29 * of mesh spaces.
30 ** The edge ratio computations for a pyramid cell types is not defined in the
31 * VTK implementation, but is provided here.
32 ** See: The Verdict Library Reference Manual (for per-cell-type metric formulae)
33 * See: vtk/ThirdParty/verdict/vtkverdict (for VTK code implementation of this metric)
34 */
35 
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 
42 #define UNUSED(expr) (void)(expr);
43 
44 namespace vtkm
45 {
46 namespace worklet
47 {
48 namespace cellmetrics
49 {
50 
52 
53 template <typename OutType, typename VecType>
54 VTKM_EXEC inline OutType ComputeDiagonalRatio(const VecType& diagonals)
55 {
56  const vtkm::Id numDiagonals = diagonals.GetNumberOfComponents();
57 
58  //Compare diagonal lengths to determine the longest and shortest
59  //TODO: Could we use lambda expression here?
60 
61  FloatType d0Len = (FloatType)vtkm::MagnitudeSquared(diagonals[0]);
62  FloatType currLen, minLen = d0Len, maxLen = d0Len;
63  for (int i = 1; i < numDiagonals; i++)
64  {
65  currLen = (FloatType)vtkm::MagnitudeSquared(diagonals[i]);
66  if (currLen < minLen)
67  minLen = currLen;
68  if (currLen > maxLen)
69  maxLen = currLen;
70  }
71 
72  if (minLen <= OutType(0.0))
73  return vtkm::Infinity<OutType>();
74 
75  //Take square root because we only did magnitude squared before
76  OutType diagonalRatio = (OutType)vtkm::Sqrt(minLen / maxLen);
77  return diagonalRatio;
78 }
79 
80 // By default, cells have zero shape unless the shape type template is specialized below.
81 template <typename OutType, typename PointCoordVecType, typename CellShapeType>
83  const PointCoordVecType& pts,
84  CellShapeType shape,
86 {
87  UNUSED(numPts);
88  UNUSED(pts);
89  UNUSED(shape);
90  return OutType(-1.0);
91 }
92 
93 // ========================= 2D cells ==================================
94 // Compute the diagonal ratio of a quadrilateral.
95 // Formula: Maximum diagonal length divided by minimum diagonal length
96 // Equals 1 for a unit square
97 // Full range: [1,FLOAT_MAX]
98 template <typename OutType, typename PointCoordVecType>
100  const PointCoordVecType& pts,
101  vtkm::CellShapeTagQuad,
102  vtkm::ErrorCode& ec)
103 {
104  if (numPts != 4)
105  {
107  return OutType(0.0);
108  }
109 
110  vtkm::IdComponent numDiagonals = 2; //pts.GetNumberOfComponents();
111 
112  //The 2 diagonals of a quadrilateral
113  using Diagonal = typename PointCoordVecType::ComponentType;
114  const Diagonal QuadDiagonals[2] = { pts[2] - pts[0], pts[3] - pts[1] };
115 
116  return vtkm::worklet::cellmetrics::ComputeDiagonalRatio<OutType>(
117  vtkm::make_VecC(QuadDiagonals, numDiagonals));
118 }
119 
120 // ============================= 3D Volume cells ==================================
121 // Compute the diagonal ratio of a hexahedron.
122 // Formula: Maximum diagonal length divided by minimum diagonal length
123 // Equals 1 for a unit cube
124 // Acceptable Range: [0.65, 1]
125 // Normal Range: [0, 1]
126 // Full range: [1,FLOAT_MAX]
127 template <typename OutType, typename PointCoordVecType>
129  const PointCoordVecType& pts,
130  vtkm::CellShapeTagHexahedron,
131  vtkm::ErrorCode& ec)
132 {
133  if (numPts != 8)
134  {
136  return OutType(0.0);
137  }
138 
139  vtkm::IdComponent numDiagonals = 4; //pts.GetNumberOfComponents();
140 
141  //The 4 diagonals of a hexahedron
142  using Diagonal = typename PointCoordVecType::ComponentType;
143  const Diagonal HexDiagonals[4] = {
144  pts[6] - pts[0], pts[7] - pts[1], pts[4] - pts[2], pts[5] - pts[3]
145  };
146 
147  return vtkm::worklet::cellmetrics::ComputeDiagonalRatio<OutType>(
148  vtkm::make_VecC(HexDiagonals, numDiagonals));
149 }
150 } // namespace cellmetrics
151 } // namespace worklet
152 } // namespace vtkm
153 #endif // vtk_m_worklet_cellmetrics_CellDiagonalRatioMetric_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::worklet::cellmetrics::CellDiagonalRatioMetric
VTKM_EXEC OutType CellDiagonalRatioMetric(const vtkm::IdComponent &numPts, const PointCoordVecType &pts, CellShapeType shape, vtkm::ErrorCode &)
Definition: CellDiagonalRatioMetric.h:82
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::worklet::cellmetrics::FloatType
vtkm::FloatDefault FloatType
Definition: CellAspectFrobeniusMetric.h:50
CellShape.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
ErrorCode.h
VectorAnalysis.h
vtkm::worklet::cellmetrics::ComputeDiagonalRatio
VTKM_EXEC OutType ComputeDiagonalRatio(const VecType &diagonals)
Definition: CellDiagonalRatioMetric.h:54
UNUSED
#define UNUSED(expr)
Definition: CellDiagonalRatioMetric.h:42
vtkm::FloatDefault
vtkm::Float32 FloatDefault
The floating point type to use when no other precision is specified.
Definition: Types.h:198
CellTraits.h
VecTraits.h
vtkm::ErrorCode::InvalidNumberOfPoints
@ InvalidNumberOfPoints