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

Analyzer.hpp « GCode « libslic3r « src « xs - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2f5dafb3caacdc8f15d9fedd9ad97a971e1b9a83 (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
#ifndef slic3r_GCode_Analyzer_hpp_
#define slic3r_GCode_Analyzer_hpp_

#include "../libslic3r.h"
#include "../PrintConfig.hpp"
#include "../ExtrusionEntity.hpp"

//############################################################################################################
#if ENRICO_GCODE_PREVIEW
#include "Point.hpp"
#include "GCodeReader.hpp"
#endif // ENRICO_GCODE_PREVIEW
//############################################################################################################

namespace Slic3r {

//############################################################################################################
#if ENRICO_GCODE_PREVIEW
    class Print;
#endif // ENRICO_GCODE_PREVIEW
//############################################################################################################

//############################################################################################################
#if !ENRICO_GCODE_PREVIEW
//############################################################################################################
enum GCodeMoveType
{
    GCODE_MOVE_TYPE_NOOP,
    GCODE_MOVE_TYPE_RETRACT,
    GCODE_MOVE_TYPE_UNRETRACT,
    GCODE_MOVE_TYPE_TOOL_CHANGE,
    GCODE_MOVE_TYPE_MOVE,
    GCODE_MOVE_TYPE_EXTRUDE,
};

// For visualization purposes, for the purposes of the G-code analysis and timing.
// The size of this structure is 56B.
// Keep the size of this structure as small as possible, because all moves of a complete print
// may be held in RAM.
struct GCodeMove
{
    bool        moving_xy(const float* pos_start)   const { return fabs(pos_end[0] - pos_start[0]) > 0.f || fabs(pos_end[1] - pos_start[1]) > 0.f; }
    bool        moving_xy()                         const { return moving_xy(get_pos_start()); }
    bool        moving_z (const float* pos_start)   const { return fabs(pos_end[2] - pos_start[2]) > 0.f; }
    bool        moving_z ()                         const { return moving_z(get_pos_start()); }
    bool        extruding(const float* pos_start)   const { return moving_xy() && pos_end[3] > pos_start[3]; }
    bool        extruding()                         const { return extruding(get_pos_start()); }
    bool        retracting(const float* pos_start)  const { return pos_end[3] < pos_start[3]; }
    bool        retracting()                        const { return retracting(get_pos_start()); }    
    bool        deretracting(const float* pos_start)  const { return ! moving_xy() && pos_end[3] > pos_start[3]; }
    bool        deretracting()                      const { return deretracting(get_pos_start()); }

    float       dist_xy2(const float* pos_start)    const { return (pos_end[0] - pos_start[0]) * (pos_end[0] - pos_start[0]) + (pos_end[1] - pos_start[1]) * (pos_end[1] - pos_start[1]); }
    float       dist_xy2()                          const { return dist_xy2(get_pos_start()); }
    float       dist_xyz2(const float* pos_start)   const { return (pos_end[0] - pos_start[0]) * (pos_end[0] - pos_start[0]) + (pos_end[1] - pos_start[1]) * (pos_end[1] - pos_start[1]) + (pos_end[2] - pos_start[2]) * (pos_end[2] - pos_start[2]); }
    float       dist_xyz2()                         const { return dist_xyz2(get_pos_start()); }

    float       dist_xy(const float* pos_start)     const { return sqrt(dist_xy2(pos_start)); }
    float       dist_xy()                           const { return dist_xy(get_pos_start()); }
    float       dist_xyz(const float* pos_start)    const { return sqrt(dist_xyz2(pos_start)); }
    float       dist_xyz()                          const { return dist_xyz(get_pos_start()); }

    float       dist_e(const float* pos_start)      const { return fabs(pos_end[3] - pos_start[3]); }
    float       dist_e()                            const { return dist_e(get_pos_start()); }

    float       feedrate()                          const { return pos_end[4]; }
    float       time(const float* pos_start)        const { return dist_xyz(pos_start) / feedrate(); }
    float       time()                              const { return time(get_pos_start()); }
    float       time_inv(const float* pos_start)    const { return feedrate() / dist_xyz(pos_start); }
    float       time_inv()                          const { return time_inv(get_pos_start()); }

    const float*    get_pos_start() const { assert(type != GCODE_MOVE_TYPE_NOOP); return this[-1].pos_end; }

    // Pack the enums to conserve space. With C++x11 the allocation size could be declared for enums, but for old C++ this is the only portable way.
    // GCodeLineType
    uint8_t         type;
    // Index of the active extruder.
    uint8_t         extruder_id;
    // ExtrusionRole
    uint8_t         extrusion_role;
    // For example, is it a bridge flow? Is the fan on?
    uint8_t         flags;
    // X,Y,Z,E,F. Storing the state of the currently active extruder only.
    float           pos_end[5];
    // Extrusion width, height for this segment in um.
    uint16_t        extrusion_width;
    uint16_t        extrusion_height;
};

typedef std::vector<GCodeMove> GCodeMoves;

struct GCodeLayer
{
    // Index of an object printed.
    size_t                  object_idx;
    // Index of an object instance printed.
    size_t                  object_instance_idx;
    // Index of the layer printed.
    size_t                  layer_idx;
    // Top z coordinate of the layer printed.
    float                   layer_z_top;

    // Moves over this layer. The 0th move is always of type GCODELINETYPE_NOOP and
    // it sets the initial position and tool for the layer.
    GCodeMoves              moves;

    // Indices into m_moves, where the tool changes happen.
    // This is useful, if one wants to display just only a piece of the path quickly.
    std::vector<size_t>     tool_changes;
};

typedef std::vector<GCodeLayer*> GCodeLayerPtrs;

class GCodeMovesDB
{
public:
    GCodeMovesDB() {};
    ~GCodeMovesDB() { reset(); }
    void reset();
    GCodeLayerPtrs      m_layers;
};

// Processes a G-code to extract moves and their types.
// This information is then used to render the print simulation colored by the extrusion type
// or various speeds.
// The GCodeAnalyzer is employed as a G-Code filter. It reads the G-code as it is generated,
// parses the comments generated by Slic3r just for the analyzer, and removes these comments.
class GCodeAnalyzer
{
public:
    GCodeAnalyzer(const Slic3r::GCodeConfig *config);
    ~GCodeAnalyzer();

    void reset();

    // Process a next batch of G-code lines. Flush the internal buffers if asked for.
    const char* process(const char *szGCode, bool flush);
    // Length of the buffer returned by process().
    size_t get_output_buffer_length() const { return output_buffer_length; }

private:
    // Keeps the reference, does not own the config.
    const Slic3r::GCodeConfig      *m_config;

    // Internal data.
    // X,Y,Z,E,F
    float                           m_current_pos[5];
    size_t                          m_current_extruder;
    ExtrusionRole                   m_current_extrusion_role;
    uint16_t                        m_current_extrusion_width;
    uint16_t                        m_current_extrusion_height;
    bool                            m_retracted;

    GCodeMovesDB                   *m_moves;

    // Output buffer will only grow. It will not be reallocated over and over.
    std::vector<char>               output_buffer;
    size_t                          output_buffer_length;

    bool process_line(const char *line, const size_t len);

    // Push the text to the end of the output_buffer.
    void push_to_output(const char *text, const size_t len, bool add_eol = true);
};
//############################################################################################################
#endif // !ENRICO_GCODE_PREVIEW
//############################################################################################################

//############################################################################################################
#if ENRICO_GCODE_PREVIEW
class GCodeAnalyzer
{
public:
    static const std::string Extrusion_Role_Tag;
    static const std::string Mm3_Per_Mm_Tag;
    static const std::string Width_Tag;
    static const std::string Height_Tag;

    static const double Default_mm3_per_mm;
    static const float Default_Width;
    static const float Default_Height;

    enum EUnits : unsigned char
    {
        Millimeters,
        Inches
    };

    enum EAxis : unsigned char
    {
        X,
        Y,
        Z,
        E,
        Num_Axis
    };

    enum EPositioningType : unsigned char
    {
        Absolute,
        Relative
    };

    struct Metadata
    {
        ExtrusionRole extrusion_role;
        unsigned int extruder_id;
        double mm3_per_mm;
        float width;
        float height;
        float feedrate;

        Metadata();
        Metadata(ExtrusionRole extrusion_role, unsigned int extruder_id, double mm3_per_mm, float width, float height, float feedrate);

        bool operator != (const Metadata& other) const;
    };

    struct GCodeMove
    {
        enum EType : unsigned char
        {
            Noop,
            Retract,
            Unretract,
            Tool_change,
            Move,
            Extrude,
            Num_Types
        };

        EType type;
        Metadata data;
        Pointf3 start_position;
        Pointf3 end_position;
        float delta_extruder;

        GCodeMove(EType type, ExtrusionRole extrusion_role, unsigned int extruder_id, double mm3_per_mm, float width, float height, float feedrate, const Pointf3& start_position, const Pointf3& end_position, float delta_extruder);
        GCodeMove(EType type, const Metadata& data, const Pointf3& start_position, const Pointf3& end_position, float delta_extruder);
    };

    typedef std::vector<GCodeMove> GCodeMovesList;
    typedef std::map<GCodeMove::EType, GCodeMovesList> TypeToMovesMap;

private:
    struct State
    {
        EUnits units;
        EPositioningType positioning_xyz_type;
        EPositioningType positioning_e_type;
        Metadata data;
        Pointf3 start_position;
        float start_extrusion;
        float position[Num_Axis];
    };

public:
    struct PreviewData
    {
        struct Color
        {
            float rgba[4];

            Color();
            Color(float r, float g, float b, float a);

            static const Color Dummy;
        };

        struct Range
        {
            static const unsigned int Colors_Count = 10;
            static const Color Default_Colors[Colors_Count];

            Color colors[Colors_Count];
            float min;
            float max;

            Range();

            void reset();
            bool empty() const;
            void update_from(float value);
            void set_from(const Range& other);

            const Color& get_color_at(float value) const;
            const Color& get_color_at_max() const;

        private:
            float _step() const;
        };

        struct Extrusion
        {
            enum EViewType : unsigned char
            {
                FeatureType,
                Height,
                Width,
                Feedrate,
                Num_View_Types
            };

            static const unsigned int Num_Extrusion_Roles = (unsigned int)erMixed + 1;
            static const Color Default_Extrusion_Role_Colors[Num_Extrusion_Roles];
            static const EViewType Default_View_Type; 

            struct Ranges
            {
                Range height;
                Range width;
                Range feedrate;
            };

            struct Layer
            {
                float z;
                ExtrusionPaths paths;

                Layer(float z, const ExtrusionPaths& paths);
            };

            typedef std::vector<Layer> LayersList;

            EViewType view_type;
            Color role_colors[Num_Extrusion_Roles];
            Ranges ranges;
            LayersList layers;
            unsigned int role_flags;

            void set_default();
            bool is_role_flag_set(ExtrusionRole role) const;

            static bool is_role_flag_set(unsigned int flags, ExtrusionRole role);
        };

        struct Travel
        {
            enum EType : unsigned char
            {
                Move,
                Extrude,
                Retract,
                Num_Types
            };

            static const float Default_Width;
            static const float Default_Height;
            static const Color Default_Type_Colors[Num_Types];

            struct Polyline
            {
                enum EDirection
                {
                    Vertical,
                    Generic,
                    Num_Directions
                };

                EType type;
                EDirection direction;
                Polyline3 polyline;

                Polyline(EType type, EDirection direction, const Polyline3& polyline);
            };

            typedef std::vector<Polyline> PolylinesList;

            PolylinesList polylines;
            float width;
            float height;
            Color type_colors[Num_Types];
            bool is_visible;

            void set_default();
        };

        struct Retraction
        {
            static const Color Default_Color;

            struct Position
            {
                Point3 position;
                float width;
                float height;

                Position(const Point3& position, float width, float height);
            };

            typedef std::vector<Position> PositionsList;

            PositionsList positions;
            Color color;
            bool is_visible;

            void set_default();
        };

        Extrusion extrusion;
        Travel travel;
        Retraction retraction;
        Retraction unretraction;

        PreviewData();

        void set_default();
        void reset();

        const Color& get_extrusion_role_color(ExtrusionRole role) const;
        const Color& get_extrusion_height_color(float height) const;
        const Color& get_extrusion_width_color(float width) const;
        const Color& get_extrusion_feedrate_color(float feedrate) const;
    };

private:
    State m_state;
    GCodeReader m_parser;
    TypeToMovesMap m_moves_map;

    // The output of process_layer()
    std::string m_process_output;

public:
    GCodeAnalyzer();

    // Reinitialize the analyzer
    void reset();

    // Adds the gcode contained in the given string to the analysis and returns it after removing the workcodes
    const std::string& process_gcode(const std::string& gcode);

    // Calculates all data needed for gcode visualization
    void calc_gcode_preview_data(Print& print);

private:
    // Processes the given gcode line
    void _process_gcode_line(GCodeReader& reader, const GCodeReader::GCodeLine& line);

    // Move
    void _processG1(const GCodeReader::GCodeLine& line);

    // Firmware controlled Retract
    void _processG22(const GCodeReader::GCodeLine& line);

    // Firmware controlled Unretract
    void _processG23(const GCodeReader::GCodeLine& line);

    // Set to Absolute Positioning
    void _processG90(const GCodeReader::GCodeLine& line);

    // Set to Relative Positioning
    void _processG91(const GCodeReader::GCodeLine& line);

    // Set Position
    void _processG92(const GCodeReader::GCodeLine& line);

    // Set extruder to absolute mode
    void _processM82(const GCodeReader::GCodeLine& line);

    // Set extruder to relative mode
    void _processM83(const GCodeReader::GCodeLine& line);

    // Processes T line (Select Tool)
    void _processT(const GCodeReader::GCodeLine& line);

    // Processes the tags
    // Returns true if any tag has been processed
    bool _process_tags(const GCodeReader::GCodeLine& line);

    // Processes extrusion role tag
    void _process_extrusion_role_tag(const std::string& comment, size_t pos);

    // Processes mm3_per_mm tag
    void _process_mm3_per_mm_tag(const std::string& comment, size_t pos);

    // Processes width tag
    void _process_width_tag(const std::string& comment, size_t pos);

    // Processes height tag
    void _process_height_tag(const std::string& comment, size_t pos);

    void _set_units(EUnits units);
    EUnits _get_units() const;

    void _set_positioning_xyz_type(EPositioningType type);
    EPositioningType _get_positioning_xyz_type() const;

    void _set_positioning_e_type(EPositioningType type);
    EPositioningType _get_positioning_e_type() const;

    void _set_extrusion_role(ExtrusionRole extrusion_role);
    ExtrusionRole _get_extrusion_role() const;

    void _set_extruder_id(unsigned int id);
    unsigned int _get_extruder_id() const;

    void _set_mm3_per_mm(double value);
    double _get_mm3_per_mm() const;

    void _set_width(float width);
    float _get_width() const;

    void _set_height(float height);
    float _get_height() const;

    void _set_feedrate(float feedrate_mm_sec);
    float _get_feedrate() const;

    void _set_axis_position(EAxis axis, float position);
    float _get_axis_position(EAxis axis) const;

    // Sets axes position to zero
    void _reset_axes_position();

    void _set_start_position(const Pointf3& position);
    const Pointf3& _get_start_position() const;

    void _set_start_extrusion(float extrusion);
    float _get_start_extrusion() const;
    float _get_delta_extrusion() const;

    // Returns current xyz position (from m_state.position[])
    Pointf3 _get_end_position() const;

    // Adds a new move with the given data
    void _store_move(GCodeMove::EType type);

    // Checks if the given int is a valid extrusion role (contained into enum ExtrusionRole)
    bool _is_valid_extrusion_role(int value) const;

    void _calc_gcode_preview_extrusion_layers(Print& print);
    void _calc_gcode_preview_travel(Print& print);
    void _calc_gcode_preview_retractions(Print& print);
    void _calc_gcode_preview_unretractions(Print& print);
};

GCodeAnalyzer::PreviewData::Color operator + (const GCodeAnalyzer::PreviewData::Color& c1, const GCodeAnalyzer::PreviewData::Color& c2);
GCodeAnalyzer::PreviewData::Color operator * (float f, const GCodeAnalyzer::PreviewData::Color& color);

#endif // ENRICO_GCODE_PREVIEW
//############################################################################################################

} // namespace Slic3r

#endif /* slic3r_GCode_Analyzer_hpp_ */