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

#include "ofMain.h"
#include <boost/bimap.hpp>
#include <boost/format.hpp>
#include <pcl/point_types.h>

#include "ofxCloudLib/SmartCloud.hpp"
#include "ofxFontStash.h"

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace ofxCloudLib
{

class Singleton
{
  private:
    Singleton() {}
    ~Singleton() {}
    Singleton(const Singleton &);
    Singleton &operator=(const Singleton &);

  public:
    ofxFontStash unicodeFont;
    bool isAllocated = false;
    static Singleton &getInstance()
expoclient's avatar
expoclient committed
27
    {
28
29
30
31
32
33
34
35
36
        static Singleton _instance;
        return _instance;
    }
};

class ClusterMesh
{
  public:
    bool fresh = true;
37
    shared_ptr<ofVboMesh> mesh;
38
39
    ofConePrimitive cone;
    Eigen::Vector2f direction;
40
    ofVboMesh centroid_mesh_;
41
    float distance_;
42
43
44
45
46
47

    Eigen::Vector4f centroid;
    Eigen::Vector4f min;
    Eigen::Vector4f max;
    Eigen::Vector4f vol;

48
49
    glm::vec3 last_considered_centroid_;

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

    float speed     = 0;
    int age         = 0;
    int activeSpree = 0;
    int scale       = 1; // caduc selon nouvelle approche (données réelles; scale au render)
    int id_;
    ofxFontStash unicodeFont;
    shared_ptr<SmartCloud> smartCloud;
    bool lost    = false;
    int lostTime = 0;
    ofFloatColor color_;

    ofTrueTypeFont font;

    //   static _StaticUnicodeFont staticUnicodeFont;


    boost::bimap<shared_ptr<SmartCloud>, float> distances;

    ClusterMesh(shared_ptr<SmartCloud> cloud)
expoclient's avatar
expoclient committed
70
    {
71
72
73
74
75
76
        smartCloud = cloud;
        font.load("fonts/SourceCodePro-Bold.ttf", 10);
    }
    ClusterMesh()
    {
        //       cone.setOrientation(90, ofVec3f(0,1,0))
77
        cone.tiltDeg(-90);
78
79
80
81
82
83
84
85
86
87
        font.load("fonts/SourceCodePro-Bold.ttf", 10);
    }

    void setupFont()
    {

        Singleton &s = Singleton::getInstance();
        if (!s.isAllocated) {
            s.isAllocated = true;
            s.unicodeFont.setup("fonts/SourceCodePro-Bold.ttf", 1.0, 1024, true, 8, 2.0f);
expoclient's avatar
expoclient committed
88
        }
89
90
91
92
93
94
95
96
97
    }

    void setupMesh(ofFloatColor color, int id)
    {
        id_    = id;
        color_ = color;
        setupMesh();
    }

98
99
100
101
102
    void clear_history() {
        distance_ = 0;
        centroid_mesh_.clear();

    }
103
104
105
106
107
108
109
110
    void setupMesh()
    {

        if (fresh) {
            centroid = smartCloud->centroid;
            min      = smartCloud->min;
            max      = smartCloud->max;
            fresh    = false;
111
112
113
114
            clear_history();



expoclient's avatar
expoclient committed
115
        }
116
117
        mesh = make_shared<ofVboMesh>();
//        mesh.reset(new ofMesh);
118
119
120
121
122
        for (unsigned int qq = 0; qq < smartCloud->cloud->points.size(); qq++) {
            mesh->addVertex(ofPoint(smartCloud->cloud->points[qq].x * scale,
                                    smartCloud->cloud->points[qq].y * scale,
                                    smartCloud->cloud->points[qq].z * scale));
            mesh->addColor(color_);
expoclient's avatar
expoclient committed
123
        }
124
125
    }

expoclient's avatar
expoclient committed
126
127
128
#define GRAPPE_AGE_ADULTE 2
#define GRAPPE_AU_DELA 10
#define GRAPPE_LOWPASS_COEFF .66
129
130
131
132
133

    void update()
    {
        // lowpass de pauvre
        centroid = centroid * GRAPPE_LOWPASS_COEFF + smartCloud->centroid * (1.0 - GRAPPE_LOWPASS_COEFF);
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

        glm::vec3 centre = glm::vec3(centroid[0], centroid[1], centroid[2]);

        if (centroid_mesh_.getNumVertices() == 0) {
            last_considered_centroid_ = centre;

        } else {
            float d = glm::distance(centre, last_considered_centroid_);
            if (d > .05) {
                distance_ += d;
                last_considered_centroid_ = centre;
            }
        }


        centroid_mesh_.addVertex(centre);


152
153
154
155
156
157
158
159
        min      = min * GRAPPE_LOWPASS_COEFF + smartCloud->min * (1.0 - GRAPPE_LOWPASS_COEFF);
        max      = max * GRAPPE_LOWPASS_COEFF + smartCloud->max * (1.0 - GRAPPE_LOWPASS_COEFF);
        vol      = max - min;

        cone.set((sqrt(pow(vol[0] * scale, 2) + pow(vol[1] * scale, 2)) / 2) / 2, vol[2] * scale);
        cone.setPosition(centroid[0] * scale, centroid[1] * scale, centroid[2] * scale);
    }

160
    void draw(bool draw_centroid = true, bool draw_colors = true, bool draw_cursor = true, bool draw_clouds = true)
161
162
163
    {
        if (lostTime < GRAPPE_AU_DELA) {

164
165
166
167
168
            if (draw_clouds) {
                if (!draw_colors) {
                    mesh->disableColors();
                }
                mesh->drawVertices();
169
            }
170

171
            if (draw_cursor) {
172
                ofDrawLine(centroid[0], 0, centroid[2], centroid[0], 20, centroid[2]);
173
            }
174

175
176
177
            if (draw_centroid) {
                centroid_mesh_.drawWireframe();
            }
178

179
            /*
expoclient's avatar
expoclient committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
                 
                 auto dimColor = color_;
                 dimColor.a = .3;
                 dimColor.setSaturation(dimColor.getSaturation()/3);
                 ofSetColor(dimColor);
                 
                 cone.drawWireframe();
                 
                 ofSetColor(255,255,255,128);
                 string label = str( boost::format("%1%:%2%\n%3$01.2f,%4$01.2f,%5$01.2f") % id_ % age % centroid[0] % centroid[1] % centroid[2] );
                 
                 ofPushMatrix();
                 {
                 ofTranslate(0,0,smartCloud->centroid[2]);
                 font.drawString(label, smartCloud->centroid[0], smartCloud->centroid[1]);
                 }
                 ofPopMatrix();
                 
                 
199
200
201
202
                 Singleton::getInstance().unicodeFont.drawMultiLine(label , //text to draw
                 .10,   //font size
                 smartCloud->centroid[0]*scale,     //x coord where to draw
                 smartCloud->centroid[1]*scale      //y coord where to draw
expoclient's avatar
expoclient committed
203
204
205
                 );
                 */
        }
206
207
    }
};
expoclient's avatar
expoclient committed
208
209
210
}

#endif // CLOUDLIB_CLUSTERMESH_HPP_