test_interop_vtk.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #define BOOST_TEST_MODULE TestInteropVTK
  11. #include <boost/test/unit_test.hpp>
  12. #include <vtkFloatArray.h>
  13. #include <vtkMatrix4x4.h>
  14. #include <vtkNew.h>
  15. #include <vtkPoints.h>
  16. #include <vtkSmartPointer.h>
  17. #include <vtkUnsignedCharArray.h>
  18. #include <boost/compute/system.hpp>
  19. #include <boost/compute/algorithm/sort.hpp>
  20. #include <boost/compute/container/vector.hpp>
  21. #include <boost/compute/detail/is_contiguous_iterator.hpp>
  22. #include <boost/compute/interop/vtk.hpp>
  23. #include "check_macros.hpp"
  24. #include "context_setup.hpp"
  25. namespace compute = boost::compute;
  26. BOOST_AUTO_TEST_CASE(bounds)
  27. {
  28. using compute::float4_;
  29. // create vtk points
  30. vtkNew<vtkPoints> points;
  31. points->InsertNextPoint(0.0, 0.0, 0.0);
  32. points->InsertNextPoint(1.0, 2.0, 1.0);
  33. points->InsertNextPoint(-1.0, -3.0, -1.0);
  34. points->InsertNextPoint(0.5, 2.5, 1.5);
  35. // copy points to vector on gpu
  36. compute::vector<float4_> vector(points->GetNumberOfPoints(), context);
  37. compute::vtk_copy_points_to_buffer(points.GetPointer(), vector.begin(), queue);
  38. // compute bounds
  39. double bounds[6];
  40. compute::vtk_compute_bounds(vector.begin(), vector.end(), bounds, queue);
  41. // check bounds
  42. BOOST_CHECK_CLOSE(bounds[0], -1.0, 1e-8);
  43. BOOST_CHECK_CLOSE(bounds[1], 1.0, 1e-8);
  44. BOOST_CHECK_CLOSE(bounds[2], -3.0, 1e-8);
  45. BOOST_CHECK_CLOSE(bounds[3], 2.5, 1e-8);
  46. BOOST_CHECK_CLOSE(bounds[4], -1.0, 1e-8);
  47. BOOST_CHECK_CLOSE(bounds[5], 1.5, 1e-8);
  48. }
  49. BOOST_AUTO_TEST_CASE(copy_uchar_array)
  50. {
  51. // create vtk uchar vector containing 3 RGBA colors
  52. vtkNew<vtkUnsignedCharArray> array;
  53. array->SetNumberOfComponents(4);
  54. unsigned char red[4] = { 255, 0, 0, 255 };
  55. array->InsertNextTupleValue(red);
  56. unsigned char green[4] = { 0, 255, 0, 255 };
  57. array->InsertNextTupleValue(green);
  58. unsigned char blue[4] = { 0, 0, 255, 255 };
  59. array->InsertNextTupleValue(blue);
  60. // create vector<uchar4> on device and copy values from vtk array
  61. compute::vector<compute::uchar4_> vector(3, context);
  62. compute::vtk_copy_data_array_to_buffer(
  63. array.GetPointer(),
  64. compute::make_buffer_iterator<compute::uchar_>(vector.get_buffer(), 0),
  65. queue
  66. );
  67. // check values
  68. std::vector<compute::uchar4_> host_vector(3);
  69. compute::copy(
  70. vector.begin(), vector.end(), host_vector.begin(), queue
  71. );
  72. BOOST_CHECK(host_vector[0] == compute::uchar4_(255, 0, 0, 255));
  73. BOOST_CHECK(host_vector[1] == compute::uchar4_(0, 255, 0, 255));
  74. BOOST_CHECK(host_vector[2] == compute::uchar4_(0, 0, 255, 255));
  75. }
  76. BOOST_AUTO_TEST_CASE(sort_float_array)
  77. {
  78. // create vtk float array
  79. vtkNew<vtkFloatArray> array;
  80. array->InsertNextValue(2.5f);
  81. array->InsertNextValue(1.0f);
  82. array->InsertNextValue(6.5f);
  83. array->InsertNextValue(4.0f);
  84. // create vector on device and copy values from vtk array
  85. compute::vector<float> vector(4, context);
  86. compute::vtk_copy_data_array_to_buffer(array.GetPointer(), vector.begin(), queue);
  87. // sort values on the gpu
  88. compute::sort(vector.begin(), vector.end(), queue);
  89. CHECK_RANGE_EQUAL(float, 4, vector, (1.0f, 2.5f, 4.0f, 6.5f));
  90. // copy sorted values back to the vtk array
  91. compute::vtk_copy_buffer_to_data_array(
  92. vector.begin(), vector.end(), array.GetPointer(), queue
  93. );
  94. BOOST_CHECK_EQUAL(array->GetValue(0), 1.0f);
  95. BOOST_CHECK_EQUAL(array->GetValue(1), 2.5f);
  96. BOOST_CHECK_EQUAL(array->GetValue(2), 4.0f);
  97. BOOST_CHECK_EQUAL(array->GetValue(3), 6.5f);
  98. }
  99. BOOST_AUTO_TEST_SUITE_END()