This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
/
bounding_box.cu
86 lines (67 loc) · 2.26 KB
/
bounding_box.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <thrust/transform_reduce.h>
#include <thrust/device_vector.h>
#include <thrust/pair.h>
#include <thrust/random.h>
#include <thrust/extrema.h>
// This example shows how to compute a bounding box
// for a set of points in two dimensions.
struct point2d
{
float x, y;
__host__ __device__
point2d() : x(0), y(0) {}
__host__ __device__
point2d(float _x, float _y) : x(_x), y(_y) {}
};
// bounding box type
typedef thrust::pair<point2d, point2d> bbox;
// reduce a pair of bounding boxes (a,b) to a bounding box containing a and b
struct bbox_reduction : public thrust::binary_function<bbox,bbox,bbox>
{
__host__ __device__
bbox operator()(bbox a, bbox b)
{
// lower left corner
point2d ll(thrust::min(a.first.x, b.first.x), thrust::min(a.first.y, b.first.y));
// upper right corner
point2d ur(thrust::max(a.second.x, b.second.x), thrust::max(a.second.y, b.second.y));
return bbox(ll, ur);
}
};
// convert a point to a bbox containing that point, (point) -> (point, point)
struct bbox_transformation : public thrust::unary_function<point2d,bbox>
{
__host__ __device__
bbox operator()(point2d point)
{
return bbox(point, point);
}
};
int main(void)
{
const size_t N = 40;
thrust::default_random_engine rng;
thrust::uniform_real_distribution<float> u01(0.0f, 1.0f);
// allocate storage for points
thrust::device_vector<point2d> points(N);
// generate some random points in the unit square
for(size_t i = 0; i < N; i++)
{
float x = u01(rng);
float y = u01(rng);
points[i] = point2d(x,y);
}
// initial bounding box contains first point
bbox init = bbox(points[0], points[0]);
// transformation operation
bbox_transformation unary_op;
// binary reduction operation
bbox_reduction binary_op;
// compute the bounding box for the point set
bbox result = thrust::transform_reduce(points.begin(), points.end(), unary_op, init, binary_op);
// print output
std::cout << "bounding box " << std::fixed;
std::cout << "(" << result.first.x << "," << result.first.y << ") ";
std::cout << "(" << result.second.x << "," << result.second.y << ")" << std::endl;
return 0;
}