Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GLGizmo.hpp « GUI « slic3r « src - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2588080b26bb8cb370347b2b4c060f4b74922f97 (plain)
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#ifndef slic3r_GLGizmo_hpp_
#define slic3r_GLGizmo_hpp_

#include <igl/AABB.h>

#include "../../slic3r/GUI/GLTexture.hpp"
#include "../../slic3r/GUI/GLCanvas3D.hpp"

#include "libslic3r/Point.hpp"
#include "libslic3r/BoundingBox.hpp"
#include "libslic3r/SLA/SLAAutoSupports.hpp"

#include <array>
#include <vector>
#include <memory>


class wxWindow;
class GLUquadric;
typedef class GLUquadric GLUquadricObj;


namespace Slic3r {

class BoundingBoxf3;
class Linef3;
class ModelObject;

namespace GUI {

class GLCanvas3D;
#if ENABLE_IMGUI
class ImGuiWrapper;
#endif // ENABLE_IMGUI

class GLGizmoBase
{
protected:
    struct Grabber
    {
        static const float SizeFactor;
        static const float MinHalfSize;
        static const float DraggingScaleFactor;

        Vec3d center;
        Vec3d angles;
        float color[3];
        bool enabled;
        bool dragging;

        Grabber();

        void render(bool hover, float size) const;
        void render_for_picking(float size) const { render(size, color, false); }

        float get_half_size(float size) const;
        float get_dragging_half_size(float size) const;

    private:
        void render(float size, const float* render_color, bool use_lighting) const;
        void render_face(float half_size) const;
    };

public:
    enum EState
    {
        Off,
        Hover,
        On,
        Num_States
    };

    struct UpdateData
    {
        const Linef3 mouse_ray;
        const Point* mouse_pos;
        bool shift_down;

        UpdateData(const Linef3& mouse_ray, const Point* mouse_pos = nullptr, bool shift_down = false)
            : mouse_ray(mouse_ray), mouse_pos(mouse_pos), shift_down(shift_down)
        {}
    };

protected:
    GLCanvas3D& m_parent;

    int m_group_id;
    EState m_state;
    int m_shortcut_key;
    // textures are assumed to be square and all with the same size in pixels, no internal check is done
    GLTexture m_textures[Num_States];
    int m_hover_id;
    bool m_dragging;
    float m_base_color[3];
    float m_drag_color[3];
    float m_highlight_color[3];
    mutable std::vector<Grabber> m_grabbers;
#if ENABLE_IMGUI
    ImGuiWrapper* m_imgui;
#endif // ENABLE_IMGUI

public:
    explicit GLGizmoBase(GLCanvas3D& parent);
    virtual ~GLGizmoBase() {}

    bool init() { return on_init(); }

    std::string get_name() const { return on_get_name(); }

    int get_group_id() const { return m_group_id; }
    void set_group_id(int id) { m_group_id = id; }

    EState get_state() const { return m_state; }
    void set_state(EState state) { m_state = state; on_set_state(); }

    int get_shortcut_key() const { return m_shortcut_key; }
    void set_shortcut_key(int key) { m_shortcut_key = key; }

    bool is_activable(const GLCanvas3D::Selection& selection) const { return on_is_activable(selection); }
    bool is_selectable() const { return on_is_selectable(); }

    unsigned int get_texture_id() const { return m_textures[m_state].get_id(); }
    int get_textures_size() const { return m_textures[Off].get_width(); }

    int get_hover_id() const { return m_hover_id; }
    void set_hover_id(int id);
    
    void set_highlight_color(const float* color);

    void enable_grabber(unsigned int id);
    void disable_grabber(unsigned int id);

    void start_dragging(const GLCanvas3D::Selection& selection);
    void stop_dragging();
    bool is_dragging() const { return m_dragging; }

    void update(const UpdateData& data);

    void render(const GLCanvas3D::Selection& selection) const { on_render(selection); }
    void render_for_picking(const GLCanvas3D::Selection& selection) const { on_render_for_picking(selection); }

#if !ENABLE_IMGUI
    virtual void create_external_gizmo_widgets(wxWindow *parent);
#endif // not ENABLE_IMGUI

#if ENABLE_IMGUI
    void render_input_window(float x, float y, const GLCanvas3D::Selection& selection) { on_render_input_window(x, y, selection); }
#endif // ENABLE_IMGUI

protected:
    virtual bool on_init() = 0;
    virtual std::string on_get_name() const = 0;
    virtual void on_set_state() {}
    virtual void on_set_hover_id() {}
    virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const { return true; }
    virtual bool on_is_selectable() const { return true; }
    virtual void on_enable_grabber(unsigned int id) {}
    virtual void on_disable_grabber(unsigned int id) {}
    virtual void on_start_dragging(const GLCanvas3D::Selection& selection) {}
    virtual void on_stop_dragging() {}
    virtual void on_update(const UpdateData& data) = 0;
    virtual void on_render(const GLCanvas3D::Selection& selection) const = 0;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const = 0;

#if ENABLE_IMGUI
    virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) {}
#endif // ENABLE_IMGUI

    float picking_color_component(unsigned int id) const;
    void render_grabbers(const BoundingBoxf3& box) const;
    void render_grabbers(float size) const;
    void render_grabbers_for_picking(const BoundingBoxf3& box) const;

    void set_tooltip(const std::string& tooltip) const;
    std::string format(float value, unsigned int decimals) const;
};

class GLGizmoRotate : public GLGizmoBase
{
    static const float Offset;
    static const unsigned int CircleResolution;
    static const unsigned int AngleResolution;
    static const unsigned int ScaleStepsCount;
    static const float ScaleStepRad;
    static const unsigned int ScaleLongEvery;
    static const float ScaleLongTooth;
    static const unsigned int SnapRegionsCount;
    static const float GrabberOffset;

public:
    enum Axis : unsigned char
    {
        X,
        Y,
        Z
    };

private:
    Axis m_axis;
    double m_angle;

    GLUquadricObj* m_quadric;

    mutable Vec3d m_center;
    mutable float m_radius;

    mutable float m_snap_coarse_in_radius;
    mutable float m_snap_coarse_out_radius;
    mutable float m_snap_fine_in_radius;
    mutable float m_snap_fine_out_radius;

public:
    GLGizmoRotate(GLCanvas3D& parent, Axis axis);
    GLGizmoRotate(const GLGizmoRotate& other);
    virtual ~GLGizmoRotate();

    double get_angle() const { return m_angle; }
    void set_angle(double angle);

protected:
    virtual bool on_init();
    virtual std::string on_get_name() const { return ""; }
    virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
    virtual void on_update(const UpdateData&  data);
    virtual void on_render(const GLCanvas3D::Selection& selection) const;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;

private:
    void render_circle() const;
    void render_scale() const;
    void render_snap_radii() const;
    void render_reference_radius() const;
    void render_angle() const;
    void render_grabber(const BoundingBoxf3& box) const;
    void render_grabber_extension(const BoundingBoxf3& box, bool picking) const;

    void transform_to_local() const;
    // returns the intersection of the mouse ray with the plane perpendicular to the gizmo axis, in local coordinate
    Vec3d mouse_position_in_local_plane(const Linef3& mouse_ray) const;
};

class GLGizmoRotate3D : public GLGizmoBase
{
    std::vector<GLGizmoRotate> m_gizmos;

public:
    explicit GLGizmoRotate3D(GLCanvas3D& parent);

    Vec3d get_rotation() const { return Vec3d(m_gizmos[X].get_angle(), m_gizmos[Y].get_angle(), m_gizmos[Z].get_angle()); }
    void set_rotation(const Vec3d& rotation) { m_gizmos[X].set_angle(rotation(0)); m_gizmos[Y].set_angle(rotation(1)); m_gizmos[Z].set_angle(rotation(2)); }

protected:
    virtual bool on_init();
    virtual std::string on_get_name() const;
    virtual void on_set_state()
    {
        for (GLGizmoRotate& g : m_gizmos)
        {
            g.set_state(m_state);
        }
    }
    virtual void on_set_hover_id()
    {
        for (unsigned int i = 0; i < 3; ++i)
        {
            m_gizmos[i].set_hover_id((m_hover_id == i) ? 0 : -1);
        }
    }
    virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const { return !selection.is_wipe_tower(); }
    virtual void on_enable_grabber(unsigned int id)
    {
        if ((0 <= id) && (id < 3))
            m_gizmos[id].enable_grabber(0);
    }
    virtual void on_disable_grabber(unsigned int id)
    {
        if ((0 <= id) && (id < 3))
            m_gizmos[id].disable_grabber(0);
    }
    virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
    virtual void on_stop_dragging();
    virtual void on_update(const UpdateData& data)
    {
        for (GLGizmoRotate& g : m_gizmos)
        {
            g.update(data);
        }
    }
    virtual void on_render(const GLCanvas3D::Selection& selection) const;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const
    {
        for (const GLGizmoRotate& g : m_gizmos)
        {
            g.render_for_picking(selection);
        }
    }

#if ENABLE_IMGUI
    virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection);
#endif // ENABLE_IMGUI
};

class GLGizmoScale3D : public GLGizmoBase
{
    static const float Offset;

    mutable BoundingBoxf3 m_box;

    Vec3d m_scale;

    double m_snap_step;

    Vec3d m_starting_scale;
    Vec3d m_starting_drag_position;
    BoundingBoxf3 m_starting_box;

public:
    explicit GLGizmoScale3D(GLCanvas3D& parent);

    double get_snap_step(double step) const { return m_snap_step; }
    void set_snap_step(double step) { m_snap_step = step; }

    const Vec3d& get_scale() const { return m_scale; }
    void set_scale(const Vec3d& scale) { m_starting_scale = scale; m_scale = scale; }

protected:
    virtual bool on_init();
    virtual std::string on_get_name() const;
    virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const { return !selection.is_wipe_tower(); }
    virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
    virtual void on_update(const UpdateData& data);
    virtual void on_render(const GLCanvas3D::Selection& selection) const;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;

#if ENABLE_IMGUI
    virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection);
#endif // ENABLE_IMGUI

private:
    void render_grabbers_connection(unsigned int id_1, unsigned int id_2) const;

    void do_scale_x(const UpdateData& data);
    void do_scale_y(const UpdateData& data);
    void do_scale_z(const UpdateData& data);
    void do_scale_uniform(const UpdateData& data);

    double calc_ratio(const UpdateData& data) const;
};

class GLGizmoMove3D : public GLGizmoBase
{
    static const double Offset;

    Vec3d m_displacement;

    double m_snap_step;

    Vec3d m_starting_drag_position;
    Vec3d m_starting_box_center;
    Vec3d m_starting_box_bottom_center;

    GLUquadricObj* m_quadric;

public:
    explicit GLGizmoMove3D(GLCanvas3D& parent);
    virtual ~GLGizmoMove3D();

    double get_snap_step(double step) const { return m_snap_step; }
    void set_snap_step(double step) { m_snap_step = step; }

    const Vec3d& get_displacement() const { return m_displacement; }

protected:
    virtual bool on_init();
    virtual std::string on_get_name() const;
    virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
    virtual void on_stop_dragging();
    virtual void on_update(const UpdateData& data);
    virtual void on_render(const GLCanvas3D::Selection& selection) const;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;

#if ENABLE_IMGUI
    virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection);
#endif // ENABLE_IMGUI

private:
    double calc_projection(const UpdateData& data) const;
    void render_grabber_extension(Axis axis, const BoundingBoxf3& box, bool picking) const;
};

class GLGizmoFlatten : public GLGizmoBase
{
// This gizmo does not use grabbers. The m_hover_id relates to polygon managed by the class itself.

private:
    mutable Vec3d m_normal;

    struct PlaneData {
        std::vector<Vec3d> vertices;
        Vec3d normal;
        float area;
    };

    // This holds information to decide whether recalculation is necessary:
    std::vector<Transform3d> m_volumes_matrices;

    std::vector<PlaneData> m_planes;
    mutable Vec3d m_starting_center;
    const ModelObject* m_model_object = nullptr;
    std::vector<const Transform3d*> instances_matrices;

    void update_planes();
    bool is_plane_update_necessary() const;

public:
    explicit GLGizmoFlatten(GLCanvas3D& parent);

    void set_flattening_data(const ModelObject* model_object);
    Vec3d get_flattening_normal() const;

protected:
    virtual bool on_init();
    virtual std::string on_get_name() const;
    virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const;
    virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
    virtual void on_update(const UpdateData& data) {}
    virtual void on_render(const GLCanvas3D::Selection& selection) const;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;
    virtual void on_set_state()
    {
        if (m_state == On && is_plane_update_necessary())
            update_planes();
    }
};

class GLGizmoSlaSupports : public GLGizmoBase
{
private:
    ModelObject* m_model_object = nullptr;
#if ENABLE_SLA_SUPPORT_GIZMO_MOD
    ModelObject* m_old_model_object = nullptr;
    int m_old_instance_id = -1;
#else
    Transform3d m_instance_matrix;
#endif // ENABLE_SLA_SUPPORT_GIZMO_MOD
    Vec3f unproject_on_mesh(const Vec2d& mouse_pos);

#if ENABLE_SLA_SUPPORT_GIZMO_MOD
    GLUquadricObj* m_quadric;
#endif // ENABLE_SLA_SUPPORT_GIZMO_MOD

    Eigen::MatrixXf m_V; // vertices
    Eigen::MatrixXi m_F; // facets indices
    igl::AABB<Eigen::MatrixXf,3> m_AABB;

    struct SourceDataSummary {
#if !ENABLE_SLA_SUPPORT_GIZMO_MOD
        BoundingBoxf3 bounding_box;
        Transform3d matrix;
#endif // !ENABLE_SLA_SUPPORT_GIZMO_MOD
        Vec3d mesh_first_point;
    };

    // This holds information to decide whether recalculation is necessary:
    SourceDataSummary m_source_data;

    mutable Vec3d m_starting_center;

public:
    explicit GLGizmoSlaSupports(GLCanvas3D& parent);
#if ENABLE_SLA_SUPPORT_GIZMO_MOD
    virtual ~GLGizmoSlaSupports();
    void set_sla_support_data(ModelObject* model_object, const GLCanvas3D::Selection& selection);
#else
    void set_model_object_ptr(ModelObject* model_object);
#endif // ENABLE_SLA_SUPPORT_GIZMO_MOD
    void clicked_on_object(const Vec2d& mouse_position);
    void delete_current_grabber(bool delete_all);

private:
    bool on_init();
    void on_update(const UpdateData& data);
    virtual void on_render(const GLCanvas3D::Selection& selection) const;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;

#if ENABLE_SLA_SUPPORT_GIZMO_MOD
    void render_grabbers(const GLCanvas3D::Selection& selection, bool picking = false) const;
#else
    void render_grabbers(bool picking = false) const;
#endif // ENABLE_SLA_SUPPORT_GIZMO_MOD
    bool is_mesh_update_necessary() const;
    void update_mesh();

#if !ENABLE_IMGUI
    void render_tooltip_texture() const;
    mutable GLTexture m_tooltip_texture;
    mutable GLTexture m_reset_texture;
#endif // not ENABLE_IMGUI

protected:
    void on_set_state() override {
        if (m_state == On && is_mesh_update_necessary()) {
            update_mesh();
        }
    }

#if ENABLE_IMGUI
    virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection) override;
#endif // ENABLE_IMGUI

    virtual std::string on_get_name() const;
    virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const;
    virtual bool on_is_selectable() const;
};


#if !ENABLE_IMGUI
class GLGizmoCutPanel;
#endif // not ENABLE_IMGUI

class GLGizmoCut : public GLGizmoBase
{
    static const double Offset;
    static const double Margin;
    static const std::array<float, 3> GrabberColor;

    mutable double m_cut_z;
    double m_start_z;
    mutable double m_max_z;
    Vec3d m_drag_pos;
    Vec3d m_drag_center;
    bool m_keep_upper;
    bool m_keep_lower;
    bool m_rotate_lower;
#if !ENABLE_IMGUI
    GLGizmoCutPanel *m_panel;
#endif // not ENABLE_IMGUI

public:
    explicit GLGizmoCut(GLCanvas3D& parent);

#if !ENABLE_IMGUI
    virtual void create_external_gizmo_widgets(wxWindow *parent);
#endif // not ENABLE_IMGUI
#if !ENABLE_IMGUI
#endif // not ENABLE_IMGUI

protected:
    virtual bool on_init();
    virtual std::string on_get_name() const;
    virtual void on_set_state();
    virtual bool on_is_activable(const GLCanvas3D::Selection& selection) const;
    virtual void on_start_dragging(const GLCanvas3D::Selection& selection);
    virtual void on_update(const UpdateData& data);
    virtual void on_render(const GLCanvas3D::Selection& selection) const;
    virtual void on_render_for_picking(const GLCanvas3D::Selection& selection) const;

#if ENABLE_IMGUI
    virtual void on_render_input_window(float x, float y, const GLCanvas3D::Selection& selection);
#endif // ENABLE_IMGUI
private:
    void update_max_z(const GLCanvas3D::Selection& selection) const;
    void set_cut_z(double cut_z) const;
    void perform_cut(const GLCanvas3D::Selection& selection);
    double calc_projection(const Linef3& mouse_ray) const;
};


} // namespace GUI
} // namespace Slic3r

#endif // slic3r_GLGizmo_hpp_