-
Notifications
You must be signed in to change notification settings - Fork 30
/
BitmapPrimitiveShape.cpp
353 lines (315 loc) · 10.8 KB
/
BitmapPrimitiveShape.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "BitmapPrimitiveShape.h"
#include "Bitmap.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include "IndexIterator.h"
#include <MiscLib/Performance.h>
#include <float.h>
using namespace MiscLib;
void BitmapPrimitiveShape::PreWrapBitmap(
const GfxTL::AABox< GfxTL::Vector2Df > &bbox, float epsilon,
size_t uextent, size_t vextent, MiscLib::Vector< char > *bmp) const
{
// the default case is: do nothing
}
void BitmapPrimitiveShape::WrapComponents(
const GfxTL::AABox< GfxTL::Vector2Df > &bbox,
float epsilon, size_t uextent, size_t vextent,
MiscLib::Vector< int > *componentImg,
MiscLib::Vector< std::pair< int, size_t > > *labels) const
{
// default: do nothing
}
bool BitmapPrimitiveShape::Init(bool binary, std::istream *i)
{
if(binary)
{
GfxTL::AABox< GfxTL::Vector2Df > bbox;
size_t uextent, vextent;
// read number of components
size_t size;
i->read((char *)&size, sizeof(size));
if(size)
{
// read bbox
i->read((char *)&bbox, sizeof(bbox));
// read uextent and vextent
i->read((char *)&uextent, sizeof(uextent));
i->read((char *)&vextent, sizeof(vextent));
// read every component
for(size_t j = 0; j < size; ++j)
{
// read number of polys in component
size_t numPolys;
i->read((char *)&numPolys, sizeof(numPolys));
for(size_t k = 0; k < numPolys; ++k)
{
// read number of points in poly
size_t numPoints;
i->read((char *)&numPoints, sizeof(numPoints));
GfxTL::VectorXD< 2, size_t > pp;
for(size_t l = 0; l < numPoints; ++l)
i->read((char *)&pp, sizeof(pp));
}
}
}
}
else
{
GfxTL::AABox< GfxTL::Vector2Df > bbox;
size_t uextent, vextent;
// read number of components
size_t size;
(*i) >> size;
if(size)
{
// read bbox
(*i) >> bbox.Min()[0] >> bbox.Max()[0]
>> bbox.Min()[1] >> bbox.Max()[1];
// read uextent and vextent
(*i) >> uextent >> vextent;
// read every component
for(size_t j = 0; j < size; ++j)
{
// read number of polys in component
size_t numPolys;
(*i) >> numPolys;
for(size_t k = 0; k < numPolys; ++k)
{
// read number of points in poly
size_t numPoints;
(*i) >> numPoints;
GfxTL::VectorXD< 2, size_t > pp;
for(size_t l = 0; l < numPoints; ++l)
(*i) >> pp[0] >> pp[1];
}
}
}
}
return true;
}
size_t BitmapPrimitiveShape::AllConnectedComponents(const PointCloud &pc, float epsilon,
BitmapInfo& bitmapInfo, MiscLib::Vector< size_t > *indices, MiscLib::Vector< int >& componentsImg,
MiscLib::Vector< std::pair< int, size_t > >& labels, bool doFiltering )
{
// first find the extent in the parametrization
// but remember the parametrized points for projection into a bitmap
size_t size = indices->size();
if(!size)
return 0;
// set up bitmap
MiscLib::Vector< std::pair< float, float > > extParams;
BuildBitmap(pc, &epsilon, indices->begin(), indices->end(), &bitmapInfo.params,
&bitmapInfo.bbox, &bitmapInfo.bitmap, &bitmapInfo.uextent, &bitmapInfo.vextent, &bitmapInfo.bmpIdx);
/*static int fname_int = 0;
std::ostringstream fn;
fn << "bitmapImg" << fname_int++ << ".txt";
std::ofstream file;
file.open(fn.str().c_str(), std::ios::out);
for(size_t j = 0; j < vextent; ++j)
{
for(size_t i = 0; i < uextent; ++i)
file << bitmap[j * uextent + i];
file << std::endl;
}
file.close();*/
// do a wrapping by copying pixels
PreWrapBitmap(bitmapInfo.bbox, epsilon, bitmapInfo.uextent, bitmapInfo.vextent, &bitmapInfo.bitmap);
MiscLib::Vector< char > tempBmp(bitmapInfo.bitmap.size()); // temporary bitmap object
bool uwrap, vwrap;
WrapBitmap(bitmapInfo.bbox, epsilon, &uwrap, &vwrap);
if (doFiltering)
{
// closing
DilateCross(bitmapInfo.bitmap, bitmapInfo.uextent, bitmapInfo.vextent, uwrap, vwrap, &tempBmp);
ErodeCross(tempBmp, bitmapInfo.uextent, bitmapInfo.vextent, uwrap, vwrap, &bitmapInfo.bitmap);
// opening
//ErodeCross(bitmap, uextent, vextent, uwrap, vwrap, &tempBmp);
//DilateCross(tempBmp, uextent, vextent, uwrap, vwrap, &bitmap);
}
Components(bitmapInfo.bitmap, bitmapInfo.uextent, bitmapInfo.vextent, uwrap, vwrap, &componentsImg,
&labels);
if(labels.size() <= 1) // found no connected component!
{
return 0; // associate no points with this shape
}
WrapComponents(bitmapInfo.bbox, epsilon, bitmapInfo.uextent, bitmapInfo.vextent, &componentsImg, &labels);
return labels.size();
}
size_t BitmapPrimitiveShape::ConnectedComponent(
const PointCloud &pc, float epsilon,
MiscLib::Vector< size_t > *indices, bool doFiltering, float* borderRatio )
{
MiscLib::Vector< int > componentsImg;
MiscLib::Vector< std::pair< int, size_t > > labels;
BitmapInfo bitmapInfo;
if( AllConnectedComponents( pc, epsilon, bitmapInfo, indices, componentsImg, labels, doFiltering ) <= 1 )
return 0;
size_t size = indices->size();
MiscLib::Vector< size_t >::iterator begin = indices->begin();
// find the largest component
size_t maxComp = 1;
for(size_t i = 2; i < labels.size(); ++i)
if(labels[maxComp].second < labels[i].second)
maxComp = i;
GfxTL::AABox< GfxTL::Vector2Df > bbox;
bbox.Min() = GfxTL::Vector2Df(
std::numeric_limits< float >::infinity(),
std::numeric_limits< float >::infinity());
bbox.Max() = -bbox.Min();
// compute bbox and update indices
size_t offset = 0;
for(size_t i = 0; i < size; ++i)
{
if(componentsImg[bitmapInfo.bmpIdx[i]] == labels[maxComp].first)
{
std::swap(begin[offset], begin[i]);
offset++;
// update bounding box
if(bbox.Min()[0] > bitmapInfo.params[i].first)
bbox.Min()[0] = bitmapInfo.params[i].first;
if(bbox.Max()[0] < bitmapInfo.params[i].first)
bbox.Max()[0] = bitmapInfo.params[i].first;
if(bbox.Min()[1] > bitmapInfo.params[i].second)
bbox.Min()[1] = bitmapInfo.params[i].second;
if(bbox.Max()[1] < bitmapInfo.params[i].second)
bbox.Max()[1] = bitmapInfo.params[i].second;
}
}
// ratio between border and connected-comp size should be calculated if borderRatio is a valid pointer
if( borderRatio )
{
int borderPixels = 0;
int maxLabel = labels[maxComp].first;
int row = bitmapInfo.uextent;
int pos = 0;
char numNeighbours = 0;
int ccSize = 0;
// test neightbourhood for all bitmappixels that are not marginal
for( size_t v = 1; v < bitmapInfo.vextent-1; ++v )
{
for( size_t u = 1; u < bitmapInfo.uextent-1; ++u )
{
pos = row + u;
if( componentsImg[pos] == maxLabel )
{
ccSize++;
numNeighbours = bitmapInfo.bitmap[pos-1] + bitmapInfo.bitmap[pos+1] +
bitmapInfo.bitmap[pos-bitmapInfo.uextent-1] + bitmapInfo.bitmap[pos-bitmapInfo.uextent+1] +
bitmapInfo.bitmap[pos+bitmapInfo.uextent-1] + bitmapInfo.bitmap[pos+bitmapInfo.uextent+1] +
bitmapInfo.bitmap[pos-bitmapInfo.uextent] + bitmapInfo.bitmap[pos+bitmapInfo.uextent];
if( (int)numNeighbours != 8 )
++borderPixels;
}
}
row += bitmapInfo.uextent;
}
// check left/right margins
row = bitmapInfo.uextent;
for( size_t v = 1; v < bitmapInfo.vextent-1; ++v )
{
ccSize++;
if( componentsImg[row] == maxLabel )
++borderPixels;
ccSize++;
if( componentsImg[row+bitmapInfo.uextent-1] == maxLabel )
++borderPixels;
row += bitmapInfo.uextent;
}
// check top/bottom margins
row = ( bitmapInfo.vextent-1 ) * bitmapInfo.uextent;
for( size_t u = 0; u < bitmapInfo.uextent; ++u )
{
ccSize++;
if( componentsImg[u] == maxLabel )
++borderPixels;
ccSize++;
if( componentsImg[row + u] == maxLabel )
++borderPixels;
}
*borderRatio = static_cast<float>( borderPixels ) / static_cast<float>( ccSize );
}
m_extBbox = bbox;
return offset;
}
void BitmapPrimitiveShape::TrimmingPolygons(const PointCloud &pc,
float epsilon, size_t begin, size_t end,
std::deque< ComponentPolygons > *polys) const
{
GfxTL::AABox< GfxTL::Vector2Df > bbox;
size_t uextent, vextent;
BuildPolygons(pc, epsilon, begin, end, &bbox, &uextent, &vextent, polys);
}
void BitmapPrimitiveShape::GenerateBitmapPoints(const PointCloud &pc,
float epsilon, size_t begin, size_t end, PointCloud *bmpPc) const
{
// constructing the bitmap is similar to ConnectedComponent
MiscLib::Vector< std::pair< float, float > > params, extParams;
MiscLib::Vector< char > bitmap;
MiscLib::Vector< size_t > bmpIdx;
GfxTL::AABox< GfxTL::Vector2Df > bbox;
size_t uextent, vextent;
BuildBitmap(pc, &epsilon, IndexIterator(begin), IndexIterator(end), ¶ms,
&bbox, &bitmap, &uextent, &vextent, &bmpIdx);
m_extBbox = bbox;
//// do closing
//MiscLib::Vector< char > tempBmp(bitmap.size());
//DilateSquare(bitmap, uextent, vextent, false, false, &tempBmp);
//ErodeSquare(tempBmp, uextent, vextent, false, false, &bitmap);
//// do opening
//ErodeSquare(bitmap, uextent, vextent, false, false, &tempBmp);
//DilateSquare(tempBmp, uextent, vextent, false, false, &bitmap);
if(bmpPc)
{
size_t count = 0;
for(size_t i = 0; i < vextent * uextent; ++i)
{
// create point on surface
if(!bitmap[i])
continue;
if(count >= bmpPc->size())
bmpPc->resize(2 * count + 1);
if(InSpace(i % uextent, i / uextent, epsilon, bbox, uextent,
vextent, &(*bmpPc)[count].pos, &(*bmpPc)[count].normal))
++count;
}
bmpPc->resize(count);
}
}
void BitmapPrimitiveShape::BuildPolygons(const PointCloud &pc, float epsilon,
size_t begin, size_t end, GfxTL::AABox< GfxTL::Vector2Df > *bbox,
size_t *uextent, size_t *vextent,
std::deque< ComponentPolygons > *polys) const
{
// curves are extracted in the following way:
// first the bitmap is constructed
// then connected components are found
// for each component the curves are found
// constructing the bitmap is similar to ConnectedComponent
// -> use the same code
MiscLib::Vector< std::pair< float, float > > params;
MiscLib::Vector< char > bitmap;
MiscLib::Vector< size_t > bmpIdx;
BuildBitmap(pc, &epsilon, IndexIterator(begin), IndexIterator(end), ¶ms,
bbox, &bitmap, uextent, vextent, &bmpIdx);
// do closing
MiscLib::Vector< char > tempBmp(bitmap.size());
DilateCross(bitmap, *uextent, *vextent, false, false, &tempBmp);
ErodeCross(tempBmp, *uextent, *vextent, false, false, &bitmap);
// find connected components
MiscLib::Vector< int > componentsImg;
MiscLib::Vector< std::pair< int, size_t > > labels;
Components(bitmap, *uextent, *vextent, false, false, &componentsImg,
&labels);
if(labels.size() <= 1) // found no connected component!
return;
// for each component find all polygons
for(size_t i = 1; i < labels.size(); ++i)
{
polys->resize(polys->size() + 1);
ComponentLoops(componentsImg, *uextent, *vextent, labels[i].first,
false, false, &(*polys)[polys->size() - 1]);
}
}