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