forked from claytronics/shasimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
region.h
205 lines (187 loc) · 5.06 KB
/
region.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef _REGION_H_
#define _REGION_H_
#include <vector>
#include <assert.h>
#include "hash.h"
#include "set.h"
#include "point.h"
class Font;
typedef std::vector<Point>::iterator RPIterator;
typedef int Nearest;
template <class Pixel> class Image;
class RegionList;
class Distance {
public:
Point start;
Point end;
double reldist;
Distance(Point s, Point e, double d=0.0) : start(s), end(e), reldist(d) {}
};
class Region {
private:
Point _ulPoint;
Point _ul;
Point _lr;
int _area;
int valid;
int lastPerimUpdate;
double lineHeight;
public:
int id;
std::vector<Point> list;
Set<Region*> neighbors;
Nearest closestRegions;
std::vector<Distance> distances[4];
int commonSides;
std::vector<Point> boundary;
Region(int _id=0): valid(0), id(_id), lastPerimUpdate(0), commonSides(0), lineHeight(0) {
}
~Region() {
//printf("Deleting %d\n", id);
}
RPIterator begin(void) {
return list.begin();
}
RPIterator end(void) {
return list.end();
}
long int size(void) const {
return list.size();
}
// set lineheight of this region
void setLineHeight(double lh) {
lineHeight = lh;
}
double getLineHeight(void) const {
return lineHeight;
}
// add a point to this region
void add(Point p) {
allValid(false);
if (0) {
for (int i=0; i<list.size(); i++) {
Point q = list[i];
assert (q != p);
}
}
list.push_back(p);
}
// add another region to this region
void add(Region* r);
// get a particular point
Point get(int idx=0) {
return list[idx];
}
// find all perimeter points and distances to other regions
void findPerimeter(Image<int>* img, RegionList* allRegions);
int countMultipleSides(Image<int>* img, std::vector<int>* tids = NULL);
int getArea(void) {
if (!isULvalid()) helpUL();
return _area;
}
Point getUL(void) {
if (!isULvalid()) helpUL();
return _ul;
}
Point getLR(void) {
if (!isULvalid()) helpUL();
return _lr;
}
Point getULPoint(void) {
if (!isULvalid()) helpUL();
return _ulPoint;
}
Set<Region*>* getNeighbors(Image<int>* img, RegionList* allRegions) {
if (!isNeighborvalid()) findNeighbors(img, allRegions);
return &neighbors;
}
void label(Image<int>* img, Font* f, double scale);
void addInfo(Image<int>* img, Font* f, const char* str, double scale=1.0);
private:
void helpUL(void);
inline bool isULvalid() { return valid & 1; }
inline bool isNeighborvalid() { return valid & 2; }
inline bool isClosestvalid() { return valid & 4; }
inline void ULValid(bool x) { if (x) valid |= 1; else valid &= ~1; }
inline void NbrValid(bool x) { if (x) valid |= 2; else valid &= ~2; }
inline void ClosestValid(bool x) { if (x) valid |= 4; else valid &= ~4; }
void collect(Hash<int,int>& targets);
// find all neighbors to a region
void findNeighbors(Image<int>* img, RegionList* allRegions);
public:
inline void allValid(bool x) { if (x) valid = ~0; else valid = 0; }
inline bool isPerimValid() { return valid & 8; }
inline void PerimValid(bool x) { if (x) valid |= 8; else valid &= ~8; }
};
inline int hash(const Region* const p)
{
unsigned long int x = (unsigned long int)p;
return x ^ (x>>16);
}
inline int isEqual(const Region* const p, const Region* const q)
{
return (p == q);
}
class RegionList {
public:
int iter;
std::vector<Region*> all;
int maxreg = 0;
int numreg = 0;
RegionList() { all.resize(10, NULL); }
~RegionList();
int numRegions(void) {
return numreg;
}
int size(void) {
return all.size();
}
Region* get(int x) {
return all[x];
}
void addPixel2Region(int x, int y, int rid) {
all[rid]->add(Point(x, y));
}
void merge(Region* dest, int src, Image<int>* img) {
merge(dest, get(src), img);
}
void merge(int dest, int src, Image<int>* img) {
merge(get(dest), get(src), img);
}
void merge(int dest, Region* src, Image<int>* img) {
merge(get(dest), src, img);
}
void merge(Region* dest, Region* src, Image<int>* img);
void set(int x, Region* v) {
while (x >= all.size()) all.resize(all.size()*2, NULL);
all[x] = v;
if (x > maxreg) maxreg = x;
if (v != NULL) numreg++; else numreg--;
if (0) {
if (v != NULL)
printf("Adding %ld points to region %d\n", v->size(), x);
else
printf("got rid of %d\n", x);
}
}
void start(void) {
iter = -1;
next();
}
bool hasMore(void) {
return ((iter < all.size()) && (all[iter] != NULL));
}
void next(void) {
iter++;
while ((iter < all.size()) && (all[iter] == NULL)) iter++;
}
Region* current(void) {
return all[iter];
}
void addLine2Region(int tx, int ty, int yx, int yy, int targetRegionId);
};
#endif
// Local Variables:
// indent-tabs-mode: nil
// c-basic-offset: 4
// End: