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
67
//            if (cloud->size() > 10) {


  //              TS_START("EuclidianExtraction");
68
69
70
71
72
73
74
75
76
77

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

                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);
78
                ec.setMaxClusterSize(100000); // whatever? mettre 100k au lieu de 25k semble regler un crash rare
79
80
81
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);

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

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

                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
                vector<shared_ptr<ClusterMesh>>::iterator clusterMesh;
                vector<shared_ptr<SmartCloud>> candidateClouds = newClouds;
                vector<shared_ptr<SmartCloud>> nextCandidateClouds;
108

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

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

115
116
117
118
119
120
121
122
                        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;
123
124

#define HALO .2
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
                        } 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;
                                }
142
143
                            }

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

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

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
                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;
                                }
189
190
191
                            }
                        }

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

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

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

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
                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++;
                    }
237
                }
238
                extracted_.send(fresh_clouds);
239
240
    //            TS_STOP("EuclidianExtraction");
 //           }
241
242
243
244
245
246
247
248
249
250
        }
    }

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

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

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

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

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

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
        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
309
            }
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
            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
332
#define HALO .2
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
                } 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
349
350
                        }
                    }
351
352
353

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

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

        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
396
397
                        }
                    }
398
399
400
401
402
403
404
405
                }

                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
406
407
                    }
                }
408
409
410
411

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

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

        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
444
445
            }
        }
446
447
448
449
450
451
452
453
454
455
456
457
        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;

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

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

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    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
487
488
489
}

#endif // CLOUDLIB_EUCLIDIANEXTRACTOR_HPP_