-
Notifications
You must be signed in to change notification settings - Fork 10
/
intersect.h
88 lines (68 loc) · 2.15 KB
/
intersect.h
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
#ifndef IMAGEPROCESSING_INTERSECT_H__
#define IMAGEPROCESSING_INTERSECT_H__
#include "ExplicitVolume.h"
#include <util/assert.h>
/**
* Stores the result of intersecting two volumes A and B in C. The intersection
* is the smallest box containing voxels that are non-zero in both A and B. The
* values of voxels in C are the minimal values of the corresponding voxels in A
* and B.
*
* A and B need to have the same resolution.
*
* @param[in] a
* The first volume.
* @param[in] b
* The second volume.
* @param[out] c
* The result of intersecting the given volumes.
*/
template <typename T>
void intersect(
const ExplicitVolume<T>& a,
const ExplicitVolume<T>& b,
ExplicitVolume<T>& c) {
UTIL_ASSERT_REL(a.getResolution(), ==, b.getResolution());
// discrete offset from a to b
util::point<int, 3> offsetAB = (a.getOffset() - b.getOffset())/a.getResolution();
// get discrete bounding box of c relative to discrete bounding box of a
util::box<int, 3> c_dbb;
for (int z = 0; z < a.depth(); z++)
for (int y = 0; y < a.height(); y++)
for (int x = 0; x < a.width(); x++) {
T valueA = a(x, y, z);
if (valueA == 0)
continue;
util::point<int, 3> bpos(
x + offsetAB.x(),
y + offsetAB.y(),
z + offsetAB.z());
if (b.getDiscreteBoundingBox().contains(bpos) && b[bpos] > 0)
c_dbb.fit(util::box<int, 3>(x, y, z, x+1, y+1, z+1));
}
c.resize(c_dbb.width(), c_dbb.height(), c_dbb.depth());
c.data() = 0;
c.setResolution(a.getResolution());
c.setOffset(a.getOffset() + c_dbb.min()*a.getResolution());
util::point<int, 3> offsetAC = -c_dbb.min();
util::point<int, 3> offsetBC = -offsetAB + offsetAC;
// fill non-zero values of c
for (int z = 0; z < c.depth(); z++)
for (int y = 0; y < c.height(); y++)
for (int x = 0; x < c.width(); x++) {
util::point<int, 3> posA(
x - offsetAC.x(),
y - offsetAC.y(),
z - offsetAC.z());
T valueA = a[posA];
if (valueA == 0)
continue;
util::point<int, 3> posB(
x - offsetBC.x(),
y - offsetBC.y(),
z - offsetBC.z());
T valueB = b[posB];
c(x, y, z) = std::min(valueA, valueB);
}
}
#endif // IMAGEPROCESSING_INTERSECT_H__