2
0
mirror of https://github.com/boostorg/compute.git synced 2026-01-24 05:42:14 +00:00
Files
compute/example/point_centroid.cpp
Kyle Lutz c70056491d Cleanup example code
This cleans up the example code. Now all of the examples use
the "namespace compute = boost::compute" alias. This shortens
the example code making it less verbose and more clear. Also
cleans up a few style issues.
2013-05-20 20:50:12 -04:00

57 lines
1.6 KiB
C++

//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
//[point_centroid_example
#include <iostream>
#include <boost/compute.hpp>
namespace compute = boost::compute;
// the point centroid example calculates and displays the
// centroid of a set of 3D points stored as float4's
int main()
{
using compute::float4_;
// point coordinates
float points[] = { 1.0f, 2.0f, 3.0f, 0.0f,
-2.0f, -3.0f, 4.0f, 0.0f,
1.0f, -2.0f, 2.5f, 0.0f,
-7.0f, -3.0f, -2.0f, 0.0f,
3.0f, 4.0f, -5.0f, 0.0f };
// create vector for five points
compute::vector<float4_> vector(5);
// copy point data to the device
compute::copy(
reinterpret_cast<float4_ *>(points),
reinterpret_cast<float4_ *>(points) + 5,
vector.begin()
);
// calculate sum
float4_ sum = compute::accumulate(vector.begin(),
vector.end(),
float4_(0, 0, 0, 0));
// calculate centroid
float4_ centroid;
for(size_t i = 0; i < 3; i++){
centroid[i] = sum[i] / 5.0f;
}
// print centroid
std::cout << "centroid: " << centroid << std::endl;
}
//]