Viskores  1.0
VectorAnalysis.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_VectorAnalysis_h
19 #define viskores_VectorAnalysis_h
20 
21 // This header file defines math functions that deal with linear albegra functions
22 
23 #include <viskores/Math.h>
24 #include <viskores/TypeTraits.h>
25 #include <viskores/Types.h>
26 #include <viskores/VecTraits.h>
27 
28 namespace viskores
29 {
30 
31 // ----------------------------------------------------------------------------
39 template <typename ValueType, typename WeightType>
40 inline VISKORES_EXEC_CONT ValueType Lerp(const ValueType& value0,
41  const ValueType& value1,
42  const WeightType& weight)
43 {
44  using ScalarType = typename detail::FloatingPointReturnType<ValueType>::Type;
45  return static_cast<ValueType>((WeightType(1) - weight) * static_cast<ScalarType>(value0) +
46  weight * static_cast<ScalarType>(value1));
47 }
48 template <typename ValueType, viskores::IdComponent N, typename WeightType>
50  const viskores::Vec<ValueType, N>& value1,
51  const WeightType& weight)
52 {
53  return (WeightType(1) - weight) * value0 + weight * value1;
54 }
55 template <typename ValueType, viskores::IdComponent N>
57  const viskores::Vec<ValueType, N>& value1,
58  const viskores::Vec<ValueType, N>& weight)
59 {
60  const viskores::Vec<ValueType, N> One(ValueType(1));
61  return (One - weight) * value0 + weight * value1;
62 }
63 
64 // ----------------------------------------------------------------------------
71 template <typename T>
72 VISKORES_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type MagnitudeSquared(const T& x)
73 {
74  using U = typename detail::FloatingPointReturnType<T>::Type;
75  return static_cast<U>(viskores::Dot(x, x));
76 }
77 
78 // ----------------------------------------------------------------------------
79 namespace detail
80 {
81 template <typename T>
82 VISKORES_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type MagnitudeTemplate(
83  T x,
85 {
86  return static_cast<typename detail::FloatingPointReturnType<T>::Type>(viskores::Abs(x));
87 }
88 
89 template <typename T>
90 VISKORES_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type MagnitudeTemplate(
91  const T& x,
93 {
95 }
96 
97 } // namespace detail
98 
107 template <typename T>
108 VISKORES_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type Magnitude(const T& x)
109 {
110  return detail::MagnitudeTemplate(x, typename viskores::TypeTraits<T>::DimensionalityTag());
111 }
112 
113 // ----------------------------------------------------------------------------
114 namespace detail
115 {
116 template <typename T>
117 VISKORES_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type RMagnitudeTemplate(
118  T x,
120 {
121  return T(1) / viskores::Abs(x);
122 }
123 
124 template <typename T>
125 VISKORES_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type RMagnitudeTemplate(
126  const T& x,
128 {
129  return viskores::RSqrt(viskores::MagnitudeSquared(x));
130 }
131 } // namespace detail
132 
139 template <typename T>
140 VISKORES_EXEC_CONT typename detail::FloatingPointReturnType<T>::Type RMagnitude(const T& x)
141 {
142  return detail::RMagnitudeTemplate(x, typename viskores::TypeTraits<T>::DimensionalityTag());
143 }
144 
145 // ----------------------------------------------------------------------------
146 namespace detail
147 {
148 template <typename T>
150 {
151  return viskores::CopySign(T(1), x);
152 }
153 
154 template <typename T>
155 VISKORES_EXEC_CONT T NormalTemplate(const T& x, viskores::TypeTraitsVectorTag)
156 {
157  return viskores::RMagnitude(x) * x;
158 }
159 } // namespace detail
160 
165 template <typename T>
167 {
168  return detail::NormalTemplate(x, typename viskores::TypeTraits<T>::DimensionalityTag());
169 }
170 
171 // ----------------------------------------------------------------------------
176 template <typename T>
178 {
179  x = viskores::Normal(x);
180 }
181 
182 // ----------------------------------------------------------------------------
187 template <typename T>
189  const viskores::Vec<T, 3>& x,
190  const viskores::Vec<T, 3>& y)
191 {
193  DifferenceOfProducts(x[1], y[2], x[2], y[1]),
194  DifferenceOfProducts(x[2], y[0], x[0], y[2]),
195  DifferenceOfProducts(x[0], y[1], x[1], y[0]));
196 }
197 
198 //-----------------------------------------------------------------------------
209 template <typename T>
212  const viskores::Vec<T, 3>& b,
213  const viskores::Vec<T, 3>& c)
214 {
215  return viskores::Cross(b - a, c - a);
216 }
217 
218 //-----------------------------------------------------------------------------
226 template <typename T, int N>
228  const viskores::Vec<T, N>& u)
229 {
230  T uu = viskores::Dot(u, u);
231  T uv = viskores::Dot(u, v);
232  T factor = uv / uu;
233  viskores::Vec<T, N> result = factor * u;
234  return result;
235 }
236 
237 //-----------------------------------------------------------------------------
244 template <typename T, int N>
246 {
247  T uu = viskores::Dot(u, u);
248  T uv = viskores::Dot(u, v);
249  T factor = uv / uu;
250  return factor;
251 }
252 
253 //-----------------------------------------------------------------------------
269 template <typename T, int N>
271  viskores::Vec<viskores::Vec<T, N>, N>& outputs,
272  T tol = static_cast<T>(1e-6))
273 {
274  T tolsqr = tol * tol;
275  int j = 0; // j is the number of non-zero-length, non-collinear inputs encountered.
277  for (int i = 0; i < N; ++i)
278  {
279  u[j] = inputs[i];
280  for (int k = 0; k < j; ++k)
281  {
282  u[j] -= viskores::Project(inputs[i], u[k]);
283  }
284  T magsqr = viskores::MagnitudeSquared(u[j]);
285  if (magsqr <= tolsqr)
286  {
287  // skip this vector, it is zero-length or collinear with others.
288  continue;
289  }
290  outputs[j] = viskores::RSqrt(magsqr) * u[j];
291  ++j;
292  }
293  for (int i = j; i < N; ++i)
294  {
295  outputs[j] = Vec<T, N>{ 0. };
296  }
297  return j;
298 }
299 
300 } // namespace viskores
301 
302 #endif //viskores_VectorAnalysis_h
viskores::Orthonormalize
int Orthonormalize(const viskores::Vec< viskores::Vec< T, N >, N > &inputs, viskores::Vec< viskores::Vec< T, N >, N > &outputs, T tol=static_cast< T >(1e-6))
Convert a set of vectors to an orthonormal basis.
Definition: VectorAnalysis.h:270
Types.h
viskores::Normal
T Normal(const T &x)
Returns a normalized version of the given vector.
Definition: VectorAnalysis.h:166
viskores::Normalize
void Normalize(T &x)
Changes a vector to be normal.
Definition: VectorAnalysis.h:177
viskores::ProjectedDistance
T ProjectedDistance(const viskores::Vec< T, N > &v, const viskores::Vec< T, N > &u)
Project a vector onto another vector, returning only the projected distance.
Definition: VectorAnalysis.h:245
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Sqrt
viskores::Float32 Sqrt(viskores::Float32 x)
Definition: Math.h:951
viskores::Vec< T, 3 >
Definition: Types.h:1025
viskores::Cross
viskores::Vec< typename detail::FloatingPointReturnType< T >::Type, 3 > Cross(const viskores::Vec< T, 3 > &x, const viskores::Vec< T, 3 > &y)
Find the cross product of two vectors.
Definition: VectorAnalysis.h:188
viskores::TypeTraitsScalarTag
Tag used to identify 0 dimensional types (scalars).
Definition: TypeTraits.h:52
TypeTraits.h
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
Math.h
viskores::TypeTraitsUnknownTag
Tag used to identify types that aren't Real, Integer, Scalar or Vector.
Definition: TypeTraits.h:28
viskores::RMagnitude
detail::FloatingPointReturnType< T >::Type RMagnitude(const T &x)
Returns the reciprocal magnitude of a vector.
Definition: VectorAnalysis.h:140
viskores::DifferenceOfProducts
T DifferenceOfProducts(T a, T b, T c, T d)
Definition: Math.h:2786
viskores::Project
viskores::Vec< T, N > Project(const viskores::Vec< T, N > &v, const viskores::Vec< T, N > &u)
Project a vector onto another vector.
Definition: VectorAnalysis.h:227
viskores::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:40
viskores::Magnitude
detail::FloatingPointReturnType< T >::Type Magnitude(const T &x)
Returns the magnitude of a vector.
Definition: VectorAnalysis.h:108
viskores::MagnitudeSquared
detail::FloatingPointReturnType< T >::Type MagnitudeSquared(const T &x)
Returns the square of the magnitude of a vector.
Definition: VectorAnalysis.h:72
viskores::TriangleNormal
viskores::Vec< typename detail::FloatingPointReturnType< T >::Type, 3 > TriangleNormal(const viskores::Vec< T, 3 > &a, const viskores::Vec< T, 3 > &b, const viskores::Vec< T, 3 > &c)
Find the normal of a triangle.
Definition: VectorAnalysis.h:211
viskores::TypeTraitsVectorTag
Tag used to identify 1 dimensional types (vectors).
Definition: TypeTraits.h:59
viskores::Vec
A short fixed-length array.
Definition: Types.h:365
VecTraits.h