Line data Source code
1 : #pragma once 2 : 3 : #include "Functions.hpp" 4 : #include "TypeTraits.hpp" 5 : 6 : #include <thrust/transform.h> 7 : 8 : namespace elsa 9 : { 10 : namespace detail 11 : { 12 : template <class T> 13 : struct DivVectorScalar { 14 377 : DivVectorScalar(T scalar) : scalar_(scalar) {} 15 : 16 : template <class data_t> 17 : __host__ __device__ auto operator()(const data_t& x) -> std::common_type_t<T, data_t> 18 27687 : { 19 27687 : using U = std::common_type_t<T, data_t>; 20 27687 : return static_cast<U>(x) / static_cast<U>(scalar_); 21 27687 : } 22 : 23 : T scalar_; 24 : }; 25 : 26 : template <class T> 27 : struct DivScalarVector { 28 39 : DivScalarVector(T scalar) : scalar_(scalar) {} 29 : 30 : template <class data_t> 31 : __host__ __device__ auto operator()(const data_t& x) -> std::common_type_t<T, data_t> 32 19778 : { 33 19778 : using U = std::common_type_t<T, data_t>; 34 19778 : return static_cast<U>(scalar_) / static_cast<U>(x); 35 19778 : } 36 : 37 : T scalar_; 38 : }; 39 : } // namespace detail 40 : 41 : /// @brief Compute the component wise division of two vectors 42 : /// @ingroup transforms 43 : template <class InputIter1, class InputIter2, class OutIter> 44 : void div(InputIter1 xfirst, InputIter1 xlast, InputIter2 yfirst, OutIter out) 45 98 : { 46 98 : thrust::transform(xfirst, xlast, yfirst, out, elsa::divides{}); 47 98 : } 48 : 49 : /// @brief Compute the component wise division of a vectors and a scalar 50 : /// @ingroup transforms 51 : template <class data_t, class InputIter, class OutIter> 52 : void divScalar(InputIter first, InputIter last, const data_t& scalar, OutIter out) 53 377 : { 54 : // TODO: Find out why a lambda doesn't work here! 55 377 : thrust::transform(first, last, out, detail::DivVectorScalar(scalar)); 56 377 : } 57 : 58 : /// @brief Compute the component wise division of a scalar and a vector 59 : /// @ingroup transforms 60 : template <class data_t, class InputIter, class OutIter> 61 : void divScalar(const data_t& scalar, InputIter first, InputIter last, OutIter out) 62 39 : { 63 : // TODO: Find out why a lambda doesn't work here! 64 39 : thrust::transform(first, last, out, detail::DivScalarVector(scalar)); 65 39 : } 66 : } // namespace elsa