VectorAnalysis.h 7.09 KB
 Kenneth Moreland committed Jul 07, 2015 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ``````//============================================================================= // // Copyright (c) Kitware, Inc. // All rights reserved. // See LICENSE.txt for details. // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notice for more information. // // Copyright 2015 Sandia Corporation. // Copyright 2015 UT-Battelle, LLC. // Copyright 2015 Los Alamos National Security. // // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, // the U.S. Government retains certain rights in this software. // Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National // Laboratory (LANL), the U.S. Government retains certain rights in // this software. // //============================================================================= #ifndef vtk_m_VectorAnalysis_h #define vtk_m_VectorAnalysis_h // This header file defines math functions that deal with linear albegra funcitons #include #include `````` Kitware Robot committed May 18, 2017 29 ``````#include `````` Kenneth Moreland committed Jul 07, 2015 30 31 32 33 34 35 36 37 38 39 40 41 42 ``````#include namespace vtkm { // ---------------------------------------------------------------------------- /// \brief Returns the linear interpolation of two values based on weight /// /// \c Lerp interpolates return the linerar interpolation of v0 and v1 based on w. v0 /// and v1 are scalars or vectors of same length. w can either be a scalar or a /// vector of the same length as x and y. If w is outside [0,1] then lerp /// extrapolates. If w=0 => v0 is returned if w=1 => v1 is returned. /// `````` Kenneth Moreland committed Aug 27, 2015 43 ``````template `````` Kenneth Moreland committed Nov 15, 2016 44 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Aug 27, 2015 45 ``````ValueType Lerp(const ValueType &value0, `````` Chuck Atkins committed Mar 18, 2016 46 `````` const ValueType &value1, `````` Kenneth Moreland committed Aug 27, 2015 47 `````` const WeightType &weight) `````` Kenneth Moreland committed Jul 07, 2015 48 ``````{ `````` Chuck Atkins committed Mar 18, 2016 49 `````` return static_cast((WeightType(1)-weight)*value0+weight*value1); `````` Kenneth Moreland committed Jul 07, 2015 50 ``````} `````` Kenneth Moreland committed Aug 27, 2015 51 ``````template `````` Kenneth Moreland committed Nov 15, 2016 52 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Aug 27, 2015 53 54 55 ``````vtkm::Vec Lerp(const vtkm::Vec &value0, const vtkm::Vec &value1, const WeightType &weight) `````` Kenneth Moreland committed Jul 07, 2015 56 ``````{ `````` Chuck Atkins committed Mar 18, 2016 57 `````` return (WeightType(1)-weight)*value0+weight*value1; `````` Kenneth Moreland committed Aug 27, 2015 58 59 ``````} template `````` Kenneth Moreland committed Nov 15, 2016 60 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Aug 27, 2015 61 62 63 64 ``````vtkm::Vec Lerp(const vtkm::Vec &value0, const vtkm::Vec &value1, const vtkm::Vec &weight) { `````` Chuck Atkins committed Mar 18, 2016 65 66 `````` static const vtkm::Vec One(ValueType(1)); return (One-weight)*value0+weight*value1; `````` Kenneth Moreland committed Jul 07, 2015 67 68 69 70 71 72 73 74 75 76 ``````} // ---------------------------------------------------------------------------- /// \brief Returns the square of the magnitude of a vector. /// /// It is usually much faster to compute the square of the magnitude than the /// square, so you should use this function in place of Magnitude or RMagnitude /// when possible. /// template `````` Kenneth Moreland committed Nov 15, 2016 77 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Jul 07, 2015 78 79 80 81 82 83 84 85 86 ``````typename vtkm::VecTraits::ComponentType MagnitudeSquared(const T &x) { return vtkm::dot(x,x); } // ---------------------------------------------------------------------------- namespace detail { template `````` Kenneth Moreland committed Nov 15, 2016 87 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 88 ``````typename detail::FloatingPointReturnType::Type `````` Thomas Otahal committed Feb 06, 2017 89 ``````MagnitudeTemplate(T x, vtkm::TypeTraitsScalarTag) `````` Kenneth Moreland committed Jul 07, 2015 90 ``````{ `````` Thomas Otahal committed Feb 07, 2017 91 `````` return static_cast::Type>(vtkm::Abs(x)); `````` Kenneth Moreland committed Jul 07, 2015 92 93 94 ``````} template `````` Kenneth Moreland committed Nov 15, 2016 95 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 96 97 ``````typename detail::FloatingPointReturnType::Type MagnitudeTemplate(const T &x, vtkm::TypeTraitsVectorTag) `````` Kenneth Moreland committed Jul 07, 2015 98 99 100 ``````{ return vtkm::Sqrt(vtkm::MagnitudeSquared(x)); } `````` Thomas Otahal committed Jan 19, 2017 101 `````` `````` Kenneth Moreland committed Jul 07, 2015 102 103 104 105 106 107 108 109 110 111 112 ``````} // namespace detail /// \brief Returns the magnitude of a vector. /// /// It is usually much faster to compute MagnitudeSquared, so that should be /// substituted when possible (unless you are just going to take the square /// root, which would be besides the point). On some hardware it is also faster /// to find the reciprocal magnitude, so RMagnitude should be used if you /// actually plan to divide by the magnitude. /// template `````` Kenneth Moreland committed Nov 15, 2016 113 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 114 115 ``````typename detail::FloatingPointReturnType::Type Magnitude(const T &x) `````` Kenneth Moreland committed Jul 07, 2015 116 117 118 119 120 121 122 123 ``````{ return detail::MagnitudeTemplate( x, typename vtkm::TypeTraits::DimensionalityTag()); } // ---------------------------------------------------------------------------- namespace detail { template `````` Kenneth Moreland committed Nov 15, 2016 124 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 125 126 ``````typename detail::FloatingPointReturnType::Type RMagnitudeTemplate(T x, vtkm::TypeTraitsScalarTag) `````` Kenneth Moreland committed Jul 07, 2015 127 ``````{ `````` Thomas Otahal committed Feb 06, 2017 128 `````` return T(1)/vtkm::Abs(x); `````` Kenneth Moreland committed Jul 07, 2015 129 130 131 ``````} template `````` Kenneth Moreland committed Nov 15, 2016 132 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 133 ``````typename detail::FloatingPointReturnType::Type `````` Kenneth Moreland committed Jul 07, 2015 134 135 136 137 138 139 140 141 142 143 144 145 ``````RMagnitudeTemplate(const T &x, vtkm::TypeTraitsVectorTag) { return vtkm::RSqrt(vtkm::MagnitudeSquared(x)); } } // namespace detail /// \brief Returns the reciprocal magnitude of a vector. /// /// On some hardware RMagnitude is faster than Magnitude, but neither is /// as fast as MagnitudeSquared. /// template `````` Kenneth Moreland committed Nov 15, 2016 146 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 147 ``````typename detail::FloatingPointReturnType::Type `````` Kenneth Moreland committed Jul 07, 2015 148 149 150 151 152 153 154 155 156 ``````RMagnitude(const T &x) { return detail::RMagnitudeTemplate( x, typename vtkm::TypeTraits::DimensionalityTag()); } // ---------------------------------------------------------------------------- namespace detail { template `````` Kenneth Moreland committed Nov 15, 2016 157 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Jul 07, 2015 158 159 160 161 162 163 ``````T NormalTemplate(T x, vtkm::TypeTraitsScalarTag) { return vtkm::CopySign(T(1), x); } template `````` Kenneth Moreland committed Nov 15, 2016 164 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Jul 07, 2015 165 166 167 168 169 170 171 172 173 174 175 ``````T NormalTemplate(const T &x, vtkm::TypeTraitsVectorTag) { return vtkm::RMagnitude(x)*x; } } // namespace detail /// \brief Returns a normalized version of the given vector. /// /// The resulting vector points in the same direction but has unit length. /// template `````` Kenneth Moreland committed Nov 15, 2016 176 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Jul 07, 2015 177 178 179 180 181 182 183 184 185 186 187 188 ``````T Normal(const T &x) { return detail::NormalTemplate( x, typename vtkm::TypeTraits::DimensionalityTag()); } // ---------------------------------------------------------------------------- /// \brief Changes a vector to be normal. /// /// The given vector is scaled to be unit length. /// template `````` Kenneth Moreland committed Nov 15, 2016 189 ``````VTKM_EXEC_CONT `````` Kenneth Moreland committed Jul 07, 2015 190 191 192 193 194 195 196 197 198 ``````void Normalize(T &x) { x = vtkm::Normal(x); } // ---------------------------------------------------------------------------- /// \brief Find the cross product of two vectors. /// template `````` Kenneth Moreland committed Nov 15, 2016 199 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 200 201 ``````vtkm::Vec::Type,3> Cross(const vtkm::Vec &x, const vtkm::Vec &y) `````` Kenneth Moreland committed Jul 07, 2015 202 ``````{ `````` Thomas Otahal committed Jan 26, 2017 203 204 205 `````` return vtkm::Vec::Type,3>(x[1]*y[2] - x[2]*y[1], x[2]*y[0] - x[0]*y[2], x[0]*y[1] - x[1]*y[0]); `````` Kenneth Moreland committed Jul 07, 2015 206 207 208 209 210 211 212 213 214 215 ``````} //----------------------------------------------------------------------------- /// \brief Find the normal of a triangle. /// /// Given three coordinates in space, which, unless degenerate, uniquely define /// a triangle and the plane the triangle is on, returns a vector perpendicular /// to that triangle/plane. /// template `````` Kenneth Moreland committed Nov 15, 2016 216 ``````VTKM_EXEC_CONT `````` Thomas Otahal committed Jan 26, 2017 217 218 219 220 ``````vtkm::Vec::Type,3> TriangleNormal(const vtkm::Vec &a, const vtkm::Vec &b, const vtkm::Vec &c) `````` Kenneth Moreland committed Jul 07, 2015 221 222 223 224 225 226 227 228 ``````{ return vtkm::Cross(b-a, c-a); } } // namespace vtkm #endif //vtk_m_VectorAnalysis_h``````