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 =========================================================================*/
22 #ifndef vtkPistonMinMax_h
23 #define vtkPistonMinMax_h
24 
25 #include <thrust/device_vector.h>
26 #include <thrust/host_vector.h>
27 #include <thrust/transform_reduce.h>
28 #include <thrust/functional.h>
29 #include <thrust/extrema.h>
30 #include <thrust/random.h>
31 
32 namespace vtkPiston
33 {
34  // Compute minimum and maximum values in a single reduction
35 
36  // minmax_pair stores the minimum and maximum
37  // values that have been encountered so far
38  template <typename T>
39  struct minmax_pair
40  {
43  };
44 
45  // minmax_unary_op is a functor that takes in a value x and
46  // returns a minmax_pair whose minimum and maximum values
47  // are initialized to x.
48  template <typename T>
50  : public thrust::unary_function< T, minmax_pair<T> >
51  {
52  __host__ __device__
53  minmax_pair<T> operator()(const T& x) const
54  {
55  minmax_pair<T> result;
56  result.min_val = x;
57  result.max_val = x;
58  return result;
59  }
60  };
61 
62  // minmax_binary_op is a functor that accepts two minmax_pair
63  // structs and returns a new minmax_pair whose minimum and
64  // maximum values are the min() and max() respectively of
65  // the minimums and maximums of the input pairs
66  template <typename T>
68  : public thrust::binary_function< minmax_pair<T>, minmax_pair<T>, minmax_pair<T> >
69  {
70  __host__ __device__
72  {
73  minmax_pair<T> result;
74  result.min_val = thrust::min(x.min_val, y.min_val);
75  result.max_val = thrust::max(x.max_val, y.max_val);
76  return result;
77  }
78  };
79 
80  template <typename T>
81  minmax_pair<T> find_min_max(thrust::device_vector<T>* data)
82  {
83  // Setup arguments
84  minmax_unary_op<T> unary_op;
85  minmax_binary_op<T> binary_op;
86 
87  // Initialize reduction with the first value
88  minmax_pair<T> init = unary_op((*data)[0]);
89 
90  // Compute minimum and maximum values
91  minmax_pair<T> result = thrust::transform_reduce(
92  data->begin(), data->end(), unary_op, init, binary_op);
93 
94  return result;
95  }
96 }
97 
98 #endif // vtkPistonMinMax_h
99 // 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)