VTK-m  2.0
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 
130 template <typename T>
131 VTKM_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type RMagnitude(const T& x)
132 {
133  return detail::RMagnitudeTemplate(x, typename vtkm::TypeTraits<T>::DimensionalityTag());
134 }
135 
136 // ----------------------------------------------------------------------------
137 namespace detail
138 {
139 template <typename T>
140 VTKM_EXEC_CONT T NormalTemplate(T x, vtkm::TypeTraitsScalarTag)
141 {
142  return vtkm::CopySign(T(1), x);
143 }
144 
145 template <typename T>
146 VTKM_EXEC_CONT T NormalTemplate(const T& x, vtkm::TypeTraitsVectorTag)
147 {
148  return vtkm::RMagnitude(x) * x;
149 }
150 } // namespace detail
151 
156 template <typename T>
157 VTKM_EXEC_CONT T Normal(const T& x)
158 {
159  return detail::NormalTemplate(x, typename vtkm::TypeTraits<T>::DimensionalityTag());
160 }
161 
162 // ----------------------------------------------------------------------------
167 template <typename T>
169 {
170  x = vtkm::Normal(x);
171 }
172 
173 // ----------------------------------------------------------------------------
176 template <typename T>
178  const vtkm::Vec<T, 3>& x,
179  const vtkm::Vec<T, 3>& y)
180 {
182  DifferenceOfProducts(x[1], y[2], x[2], y[1]),
183  DifferenceOfProducts(x[2], y[0], x[0], y[2]),
184  DifferenceOfProducts(x[0], y[1], x[1], y[0]));
185 }
186 
187 //-----------------------------------------------------------------------------
198 template <typename T>
201 {
202  return vtkm::Cross(b - a, c - a);
203 }
204 
205 //-----------------------------------------------------------------------------
213 template <typename T, int N>
215 {
216  T uu = vtkm::Dot(u, u);
217  T uv = vtkm::Dot(u, v);
218  T factor = uv / uu;
219  vtkm::Vec<T, N> result = factor * u;
220  return result;
221 }
222 
223 //-----------------------------------------------------------------------------
230 template <typename T, int N>
232 {
233  T uu = vtkm::Dot(u, u);
234  T uv = vtkm::Dot(u, v);
235  T factor = uv / uu;
236  return factor;
237 }
238 
239 //-----------------------------------------------------------------------------
253 template <typename T, int N>
255  vtkm::Vec<vtkm::Vec<T, N>, N>& outputs,
256  T tol = static_cast<T>(1e-6))
257 {
258  T tolsqr = tol * tol;
259  int j = 0; // j is the number of non-zero-length, non-collinear inputs encountered.
261  for (int i = 0; i < N; ++i)
262  {
263  u[j] = inputs[i];
264  for (int k = 0; k < j; ++k)
265  {
266  u[j] -= vtkm::Project(inputs[i], u[k]);
267  }
268  T magsqr = vtkm::MagnitudeSquared(u[j]);
269  if (magsqr <= tolsqr)
270  {
271  // skip this vector, it is zero-length or collinear with others.
272  continue;
273  }
274  outputs[j] = vtkm::RSqrt(magsqr) * u[j];
275  ++j;
276  }
277  for (int i = j; i < N; ++i)
278  {
279  outputs[j] = Vec<T, N>{ 0. };
280  }
281  return j;
282 }
283 
284 } // namespace vtkm
285 
286 #endif //vtk_m_VectorAnalysis_h
vtkm::Sqrt
VTKM_EXEC_CONT vtkm::Float32 Sqrt(vtkm::Float32 x)
Compute the square root of x.
Definition: Math.h:958
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
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::RMagnitude
VTKM_EXEC_CONT detail::FloatingPointReturnType< T >::Type RMagnitude(const T &x)
Returns the reciprocal magnitude of a vector.
Definition: VectorAnalysis.h:131
vtkm::Normalize
VTKM_EXEC_CONT void Normalize(T &x)
Changes a vector to be normal.
Definition: VectorAnalysis.h:168
vtkm::Magnitude
VTKM_EXEC_CONT detail::FloatingPointReturnType< T >::Type Magnitude(const T &x)
Returns the magnitude of a vector.
Definition: VectorAnalysis.h:100
vtkm::Orthonormalize
VTKM_EXEC_CONT 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))
Perform Gram-Schmidt orthonormalization for 3-D vectors.
Definition: VectorAnalysis.h:254
vtkm::Normal
VTKM_EXEC_CONT T Normal(const T &x)
Returns a normalized version of the given vector.
Definition: VectorAnalysis.h:157
TypeTraits.h
Math.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
vtkm::ProjectedDistance
VTKM_EXEC_CONT 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:231
vtkm::DifferenceOfProducts
VTKM_EXEC_CONT T DifferenceOfProducts(T a, T b, T c, T d)
Definition: Math.h:2728
vtkm::TriangleNormal
VTKM_EXEC_CONT 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:200
vtkm::Lerp
VTKM_EXEC_CONT 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::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:975
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
VecTraits.h
vtkm::Project
VTKM_EXEC_CONT 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:214