EuclidianExtractor.hpp 21.5 KB
Newer Older
expoclient's avatar
expoclient committed
1
2
3
4
5
6
7
8
9
10
11
#ifndef CLOUDLIB_EUCLIDIANEXTRACTOR_HPP_
#define CLOUDLIB_EUCLIDIANEXTRACTOR_HPP_

#include <pcl/common/transforms.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/point_types.h>
#include <pcl/segmentation/extract_clusters.h>

#include "ClusterMesh.hpp"
#include "SmartCloud.hpp"

12
13
14
namespace ofxCloudLib
{

15
class EuclidianExtractor : public ofThread
16
17
18
{
  public:
    EuclidianExtractor()
expoclient's avatar
expoclient committed
19
    {
20
21
22
23
24
25
26
        parameters.setName("Euclidian Extraction");
        parameters.add(cluster_distance_tolerance.set("tolerance", .15, .01, 1));
        parameters.add(cluster_minimum_points.set("minimum points", 250, 1, 500));
        parameters.add(cluster_dither_amount.set("dither", 0.4, 0, 10));
        parameters.add(cluster_voxel_resolution.set("voxel resolution", .03, .01, .5));
        parameters.add(cluster_historical_threshold.set("cluster histo thresh", .3, .1, 1));
        parameters.add(cluster_halo.set("halo de tolérance", 0.2, 0, 2));
27
        startThread();
28
29
    };

30
31
32
33
34
35
36
    ~EuclidianExtractor()
    {
        extracted_.close();
        to_extract_.close();
        waitForThread(true);
    }

37
38
39
40
    void draw()
    {
        for (const auto &clustermesh : clusterMeshes) {
            clustermesh->draw();
expoclient's avatar
expoclient committed
41
        }
42
43
44
45
46
47
48
49
    }

    void toPCL(const ofMesh &mesh, pcl::PointCloud<pcl::PointXYZ>::Ptr pc)
    {
        pc->clear();
        for (unsigned int i = 0; i < mesh.getVertices().size(); i++) {
            const ofVec3f &v = mesh.getVertices()[i];
            pc->push_back(pcl::PointXYZ(v.x, v.y, v.z));
expoclient's avatar
expoclient committed
50
        }
51
52
53
54
55
56
57
    }

    double random_real()
    {
        return double(rand()) / double(RAND_MAX);
    }

58
59
60
61
62
63
    void threadedFunction()
    {
        pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;

        while (to_extract_.receive(cloud)) {

64
65
66


  //              TS_START("EuclidianExtraction");
67
68
69

                std::vector<pcl::PointIndices> cluster_indices;
                pcl::PointCloud<pcl::PointXYZRGBA>::Ptr supercloudFilteredRGBA;
70
71
72
                supercloudFilteredRGBA.reset(new pcl::PointCloud<pcl::PointXYZRGBA>);

                auto fresh_clouds = make_shared<vector<shared_ptr<SmartCloud>>>();
73

74
            if (cloud->size() > 10) {
75
76
77
78
79
80
                pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
                tree->setInputCloud(cloud);

                pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
                ec.setClusterTolerance(cluster_distance_tolerance);
                ec.setMinClusterSize(cluster_minimum_points);
81
                ec.setMaxClusterSize(100000); // whatever? mettre 100k au lieu de 25k semble regler un crash rare
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
                ec.setSearchMethod(tree);
                ec.setInputCloud(cloud);
                ec.extract(cluster_indices);


                for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
                    // nécessaire de copier, ou simplement passer des indices???
                    fresh_clouds->push_back(shared_ptr<SmartCloud>(new SmartCloud));
                    for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); ++pit) {
                        fresh_clouds->back().get()->cloud->points.push_back(cloud->points[*pit]);
                        pcl::PointXYZRGBA p;

                        double ditherX = (random_real() * (cluster_voxel_resolution * cluster_dither_amount)) - cluster_voxel_resolution * (cluster_dither_amount / 2);
                        double ditherY = (random_real() * (cluster_voxel_resolution * cluster_dither_amount)) - cluster_voxel_resolution * (cluster_dither_amount / 2);
                        double ditherZ = (random_real() * (cluster_voxel_resolution * cluster_dither_amount)) - cluster_voxel_resolution * (cluster_dither_amount / 2);
                        p.x            = cloud->points[*pit].x + ditherX;
                        p.y            = cloud->points[*pit].y + ditherY;
                        p.z            = cloud->points[*pit].z + ditherZ;
                        supercloudFilteredRGBA->points.push_back(p);
                    }
                    fresh_clouds->back().get()->setup();
103
                }
104
            }
105

106
107
108
                vector<shared_ptr<ClusterMesh>>::iterator clusterMesh;
                vector<shared_ptr<SmartCloud>> candidateClouds = newClouds;
                vector<shared_ptr<SmartCloud>> nextCandidateClouds;
109

110
                for (clusterMesh = clusterMeshes.begin(); clusterMesh != clusterMeshes.end(); clusterMesh++) {
111

112
113
114
                    bool orphan = true;
                    vector<shared_ptr<SmartCloud>>::iterator newCloud;
                    for (newCloud = candidateClouds.begin(); newCloud != candidateClouds.end(); newCloud++) {
115

116
117
118
119
120
121
122
123
                        Eigen::Vector4f difference = (*clusterMesh)->smartCloud->centroid - (*newCloud)->centroid;
                        float d                    = sqrt(pow(difference[0], 2) + pow(difference[1], 2) + pow(difference[2], 2));
                        if (d < cluster_historical_threshold) {
                            //                std::cout << "maintain: d = " << d <<std::endl;
                            (*clusterMesh)->smartCloud = *newCloud;
                            (*clusterMesh)->setupMesh();
                            (*clusterMesh)->lostTime = 0;
                            orphan                   = false;
124
125

#define HALO .2
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
                        } else if ((*clusterMesh)->lost == true) {
                            if ((((*newCloud)->min[0] < (*clusterMesh)->max[0] + cluster_halo) && ((*newCloud)->max[0] > (*clusterMesh)->min[0] - cluster_halo))) {
                                // overlap X

                                if ((((*newCloud)->min[1] < (*clusterMesh)->max[1] + cluster_halo) && ((*newCloud)->max[1] > (*clusterMesh)->min[1] - cluster_halo))) {
                                    // overlap X+Y

                                    std::cout << "OVERLAP 1 " << std::endl;
                                    (*clusterMesh)->smartCloud = *newCloud;
                                    (*clusterMesh)->setupMesh();
                                    (*clusterMesh)->lostTime    = 0;
                                    (*clusterMesh)->activeSpree = 0;
                                    (*clusterMesh)->lost        = false;
                                    //                       newCloud = candidateClouds.erase(newCloud);
                                    orphan = false;
                                    //                        break;
                                }
143
144
                            }

145
146
147
                        } else {
                            nextCandidateClouds.push_back(*newCloud);
                        }
148
                    }
149
150
151
                    (*clusterMesh)->lost = orphan;
                    candidateClouds      = nextCandidateClouds;
                    nextCandidateClouds.clear();
152
153
                }

154
                /*
155
156
157
158
159
             3) pour chaque mesh restant:
             flag de la mort (fadeout)
             si flag < 0 = retrait du mesh
             */

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
                for (clusterMesh = clusterMeshes.begin(); clusterMesh != clusterMeshes.end();) {
                    (*clusterMesh)->age++;
                    if ((*clusterMesh)->lost == true) {


                        bool reborn = false;
                        vector<shared_ptr<SmartCloud>>::iterator c;
                        for (c = candidateClouds.begin(); c != candidateClouds.end(); c++) {

                            // test x

                            //                        if ((((*c)->min[0] < (*clusterMesh)->max[0]) && ((*c)->max[0] > (*clusterMesh)->min[0]))) {
                            //                            // overlap X
                            //                            if ((((*c)->min[1] < (*clusterMesh)->max[1]) && ((*c)->max[1] > (*clusterMesh)->min[1]))) {
                            //                                // overlap X+Y
                            if ((((*c)->min[0] < (*clusterMesh)->max[0] + cluster_halo) && ((*c)->max[0] > (*clusterMesh)->min[0] - cluster_halo))) {
                                // overlap X
                                if ((((*c)->min[1] < (*clusterMesh)->max[1] + cluster_halo) && ((*c)->max[1] > (*clusterMesh)->min[1] - cluster_halo))) {
                                    // overlap X+Y
                                    std::cout << "OVERLAP 2 avec tolérance" << cluster_halo << std::endl;
                                    (*clusterMesh)->smartCloud = *c;
                                    (*clusterMesh)->setupMesh();
                                    (*clusterMesh)->lostTime    = 0;
                                    (*clusterMesh)->lost        = false;
                                    (*clusterMesh)->activeSpree = 0;
                                    std::cout << "delete cloud " << std::endl;
                                    c      = candidateClouds.erase(c);
                                    reborn = true;
                                    break;
                                }
190
191
192
                            }
                        }

193
194
195
196
197
198
199
                        if (!reborn) {
                            (*clusterMesh)->lostTime++;
                            if ((*clusterMesh)->lostTime > 2) {
                                std::cout << "delete clustermesh " << std::endl;
                                clusterMeshBin.push_back(*clusterMesh);
                                clusterMesh = clusterMeshes.erase(clusterMesh);
                            }
200
201
                        }

202
203
204
205
                    } else {
                        (*clusterMesh)->activeSpree++;
                        ++clusterMesh;
                    }
206
207
                }

208
             /*
209
210
211
212
213
214
215
216
217
             
             2) pour chaque cloud non-associé:
             on regarde les mesh restant
             si un mesh est assez proche (seuil2)
             cloud = mesh -> associated = true
             sinon: newmesh!
             
             */

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
                vector<shared_ptr<SmartCloud>>::iterator newCloud;
                for (newCloud = candidateClouds.begin(); newCloud != candidateClouds.end(); newCloud++) {
                    std::cout << "new a clusterMesh" << std::endl;

                    if (clusterMeshBin.size() > 0) {
                        shared_ptr<ClusterMesh> revived = clusterMeshBin.back();
                        revived->smartCloud             = *newCloud;
                        revived->fresh                  = true;
                        revived->activeSpree            = 0;
                        revived->setupMesh();
                        clusterMeshes.push_back(revived);
                        clusterMeshBin.pop_back();

                    } else {
                        clusterMeshes.push_back(shared_ptr<ClusterMesh>(new ClusterMesh));
                        clusterMeshes.back().get()->smartCloud = *newCloud;
                        clusterMeshes.back().get()->setupFont();
                        clusterMeshes.back().get()->setupMesh(colors[clusterCounter % 12], clusterCounter);
                        clusterCounter++;
                    }
238
                }
239
                extracted_.send(fresh_clouds);
240
241
    //            TS_STOP("EuclidianExtraction");
 //           }
242
243
244
245
246
247
248
249
250
251
        }
    }

    void send(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
    {
        to_extract_.send(cloud);
    }

    bool receive()
    {
252
        if (extracted_.tryReceive(extracted_clouds_)) {
253
            return true;
254
255
        } else {
         //   ofLogNotice("EuclidianExtraction") << "nothing to receive";
256
257
258
259
        }
        return false;
    }

260
    shared_ptr<vector<shared_ptr<SmartCloud>>> get_clouds()
261
262
263
264
    {
        return extracted_clouds_;
    }

265
266
267
    vector<shared_ptr<SmartCloud>> *extract(const ofMesh &mesh)
    {

268
269
        ofLogNotice("Extractor") << "extract";

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
        cloud_.reset(new pcl::PointCloud<pcl::PointXYZ>);

        std::vector<pcl::PointIndices> cluster_indices;
        pcl::PointCloud<pcl::PointXYZRGBA>::Ptr supercloudFilteredRGBA;

        toPCL(mesh, cloud_);

        pcl::VoxelGrid<pcl::PointXYZ> voxel_filter;
        voxel_filter.setInputCloud(cloud_);
        voxel_filter.setLeafSize(cluster_voxel_resolution, cluster_voxel_resolution, cluster_voxel_resolution);
        voxel_filter.filter(*cloud_);

        pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
        tree->setInputCloud(cloud_);

        pcl::EuclideanClusterExtraction<pcl::PointXYZ> ec;
        ec.setClusterTolerance(cluster_distance_tolerance);
        ec.setMinClusterSize(cluster_minimum_points);
        ec.setMaxClusterSize(25000); // whatever?
        ec.setSearchMethod(tree);
        ec.setInputCloud(cloud_);
        ec.extract(cluster_indices);

        supercloudFilteredRGBA.reset(new pcl::PointCloud<pcl::PointXYZRGBA>);

        newClouds.clear();
        for (std::vector<pcl::PointIndices>::const_iterator it = cluster_indices.begin(); it != cluster_indices.end(); ++it) {
            // nécessaire de copier, ou simplement passer des indices???
            newClouds.push_back(shared_ptr<SmartCloud>(new SmartCloud));
            for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end(); ++pit) {
                newClouds.back().get()->cloud->points.push_back(cloud_->points[*pit]);
                pcl::PointXYZRGBA p;

                double ditherX = (random_real() * (cluster_voxel_resolution * cluster_dither_amount)) - cluster_voxel_resolution * (cluster_dither_amount / 2);
                double ditherY = (random_real() * (cluster_voxel_resolution * cluster_dither_amount)) - cluster_voxel_resolution * (cluster_dither_amount / 2);
                double ditherZ = (random_real() * (cluster_voxel_resolution * cluster_dither_amount)) - cluster_voxel_resolution * (cluster_dither_amount / 2);
                p.x            = cloud_->points[*pit].x + ditherX;
                p.y            = cloud_->points[*pit].y + ditherY;
                p.z            = cloud_->points[*pit].z + ditherZ;
                supercloudFilteredRGBA->points.push_back(p);
expoclient's avatar
expoclient committed
310
            }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
            newClouds.back().get()->setup();
        }

        vector<shared_ptr<ClusterMesh>>::iterator clusterMesh;
        vector<shared_ptr<SmartCloud>> candidateClouds = newClouds;
        vector<shared_ptr<SmartCloud>> nextCandidateClouds;

        for (clusterMesh = clusterMeshes.begin(); clusterMesh != clusterMeshes.end(); clusterMesh++) {

            bool orphan = true;
            vector<shared_ptr<SmartCloud>>::iterator newCloud;
            for (newCloud = candidateClouds.begin(); newCloud != candidateClouds.end(); newCloud++) {

                Eigen::Vector4f difference = (*clusterMesh)->smartCloud->centroid - (*newCloud)->centroid;
                float d                    = sqrt(pow(difference[0], 2) + pow(difference[1], 2) + pow(difference[2], 2));
                if (d < cluster_historical_threshold) {
                    //                std::cout << "maintain: d = " << d <<std::endl;
                    (*clusterMesh)->smartCloud = *newCloud;
                    (*clusterMesh)->setupMesh();
                    (*clusterMesh)->lostTime = 0;
                    orphan                   = false;

expoclient's avatar
expoclient committed
333
#define HALO .2
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
                } else if ((*clusterMesh)->lost == true) {
                    if ((((*newCloud)->min[0] < (*clusterMesh)->max[0] + cluster_halo) && ((*newCloud)->max[0] > (*clusterMesh)->min[0] - cluster_halo))) {
                        // overlap X

                        if ((((*newCloud)->min[1] < (*clusterMesh)->max[1] + cluster_halo) && ((*newCloud)->max[1] > (*clusterMesh)->min[1] - cluster_halo))) {
                            // overlap X+Y

                            std::cout << "OVERLAP 1 " << std::endl;
                            (*clusterMesh)->smartCloud = *newCloud;
                            (*clusterMesh)->setupMesh();
                            (*clusterMesh)->lostTime    = 0;
                            (*clusterMesh)->activeSpree = 0;
                            (*clusterMesh)->lost        = false;
                            //                       newCloud = candidateClouds.erase(newCloud);
                            orphan = false;
                            //                        break;
expoclient's avatar
expoclient committed
350
351
                        }
                    }
352
353
354

                } else {
                    nextCandidateClouds.push_back(*newCloud);
expoclient's avatar
expoclient committed
355
356
                }
            }
357
358
359
360
361
362
            (*clusterMesh)->lost = orphan;
            candidateClouds      = nextCandidateClouds;
            nextCandidateClouds.clear();
        }

        /*
expoclient's avatar
expoclient committed
363
364
365
366
             3) pour chaque mesh restant:
             flag de la mort (fadeout)
             si flag < 0 = retrait du mesh
             */
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

        for (clusterMesh = clusterMeshes.begin(); clusterMesh != clusterMeshes.end();) {
            (*clusterMesh)->age++;
            if ((*clusterMesh)->lost == true) {


                bool reborn = false;
                vector<shared_ptr<SmartCloud>>::iterator c;
                for (c = candidateClouds.begin(); c != candidateClouds.end(); c++) {

                    // test x

                    //                        if ((((*c)->min[0] < (*clusterMesh)->max[0]) && ((*c)->max[0] > (*clusterMesh)->min[0]))) {
                    //                            // overlap X
                    //                            if ((((*c)->min[1] < (*clusterMesh)->max[1]) && ((*c)->max[1] > (*clusterMesh)->min[1]))) {
                    //                                // overlap X+Y
                    if ((((*c)->min[0] < (*clusterMesh)->max[0] + cluster_halo) && ((*c)->max[0] > (*clusterMesh)->min[0] - cluster_halo))) {
                        // overlap X
                        if ((((*c)->min[1] < (*clusterMesh)->max[1] + cluster_halo) && ((*c)->max[1] > (*clusterMesh)->min[1] - cluster_halo))) {
                            // overlap X+Y
                            std::cout << "OVERLAP 2 avec tolérance" << cluster_halo << std::endl;
                            (*clusterMesh)->smartCloud = *c;
                            (*clusterMesh)->setupMesh();
                            (*clusterMesh)->lostTime    = 0;
                            (*clusterMesh)->lost        = false;
                            (*clusterMesh)->activeSpree = 0;
                            std::cout << "delete cloud " << std::endl;
                            c      = candidateClouds.erase(c);
                            reborn = true;
                            break;
expoclient's avatar
expoclient committed
397
398
                        }
                    }
399
400
401
402
403
404
405
406
                }

                if (!reborn) {
                    (*clusterMesh)->lostTime++;
                    if ((*clusterMesh)->lostTime > 2) {
                        std::cout << "delete clustermesh " << std::endl;
                        clusterMeshBin.push_back(*clusterMesh);
                        clusterMesh = clusterMeshes.erase(clusterMesh);
expoclient's avatar
expoclient committed
407
408
                    }
                }
409
410
411
412

            } else {
                (*clusterMesh)->activeSpree++;
                ++clusterMesh;
expoclient's avatar
expoclient committed
413
            }
414
415
416
        }

        /*
expoclient's avatar
expoclient committed
417
418
419
420
421
422
423
424
             
             2) pour chaque cloud non-associé:
             on regarde les mesh restant
             si un mesh est assez proche (seuil2)
             cloud = mesh -> associated = true
             sinon: newmesh!
             
             */
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

        vector<shared_ptr<SmartCloud>>::iterator newCloud;
        for (newCloud = candidateClouds.begin(); newCloud != candidateClouds.end(); newCloud++) {
            std::cout << "new a clusterMesh" << std::endl;

            if (clusterMeshBin.size() > 0) {
                shared_ptr<ClusterMesh> revived = clusterMeshBin.back();
                revived->smartCloud             = *newCloud;
                revived->fresh                  = true;
                revived->activeSpree            = 0;
                revived->setupMesh();
                clusterMeshes.push_back(revived);
                clusterMeshBin.pop_back();

            } else {
                clusterMeshes.push_back(shared_ptr<ClusterMesh>(new ClusterMesh));
                clusterMeshes.back().get()->smartCloud = *newCloud;
                clusterMeshes.back().get()->setupFont();
                clusterMeshes.back().get()->setupMesh(colors[clusterCounter % 12], clusterCounter);
                clusterCounter++;
expoclient's avatar
expoclient committed
445
446
            }
        }
447
448
449
450
451
452
453
454
455
456
457
458
        return &newClouds;
    }

    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_;
    pcl::PointCloud<pcl::PointXYZRGBA>::Ptr supercloudFilteredRGBA;

    std::vector<pcl::PointIndices> cluster_indices;
    vector<shared_ptr<SmartCloud>> newClouds;

    vector<shared_ptr<ClusterMesh>> clusterMeshes;
    deque<shared_ptr<ClusterMesh>> clusterMeshBin;

459
460
461
462
463
    ofThreadChannel<pcl::PointCloud<pcl::PointXYZ>::Ptr> to_extract_;
    ofThreadChannel<shared_ptr<vector<shared_ptr<SmartCloud>>>> extracted_;

    shared_ptr<vector<shared_ptr<SmartCloud>>> extracted_clouds_;

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
    ofParameterGroup parameters;
    ofParameter<float> cluster_distance_tolerance;
    ofParameter<float> cluster_minimum_points;
    ofParameter<float> cluster_dither_amount;
    ofParameter<float> cluster_voxel_resolution;
    ofParameter<float> cluster_historical_threshold;
    ofParameter<float> cluster_halo;

    int clusterCounter = 0;

    vector<ofFloatColor> colors = {
        ofFloatColor(1, 1, 0, 1),
        ofFloatColor(1, 0, 1, 1),
        ofFloatColor(0, 1, 1, 1),
        ofFloatColor(1, 0, 0, 1),
        ofFloatColor(0, 1, 0, 1),
        ofFloatColor(.1, .1, 1, 1),
        ofFloatColor(1, .5, 0, 1),
        ofFloatColor(.5, 1, 0, 1),
        ofFloatColor(0, .5, 1, 1),
        ofFloatColor(0, 1, .5, 1),
        ofFloatColor(.5, 0, 255, 1),
        ofFloatColor(1, 0, .5, 1)};
};
expoclient's avatar
expoclient committed
488
489
490
}

#endif // CLOUDLIB_EUCLIDIANEXTRACTOR_HPP_