VTK-m  2.2
VectorAnalysis.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 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_VectorAnalysis_h
11 #define vtk_m_VectorAnalysis_h
12 
13 // This header file defines math functions that deal with linear albegra functions
14 
15 #include <vtkm/Math.h>
16 #include <vtkm/TypeTraits.h>
17 #include <vtkm/Types.h>
18 #include <vtkm/VecTraits.h>
19 
20 namespace vtkm
21 {
22 
23 // ----------------------------------------------------------------------------
31 template <typename ValueType, typename WeightType>
32 inline VTKM_EXEC_CONT ValueType Lerp(const ValueType& value0,
33  const ValueType& value1,
34  const WeightType& weight)
35 {
36  using ScalarType = typename detail::FloatingPointReturnType<ValueType>::Type;
37  return static_cast<ValueType>((WeightType(1) - weight) * static_cast<ScalarType>(value0) +
38  weight * static_cast<ScalarType>(value1));
39 }
40 template <typename ValueType, vtkm::IdComponent N, typename WeightType>
42  const vtkm::Vec<ValueType, N>& value1,
43  const WeightType& weight)
44 {
45  return (WeightType(1) - weight) * value0 + weight * value1;
46 }
47 template <typename ValueType, vtkm::IdComponent N>
49  const vtkm::Vec<ValueType, N>& value1,
50  const vtkm::Vec<ValueType, N>& weight)
51 {
52  const vtkm::Vec<ValueType, N> One(ValueType(1));
53  return (One - weight) * value0 + weight * value1;
54 }
55 
56 // ----------------------------------------------------------------------------
63 template <typename T>
64 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type MagnitudeSquared(const T& x)
65 {
66  using U = typename detail::FloatingPointReturnType<T>::Type;
67  return static_cast<U>(vtkm::Dot(x, x));
68 }
69 
70 // ----------------------------------------------------------------------------
71 namespace detail
72 {
73 template <typename T>
74 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type MagnitudeTemplate(
75  T x,
77 {
78  return static_cast<typename detail::FloatingPointReturnType<T>::Type>(vtkm::Abs(x));
79 }
80 
81 template <typename T>
82 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type MagnitudeTemplate(
83  const T& x,
85 {
87 }
88 
89 } // namespace detail
90 
99 template <typename T>
100 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type Magnitude(const T& x)
101 {
102  return detail::MagnitudeTemplate(x, typename vtkm::TypeTraits<T>::DimensionalityTag());
103 }
104 
105 // ----------------------------------------------------------------------------
106 namespace detail
107 {
108 template <typename T>
109 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type RMagnitudeTemplate(
110  T x,
112 {
113  return T(1) / vtkm::Abs(x);
114 }
115 
116 template <typename T>
117 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type RMagnitudeTemplate(
118  const T& x,
120 {
121  return vtkm::RSqrt(vtkm::MagnitudeSquared(x));
122 }
123 } // namespace detail
124 
131 template <typename T>
132 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type RMagnitude(const T& x)
133 {
134  return detail::RMagnitudeTemplate(x, typename vtkm::TypeTraits<T>::DimensionalityTag());
135 }
136 
137 // ----------------------------------------------------------------------------
138 namespace detail
139 {
140 template <typename T>
141 VTKM_EXEC_CONT T NormalTemplate(T x, vtkm::TypeTraitsScalarTag)
142 {
143  return vtkm::CopySign(T(1), x);
144 }
145 
146 template <typename T>
147 VTKM_EXEC_CONT T NormalTemplate(const T& x, vtkm::TypeTraitsVectorTag)
148 {
149  return vtkm::RMagnitude(x) * x;
150 }
151 } // namespace detail
152 
157 template <typename T>
158 VTKM_EXEC_CONT T Normal(const T& x)
159 {
160  return detail::NormalTemplate(x, typename vtkm::TypeTraits<T>::DimensionalityTag());
161 }
162 
163 // ----------------------------------------------------------------------------
168 template <typename T>
170 {
171  x = vtkm::Normal(x);
172 }
173 
174 // ----------------------------------------------------------------------------
179 template <typename T>
181  const vtkm::Vec<T, 3>& x,
182  const vtkm::Vec<T, 3>& y)
183 {
185  DifferenceOfProducts(x[1], y[2], x[2], y[1]),
186  DifferenceOfProducts(x[2], y[0], x[0], y[2]),
187  DifferenceOfProducts(x[0], y[1], x[1], y[0]));
188 }
189 
190 //-----------------------------------------------------------------------------
201 template <typename T>
204 {
205  return vtkm::Cross(b - a, c - a);
206 }
207 
208 //-----------------------------------------------------------------------------
216 template <typename T, int N>
218 {
219  T uu = vtkm::Dot(u, u);
220  T uv = vtkm::Dot(u, v);
221  T factor = uv / uu;
222  vtkm::Vec<T, N> result = factor * u;
223  return result;
224 }
225 
226 //-----------------------------------------------------------------------------
233 template <typename T, int N>
235 {
236  T uu = vtkm::Dot(u, u);
237  T uv = vtkm::Dot(u, v);
238  T factor = uv / uu;
239  return factor;
240 }
241 
242 //-----------------------------------------------------------------------------
258 template <typename T, int N>
260  vtkm::Vec<vtkm::Vec<T, N>, N>& outputs,
261  T tol = static_cast<T>(1e-6))
262 {
263  T tolsqr = tol * tol;
264  int j = 0; // j is the number of non-zero-length, non-collinear inputs encountered.
266  for (int i = 0; i < N; ++i)
267  {
268  u[j] = inputs[i];
269  for (int k = 0; k < j; ++k)
270  {
271  u[j] -= vtkm::Project(inputs[i], u[k]);
272  }
273  T magsqr = vtkm::MagnitudeSquared(u[j]);
274  if (magsqr <= tolsqr)
275  {
276  // skip this vector, it is zero-length or collinear with others.
277  continue;
278  }
279  outputs[j] = vtkm::RSqrt(magsqr) * u[j];
280  ++j;
281  }
282  for (int i = j; i < N; ++i)
283  {
284  outputs[j] = Vec<T, N>{ 0. };
285  }
286  return j;
287 }
288 
289 } // namespace vtkm
290 
291 #endif //vtk_m_VectorAnalysis_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Sqrt
vtkm::Float32 Sqrt(vtkm::Float32 x)
Definition: Math.h:943
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Project
vtkm::Vec< T, N > Project(const vtkm::Vec< T, N > &v, const vtkm::Vec< T, N > &u)
Project a vector onto another vector.
Definition: VectorAnalysis.h:217
vtkm::TriangleNormal
vtkm::Vec< typename detail::FloatingPointReturnType< T >::Type, 3 > TriangleNormal(const vtkm::Vec< T, 3 > &a, const vtkm::Vec< T, 3 > &b, const vtkm::Vec< T, 3 > &c)
Find the normal of a triangle.
Definition: VectorAnalysis.h:203
vtkm::Lerp
ValueType Lerp(const ValueType &value0, const ValueType &value1, const WeightType &weight)
Returns the linear interpolation of two values based on weight.
Definition: VectorAnalysis.h:32
vtkm::Normal
T Normal(const T &x)
Returns a normalized version of the given vector.
Definition: VectorAnalysis.h:158
vtkm::Cross
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:180
vtkm::DifferenceOfProducts
T DifferenceOfProducts(T a, T b, T c, T d)
Definition: Math.h:2778
TypeTraits.h
vtkm::MagnitudeSquared
detail::FloatingPointReturnType< T >::Type MagnitudeSquared(const T &x)
Returns the square of the magnitude of a vector.
Definition: VectorAnalysis.h:64
Math.h
vtkm::ProjectedDistance
T ProjectedDistance(const vtkm::Vec< T, N > &v, const vtkm::Vec< T, N > &u)
Project a vector onto another vector, returning only the projected distance.
Definition: VectorAnalysis.h:234
vtkm::Normalize
void Normalize(T &x)
Changes a vector to be normal.
Definition: VectorAnalysis.h:169
vtkm::RMagnitude
detail::FloatingPointReturnType< T >::Type RMagnitude(const T &x)
Returns the reciprocal magnitude of a vector.
Definition: VectorAnalysis.h:132
vtkm::TypeTraitsUnknownTag
Tag used to identify types that aren't Real, Integer, Scalar or Vector.
Definition: TypeTraits.h:20
vtkm::TypeTraitsScalarTag
Tag used to identify 0 dimensional types (scalars).
Definition: TypeTraits.h:44
vtkm::TypeTraitsVectorTag
Tag used to identify 1 dimensional types (vectors).
Definition: TypeTraits.h:51
vtkm::Vec< T, 3 >
Definition: Types.h:1013
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::Magnitude
detail::FloatingPointReturnType< T >::Type Magnitude(const T &x)
Returns the magnitude of a vector.
Definition: VectorAnalysis.h:100
vtkm::Orthonormalize
int Orthonormalize(const vtkm::Vec< vtkm::Vec< T, N >, N > &inputs, vtkm::Vec< vtkm::Vec< T, N >, N > &outputs, T tol=static_cast< T >(1e-6))
Convert a set of vectors to an orthonormal basis.
Definition: VectorAnalysis.h:259
VecTraits.h