Merge branch 'lh_gcode_export'

This commit is contained in:
Lukáš Hejl 2021-09-11 00:56:34 +02:00
commit 4f5dddbf11
6 changed files with 2510 additions and 24 deletions

2449
src/fast_float/fast_float.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,6 @@
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/nowide/fstream.hpp>
#include <charconv>
#include <fstream>
#include <iostream>
#include <iomanip>
@ -10,6 +9,7 @@
#include "LocalesUtils.hpp"
#include <Shiny/Shiny.h>
#include <fast_float/fast_float.h>
namespace Slic3r {
@ -71,16 +71,9 @@ const char* GCodeReader::parse_line_internal(const char *ptr, const char *end, G
}
if (axis != NUM_AXES_WITH_UNKNOWN) {
// Try to parse the numeric value.
#ifdef WIN32
double v;
auto [pend, ec] = std::from_chars(++ c, end, v);
auto [pend, ec] = fast_float::from_chars(++ c, end, v);
if (pend != c && is_end_of_word(*pend)) {
#else
// The older version of GCC and Clang support std::from_chars just for integers, so strtod we used it instead.
char *pend = nullptr;
double v = strtod(++ c, &pend);
if (pend != nullptr && is_end_of_word(*pend)) {
#endif
// The axis value has been parsed correctly.
if (axis != UNKNOWN_AXIS)
gline.m_axis[int(axis)] = float(v);

View File

@ -7,6 +7,10 @@
#include <map>
#include <assert.h>
#ifdef __APPLE__
#include <boost/spirit/include/karma.hpp>
#endif
#define XYZF_EXPORT_DIGITS 3
#define E_EXPORT_DIGITS 5
@ -273,17 +277,47 @@ public:
}
void emit_axis(const char axis, const double v, size_t digits) {
*ptr_err.ptr ++ = ' '; *ptr_err.ptr ++ = axis;
#ifdef WIN32
this->ptr_err = std::to_chars(this->ptr_err.ptr, this->buf_end, v, std::chars_format::fixed, digits);
assert(digits <= 6);
static constexpr const std::array<int, 7> pow_10{1, 10, 100, 1000, 10000, 100000, 1000000};
*ptr_err.ptr++ = ' '; *ptr_err.ptr++ = axis;
char *base_ptr = this->ptr_err.ptr;
auto v_int = int64_t(std::round(v * pow_10[digits]));
// Older stdlib on macOS doesn't support std::from_chars at all, so it is used boost::spirit::karma::generate instead of it.
// That is a little bit slower than std::to_chars but not much.
#ifdef __APPLE__
boost::spirit::karma::generate(this->ptr_err.ptr, boost::spirit::karma::int_generator<int64_t>(), v_int);
#else
int buf_capacity = int(this->buf_end - this->ptr_err.ptr);
int ret = snprintf(this->ptr_err.ptr, buf_capacity, "%.*lf", int(digits), v);
if (ret <= 0 || ret > buf_capacity)
ptr_err.ec = std::errc::value_too_large;
else
this->ptr_err.ptr = this->ptr_err.ptr + ret;
// this->buf_end minus 1 because we need space for adding the extra decimal point.
this->ptr_err = std::to_chars(this->ptr_err.ptr, this->buf_end - 1, v_int);
#endif
size_t writen_digits = (this->ptr_err.ptr - base_ptr) - (v_int < 0 ? 1 : 0);
if (writen_digits < digits) {
// Number is smaller than 10^digits, so that we will pad it with zeros.
size_t remaining_digits = digits - writen_digits;
// Move all newly inserted chars by remaining_digits to allocate space for padding with zeros.
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)
*to_ptr = *from_ptr;
memset(this->ptr_err.ptr - writen_digits, '0', remaining_digits);
this->ptr_err.ptr += remaining_digits;
}
// Move all newly inserted chars by one to allocate space for a decimal point.
for (char *to_ptr = this->ptr_err.ptr, *from_ptr = to_ptr - 1; from_ptr >= this->ptr_err.ptr - digits; --to_ptr, --from_ptr)
*to_ptr = *from_ptr;
*(this->ptr_err.ptr - digits) = '.';
for (size_t i = 0; i < digits; ++i) {
if (*this->ptr_err.ptr != '0')
break;
this->ptr_err.ptr--;
}
if (*this->ptr_err.ptr == '.')
this->ptr_err.ptr--;
if ((this->ptr_err.ptr + 1) == base_ptr || *this->ptr_err.ptr == '-')
*(++this->ptr_err.ptr) = '0';
this->ptr_err.ptr++;
}
void emit_xy(const Vec2d &point) {

View File

@ -122,7 +122,9 @@ void CopyrightsDialog::fill_entries()
{ "AppImage packaging for Linux using AppImageKit"
, "2004-2019 Simon Peter and contributors" , "https://appimage.org/" },
{ "lib_fts"
, "Forrest Smith" , "https://www.forrestthewoods.com/" }
, "Forrest Smith" , "https://www.forrestthewoods.com/" },
{ "fast_float"
, "Daniel Lemire, João Paulo Magalhaes and contributors", "https://github.com/fastfloat/fast_float" }
};
}

View File

@ -36,7 +36,15 @@ plan tests => 8;
$layer_infill{$self->Z} = 1;
}
}
$layers{$args->{Z}} = 1 if $cmd eq 'G1' && $info->{dist_Z} > 0;
# Previously, all G-code commands had a fixed number of decimal points with means with redundant zeros after decimal points.
# We changed this behavior and got rid of these redundant padding zeros, which caused this test to fail
# because the position in Z-axis is compared as a string, and previously, G-code contained the following two commands:
# "G1 Z5 F5000 ; lift nozzle"
# "G1 Z5.000 F7800.000"
# That has a different Z-axis position from the view of string comparisons of floating-point numbers.
# To correct the computation of the number of printed layers, even in the case of string comparisons of floating-point numbers,
# we filtered out the G-code command with the commend 'lift nozzle'.
$layers{$args->{Z}} = 1 if $cmd eq 'G1' && $info->{dist_Z} && index($info->{comment}, 'lift nozzle') == -1;
});
my $layers_with_perimeters = scalar(keys %layer_infill);

View File

@ -78,13 +78,13 @@ SCENARIO("set_speed emits values with fixed-point output.", "[GCodeWriter]") {
}
}
WHEN("set_speed is called to set speed to 1") {
THEN("Output string is G1 F1.000") {
REQUIRE_THAT(writer.set_speed(1.0), Catch::Equals("G1 F1.000\n"));
THEN("Output string is G1 F1") {
REQUIRE_THAT(writer.set_speed(1.0), Catch::Equals("G1 F1\n"));
}
}
WHEN("set_speed is called to set speed to 203.200022") {
THEN("Output string is G1 F203.200") {
REQUIRE_THAT(writer.set_speed(203.200022), Catch::Equals("G1 F203.200\n"));
THEN("Output string is G1 F203.2") {
REQUIRE_THAT(writer.set_speed(203.200022), Catch::Equals("G1 F203.2\n"));
}
}
WHEN("set_speed is called to set speed to 203.200522") {