bounds.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP
  11. #define BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP
  12. #include <vector>
  13. #include <iterator>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/algorithm/copy_n.hpp>
  17. #include <boost/compute/algorithm/reduce.hpp>
  18. #include <boost/compute/container/array.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// Calculates the bounds for the points in the range [\p first, \p last) and
  22. /// stores the result in \p bounds.
  23. ///
  24. /// For example, this can be used to implement the GetBounds() method for a
  25. /// vtkMapper subclass.
  26. template<class PointIterator>
  27. inline void vtk_compute_bounds(PointIterator first,
  28. PointIterator last,
  29. double bounds[6],
  30. command_queue &queue = system::default_queue())
  31. {
  32. typedef typename std::iterator_traits<PointIterator>::value_type T;
  33. const context &context = queue.get_context();
  34. // compute min and max point
  35. array<T, 2> extrema(context);
  36. reduce(first, last, extrema.begin() + 0, min<T>(), queue);
  37. reduce(first, last, extrema.begin() + 1, max<T>(), queue);
  38. // copy results to host buffer
  39. std::vector<T> buffer(2);
  40. copy_n(extrema.begin(), 2, buffer.begin(), queue);
  41. // copy to vtk-style bounds
  42. bounds[0] = buffer[0][0]; bounds[1] = buffer[1][0];
  43. bounds[2] = buffer[0][1]; bounds[3] = buffer[1][1];
  44. bounds[4] = buffer[0][2]; bounds[5] = buffer[1][2];
  45. }
  46. } // end compute namespace
  47. } // end boost namespace
  48. #endif // BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP