diff options
author | FormerLurker <hochgebe@gmail.com> | 2020-10-17 02:34:25 +0300 |
---|---|---|
committer | FormerLurker <hochgebe@gmail.com> | 2020-10-17 02:34:25 +0300 |
commit | 4fd38897fd66c245991a4066c7bb3db373087e70 (patch) | |
tree | 30a91cd0ded86278929869ec265c939fc839e18a /ArcWelder | |
parent | 04958af691abfebc8314b300b4d8676f074439a7 (diff) |
Add additional statistics. Fix windows c++ build for python 2.7 compilers.
Diffstat (limited to 'ArcWelder')
-rw-r--r-- | ArcWelder/arc_welder.cpp | 109 | ||||
-rw-r--r-- | ArcWelder/arc_welder.h | 298 | ||||
-rw-r--r-- | ArcWelder/segmented_arc.cpp | 5 | ||||
-rw-r--r-- | ArcWelder/segmented_arc.h | 1 | ||||
-rw-r--r-- | ArcWelder/segmented_shape.cpp | 18 | ||||
-rw-r--r-- | ArcWelder/segmented_shape.h | 2 | ||||
-rw-r--r-- | ArcWelder/unwritten_command.h | 11 |
7 files changed, 377 insertions, 67 deletions
diff --git a/ArcWelder/arc_welder.cpp b/ArcWelder/arc_welder.cpp index 2d3eac5..c283e26 100644 --- a/ArcWelder/arc_welder.cpp +++ b/ArcWelder/arc_welder.cpp @@ -22,6 +22,9 @@ // You can contact the author at the following email address: // FormerLurker@pm.me //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#if _MSC_VER > 1200 +#define _CRT_SECURE_NO_DEPRECATE +#endif #include "arc_welder.h" #include <vector> @@ -31,7 +34,9 @@ #include <fstream> #include <iomanip> #include <sstream> -arc_welder::arc_welder(std::string source_path, std::string target_path, logger * log, double resolution_mm, double max_radius, gcode_position_args args) : current_arc_(DEFAULT_MIN_SEGMENTS, gcode_position_args_.position_buffer_size - 5, resolution_mm, max_radius) + + +arc_welder::arc_welder(std::string source_path, std::string target_path, logger * log, double resolution_mm, double max_radius, bool g90_g91_influences_extruder, int buffer_size, progress_callback callback) : current_arc_(DEFAULT_MIN_SEGMENTS, buffer_size - 5, resolution_mm, max_radius), segment_statistics_(segment_statistic_lengths, segment_statistic_lengths_count, log) { p_logger_ = log; debug_logging_enabled_ = false; @@ -40,12 +45,12 @@ arc_welder::arc_welder(std::string source_path, std::string target_path, logger verbose_logging_enabled_ = false; logger_type_ = 0; - progress_callback_ = NULL; + progress_callback_ = callback; verbose_output_ = false; source_path_ = source_path; target_path_ = target_path; resolution_mm_ = resolution_mm; - gcode_position_args_ = args; + gcode_position_args_ = get_args_(g90_g91_influences_extruder, buffer_size); notification_period_seconds = 1; lines_processed_ = 0; gcodes_processed_ = 0; @@ -66,19 +71,8 @@ arc_welder::arc_welder(std::string source_path, std::string target_path, logger } // We don't care about the printer settings, except for g91 influences extruder. - p_source_position_ = new gcode_position(gcode_position_args_); -} - -arc_welder::arc_welder(std::string source_path, std::string target_path, logger* log, double resolution_mm, double max_radius, bool g90_g91_influences_extruder, int buffer_size) - : arc_welder(source_path, target_path, log, resolution_mm, max_radius, arc_welder::get_args_(g90_g91_influences_extruder, buffer_size)) -{ -} - -arc_welder::arc_welder(std::string source_path, std::string target_path, logger * log, double resolution_mm, double max_radius, bool g90_g91_influences_extruder, int buffer_size, progress_callback callback) - : arc_welder(source_path, target_path, log, resolution_mm, max_radius, arc_welder::get_args_(g90_g91_influences_extruder, buffer_size)) -{ - progress_callback_ = callback; + p_source_position_ = new gcode_position(gcode_position_args_); } gcode_position_args arc_welder::get_args_(bool g90_g91_influences_extruder, int buffer_size) @@ -236,7 +230,7 @@ arc_welder_results results; stream << "Parsing: " << line; p_logger_->log(logger_type_, VERBOSE, stream.str()); } - parser_.try_parse_gcode(line.c_str(), cmd); + parser_.try_parse_gcode(line.c_str(), cmd, true); bool has_gcode = false; if (cmd.gcode.length() > 0) { @@ -251,7 +245,7 @@ arc_welder_results results; // Always process the command through the printer, even if no command is found // This is important so that comments can be analyzed //std::cout << "stabilization::process_file - updating position..."; - process_gcode(cmd, false); + process_gcode(cmd, false, false); // Only continue to process if we've found a command and either a progress_callback_ is supplied, or debug loggin is enabled. if (has_gcode && (progress_callback_ != NULL || info_logging_enabled_)) @@ -271,19 +265,20 @@ arc_welder_results results; if (current_arc_.is_shape() && waiting_for_arc_) { p_logger_->log(logger_type_, DEBUG, "The target file opened successfully."); - process_gcode(cmd, true); + process_gcode(cmd, true, false); } p_logger_->log(logger_type_, DEBUG, "Writing all unwritten gcodes to the target file."); write_unwritten_gcodes_to_file(); + p_logger_->log(logger_type_, DEBUG, "Fetching the final progress struct."); - p_logger_->log(logger_type_, DEBUG, "Processing complete, closing source and target file."); arc_welder_progress final_progress = get_progress_(static_cast<long>(file_size_), static_cast<double>(start_clock)); if (progress_callback_ != NULL || info_logging_enabled_) { // Sending final progress update message + p_logger_->log(logger_type_, VERBOSE, "Sending final progress update message."); on_progress_(final_progress); } - + p_logger_->log(logger_type_, DEBUG, "Processing complete, closing source and target file."); output_file_.close(); gcodeFile.close(); const clock_t end_clock = clock(); @@ -291,6 +286,8 @@ arc_welder_results results; results.success = continue_processing; results.cancelled = !continue_processing; results.progress = final_progress; + p_logger_->log(logger_type_, DEBUG, "Returning processing results."); + return results; } @@ -329,20 +326,20 @@ arc_welder_progress arc_welder::get_progress_(long source_file_position, double progress.compression_percent = (1.0 - (static_cast<float>(progress.target_file_size) / static_cast<float>(source_file_position))) * 100.0f; } + progress.segment_statistics = segment_statistics_; return progress; } -int arc_welder::process_gcode(parsed_command cmd, bool is_end) +int arc_welder::process_gcode(parsed_command cmd, bool is_end, bool is_reprocess) { // Update the position for the source gcode file p_source_position_->update(cmd, lines_processed_, gcodes_processed_, -1); - position* p_cur_pos = p_source_position_->get_current_position_ptr(); position* p_pre_pos = p_source_position_->get_previous_position_ptr(); extruder extruder_current = p_cur_pos->get_current_extruder(); extruder previous_extruder = p_pre_pos->get_current_extruder(); - point p(p_cur_pos->x, p_cur_pos->y, p_cur_pos->z, extruder_current.e_relative); + point p(p_cur_pos->get_gcode_x(), p_cur_pos->get_gcode_y(), p_cur_pos->get_gcode_z(), extruder_current.e_relative); //std::cout << lines_processed_ << " - " << cmd.gcode << ", CurrentEAbsolute: " << cur_extruder.e <<", ExtrusionLength: " << cur_extruder.extrusion_length << ", Retraction Length: " << cur_extruder.retraction_length << ", IsExtruding: " << cur_extruder.is_extruding << ", IsRetracting: " << cur_extruder.is_retracting << ".\n"; int lines_written = 0; @@ -351,12 +348,28 @@ int arc_welder::process_gcode(parsed_command cmd, bool is_end) bool arc_added = false; bool clear_shapes = false; + // Update the source file statistics + if (p_cur_pos->has_xy_position_changed && (extruder_current.is_extruding || extruder_current.is_retracting) && !is_reprocess) + { + double movement_length_mm = utilities::get_cartesian_distance(p_pre_pos->x, p_pre_pos->y, p_cur_pos->x, p_cur_pos->y); + if (movement_length_mm > 0) + { + segment_statistics_.update(movement_length_mm, true); + } + } + // We need to make sure the printer is using absolute xyz, is extruding, and the extruder axis mode is the same as that of the previous position // TODO: Handle relative XYZ axis. This is possible, but maybe not so important. if ( !is_end && cmd.is_known_command && !cmd.is_empty && ( (cmd.command == "G0" || cmd.command == "G1") && utilities::is_equal(p_cur_pos->z, p_pre_pos->z) && + utilities::is_equal(p_cur_pos->x_offset, p_pre_pos->x_offset) && + utilities::is_equal(p_cur_pos->y_offset, p_pre_pos->y_offset) && + utilities::is_equal(p_cur_pos->z_offset, p_pre_pos->z_offset) && + utilities::is_equal(p_cur_pos->x_firmware_offset, p_pre_pos->x_firmware_offset) && + utilities::is_equal(p_cur_pos->y_firmware_offset, p_pre_pos->y_firmware_offset) && + utilities::is_equal(p_cur_pos->z_firmware_offset, p_pre_pos->z_firmware_offset) && !p_cur_pos->is_relative && ( !waiting_for_arc_ || @@ -378,7 +391,7 @@ int arc_welder::process_gcode(parsed_command cmd, bool is_end) } write_unwritten_gcodes_to_file(); // add the previous point as the starting point for the current arc - point previous_p(p_pre_pos->x, p_pre_pos->y, p_pre_pos->z, previous_extruder.e_relative); + point previous_p(p_pre_pos->get_gcode_x(), p_pre_pos->get_gcode_y(), p_pre_pos->get_gcode_z(), previous_extruder.e_relative); // Don't add any extrusion, or you will over extrude! //std::cout << "Trying to add first point (" << p.x << "," << p.y << "," << p.z << ")..."; current_arc_.try_add_point(previous_p, 0); @@ -488,6 +501,7 @@ int arc_welder::process_gcode(parsed_command cmd, bool is_end) } } } + if (!arc_added) { if (current_arc_.get_num_segments() < current_arc_.get_min_segments()) { @@ -507,9 +521,10 @@ int arc_welder::process_gcode(parsed_command cmd, bool is_end) if (current_arc_.is_shape()) { - // increment our statistics + // update our statistics points_compressed_ += current_arc_.get_num_segments()-1; - arcs_created_++; + arcs_created_++; // increment the number of generated arcs + //std::cout << "Arc shape found.\n"; // Get the comment now, before we remove the previous comments std::string comment = get_comment_for_arc(); @@ -548,13 +563,21 @@ int arc_welder::process_gcode(parsed_command cmd, bool is_end) if (debug_logging_enabled_) { - p_logger_->log(logger_type_, DEBUG, "Arc created with " + std::to_string(current_arc_.get_num_segments()) + " segments: " + gcode); + char buffer[20]; + std::string message = "Arc created with "; + sprintf(buffer, "%d", current_arc_.get_num_segments()); + message += buffer; + message += " segments: "; + message += gcode; + p_logger_->log(logger_type_, DEBUG, message); } // Get and alter the current position so we can add it to the unwritten commands list parsed_command arc_command = parser_.parse_gcode(gcode.c_str()); + double arc_extrusion_length = current_arc_.get_shape_length(); + unwritten_commands_.push_back( - unwritten_command(arc_command, p_cur_pos->is_extruder_relative) + unwritten_command(arc_command, p_cur_pos->is_extruder_relative, arc_extrusion_length) ); // write all unwritten commands (if we don't do this we'll mess up absolute e by adding an offset to the arc) @@ -569,7 +592,7 @@ int arc_welder::process_gcode(parsed_command cmd, bool is_end) // Reprocess this line if (!is_end) { - return process_gcode(cmd, false); + return process_gcode(cmd, false, true); } else { @@ -597,17 +620,21 @@ int arc_welder::process_gcode(parsed_command cmd, bool is_end) } } - if (clear_shapes) - { - waiting_for_arc_ = false; - current_arc_.clear(); - // The current command is unwritten, add it. - unwritten_commands_.push_back(unwritten_command(p_source_position_->get_current_position_ptr())); - } - else if (waiting_for_arc_ || !arc_added) + + + if (waiting_for_arc_ || !arc_added) { + position* cur_pos = p_source_position_->get_current_position_ptr(); + extruder& cur_extruder = cur_pos->get_current_extruder(); - unwritten_commands_.push_back(unwritten_command(p_source_position_->get_current_position_ptr())); + double length = 0; + if (p_cur_pos->has_xy_position_changed && (cur_extruder.is_extruding || cur_extruder.is_retracting)) + { + position* prev_pos = p_source_position_->get_previous_position_ptr(); + length = utilities::get_cartesian_distance(cur_pos->x, cur_pos->y, prev_pos->x, prev_pos->y); + } + + unwritten_commands_.push_back(unwritten_command(cur_pos, length)); } if (!waiting_for_arc_) @@ -648,7 +675,7 @@ std::string arc_welder::create_g92_e(double absolute_e) int arc_welder::write_gcode_to_file(std::string gcode) { - output_file_ << utilities::trim(gcode) << "\n"; + output_file_ << gcode << "\n"; return 1; } @@ -662,6 +689,10 @@ int arc_welder::write_unwritten_gcodes_to_file() { // The the current unwritten position and remove it from the list unwritten_command p = unwritten_commands_.pop_front(); + if (p.extrusion_length > 0) + { + segment_statistics_.update(p.extrusion_length, false); + } write_gcode_to_file(p.command.to_string()); } diff --git a/ArcWelder/arc_welder.h b/ArcWelder/arc_welder.h index 3e501af..d21c2c8 100644 --- a/ArcWelder/arc_welder.h +++ b/ArcWelder/arc_welder.h @@ -36,11 +36,284 @@ #include "array_list.h" #include "unwritten_command.h" #include "logger.h" +#include <cmath> + +#ifdef _MSC_VER +#define _CRT_SECURE_NO_WARNINGS +#endif + #define DEFAULT_G90_G91_INFLUENCES_EXTREUDER false +static const int segment_statistic_lengths_count = 12; +const double segment_statistic_lengths[] = { 0.002f, 0.005f, 0.01f, 0.05f, 0.1f, 0.5f, 1.0f, 5.0f, 10.0f, 20.0f, 50.0f, 100.0f }; + +struct segment_statistic { + segment_statistic(double min_length_mm, double max_length_mm) + { + count = 0; + min_mm = min_length_mm; + max_mm = max_length_mm; + } + + double min_mm; + double max_mm; + int count; +}; + +struct source_target_segment_statistics { + + + source_target_segment_statistics(const double segment_tracking_lengths[], const int num_lengths, logger* p_logger = NULL) { + total_length_source = 0; + total_length_target = 0; + total_count_source = 0; + total_count_target = 0; + max_width = 0; + max_precision = 3; + num_segment_tracking_lengths = num_lengths; + double current_min = 0; + for (int index = 0; index < num_lengths; index++) + { + double current_max = segment_tracking_lengths[index]; + source_segments.push_back(segment_statistic(current_min, segment_tracking_lengths[index])); + target_segments.push_back(segment_statistic(current_min, segment_tracking_lengths[index])); + current_min = current_max; + } + source_segments.push_back(segment_statistic(current_min, -1.0f)); + target_segments.push_back(segment_statistic(current_min, -1.0f)); + max_width = utilities::get_num_digits(current_min); + p_logger_ = p_logger_; + logger_type_ = 0; + } + + std::vector<segment_statistic> source_segments; + std::vector<segment_statistic> target_segments; + double total_length_source; + double total_length_target; + int max_width; + int max_precision; + int total_count_source; + int total_count_target; + int num_segment_tracking_lengths; + + void update(double length, bool is_source) + { + if (length <= 0) + return; + + std::vector<segment_statistic>* stats; + if (is_source) + { + total_count_source++; + total_length_source += length; + stats = &source_segments; + } + else + { + total_count_target++; + total_length_target += length; + stats = &target_segments; + } + for (int index = 0; index < (*stats).size(); index++) + { + segment_statistic& stat = (*stats)[index]; + if (stat.min_mm <= length && stat.max_mm > length || (index + 1) == (*stats).size()) + { + stat.count++; + break; + } + } + } + + std::string str() const { + + //if (p_logger_ != NULL) p_logger_->log(logger_type_, VERBOSE, "Building Segment Statistics."); + + std::stringstream output_stream; + std::stringstream format_stream; + const int min_column_size = 8; + int mm_col_size = max_width + max_precision + 2; // Adding 2 for the mm + int min_max_label_col_size = 4; + int percent_col_size = 9; + int totals_row_label_size = 22; + int count_col_size; + + // Calculate the count column size + int max_count = 0; + //if (p_logger_ != NULL) p_logger_->log(logger_type_, VERBOSE, "Calculating Column Size."); + + for (int index = 0; index < source_segments.size(); index++) + { + int source_count = source_segments[index].count; + int target_count = target_segments[index].count; + if (max_count < source_count) + { + max_count = source_count; + } + if (max_count < target_count) + { + max_count = target_count; + } + } + // Get the number of digits in the max count + count_col_size = utilities::get_num_digits(max_count); + // enforce the minimum of 6 + if (count_col_size < min_column_size) + { + count_col_size = min_column_size; + } + + if (max_precision > 0) + { + // We need an extra space in our column for the decimal. + mm_col_size++; + } + + // enforce the min column size + if (mm_col_size < min_column_size) + { + mm_col_size = min_column_size; + } + // Get the table width + int table_width = mm_col_size + min_max_label_col_size + mm_col_size + count_col_size + count_col_size + percent_col_size; + // Add a separator for the statistics + //output_stream << std::setw(table_width) << std::setfill('-') << "-" << "\n" << std::setfill(' ') ; + // Output the column headers + // Center the min and max column. + output_stream << utilities::center("Min", mm_col_size); + output_stream << std::setw(min_max_label_col_size) << ""; + output_stream << utilities::center("Max", mm_col_size); + // right align the source, target and change columns + output_stream << std::setw(count_col_size) << std::right << "Source"; + output_stream << std::setw(count_col_size) << std::right << "Target"; + output_stream << std::setw(percent_col_size) << std::right << "Change"; + output_stream << "\n"; + output_stream << std::setw(table_width) << std::setfill('-') << "" << std::setfill(' ') << "\n"; + output_stream << std::fixed << std::setprecision(max_precision); + for (int index = 0; index < source_segments.size(); index++) { + //extract the necessary variables from the source and target segments + double min_mm = source_segments[index].min_mm; + double max_mm = source_segments[index].max_mm; + int source_count = source_segments[index].count; + int target_count = target_segments[index].count; + // Calculate the percent change and create the string + // Construct the percent_change_string + std::string percent_change_string = utilities::get_percent_change_string(source_count, target_count, 1); + + // Create the strings to hold the column values + std::string min_mm_string; + std::string max_mm_string; + std::string source_count_string; + std::string target_count_string; + + // Clear the format stream and construct the min_mm_string + format_stream.str(std::string()); + format_stream << std::fixed << std::setprecision(max_precision) << min_mm << "mm"; + min_mm_string = format_stream.str(); + // Clear the format stream and construct the max_mm_string + format_stream.str(std::string()); + format_stream << std::fixed << std::setprecision(max_precision) << max_mm << "mm"; + max_mm_string = format_stream.str(); + // Clear the format stream and construct the source_count_string + format_stream.str(std::string()); + format_stream << std::fixed << std::setprecision(0) << source_count; + source_count_string = format_stream.str(); + // Clear the format stream and construct the target_count_string + format_stream.str(std::string()); + format_stream << std::fixed << std::setprecision(0) << target_count; + target_count_string = format_stream.str(); + // The min and max columns and the label need to be handled differently if this is the last item + if (index == source_segments.size() - 1) + { + // If we are on the last setment item, the 'min' value is the max, and there is no end + // The is because the last item contains the count of all items above the max length provided + // in the constructor + + // The 'min' column is empty here + output_stream << std::setw(mm_col_size) << std::internal << ""; + // Add the min/max label + output_stream << std::setw(min_max_label_col_size) << " >= "; + // Add the min mm string + output_stream << std::setw(mm_col_size) << std::internal << min_mm_string; + } + else + { + //if (p_logger_ != NULL) p_logger_->log(logger_type_, VERBOSE, "Adding row text."); + + // add the 'min' column + output_stream << std::setw(mm_col_size) << std::internal << min_mm_string; + // Add the min/max label + output_stream << std::setw(min_max_label_col_size) << " to "; + // Add the 'max' column + output_stream << std::setw(mm_col_size) << std::internal << max_mm_string; + } + // Add the source count + output_stream << std::setw(count_col_size) << source_count_string; + // Add the target count + output_stream << std::setw(count_col_size) << target_count_string; + // Add the percent change string + output_stream << std::setw(percent_col_size) << percent_change_string; + // End the line + output_stream << "\n"; + } + // Add the total rows separator + output_stream << std::setw(table_width) << std::setfill('-') << "" << std::setfill(' ') << "\n"; + // Add the total rows; + if (utilities::is_equal(total_length_source, total_length_target, 0.001)) + { + std::string total_distance_string; + format_stream.str(std::string()); + format_stream << std::fixed << std::setprecision(max_precision) << total_length_source << "mm"; + total_distance_string = format_stream.str(); + output_stream << std::setw(totals_row_label_size) << std::right << "Total distance:"; + output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_distance_string << "\n" << std::setfill(' '); + } + else + { + // We need to output two different distances (this probably should never happen) + // Format the total source distance string + std::string total_source_distance_string; + format_stream.str(std::string()); + format_stream << std::fixed << std::setprecision(max_precision) << total_length_source << "mm"; + total_source_distance_string = format_stream.str(); + // Add the total source distance row + output_stream << std::setw(totals_row_label_size) << std::right << "Total distance source:"; + output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_source_distance_string << "\n" << std::setfill(' '); + + // Format the total target distance string + std::string total_target_distance_string; + format_stream.str(std::string()); + format_stream << std::fixed << std::setprecision(max_precision) << total_length_target << "mm"; + total_target_distance_string = format_stream.str(); + // Add the total target distance row + output_stream << std::setw(totals_row_label_size) << std::right << "Total distance target:"; + output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_target_distance_string << "\n" << std::setfill(' '); + } + + // Add the total count rows + // Add the source count + output_stream << std::setprecision(0) << std::setw(totals_row_label_size) << std::right << "Total count source:"; + output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_count_source << "\n" << std::setfill(' '); + // Add the target count + output_stream << std::setw(totals_row_label_size) << std::right << "Total count target:"; + output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_count_target << "\n" << std::setfill(' '); + // Add the total percent change row + std::string total_percent_change_string = utilities::get_percent_change_string(total_count_source, total_count_target, 1); + output_stream << std::setw(totals_row_label_size) << std::right << "Total percent change:"; + output_stream << std::setw(table_width - totals_row_label_size) << std::setfill('.') << std::right << total_percent_change_string << "\n" << std::setfill(' '); + std::string output_string = output_stream.str(); + return output_string; + } + +private: + logger* p_logger_; + int logger_type_; +}; + +// Struct to hold the progress, statistics, and return values struct arc_welder_progress { - arc_welder_progress() { + arc_welder_progress() : segment_statistics(segment_statistic_lengths, segment_statistic_lengths_count, NULL) { percent_complete = 0.0; seconds_elapsed = 0.0; seconds_remaining = 0.0; @@ -53,6 +326,7 @@ struct arc_welder_progress { target_file_size = 0; compression_ratio = 0; compression_percent = 0; + } double percent_complete; double seconds_elapsed; @@ -66,11 +340,12 @@ struct arc_welder_progress { long source_file_position; long source_file_size; long target_file_size; + source_target_segment_statistics segment_statistics; std::string str() const { std::stringstream stream; stream << std::fixed << std::setprecision(2); - + stream << percent_complete << "% complete in " << seconds_elapsed << " seconds with " << seconds_remaining << " seconds remaining."; stream << " Gcodes Processed: " << gcodes_processed; stream << ", Current Line: " << lines_processed; @@ -80,8 +355,12 @@ struct arc_welder_progress { stream << ", Size Reduction: " << compression_percent << "% "; return stream.str(); } + std::string detail_str() const { + std::stringstream stream; + stream << "\n" << "Extrusion/Retraction Counts" << "\n" << segment_statistics.str() << "\n"; + return stream.str(); + } }; - // define the progress callback type typedef bool(*progress_callback)(arc_welder_progress); @@ -101,9 +380,7 @@ struct arc_welder_results { class arc_welder { public: - arc_welder(std::string source_path, std::string target_path, logger * log, double resolution_mm, double max_radius_mm, gcode_position_args args); - arc_welder(std::string source_path, std::string target_path, logger * log, double resolution_mm, double max_radius_mm, bool g90_g91_influences_extruder, int buffer_size); - arc_welder(std::string source_path, std::string target_path, logger * log, double resolution_mm, double max_radius_mm, bool g90_g91_influences_extruder, int buffer_size, progress_callback callback); + arc_welder(std::string source_path, std::string target_path, logger* log, double resolution_mm, double max_radius, bool g90_g91_influences_extruder, int buffer_size, progress_callback callback = NULL); void set_logger_type(int logger_type); virtual ~arc_welder(); arc_welder_results process(); @@ -111,12 +388,12 @@ public: protected: virtual bool on_progress_(const arc_welder_progress& progress); private: - arc_welder_progress get_progress_(long source_file_position, double start_clock); + arc_welder_progress get_progress_(long source_file_position, double start_clock); void add_arcwelder_comment_to_target(); void reset(); static gcode_position_args get_args_(bool g90_g91_influences_extruder, int buffer_size); progress_callback progress_callback_; - int process_gcode(parsed_command cmd, bool is_end); + int process_gcode(parsed_command cmd, bool is_end, bool is_reprocess); int write_gcode_to_file(std::string gcode); std::string get_arc_gcode_relative(double f, const std::string comment); std::string get_arc_gcode_absolute(double e, double f, const std::string comment); @@ -134,6 +411,7 @@ private: int last_gcode_line_written_; int points_compressed_; int arcs_created_; + source_target_segment_statistics segment_statistics_; long get_file_size(const std::string& file_path); double get_time_elapsed(double start_clock, double end_clock); double get_next_update_time() const; @@ -141,9 +419,9 @@ private: array_list<unwritten_command> unwritten_commands_; segmented_arc current_arc_; std::ofstream output_file_; - + // We don't care about the printer settings, except for g91 influences extruder. - gcode_position * p_source_position_; + gcode_position* p_source_position_; double previous_feedrate_; bool previous_is_extruder_relative_; gcode_parser parser_; diff --git a/ArcWelder/segmented_arc.cpp b/ArcWelder/segmented_arc.cpp index 220e740..297304b 100644 --- a/ArcWelder/segmented_arc.cpp +++ b/ArcWelder/segmented_arc.cpp @@ -31,11 +31,12 @@ #include <stdio.h> #include <cmath> -segmented_arc::segmented_arc() : segmented_arc(DEFAULT_MIN_SEGMENTS, DEFAULT_MAX_SEGMENTS, DEFAULT_RESOLUTION_MM, DEFAULT_MAX_RADIUS_MM) +segmented_arc::segmented_arc() : segmented_shape(DEFAULT_MIN_SEGMENTS, DEFAULT_MAX_SEGMENTS, DEFAULT_RESOLUTION_MM) { + max_radius_mm_ = DEFAULT_MAX_RADIUS_MM; } -segmented_arc::segmented_arc(int min_segments, int max_segments, double resolution_mm, double max_radius_mm) : segmented_shape(min_segments, max_segments, resolution_mm) +segmented_arc::segmented_arc(int min_segments = DEFAULT_MIN_SEGMENTS, int max_segments = DEFAULT_MAX_SEGMENTS, double resolution_mm = DEFAULT_RESOLUTION_MM, double max_radius_mm = DEFAULT_MAX_RADIUS_MM) : segmented_shape(min_segments, max_segments, resolution_mm) { if (max_radius_mm > DEFAULT_MAX_RADIUS_MM) max_radius_mm_ = DEFAULT_MAX_RADIUS_MM; else max_radius_mm_ = max_radius_mm; diff --git a/ArcWelder/segmented_arc.h b/ArcWelder/segmented_arc.h index 9a412ad..04dbeea 100644 --- a/ArcWelder/segmented_arc.h +++ b/ArcWelder/segmented_arc.h @@ -54,7 +54,6 @@ private: bool try_get_arc_(const circle& c, arc& target_arc); std::string get_shape_gcode_(bool has_e, double e, double f) const; circle arc_circle_; - int test_count_ = 0; double max_radius_mm_; }; diff --git a/ArcWelder/segmented_shape.cpp b/ArcWelder/segmented_shape.cpp index 312a7dc..cd5d808 100644 --- a/ArcWelder/segmented_shape.cpp +++ b/ArcWelder/segmented_shape.cpp @@ -227,18 +227,18 @@ point circle::get_closest_point(const point& p) const #pragma region Arc Functions bool arc::try_create_arc(const circle& c, const point& start_point, const point& mid_point, const point& end_point, double approximate_length, double resolution, arc& target_arc) { - point p1 = c.get_closest_point(start_point); - point p2 = c.get_closest_point(mid_point); - point p3 = c.get_closest_point(end_point); + //point p1 = c.get_closest_point(start_point); + //point p2 = c.get_closest_point(mid_point); + //point p3 = c.get_closest_point(end_point); /*// Get the radians between p1 and p2 (short angle) double p1_p2_rad = c.get_radians(p1, p2); double p2_p3_rad = c.get_radians(p2, p3); double p3_p1_rad = c.get_radians(p3, p1); */ - double polar_start_theta = c.get_polar_radians(p1); - double polar_mid_theta = c.get_polar_radians(p2); - double polar_end_theta = c.get_polar_radians(p3); + double polar_start_theta = c.get_polar_radians(start_point); + double polar_mid_theta = c.get_polar_radians(mid_point); + double polar_end_theta = c.get_polar_radians(end_point); // variable to hold radians double angle_radians = 0; @@ -306,11 +306,7 @@ bool arc::try_create_arc(const circle& c, const array_list<point>& points, doubl } #pragma endregion -segmented_shape::segmented_shape() : segmented_shape(DEFAULT_MIN_SEGMENTS, DEFAULT_MAX_SEGMENTS, DEFAULT_RESOLUTION_MM ) -{ -} - -segmented_shape::segmented_shape(int min_segments, int max_segments, double resolution_mm) : points_(max_segments) +segmented_shape::segmented_shape(int min_segments = DEFAULT_MIN_SEGMENTS, int max_segments = DEFAULT_MAX_SEGMENTS, double resolution_mm = DEFAULT_RESOLUTION_MM) : points_(max_segments) { max_segments_ = max_segments; diff --git a/ArcWelder/segmented_shape.h b/ArcWelder/segmented_shape.h index 9ca480b..957ceab 100644 --- a/ArcWelder/segmented_shape.h +++ b/ArcWelder/segmented_shape.h @@ -174,7 +174,7 @@ double distance_from_segment(segment s, point p); class segmented_shape { public: - segmented_shape(); + segmented_shape(int min_segments, int max_segments, double resolution_mm); segmented_shape& operator=(const segmented_shape& pos); virtual ~segmented_shape(); diff --git a/ArcWelder/unwritten_command.h b/ArcWelder/unwritten_command.h index 5591dbe..5c6f151 100644 --- a/ArcWelder/unwritten_command.h +++ b/ArcWelder/unwritten_command.h @@ -31,20 +31,25 @@ struct unwritten_command is_extruder_relative = false; e_relative = 0; offset_e = 0; + extrusion_length = 0; } - unwritten_command(parsed_command &cmd, bool is_relative) { - is_relative = false; + unwritten_command(parsed_command &cmd, bool is_relative, double command_length) { + is_extruder_relative = is_relative; command = cmd; + extrusion_length = command_length; } - unwritten_command(position* p) { + unwritten_command(position* p, double command_length) { + e_relative = p->get_current_extruder().e_relative; offset_e = p->get_current_extruder().get_offset_e(); is_extruder_relative = p->is_extruder_relative; command = p->command; + extrusion_length = command_length; } bool is_extruder_relative; double e_relative; double offset_e; + double extrusion_length; parsed_command command; std::string to_string(bool rewrite, std::string additional_comment) |