aabbn.h
2.58 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef STIM_AABBN_H
#define STIM_AABBN_H
#include <vector>
#include <stim/cuda/cudatools/callable.h>
namespace stim{
/// Structure for a 3D axis aligned bounding box
template<typename T, size_t D>
struct aabbn{
//protected:
T low[D]; //top left corner position
T high[D]; //dimensions along x and y and z
CUDA_CALLABLE void init(T* i) {
for (size_t d = 0; d < D; d++)
low[d] = high[d] = i[d];
}
CUDA_CALLABLE aabbn() {}
CUDA_CALLABLE aabbn(T* i) {
init(i);
}
/// For even inputs to the constructor, the input could be one point or a set of pairs of points
CUDA_CALLABLE aabbn(T x0, T x1) {
if (D == 1) {
low[0] = x0;
high[0] = x1;
}
else if (D == 2) {
low[0] = high[0] = x0;
low[1] = high[1] = x1;
}
}
/// In the case of 3 inputs, this must be a 3D bounding box, so initialize to a box of size 0 at (x, y, z)
/*CUDA_CALLABLE aabbn(T x, T y, T z) {
low[0] = high[0] = x;
low[1] = high[1] = y;
low[2] = high[2] = z;
}*/
CUDA_CALLABLE aabbn(T x0, T y0, T x1, T y1) {
if (D == 2) {
low[0] = x0;
high[0] = x1;
low[1] = y0;
high[1] = y1;
}
else if(D == 4){
low[0] = high[0] = x0;
low[1] = high[1] = y0;
low[2] = high[2] = x1;
low[3] = high[3] = y1;
}
}
/*CUDA_CALLABLE aabbn(T x0, T y0, T z0, T x1, T y1, T z1) {
if (D == 3) {
low[0] = x0;
high[0] = x1;
low[1] = y0;
high[1] = y1;
low[2] = z0;
high[2] = z1;
}
else if (D == 6) {
low[0] = high[0] = x0;
low[1] = high[1] = y0;
low[2] = high[2] = z0;
low[3] = high[3] = x1;
low[4] = high[4] = y1;
low[5] = high[5] = z1;
}
}*/
//insert a point into the bounding box, growing the box appropriately
CUDA_CALLABLE void insert(T* p){
for(size_t d = 0; d < D; d++){
if(p[d] < low[d]) low[d] = p[d];
if(p[d] > high[d]) high[d] = p[d];
}
}
//trim the bounding box so that the lower bounds are b(x, y, z, ...)
CUDA_CALLABLE void trim_low(T* b){
for(size_t d = 0; d < D; d++)
if(low[d] < b[d]) low[d] = b[d];
}
CUDA_CALLABLE void trim_high(T* b){
for(size_t d = 0; d < D; d++)
if(high[d] > b[d]) high[d] = b[d];
}
CUDA_CALLABLE T length(size_t d) {
return high[d] - low[d];
}
CUDA_CALLABLE aabbn<T, D> operator*(T s) {
aabbn<T, D> newbox;
for (size_t d = 0; d < D; d++) {
T c = (low[d] + high[d]) / 2;
T l = high[d] - low[d];
newbox.low[d] = c - l * s / 2;
newbox.high[d] = c + l * s / 2;
}
return newbox;
}
//translate the box along dimension d a distance of v
CUDA_CALLABLE void translate(size_t d, T v) {
for (size_t d = 0; d < D; d++) {
low[d] += v;
high[d] += v;
}
}
};
}
#endif