VTK
vtkPistonMinMax.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkPistonMapper.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
23 #ifndef vtkPistonMinMax_h
24 #define vtkPistonMinMax_h
25 
26 #include <thrust/device_vector.h>
27 #include <thrust/host_vector.h>
28 #include <thrust/transform_reduce.h>
29 #include <thrust/functional.h>
30 #include <thrust/extrema.h>
31 #include <thrust/random.h>
32 
33 namespace vtkPiston
34 {
35  // Compute minimum and maximum values in a single reduction
36 
37  // minmax_pair stores the minimum and maximum
38  // values that have been encountered so far
39  template <typename T>
40  struct minmax_pair
41  {
44  };
45 
46  // minmax_unary_op is a functor that takes in a value x and
47  // returns a minmax_pair whose minimum and maximum values
48  // are initialized to x.
49  template <typename T>
51  : public thrust::unary_function< T, minmax_pair<T> >
52  {
53  __host__ __device__
54  minmax_pair<T> operator()(const T& x) const
55  {
56  minmax_pair<T> result;
57  result.min_val = x;
58  result.max_val = x;
59  return result;
60  }
61  };
62 
63  // minmax_binary_op is a functor that accepts two minmax_pair
64  // structs and returns a new minmax_pair whose minimum and
65  // maximum values are the min() and max() respectively of
66  // the minimums and maximums of the input pairs
67  template <typename T>
69  : public thrust::binary_function< minmax_pair<T>, minmax_pair<T>, minmax_pair<T> >
70  {
71  __host__ __device__
73  {
74  minmax_pair<T> result;
75  result.min_val = thrust::min(x.min_val, y.min_val);
76  result.max_val = thrust::max(x.max_val, y.max_val);
77  return result;
78  }
79  };
80 
81  template <typename T>
82  minmax_pair<T> find_min_max(thrust::device_vector<T>* data)
83  {
84  // Setup arguments
85  minmax_unary_op<T> unary_op;
86  minmax_binary_op<T> binary_op;
87 
88  // Initialize reduction with the first value
89  minmax_pair<T> init = unary_op((*data)[0]);
90 
91  // Compute minimum and maximum values
92  minmax_pair<T> result = thrust::transform_reduce(
93  data->begin(), data->end(), unary_op, init, binary_op);
94 
95  return result;
96  }
97 }
98 
99 #endif // vtkPistonMinMax_h
100 // VTK-HeaderTest-Exclude: vtkPistonMinMax.h
minmax_pair< T > find_min_max(thrust::device_vector< T > *data)
__host__ __device__ minmax_pair< T > operator()(const minmax_pair< T > &x, const minmax_pair< T > &y) const
__host__ __device__ minmax_pair< T > operator()(const T &x) const
#define max(a, b)