Import PrusaSlicer G2/G3 arc discretization code

This commit is contained in:
Andrew Sun
2025-09-20 17:40:12 -04:00
parent 20f132e09a
commit 1394a3ccf2
7 changed files with 513 additions and 482 deletions

View File

@@ -222,6 +222,28 @@ bool GCodeReader::parse_file_raw(const std::string &filename, raw_line_callback_
[](size_t){});
}
const char* GCodeReader::axis_pos(const char *raw_str, char axis)
{
const char *c = raw_str;
// Skip the whitespaces.
c = skip_whitespaces(c);
// Skip the command.
c = skip_word(c);
// Up to the end of line or comment.
while (! is_end_of_gcode_line(*c)) {
// Skip whitespaces.
c = skip_whitespaces(c);
if (is_end_of_gcode_line(*c))
break;
// Check the name of the axis.
if (*c == axis)
return c;
// Skip the rest of the word.
c = skip_word(c);
}
return nullptr;
}
bool GCodeReader::GCodeLine::has(char axis) const
{
const char *c = m_raw.c_str();
@@ -244,6 +266,29 @@ bool GCodeReader::GCodeLine::has(char axis) const
return false;
}
std::string_view GCodeReader::GCodeLine::axis_pos(char axis) const
{
const std::string &s = this->raw();
const char *c = GCodeReader::axis_pos(this->raw().c_str(), axis);
return c ? std::string_view{ c, s.size() - (c - s.data()) } : std::string_view();
}
bool GCodeReader::GCodeLine::has_value(std::string_view axis_pos, float &value)
{
if (const char *c = axis_pos.data(); c) {
// Try to parse the numeric value.
double v = 0.;
const char *end = axis_pos.data() + axis_pos.size();
auto [pend, ec] = fast_float::from_chars(++ c, end, v);
if (pend != c && is_end_of_word(*pend)) {
// The axis value has been parsed correctly.
value = float(v);
return true;
}
}
return false;
}
bool GCodeReader::GCodeLine::has_value(char axis, float &value) const
{
assert(is_decimal_separator_point());