-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmenter.cpp
419 lines (353 loc) · 11.1 KB
/
segmenter.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <fstream>
#include <iterator>
#include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <list>
#include <stdio.h>
#include <math.h>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
using std::vector;
using std::string;
using std::list;
static const bool DEBUG_PRINT = true;
static const float MAX_DIST = .15*.15; //Maximum allowed distance between points of object. Remember to square
static const float MIN_DEPTH = .01;//Throw away point if closer
static const float MAX_COORD_VALUE = 30;
//Quad identification constants
static const int MIN_QUAD_PTS = 500;//Minimum number of points for object to be considered a quad
static const float MAX_QUAD_DEPTH = .5;//Maximum depth of quad in meters
static const float MIN_QUAD_WIDTH = .1;
static const float MAX_QUAD_WIDTH = .6;
static const float MIN_QUAD_HEIGHT = .15;
static const float MAX_QUAD_HEIGHT = .6;
static const float NOT_FOUND_SENTINEL_VALUE = -99; //Return if quad not found
vector<int> foo;
vector<vector<vector<float> > > segObjects;
struct Point4 {
public:
float depth, x, y, z;
};
struct depthCmp
{
bool operator()(const Point4* lhs,
const Point4* rhs) const
{
return lhs->depth < rhs->depth;
}
};
void sortDepth(vector<Point4* > toSort) {
//sort notSeg by depth
std::sort(toSort.begin(), toSort.end(), depthCmp());
}
//Calculates distance between 2 points
float distance(Point4& v1, Point4& v2) {
float xDif = (v1.x - v2.x);
float yDif=(v1.y - v2.y);
float zDif=(v1.z - v2.z);
return xDif*xDif + yDif*yDif + zDif * zDif;
}
void writeCloud(vector<Point4* > cloud, string name) {
//if (DEBUG_PRINT) printf("writeCloud running. 1st depth: %f\n", cloud.at(0)->depth);
std::ofstream outputFile(name);
std::ostream_iterator<float> output_iterator(outputFile, " ");
BOOST_FOREACH(Point4* point, cloud){
std::copy(&point->depth, (&point->z)+1, output_iterator);
outputFile << "\n";
}
outputFile.close();
}
void writeCloudList(vector<vector<Point4* > > cloudList ){
int cloudNum = 0;
BOOST_FOREACH(vector<Point4* > toWrite, cloudList) {
writeCloud(toWrite, "./processed/proccloud"+std::to_string(cloudNum)+".txt");
cloudNum++;
}
}
class Float4 {
public:
float data[4];
};
void readCloud(string fileName, vector<Float4 >& cloud) {
int rowNum = 0;
std::ifstream inputFile(fileName);
while (!inputFile.eof()) {
Float4 tmpPt;
float tmpFloat;
for (int j = 0; j < 4; j++) {
inputFile >> tmpFloat;
//if (DEBUG_PRINT) printf("data: %f\n", tmpFloat);
//if (inputFile.eof()) return;
tmpPt.data[j] = tmpFloat;
}
cloud.push_back(tmpPt);
// if (DEBUG_PRINT) printf("Row Number: %d\n", ++rowNum);
}
}
void generateObject(list<Point4*>& pointList, vector<Point4* >& inCloud) {
list<Point4* >::iterator nsIt;
Point4* closestPoint = pointList.front();
inCloud.push_back(closestPoint);
pointList.pop_front();
for (int i = 0; i < (int)inCloud.size(); i++) {//not at end of incloud
Point4& basePoint = *inCloud.at(i);
nsIt = pointList.begin();
while (true) {
if (nsIt != pointList.end()) {
Point4& checkPoint = **nsIt;
if (checkPoint.depth - basePoint.depth > MAX_DIST) break;
float distBet = distance(basePoint, checkPoint);
if (distBet < MAX_DIST) {
inCloud.push_back(&checkPoint);
nsIt = pointList.erase(nsIt);
}
else nsIt++;
}
else break;
}
}
}
void segmentCloudEfficient(float toSegment[][4], int size, vector<vector<Point4* > >& objList) {
list<Point4* > notSeg;
for (int i = 0; i < size; i++) {
if (toSegment[i][0] < MIN_DEPTH || std::isnan(toSegment[i][0])) continue;
if (toSegment[i][1] > MAX_COORD_VALUE || std::isnan(toSegment[i][1])) continue;
if (toSegment[i][2] > MAX_COORD_VALUE || std::isnan(toSegment[i][2])) continue;
if (toSegment[i][3] > MAX_COORD_VALUE || std::isnan(toSegment[i][3])) continue;
notSeg.push_back((Point4*) &(toSegment[i][0]));
}
notSeg.sort(depthCmp());
if (DEBUG_PRINT) printf("Generating objects\n");
while (!notSeg.empty()) {
vector<Point4* > solid;
generateObject(notSeg, solid);
objList.push_back(solid);
}
if (DEBUG_PRINT) printf("Objects generated\n");
}
//returns box width and center of dim
void getObjectDims(vector<Point4* >& solid, int dim, float* objDims) {
float max= -100;//Explore min_value if necessary
float min = 100;
float ptSums = 0;
for (int i = 0; i < solid.size(); i++) {
float pos;
switch (dim) {
case 0:
pos = solid.at(i)->x;
break;
case 1:
pos = solid.at(i)->y;
break;
case 2:
pos = solid.at(i)->z;
break;
default:
break;
}
ptSums += pos;
if (pos > max) max = pos;
if (pos < min) min = pos;
}
//if(DEBUG_PRINT) printf("sum of points: %f\n" ,ptSums);
ptSums /= solid.size();
//if (DEBUG_PRINT) printf("center: %f\n", ptSums);
//if (DEBUG_PRINT) printf("max: %f\n", max);
//if (DEBUG_PRINT) printf("min: %f\n", min);
objDims[0] = max - min;
objDims[1] = ptSums;
}
//Returns center of quad (for now list of quad objects)
void locateQuad(float* xPtr, float* yPtr, float* zPtr, vector<vector<Point4* > >& objList) {
//vector<int> potentialQuads;
for (int i = 0; i < objList.size(); i++) {
vector<Point4* >& toCheck = objList.at(i);
if (toCheck.size() < MIN_QUAD_PTS) continue;
if (toCheck.at(toCheck.size() - 1)->depth - toCheck.at(0)->depth > MAX_QUAD_DEPTH) continue;
//get object width
float width[2];
getObjectDims(toCheck, 0, width);
if (DEBUG_PRINT) printf("width: %f\n", width[0]);
if (width[0]<MIN_QUAD_WIDTH || width[0]>MAX_QUAD_WIDTH) continue;
float height[2];
getObjectDims(toCheck, 1, height);
if (DEBUG_PRINT) printf("height: %f\n", height[0]);
if (height[0]<MIN_QUAD_HEIGHT || height[0]>MAX_QUAD_HEIGHT) continue;
if(DEBUG_PRINT) printf("quad loc: %f, %f\n", width[1], height[1]);
*zPtr = toCheck.at(i)->z;
*xPtr = width[1];
*yPtr = height[1];
return;
//potentialQuads.push_back(i);
}
*xPtr = NOT_FOUND_SENTINEL_VALUE;
*yPtr = NOT_FOUND_SENTINEL_VALUE;
*zPtr = NOT_FOUND_SENTINEL_VALUE;
//return potentialQuads;
}
extern "C" {
int answer() {
foo.push_back(3);
return foo.at(0);
}
void segmentQuad(float toSegment[][4], int size, float*xLoc, float* yLoc, float* zLoc ) {
vector<vector<Point4* > > segCloud;
if (DEBUG_PRINT) printf("cpp called\n");
segmentCloudEfficient(toSegment, size, segCloud);
if (DEBUG_PRINT) printf("cloud segmented\n");
//if(DEBUG_PRINT) writeCloudList(segCloud);
locateQuad(xLoc, yLoc, zLoc, segCloud);
if (DEBUG_PRINT) printf("quad located\n");
}
}
const int numElements = 100000;
float testCloud[numElements][4];
int main(int argc, char **argv) {
// for (int i = 0; i < numElements/2; i++) {
///* vector<float> ta = { (float)i,(float)i,(float)i, (float)i };
// testCloud.push_back(ta);*/
// for (int j = 0; j < 4; j++) {
// testCloud[i][j] = (float)i;
// }
// }
// for (int i = (numElements / 2) + 10; i < numElements; i++) {
// /*vector<float> ta = { (float)i,(float)i,(float)i, (float)i };
// testCloud.push_back(ta);*/
// for (int j = 0; j < 4; j++) {
// testCloud[i][j] = (float)i;
// }
// }
//float testcloud[5][4] = { { 99,99,99,99 },{ 999,999,999,999 }, { 5,5,5,5 }, { 6,6,6,6 },{ 7,7,7,7 } } ;
typedef float float4[4];
vector<Float4> ptVectors;
readCloud("img.txt", ptVectors);
float4* foo = (float4*) ptVectors.data();
float xPrint = 0;
float yPrint = 0;
float zPrint = 0;
segmentQuad(foo,ptVectors.size(), &xPrint,&yPrint, &zPrint);
if (DEBUG_PRINT) printf("quad loc: %f, %f", xPrint, yPrint);
}
float*** convertToCVector(vector<vector<vector<float> > >&vals, int X, int Y, int Z)
{
float*** temp;
temp = new float**[X];
for (int i = 0; (i < X); i++)
{
temp[i] = new float*[Y];
for (int j = 0; (j < Y); j++)
{
temp[i][j] = new float[Z];
for (int k = 0; (k < Z); k++)
{
temp[i][j][k] = vals[i][j][k];
}
}
}
return temp;
}
//int getQuadCenter(float pointCloud[][4], int numNonZero) {
// if (numNonZero == 0) return -1;
//
// vector<vector<float> > notSeg;
// vector<vector<float> > toAdd;
// vector<vector<float> > inCloud;
//
// /*for (int i = 0; i < 4; i++) {
// print notSeg[];
// }*/
//
// //transform c array to cpp array
// for (int i = 0; i < numNonZero; i++) {
// vector<float> point;
// for (int j = 0; j < 4; j++) {
// point.push_back(pointCloud[i][j]);
// }
// notSeg.push_back(point);
// }
//
//
// /* if (DEBUG_PRINT) {
// sortDepth(notSeg);
// for (int i = 0; i < 4; i++) {
// printf("%f ", notSeg.at(0).at(i));
// }
// }*/
//
// float totalDist = 0;
// //generate objects
// while (!notSeg.empty()) {
// sortDepth(notSeg);
// toAdd.push_back(notSeg.at(0));
// notSeg.erase(notSeg.begin());
// do {
// BOOST_FOREACH(vector<float> basePoint, toAdd) {
// BOOST_FOREACH(vector<float> checkPoint, notSeg) {
// if (basePoint == checkPoint) continue;
// float distBet = distance(basePoint, checkPoint);
// if (distBet < PT_DIST) {
// toAdd.push_back(checkPoint);
// notSeg.erase(std::remove(notSeg.begin(), notSeg.end(), checkPoint), notSeg.end());
// }
// }
// inCloud.push_back(basePoint);
// toAdd.erase(std::remove(toAdd.begin(), toAdd.end(), basePoint), toAdd.end());
// }
// } while (!toAdd.empty());
// }
//
//}
//vector<vector<vector<float> > > segmentCloud(vector<vector<float> > toSegment) {
// vector<vector<float> > notSeg=toSegment;
// vector<vector<vector<float> > > objList;
//
// //generate objects
// while (!notSeg.empty()) {
// //Setup temp lists (per object)
// vector<vector<float> > toAdd;
// vector<vector<float> > inCloud;
//
// //Generate one object
// sortDepth(notSeg);
// toAdd.push_back(notSeg.at(0));
// notSeg.erase(notSeg.begin());//O(n)
// do {
// //vector<vector<float> > rmToAdd;
// //vector<vector<float> > rmNotSeg;
// vector<vector<float> > nextToAdd;
// BOOST_FOREACH(vector<float> basePoint, toAdd) {
// BOOST_FOREACH(vector<float> checkPoint, notSeg) {
// //if (basePoint == checkPoint) continue;
// float distBet = distance(basePoint, checkPoint);
// if (distBet < PT_DIST) {
// nextToAdd.push_back(checkPoint);
//
// }
// if (checkPoint.at(0) - basePoint.at(0) > PT_DIST) break;
// }
//
// //remove elements in nextToAdd from notSeg
// BOOST_FOREACH(vector<float> removePoint, nextToAdd) {
// notSeg.erase(std::remove(notSeg.begin(), notSeg.end(), removePoint), notSeg.end());
// }
// }
//
// //move toAdd into incloud
// while (!toAdd.empty()) {
// inCloud.push_back(toAdd.at(0));
// toAdd.erase(toAdd.begin());
// }
//
// //setup toAdd for next iteration
// toAdd = nextToAdd;
//
//
// } while (!toAdd.empty());
//
// objList.push_back(inCloud);
// }
// return objList;
//}