-
Notifications
You must be signed in to change notification settings - Fork 86
/
Types.hpp
163 lines (127 loc) · 4.15 KB
/
Types.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2016, Simula Research Laboratory
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef _CCTAG_MARKERS_TYPES_HPP_
#define _CCTAG_MARKERS_TYPES_HPP_
#include <memory>
#include <new>
#include <stdexcept>
#include <cctag/EdgePoint.hpp>
namespace cctag {
class EdgePointCollection
{
public:
static constexpr size_t MAX_POINTS = size_t(1) << 24;
private:
static constexpr size_t MAX_RESOLUTION = 6144;
static constexpr size_t CUDA_OFFSET = 1024; // 4 kB, one page
static constexpr size_t MAX_VOTERLIST_SIZE = 16*MAX_POINTS;
public:
using int_vector = std::vector<int>;
using voter_list = std::pair<const int*, const int*>;
private:
// These must be exported also by CUDA.
std::unique_ptr<int[]> _edgeMap;
std::unique_ptr<EdgePoint[]> _edgeList;
std::unique_ptr<int[]> _linkList; // even idx: before, odd: after
std::unique_ptr<int[]> _votersIndex; // with CUDA_OFFSET; [0] is point count
std::unique_ptr<int[]> _votersList;
// These are used only on the CPU.
std::unique_ptr<unsigned[]> _processedIn;
std::unique_ptr<unsigned[]> _processedAux;
size_t _edgeMapShape[2]{};
static_assert(sizeof(unsigned) == 4, "unsigned has wrong size");
int& point_count() { return _votersIndex[0]; }
int point_count() const { return _votersIndex[0]; }
size_t map_index(int x, int y) const { return x + y * _edgeMapShape[0]; }
static void set_bit(unsigned* v, size_t i, bool f)
{
if (i >= MAX_POINTS)
throw std::out_of_range("EdgePointCollection::set_bit");
if (f) v[i/4] |= 1U << (i & 31);
else v[i/4] &= ~(1U << (i & 31));
}
static bool test_bit(unsigned* v, size_t i)
{
if (i >= MAX_POINTS)
throw std::out_of_range("EdgePointCollection::test_bit");
return v[i/4] & (1U << (i & 31));
}
public:
EdgePointCollection() = default;
EdgePointCollection(const EdgePointCollection&) = delete;
EdgePointCollection& operator=(const EdgePointCollection&) = delete;
EdgePointCollection(size_t w, size_t h);
void add_point(int vx, int vy, float vdx, float vdy);
int get_point_count()
{
return point_count();
}
const size_t* shape() const { return _edgeMapShape; }
EdgePoint* operator()(int i) { return i >= 0 ? &_edgeList[i] : nullptr; }
EdgePoint* operator()(int i) const { return i >= 0 ? const_cast<EdgePoint*>(&_edgeList[i]) : nullptr; }
EdgePoint* operator()(int x, int y) const { return (*this)(_edgeMap[map_index(x,y)]); }
int operator()(const EdgePoint* p) const
{
if (!p)
return -1;
if (p < &_edgeList[0] || p >= &_edgeList[point_count()])
throw std::logic_error("EdgePointCollection::index: invalid pointer");
return p - &_edgeList[0];
}
void create_voter_lists(const std::vector<std::vector<int>>& voter_lists);
voter_list voters(const EdgePoint* p) const
{
int i = (*this)(p);
int b = _votersIndex[i+CUDA_OFFSET], e = _votersIndex[i+1+CUDA_OFFSET];
return std::make_pair(&_votersList[0]+b, &_votersList[0]+e);
}
int voters_size(const EdgePoint* p) const
{
int i = (*this)(p);
int b = _votersIndex[i+CUDA_OFFSET], e = _votersIndex[i+1+CUDA_OFFSET];
return e - b;
}
EdgePoint* before(EdgePoint* p) const
{
int i = (*this)(p);
return (*this)(_linkList[2*i+0]);
}
void set_before(EdgePoint* p, int link)
{
int i = (*this)(p);
_linkList[2*i+0] = link;
}
EdgePoint* after(EdgePoint* p) const
{
int i = (*this)(p);
return (*this)(_linkList[2*i+1]);
}
void set_after(EdgePoint* p, int link)
{
int i = (*this)(p);
_linkList[2*i+1] = link;
}
void set_processed_in(EdgePoint* p, bool f)
{
set_bit(&_processedIn[0], (*this)(p), f);
}
bool test_processed_in(EdgePoint* p)
{
return test_bit(&_processedIn[0], (*this)(p));
}
void set_processed_aux(EdgePoint* p, bool f)
{
set_bit(&_processedAux[0], (*this)(p), f);
}
bool test_processed_aux(EdgePoint* p)
{
return test_bit(&_processedAux[0], (*this)(p));
}
};
} // namespace cctag
#endif