OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GCodeG1Formatter Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/GCodeWriter.hpp>

Inheritance diagram for Slic3r::GCodeG1Formatter:
Collaboration diagram for Slic3r::GCodeG1Formatter:

Public Member Functions

 GCodeG1Formatter (bool high_precision)
 GCodeG1Formatter (const GCodeG1Formatter &)=delete
GCodeG1Formatter & operator= (const GCodeG1Formatter &)=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 (double v)
void emit_f (double speed)
void emit_ij (const Vec2d &point)
void emit_string (const std::string &s)
void emit_comment (bool allow_comments, const std::string &comment)
std::string string ()
void append_to (std::string &out)

Static Public Member Functions

static constexpr int xyz_digits (bool high_precision)
static constexpr int e_digits (bool high_precision)
static double quantize (double v, size_t ndigits)
static double quantize_xyz (double v, bool high_precision)
static double quantize_e (double v, bool high_precision)

Static Public Attributes

static constexpr const int F_EXPORT_DIGITS = 3
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

const bool m_high_precision
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

◆ GCodeG1Formatter() [1/2]

Slic3r::GCodeG1Formatter::GCodeG1Formatter ( bool high_precision)
inlineexplicit
408 : GCodeFormatter(high_precision) {
409 this->buf[0] = 'G';
410 this->buf[1] = '1';
411 this->buf_end = buf + buflen;
412 this->ptr_err.ptr = this->buf + 2;
413 }
static constexpr const size_t buflen
Definition GCodeWriter.hpp:400
GCodeFormatter(bool high_precision)
Definition GCodeWriter.hpp:320
char buf[buflen]
Definition GCodeWriter.hpp:401
std::to_chars_result ptr_err
Definition GCodeWriter.hpp:403
char * buf_end
Definition GCodeWriter.hpp:402

References Slic3r::GCodeFormatter::GCodeFormatter(), Slic3r::GCodeFormatter::buf, Slic3r::GCodeFormatter::buf_end, Slic3r::GCodeFormatter::buflen, and Slic3r::GCodeFormatter::ptr_err.

Referenced by GCodeG1Formatter(), and operator=().

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

◆ GCodeG1Formatter() [2/2]

Slic3r::GCodeG1Formatter::GCodeG1Formatter ( const GCodeG1Formatter & )
delete

References GCodeG1Formatter().

Here is the call graph for this function:

Member Function Documentation

◆ append_to()

void Slic3r::GCodeFormatter::append_to ( std::string & out)
inlineinherited
393 {
394 *ptr_err.ptr ++ = '\n';
395 out.append(this->buf, ptr_err.ptr - buf);
396 }

References buf, and ptr_err.

Referenced by Slic3r::GCodeWriter::extrude_arc_to_xy(), Slic3r::GCodeWriter::extrude_to_xy(), Slic3r::GCodeWriter::extrude_to_xyz(), Slic3r::GCodeWriter::set_speed(), and string().

Here is the caller graph for this function:

◆ e_digits()

constexpr int Slic3r::GCodeFormatter::e_digits ( bool high_precision)
inlinestaticconstexprinherited
335{ return high_precision ? 6 : 5; }

Referenced by Slic3r::FanMover::_print_in_middle_G1(), Slic3r::FanMover::_put_in_middle_G1(), emit_e(), Slic3r::SpiralVase::process_layer(), and quantize_e().

Here is the caller graph for this function:

◆ emit_axis()

void Slic3r::GCodeFormatter::emit_axis ( const char axis,
const double v,
size_t digits )
inherited
1683 {
1684 assert(digits <= 9);
1685 static constexpr const std::array<int, 10> pow_10{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
1686 *ptr_err.ptr++ = ' '; *ptr_err.ptr++ = axis;
1687
1688 char *base_ptr = this->ptr_err.ptr;
1689 auto v_int = int64_t(std::round(v * pow_10[digits]));
1690 // Older stdlib on macOS doesn't support std::from_chars at all, so it is used boost::spirit::karma::generate instead of it.
1691 // That is a little bit slower than std::to_chars but not much.
1692#ifdef __APPLE__
1693 boost::spirit::karma::generate(this->ptr_err.ptr, boost::spirit::karma::int_generator<int64_t>(), v_int);
1694#else
1695 // this->buf_end minus 1 because we need space for adding the extra decimal point.
1696 this->ptr_err = std::to_chars(this->ptr_err.ptr, this->buf_end - 1, v_int);
1697#endif
1698 size_t writen_digits = (this->ptr_err.ptr - base_ptr) - (v_int < 0 ? 1 : 0);
1699 if (writen_digits < digits) {
1700 // Number is smaller than 10^digits, so that we will pad it with zeros.
1701 size_t remaining_digits = digits - writen_digits;
1702 // Move all newly inserted chars by remaining_digits to allocate space for padding with zeros.
1703 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)
1704 *to_ptr = *from_ptr;
1705
1706 memset(this->ptr_err.ptr - writen_digits, '0', remaining_digits);
1707 this->ptr_err.ptr += remaining_digits;
1708 }
1709
1710 // Move all newly inserted chars by one to allocate space for a decimal point.
1711 for (char *to_ptr = this->ptr_err.ptr, *from_ptr = to_ptr - 1; from_ptr >= this->ptr_err.ptr - digits; --to_ptr, --from_ptr)
1712 *to_ptr = *from_ptr;
1713
1714 *(this->ptr_err.ptr - digits) = '.';
1715 for (size_t i = 0; i < digits; ++i) {
1716 if (*this->ptr_err.ptr != '0')
1717 break;
1718 this->ptr_err.ptr--;
1719 }
1720 if (*this->ptr_err.ptr == '.')
1721 this->ptr_err.ptr--;
1722 if ((this->ptr_err.ptr + 1) == base_ptr || *this->ptr_err.ptr == '-')
1723 *(++this->ptr_err.ptr) = '0';
1724 this->ptr_err.ptr++;
1725
1726#if 0 // #ifndef NDEBUG
1727 {
1728 // Verify that the optimized formatter produces the same result as the standard sprintf().
1729 double v1 = atof(std::string(base_ptr, this->ptr_err.ptr).c_str());
1730 char buf[2048];
1731 sprintf(buf, "%.*lf", int(digits), v);
1732 double v2 = atof(buf);
1733 // Numbers may differ when rounding at exactly or very close to 0.5 due to numerical issues when scaling the double to an integer.
1734 // Thus the complex assert.
1735// assert(v1 == v2);
1736 assert(std::abs(v1 - v) * pow_10[digits] < 0.50001);
1737 assert(std::abs(v2 - v) * pow_10[digits] < 0.50001);
1738 }
1739#endif // NDEBUG
1740}
static constexpr const std::array< double, 10 > pow_10
Definition GCodeWriter.hpp:337

References buf, pow_10, and ptr_err.

Referenced by emit_e(), emit_f(), emit_ij(), 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 )
inlineinherited
380 {
381 if (allow_comments && ! comment.empty()) {
382 *ptr_err.ptr ++ = ' '; *ptr_err.ptr ++ = ';'; *ptr_err.ptr ++ = ' ';
383 this->emit_string(comment);
384 }
385 }
void emit_string(const std::string &s)
Definition GCodeWriter.hpp:375

References emit_string(), and ptr_err.

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::_spiral_travel_to_z(), Slic3r::GCodeWriter::_travel_to_z(), Slic3r::GCodeWriter::extrude_arc_to_xy(), Slic3r::GCodeWriter::extrude_to_xy(), Slic3r::GCodeWriter::extrude_to_xyz(), 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 ( double v)
inlineinherited
362 {
363 this->emit_axis('E', v, e_digits(m_high_precision));
364 }
static constexpr int e_digits(bool high_precision)
Definition GCodeWriter.hpp:335
void emit_axis(const char axis, const double v, size_t digits)
Definition GCodeWriter.cpp:1683
const bool m_high_precision
Definition GCodeWriter.hpp:399

References e_digits(), emit_axis(), and m_high_precision.

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::extrude_arc_to_xy(), Slic3r::GCodeWriter::extrude_to_xy(), Slic3r::GCodeWriter::extrude_to_xyz(), Slic3r::PressureEqualizer::push_line_to_output(), 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)
inlineinherited
366 {
367 this->emit_axis('F', speed, F_EXPORT_DIGITS);
368 }
static constexpr const int F_EXPORT_DIGITS
Definition GCodeWriter.hpp:336

References emit_axis(), and F_EXPORT_DIGITS.

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::_spiral_travel_to_z(), 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_ij()

void Slic3r::GCodeFormatter::emit_ij ( const Vec2d & point)
inlineinherited
370 {
371 this->emit_axis('I', point.x(), xyz_digits(m_high_precision));
372 this->emit_axis('J', point.y(), xyz_digits(m_high_precision));
373 }
static constexpr int xyz_digits(bool high_precision)
Definition GCodeWriter.hpp:334

References emit_axis(), m_high_precision, and xyz_digits().

Referenced by Slic3r::GCodeWriter::_spiral_travel_to_z(), and Slic3r::GCodeWriter::extrude_arc_to_xy().

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)
inlineinherited
375 {
376 strncpy(ptr_err.ptr, s.c_str(), s.size());
377 ptr_err.ptr += s.size();
378 }

References ptr_err.

Referenced by Slic3r::GCodeWriter::_spiral_travel_to_z(), 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)
inlineinherited
347 {
348 this->emit_axis('X', point.x(), xyz_digits(m_high_precision));
349 this->emit_axis('Y', point.y(), xyz_digits(m_high_precision));
350 }

References emit_axis(), m_high_precision, and xyz_digits().

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

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)
inlineinherited
352 {
353 this->emit_axis('X', point.x(), xyz_digits(m_high_precision));
354 this->emit_axis('Y', point.y(), xyz_digits(m_high_precision));
355 this->emit_z(point.z());
356 }
void emit_z(const double z)
Definition GCodeWriter.hpp:358

References emit_axis(), emit_z(), m_high_precision, and xyz_digits().

Referenced by Slic3r::GCodeWriter::_spiral_travel_to_z(), Slic3r::GCodeWriter::_travel_to_z(), Slic3r::GCodeWriter::extrude_to_xy(), Slic3r::GCodeWriter::extrude_to_xyz(), Slic3r::GCodeWriter::travel_to_xy(), and 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)
inlineinherited
358 {
359 this->emit_axis('Z', z, xyz_digits(m_high_precision));
360 }

References emit_axis(), m_high_precision, and xyz_digits().

Referenced by Slic3r::GCodeWriter::_spiral_travel_to_z(), 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=()

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

References GCodeG1Formatter().

Here is the call graph for this function:

◆ quantize()

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

References pow_10, and pow_10_inv.

Referenced by quantize_e(), and quantize_xyz().

Here is the caller graph for this function:

◆ quantize_e()

double Slic3r::GCodeFormatter::quantize_e ( double v,
bool high_precision )
inlinestaticinherited
343{ return quantize(v, e_digits(high_precision)); }
static double quantize(double v, size_t ndigits)
Definition GCodeWriter.hpp:341

References e_digits(), and quantize().

Referenced by Slic3r::Extruder::E_to_emit().

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

◆ quantize_xyz()

double Slic3r::GCodeFormatter::quantize_xyz ( double v,
bool high_precision )
inlinestaticinherited
342{ return quantize(v, xyz_digits(high_precision)); }

References quantize(), and xyz_digits().

Referenced by Slic3r::GCode::_extrude(), Slic3r::GCodeWriter::extrude_to_xyz(), Slic3r::GCode::point_to_gcode_quantized(), and 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 ( )
inlineinherited
387 {
388 std::string out;
389 this->append_to(out);
390 return out;
391 }
void append_to(std::string &out)
Definition GCodeWriter.hpp:393

References append_to().

Referenced by Slic3r::GCodeWriter::_retract(), Slic3r::GCodeWriter::_spiral_travel_to_z(), Slic3r::GCodeWriter::_travel_to_z(), Slic3r::PressureEqualizer::push_to_output(), 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:

◆ xyz_digits()

constexpr int Slic3r::GCodeFormatter::xyz_digits ( bool high_precision)
inlinestaticconstexprinherited
334{ return high_precision ? 4 : 3; }

Referenced by Slic3r::FanMover::_print_in_middle_G1(), Slic3r::FanMover::_put_in_middle_G1(), emit_ij(), emit_xy(), emit_xyz(), emit_z(), Slic3r::SpiralVase::process_layer(), Slic3r::PressureEqualizer::push_line_to_output(), and quantize_xyz().

Here is the caller graph for this function:

Member Data Documentation

◆ buf

◆ buf_end

char* Slic3r::GCodeFormatter::buf_end
protectedinherited

◆ buflen

const size_t Slic3r::GCodeFormatter::buflen = 256
staticconstexprprotectedinherited

◆ F_EXPORT_DIGITS

const int Slic3r::GCodeFormatter::F_EXPORT_DIGITS = 3
staticconstexprinherited

Referenced by emit_f().

◆ m_high_precision

const bool Slic3r::GCodeFormatter::m_high_precision
protectedinherited

◆ pow_10

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

Referenced by emit_axis(), and quantize().

◆ pow_10_inv

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. }
staticconstexprinherited
338{ 1. / 1., 1. / 10., 1. / 100., 1. / 1000., 1. / 10000., 1. / 100000., 1. / 1000000., 1. / 10000000., 1. / 100000000., 1. / 1000000000. };

Referenced by quantize().

◆ ptr_err

std::to_chars_result Slic3r::GCodeFormatter::ptr_err
protectedinherited

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