]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //---------------------------------------------------------------------------// |
2 | // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com> | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0 | |
5 | // See accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt | |
7 | // | |
8 | // See http://boostorg.github.com/compute for more information. | |
9 | //---------------------------------------------------------------------------// | |
10 | ||
11 | #ifndef BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP | |
12 | #define BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP | |
13 | ||
14 | #include <vector> | |
15 | #include <iterator> | |
16 | ||
17 | #include <boost/compute/system.hpp> | |
18 | #include <boost/compute/command_queue.hpp> | |
19 | #include <boost/compute/algorithm/copy_n.hpp> | |
20 | #include <boost/compute/algorithm/reduce.hpp> | |
21 | #include <boost/compute/container/array.hpp> | |
22 | ||
23 | namespace boost { | |
24 | namespace compute { | |
25 | ||
26 | /// Calculates the bounds for the points in the range [\p first, \p last) and | |
27 | /// stores the result in \p bounds. | |
28 | /// | |
29 | /// For example, this can be used to implement the GetBounds() method for a | |
30 | /// vtkMapper subclass. | |
31 | template<class PointIterator> | |
32 | inline void vtk_compute_bounds(PointIterator first, | |
33 | PointIterator last, | |
34 | double bounds[6], | |
35 | command_queue &queue = system::default_queue()) | |
36 | { | |
37 | typedef typename std::iterator_traits<PointIterator>::value_type T; | |
38 | ||
39 | const context &context = queue.get_context(); | |
40 | ||
41 | // compute min and max point | |
42 | array<T, 2> extrema(context); | |
43 | reduce(first, last, extrema.begin() + 0, min<T>(), queue); | |
44 | reduce(first, last, extrema.begin() + 1, max<T>(), queue); | |
45 | ||
46 | // copy results to host buffer | |
47 | std::vector<T> buffer(2); | |
48 | copy_n(extrema.begin(), 2, buffer.begin(), queue); | |
49 | ||
50 | // copy to vtk-style bounds | |
51 | bounds[0] = buffer[0][0]; bounds[1] = buffer[1][0]; | |
52 | bounds[2] = buffer[0][1]; bounds[3] = buffer[1][1]; | |
53 | bounds[4] = buffer[0][2]; bounds[5] = buffer[1][2]; | |
54 | } | |
55 | ||
56 | } // end compute namespace | |
57 | } // end boost namespace | |
58 | ||
59 | #endif // BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP |