point_centroid.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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. //[point_centroid_example
  11. #include <iostream>
  12. #include <boost/compute/algorithm/copy.hpp>
  13. #include <boost/compute/algorithm/accumulate.hpp>
  14. #include <boost/compute/container/vector.hpp>
  15. #include <boost/compute/types/fundamental.hpp>
  16. namespace compute = boost::compute;
  17. // the point centroid example calculates and displays the
  18. // centroid of a set of 3D points stored as float4's
  19. int main()
  20. {
  21. using compute::float4_;
  22. // get default device and setup context
  23. compute::device device = compute::system::default_device();
  24. compute::context context(device);
  25. compute::command_queue queue(context, device);
  26. // point coordinates
  27. float points[] = { 1.0f, 2.0f, 3.0f, 0.0f,
  28. -2.0f, -3.0f, 4.0f, 0.0f,
  29. 1.0f, -2.0f, 2.5f, 0.0f,
  30. -7.0f, -3.0f, -2.0f, 0.0f,
  31. 3.0f, 4.0f, -5.0f, 0.0f };
  32. // create vector for five points
  33. compute::vector<float4_> vector(5, context);
  34. // copy point data to the device
  35. compute::copy(
  36. reinterpret_cast<float4_ *>(points),
  37. reinterpret_cast<float4_ *>(points) + 5,
  38. vector.begin(),
  39. queue
  40. );
  41. // calculate sum
  42. float4_ sum = compute::accumulate(
  43. vector.begin(), vector.end(), float4_(0, 0, 0, 0), queue
  44. );
  45. // calculate centroid
  46. float4_ centroid;
  47. for(size_t i = 0; i < 3; i++){
  48. centroid[i] = sum[i] / 5.0f;
  49. }
  50. // print centroid
  51. std::cout << "centroid: " << centroid << std::endl;
  52. return 0;
  53. }
  54. //]