Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::GCodeFormatter Class Reference

#include <src/libslic3r/GCodeWriter.hpp>

+ Inheritance diagram for Slic3r::GCodeFormatter:
+ Collaboration diagram for Slic3r::GCodeFormatter:

Public Member Functions

 GCodeFormatter ()
 
 GCodeFormatter (const GCodeFormatter &)=delete
 
GCodeFormatteroperator= (const GCodeFormatter &)=delete
 
void emit_axis (const char axis, const double v, size_t digits)
 
void emit_xy (const Vec2d &point)
 
void emit_xyz (const Vec3d &point)
 
void emit_z (const double z)
 
void emit_e (const std::string &axis, double v)
 
void emit_f (double speed)
 
void emit_string (const std::string &s)
 
void emit_comment (bool allow_comments, const std::string &comment)
 
std::string string ()
 

Static Public Member Functions

static double quantize (double v, size_t ndigits)
 
static double quantize_xyzf (double v)
 
static double quantize_e (double v)
 

Static Public Attributes

static constexpr const int XYZF_EXPORT_DIGITS = 3
 
static constexpr const int E_EXPORT_DIGITS = 5
 
static constexpr const std::array< double, 10 > pow_10 { 1., 10., 100., 1000., 10000., 100000., 1000000., 10000000., 100000000., 1000000000.}
 
static constexpr const std::array< double, 10 > pow_10_inv {1./1., 1./10., 1./100., 1./1000., 1./10000., 1./100000., 1./1000000., 1./10000000., 1./100000000., 1./1000000000.}
 

Protected Attributes

char buf [buflen]
 
char * buf_end
 
std::to_chars_result ptr_err
 

Static Protected Attributes

static constexpr const size_t buflen = 256
 

Detailed Description

Constructor & Destructor Documentation

◆ GCodeFormatter() [1/2]

Slic3r::GCodeFormatter::GCodeFormatter ( )
inline
123 {
124 this->buf_end = buf + buflen;
125 this->ptr_err.ptr = this->buf;
126 }
static constexpr const size_t buflen
Definition GCodeWriter.hpp:202
char buf[buflen]
Definition GCodeWriter.hpp:203
std::to_chars_result ptr_err
Definition GCodeWriter.hpp:205
char * buf_end
Definition GCodeWriter.hpp:204

References buf, buf_end, buflen, and ptr_err.

◆ GCodeFormatter() [2/2]

Slic3r::GCodeFormatter::GCodeFormatter ( const GCodeFormatter )
delete

Member Function Documentation

◆ emit_axis()

void Slic3r::GCodeFormatter::emit_axis ( const char  axis,
const double  v,
size_t  digits 
)
542 {
543 assert(digits <= 9);
544 static constexpr const std::array<int, 10> pow_10{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
545 *ptr_err.ptr++ = ' '; *ptr_err.ptr++ = axis;
546
547 char *base_ptr = this->ptr_err.ptr;
548 auto v_int = int64_t(std::round(v * pow_10[digits]));
549 // Older stdlib on macOS doesn't support std::from_chars at all, so it is used boost::spirit::karma::generate instead of it.
550 // That is a little bit slower than std::to_chars but not much.
551#ifdef __APPLE__
552 boost::spirit::karma::generate(this->ptr_err.ptr, boost::spirit::karma::int_generator<int64_t>(), v_int);
553#else
554 // this->buf_end minus 1 because we need space for adding the extra decimal point.
555 this->ptr_err = std::to_chars(this->ptr_err.ptr, this->buf_end - 1, v_int);
556#endif
557 size_t writen_digits = (this->ptr_err.ptr - base_ptr) - (v_int < 0 ? 1 : 0);
558 if (writen_digits < digits) {
559 // Number is smaller than 10^digits, so that we will pad it with zeros.
560 size_t remaining_digits = digits - writen_digits;
561 // Move all newly inserted chars by remaining_digits to allocate space for padding with zeros.
562 for (char *from_ptr = this->ptr_err.ptr - 1, *to_ptr = from_ptr + remaining_digits; from_ptr >= this->ptr_err.ptr - writen_digits; --to_ptr, --from_ptr)
563 *to_ptr = *from_ptr;
564
565 memset(this->ptr_err.ptr - writen_digits, '0', remaining_digits);
566 this->ptr_err.ptr += remaining_digits;
567 }
568
569 // Move all newly inserted chars by one to allocate space for a decimal point.
570 for (char *to_ptr = this->ptr_err.ptr, *from_ptr = to_ptr - 1; from_ptr >= this->ptr_err.ptr - digits; --to_ptr, --from_ptr)
571 *to_ptr = *from_ptr;
572
573 *(this->ptr_err.ptr - digits) = '.';
574 for (size_t i = 0; i < digits; ++i) {
575 if (*this->ptr_err.ptr != '0')
576 break;
577 this->ptr_err.ptr--;
578 }
579 if (*this->ptr_err.ptr == '.')
580 this->ptr_err.ptr--;
581 if ((this->ptr_err.ptr + 1) == base_ptr || *this->ptr_err.ptr == '-')
582 *(++this->ptr_err.ptr) = '0';
583 this->ptr_err.ptr++;
584
585#if 0 // #ifndef NDEBUG
586 {
587 // Verify that the optimized formatter produces the same result as the standard sprintf().
588 double v1 = atof(std::string(base_ptr, this->ptr_err.ptr).c_str());
589 char buf[2048];
590 sprintf(buf, "%.*lf", int(digits), v);
591 double v2 = atof(buf);
592 // Numbers may differ when rounding at exactly or very close to 0.5 due to numerical issues when scaling the double to an integer.
593 // Thus the complex assert.
594// assert(v1 == v2);
595 assert(std::abs(v1 - v) * pow_10[digits] < 0.50001);
596 assert(std::abs(v2 - v) * pow_10[digits] < 0.50001);
597 }
598#endif // NDEBUG
599}
static constexpr const std::array< double, 10 > pow_10
Definition GCodeWriter.hpp:148
__int64 int64_t
Definition unistd.h:76

References buf, pow_10, and ptr_err.

Referenced by emit_e(), emit_f(), emit_xy(), emit_xyz(), emit_z(), and Slic3r::PressureEqualizer::push_line_to_output().

+ Here is the caller graph for this function:

◆ emit_comment()

void Slic3r::GCodeFormatter::emit_comment ( bool  allow_comments,
const std::string &  comment 
)
inline
189 {
190 if (allow_comments && ! comment.empty()) {
191 *ptr_err.ptr ++ = ' '; *ptr_err.ptr ++ = ';'; *ptr_err.ptr ++ = ' ';
192 this->emit_string(comment);
193 }
194 }
void emit_string(const std::string &s)
Definition GCodeWriter.hpp:184
#define comment
Definition lexer.c:1004

References comment, emit_string(), and ptr_err.

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::_travel_to_z(), Slic3r::GCodeWriter::extrude_to_xy(), Slic3r::GCodeWriter::set_speed(), Slic3r::GCodeWriter::travel_to_xy(), Slic3r::GCodeWriter::travel_to_xyz(), and Slic3r::GCodeWriter::unretract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ emit_e()

void Slic3r::GCodeFormatter::emit_e ( const std::string &  axis,
double  v 
)
inline
173 {
174 if (! axis.empty()) {
175 // not gcfNoExtrusion
176 this->emit_axis(axis[0], v, E_EXPORT_DIGITS);
177 }
178 }
void emit_axis(const char axis, const double v, size_t digits)
Definition GCodeWriter.cpp:542
static constexpr const int E_EXPORT_DIGITS
Definition GCodeWriter.hpp:138

References E_EXPORT_DIGITS, and emit_axis().

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::extrude_to_xy(), and Slic3r::GCodeWriter::unretract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ emit_f()

void Slic3r::GCodeFormatter::emit_f ( double  speed)
inline
180 {
181 this->emit_axis('F', speed, XYZF_EXPORT_DIGITS);
182 }
static constexpr const int XYZF_EXPORT_DIGITS
Definition GCodeWriter.hpp:137

References emit_axis(), and XYZF_EXPORT_DIGITS.

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::_travel_to_z(), Slic3r::PressureEqualizer::push_line_to_output(), Slic3r::GCodeWriter::set_speed(), Slic3r::GCodeWriter::travel_to_xy(), Slic3r::GCodeWriter::travel_to_xyz(), and Slic3r::GCodeWriter::unretract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ emit_string()

void Slic3r::GCodeFormatter::emit_string ( const std::string &  s)
inline
184 {
185 strncpy(ptr_err.ptr, s.c_str(), s.size());
186 ptr_err.ptr += s.size();
187 }

References ptr_err.

Referenced by emit_comment(), Slic3r::PressureEqualizer::push_line_to_output(), and Slic3r::GCodeWriter::set_speed().

+ Here is the caller graph for this function:

◆ emit_xy()

void Slic3r::GCodeFormatter::emit_xy ( const Vec2d point)
inline
158 {
159 this->emit_axis('X', point.x(), XYZF_EXPORT_DIGITS);
160 this->emit_axis('Y', point.y(), XYZF_EXPORT_DIGITS);
161 }

References emit_axis(), and XYZF_EXPORT_DIGITS.

Referenced by Slic3r::GCodeWriter::extrude_to_xy(), and Slic3r::GCodeWriter::travel_to_xy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ emit_xyz()

void Slic3r::GCodeFormatter::emit_xyz ( const Vec3d point)
inline
163 {
164 this->emit_axis('X', point.x(), XYZF_EXPORT_DIGITS);
165 this->emit_axis('Y', point.y(), XYZF_EXPORT_DIGITS);
166 this->emit_z(point.z());
167 }
void emit_z(const double z)
Definition GCodeWriter.hpp:169

References emit_axis(), emit_z(), and XYZF_EXPORT_DIGITS.

Referenced by Slic3r::GCodeWriter::travel_to_xyz().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ emit_z()

void Slic3r::GCodeFormatter::emit_z ( const double  z)
inline
169 {
170 this->emit_axis('Z', z, XYZF_EXPORT_DIGITS);
171 }

References emit_axis(), and XYZF_EXPORT_DIGITS.

Referenced by Slic3r::GCodeWriter::_travel_to_z(), and emit_xyz().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator=()

GCodeFormatter & Slic3r::GCodeFormatter::operator= ( const GCodeFormatter )
delete

◆ quantize()

static double Slic3r::GCodeFormatter::quantize ( double  v,
size_t  ndigits 
)
inlinestatic
152{ return std::round(v * pow_10[ndigits]) * pow_10_inv[ndigits]; }
static constexpr const std::array< double, 10 > pow_10_inv
Definition GCodeWriter.hpp:149

References pow_10, and pow_10_inv.

Referenced by quantize_e(), and quantize_xyzf().

+ Here is the caller graph for this function:

◆ quantize_e()

static double Slic3r::GCodeFormatter::quantize_e ( double  v)
inlinestatic
154{ return quantize(v, E_EXPORT_DIGITS); }
static double quantize(double v, size_t ndigits)
Definition GCodeWriter.hpp:152

References E_EXPORT_DIGITS, and quantize().

Referenced by Slic3r::Extruder::extrude(), Slic3r::Extruder::retract_to_go(), and Slic3r::Wipe::wipe().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quantize_xyzf()

static double Slic3r::GCodeFormatter::quantize_xyzf ( double  v)
inlinestatic
153{ return quantize(v, XYZF_EXPORT_DIGITS); }

References quantize(), and XYZF_EXPORT_DIGITS.

Referenced by Slic3r::GCode::point_to_gcode_quantized().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ string()

std::string Slic3r::GCodeFormatter::string ( )
inline
196 {
197 *ptr_err.ptr ++ = '\n';
198 return std::string(this->buf, ptr_err.ptr - buf);
199 }

References buf, and ptr_err.

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::_travel_to_z(), Slic3r::GCodeWriter::extrude_to_xy(), Slic3r::PressureEqualizer::push_to_output(), Slic3r::GCodeWriter::set_speed(), Slic3r::GCodeWriter::travel_to_xy(), Slic3r::GCodeWriter::travel_to_xyz(), and Slic3r::GCodeWriter::unretract().

+ Here is the caller graph for this function:

Member Data Documentation

◆ buf

char Slic3r::GCodeFormatter::buf[buflen]
protected

◆ buf_end

char* Slic3r::GCodeFormatter::buf_end
protected

◆ buflen

constexpr const size_t Slic3r::GCodeFormatter::buflen = 256
staticconstexprprotected

◆ E_EXPORT_DIGITS

constexpr const int Slic3r::GCodeFormatter::E_EXPORT_DIGITS = 5
staticconstexpr

◆ pow_10

constexpr const std::array<double, 10> Slic3r::GCodeFormatter::pow_10 { 1., 10., 100., 1000., 10000., 100000., 1000000., 10000000., 100000000., 1000000000.}
staticconstexpr

Referenced by emit_axis(), and quantize().

◆ pow_10_inv

constexpr const std::array<double, 10> Slic3r::GCodeFormatter::pow_10_inv {1./1., 1./10., 1./100., 1./1000., 1./10000., 1./100000., 1./1000000., 1./10000000., 1./100000000., 1./1000000000.}
staticconstexpr

Referenced by quantize().

◆ ptr_err

std::to_chars_result Slic3r::GCodeFormatter::ptr_err
protected

◆ XYZF_EXPORT_DIGITS

constexpr const int Slic3r::GCodeFormatter::XYZF_EXPORT_DIGITS = 3
staticconstexpr

The documentation for this class was generated from the following files: