Line data Source code
1 : #pragma once 2 : 3 : #include "ContiguousStorage.h" 4 : 5 : #include <thrust/transform.h> 6 : #include <thrust/iterator/iterator_traits.h> 7 : 8 : namespace elsa 9 : { 10 : /// @brief Clip input range to `minval` and `maxval` 11 : /// @ingroup transforms 12 : template <class Iter, class OutIter, class T = thrust::iterator_value_t<Iter>, 13 : class U = thrust::iterator_value_t<Iter>> 14 : void clip(Iter first, Iter last, OutIter out, const T& minval, const U& maxval) 15 186 : { 16 186 : using data_t = thrust::iterator_value_t<Iter>; 17 : 18 5199 : thrust::transform(first, last, out, [=] __host__ __device__(const data_t& x) { 19 5199 : if (x < static_cast<data_t>(minval)) { 20 2761 : return static_cast<data_t>(minval); 21 2761 : } else if (x > static_cast<data_t>(maxval)) { 22 46 : return static_cast<data_t>(maxval); 23 2392 : } else { 24 2392 : return x; 25 2392 : } 26 5199 : }); 27 186 : } 28 : 29 : /// @brief Clip input range to `0` and `maxval` 30 : /// @ingroup transforms 31 : template <class Iter, class OutIter, class T = thrust::iterator_value_t<Iter>> 32 : void clip(Iter first, Iter last, OutIter out, const T& maxval) 33 : { 34 : using data_t = thrust::iterator_value_t<Iter>; 35 : return clip(first, last, out, data_t(0), static_cast<data_t>(maxval)); 36 : } 37 : } // namespace elsa