Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/FormerLurker/ArcWelderLib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFormerLurker <hochgebe@gmail.com>2021-11-06 22:59:27 +0300
committerFormerLurker <hochgebe@gmail.com>2021-11-06 22:59:27 +0300
commitb57d34b8e69d6d5680ba271d6040734899295d05 (patch)
tree5184a94bd4120ba7683a3abe92f5abe49854a82c /GcodeProcessorLib
parent441742cfd83667fe8e2507a35cc0207687519650 (diff)
Rewrite of ArcStraightener to support multiple firmware types and versions. Convert utilities class to namespace.
Diffstat (limited to 'GcodeProcessorLib')
-rw-r--r--GcodeProcessorLib/fpconv.cpp4
-rw-r--r--GcodeProcessorLib/fpconv.h2
-rw-r--r--GcodeProcessorLib/utilities.cpp244
-rw-r--r--GcodeProcessorLib/utilities.h192
4 files changed, 336 insertions, 106 deletions
diff --git a/GcodeProcessorLib/fpconv.cpp b/GcodeProcessorLib/fpconv.cpp
index e22731a..9c2b8ee 100644
--- a/GcodeProcessorLib/fpconv.cpp
+++ b/GcodeProcessorLib/fpconv.cpp
@@ -190,7 +190,7 @@ static int generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K)
for (divp = tens + 10; kappa > 0; divp++) {
unsigned long long div = *divp;
- unsigned digit = part1 / div;
+ unsigned digit = (unsigned int)(part1 / div);
if (digit || idx) {
digits[idx++] = digit + '0';
@@ -216,7 +216,7 @@ static int generate_digits(Fp* fp, Fp* upper, Fp* lower, char* digits, int* K)
delta *= 10;
kappa--;
- unsigned digit = part2 >> -one.exp;
+ unsigned digit = (unsigned int)(part2 >> -one.exp);
if (digit || idx) {
digits[idx++] = digit + '0';
}
diff --git a/GcodeProcessorLib/fpconv.h b/GcodeProcessorLib/fpconv.h
index 6896a3a..8afcb21 100644
--- a/GcodeProcessorLib/fpconv.h
+++ b/GcodeProcessorLib/fpconv.h
@@ -149,7 +149,7 @@ static Fp find_cachedpow10(int exp, int* k)
{
const double one_log_ten = 0.30102999566398114;
- int approx = -(exp + npowers) * one_log_ten;
+ int approx = (int)(-(exp + npowers) * one_log_ten);
int idx = (approx - firstpower) / steppowers;
while (1) {
diff --git a/GcodeProcessorLib/utilities.cpp b/GcodeProcessorLib/utilities.cpp
index e4b8526..c1ac9fc 100644
--- a/GcodeProcessorLib/utilities.cpp
+++ b/GcodeProcessorLib/utilities.cpp
@@ -19,21 +19,30 @@
// You can contact the author at the following email address:
// FormerLurker@pm.me
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
#include "utilities.h"
-#include <cmath>
-#include <sstream>
-#include <iostream>
-#include <iomanip>
-#include <algorithm>
-#include "fpconv.h"
-const std::string utilities::WHITESPACE_ = " \n\r\t\f\v";
-const char utilities::GUID_RANGE[] = "0123456789abcdef";
-const bool utilities::GUID_DASHES[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
+namespace utilities {
+ const std::string WHITESPACE_ = " \n\r\t\f\v";
+ const char GUID_RANGE[] = "0123456789abcdef";
+ const bool GUID_DASHES[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
+
+ extern const char PATH_SEPARATOR_ =
+#ifdef _WIN32
+ '\\';
+#else
+ '/';
+#endif
+
+}
bool utilities::is_zero(double x, double tolerance)
{
- return std::fabs(x) < tolerance;
+ return fabs(x) < tolerance;
+}
+bool utilities::is_zero(double x)
+{
+ return fabs(x) < ZERO_TOLERANCE;
}
int utilities::round_up_to_int(double x, double tolerance)
@@ -41,32 +50,63 @@ int utilities::round_up_to_int(double x, double tolerance)
return int(x + tolerance);
}
+int utilities::round_up_to_int(double x)
+{
+ return int(x + ZERO_TOLERANCE);
+}
+
bool utilities::is_equal(double x, double y, double tolerance)
{
- double abs_difference = std::fabs(x - y);
+ double abs_difference = fabs(x - y);
return abs_difference < tolerance;
}
+bool utilities::is_equal(double x, double y)
+{
+ double abs_difference = fabs(x - y);
+ return abs_difference < ZERO_TOLERANCE;
+}
+
bool utilities::greater_than(double x, double y, double tolerance)
{
return x > y && !is_equal(x, y, tolerance);
}
+bool utilities::greater_than(double x, double y)
+{
+ return x > y && !is_equal(x, y);
+}
+
bool utilities::greater_than_or_equal(double x, double y, double tolerance)
{
return x > y || is_equal(x, y, tolerance);
}
+bool utilities::greater_than_or_equal(double x, double y)
+{
+ return x > y || is_equal(x, y);
+}
+
bool utilities::less_than(double x, double y, double tolerance)
{
return x < y && !is_equal(x, y, tolerance);
}
+bool utilities::less_than(double x, double y)
+{
+ return x < y && !is_equal(x, y);
+}
+
bool utilities::less_than_or_equal(double x, double y, double tolerance)
{
return x < y || is_equal(x, y, tolerance);
}
+bool utilities::less_than_or_equal(double x, double y)
+{
+ return x < y || is_equal(x, y);
+}
+
double utilities::get_cartesian_distance(double x1, double y1, double x2, double y2)
{
@@ -74,7 +114,7 @@ double utilities::get_cartesian_distance(double x1, double y1, double x2, double
double xdif = x1 - x2;
double ydif = y1 - y2;
double dist_squared = xdif * xdif + ydif * ydif;
- return std::sqrt(dist_squared);
+ return sqrt(dist_squared);
}
double utilities::get_cartesian_distance(double x1, double y1, double z1, double x2, double y2, double z2)
@@ -84,7 +124,7 @@ double utilities::get_cartesian_distance(double x1, double y1, double z1, double
double ydif = y1 - y2;
double zdif = z1 - z2;
double dist_squared = xdif * xdif + ydif * ydif + zdif * zdif;
- return std::sqrt(dist_squared);
+ return sqrt(dist_squared);
}
double utilities::get_arc_distance(double x1, double y1, double z1, double x2, double y2, double z2, double i, double j, double r, bool is_clockwise)
@@ -92,10 +132,10 @@ double utilities::get_arc_distance(double x1, double y1, double z1, double x2, d
double center_x = x1 - i;
double center_y = y1 - j;
double radius = hypot(i, j);
- double z_dist = z2-z1;
+ double z_dist = z2 - z1;
double rt_x = x2 - center_x;
double rt_y = y2 - center_y;
- double angular_travel_total = std::atan2(i * rt_y - j * rt_x, i * rt_x + j * rt_y);
+ double angular_travel_total = atan2(i * rt_y - j * rt_x, i * rt_x + j * rt_y);
if (angular_travel_total < 0) { angular_travel_total += (double)(2.0 * PI_DOUBLE); }
// Adjust the angular travel if the direction is clockwise
if (is_clockwise) { angular_travel_total -= (float)(2 * PI_DOUBLE); }
@@ -104,10 +144,10 @@ double utilities::get_arc_distance(double x1, double y1, double z1, double x2, d
{
angular_travel_total += (float)(2 * PI_DOUBLE);
}
-
+
// 20200417 - FormerLurker - rename millimeters_of_travel to millimeters_of_travel_arc to better describe what we are
// calculating here
- return hypot(angular_travel_total * radius, std::fabs(z_dist));
+ return hypot(angular_travel_total * radius, fabs(z_dist));
}
@@ -142,6 +182,33 @@ std::string utilities::trim(const std::string& s)
return rtrim(ltrim(s));
}
+std::string utilities::join(const std::string* strings, size_t length, std::string sep)
+{
+ std::string output;
+ for (int i = 0; i < length; i++)
+ {
+ if (i > 0)
+ {
+ output += sep;
+ }
+ output += strings[i];
+ }
+ return output;
+}
+
+std::string utilities::join(const std::vector<std::string> strings, std::string sep)
+{
+ std::string output;
+
+ for (std::vector<std::string>::const_iterator p = strings.begin();
+ p != strings.end(); ++p) {
+ output += *p;
+ if (p != strings.end() - 1)
+ output += sep;
+ }
+ return output;
+}
+
std::istream& utilities::safe_get_line(std::istream& is, std::string& t)
{
t.clear();
@@ -174,7 +241,7 @@ std::istream& utilities::safe_get_line(std::istream& is, std::string& t)
}
}
-std::string utilities::center(std::string input, int width)
+std::string utilities::center(std::string input, int width)
{
int input_width = (int)input.length();
int difference = width - input_width;
@@ -182,7 +249,7 @@ std::string utilities::center(std::string input, int width)
{
return input;
}
- int left_padding = difference /2;
+ int left_padding = difference / 2;
int right_padding = width - left_padding - input_width;
return std::string(left_padding, ' ') + input + std::string(right_padding, ' ');
}
@@ -231,7 +298,7 @@ std::string utilities::get_percent_change_string(int v1, int v2, int precision)
int utilities::get_num_digits(int x)
{
- x = abs(x);
+ x = (int)abs(x);
return (x < 10 ? 1 :
(x < 100 ? 2 :
(x < 1000 ? 3 :
@@ -247,14 +314,14 @@ int utilities::get_num_digits(int x)
int utilities::get_num_digits(double x, int precision)
{
return get_num_digits(
- (int) std::ceil(x * std::pow(10, (double)precision) - .4999999999999)
- / std::pow(10, (double)precision)
+ (int)ceil(x * pow(10, (double)precision) - .4999999999999)
+ / pow(10, (double)precision)
);
}
int utilities::get_num_digits(double x)
{
- return get_num_digits((int) x);
+ return get_num_digits((int)x);
}
// Nice utility function found here: https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path
@@ -285,7 +352,7 @@ std::vector<std::string> utilities::splitpath(const std::string& str)
return result;
}
-bool utilities::get_file_path(const std::string& file_path, std::string & path)
+bool utilities::get_file_path(const std::string& file_path, std::string& path)
{
std::vector<std::string> file_parts = splitpath(file_path);
if (file_parts.size() == 0)
@@ -311,12 +378,12 @@ std::string utilities::create_uuid() {
bool utilities::get_temp_file_path_for_file(const std::string& file_path, std::string& temp_file_path)
{
temp_file_path = "";
- if (!utilities::get_file_path(file_path, temp_file_path))
+ if (!get_file_path(file_path, temp_file_path))
{
return false;
}
temp_file_path = temp_file_path;
- temp_file_path += utilities::create_uuid();
+ temp_file_path += create_uuid();
temp_file_path += ".tmp";
return true;
}
@@ -331,7 +398,92 @@ double utilities::hypot(double x, double y)
}
if (y == 0.0) return x;
y /= x;
- return x * std::sqrt(1.0 + y * y);
+ return x * sqrt(1.0 + y * y);
+}
+
+double utilities::atan2(double y, double x)
+{
+ return std::atan2(y, x);
+}
+
+double utilities::atan2f(double y, double x)
+{
+ return std::atan2(y, x);
+}
+
+double utilities::floor(double x)
+{
+ return std::floor(x);
+}
+
+double utilities::floorf(double x)
+{
+ return std::floor(x);
+}
+
+double utilities::ceil(double x)
+{
+ return std::ceil(x);
+}
+
+double utilities::cos(double x)
+{
+ return std::cos(x);
+}
+
+double utilities::sin(double x)
+{
+ return std::sin(x);
+}
+
+double utilities::cosf(double x)
+{
+ return std::cos(x);
+}
+
+double utilities::sinf(double x)
+{
+ return std::sin(x);
+}
+
+double utilities::abs(double x)
+{
+ return std::abs(x);
+}
+
+double utilities::fabs(double x)
+{
+ return std::fabs(x);
+}
+
+double utilities::fabsf(double x)
+{
+ return std::fabs(x);
+}
+
+double utilities::sqrt(double x)
+{
+ return std::sqrt(x);
+}
+
+double utilities::sqrtf(double x)
+{
+ return std::sqrt(x);
+}
+
+double utilities::pow(int e, double x)
+{
+ return std::pow(e, x);
+}
+
+double utilities::min(float x, float y)
+{
+ return std::min(x, y);
+}
+
+void* utilities::memcpy(void* dest, const void* src, size_t n)
+{
+ return std::memcpy(dest, src, n);
}
std::string utilities::dtos(double x, unsigned char precision)
@@ -339,14 +491,14 @@ std::string utilities::dtos(double x, unsigned char precision)
static char buffer[FPCONV_BUFFER_LENGTH];
char* p = buffer;
buffer[fpconv_dtos(x, buffer, precision)] = '\0';
- /* This is code that can be used to compare the output of the
+ /* This is code that can be used to compare the output of the
modified fpconv_dtos function to the ofstream output
Note: It currently only fails for some checks where the original double does not store
perfectly. In this case I actually think the dtos output is better than ostringstream!
std::ostringstream stream;
stream << std::fixed;
stream << std::setprecision(precision) << x;
-
+
if (std::string(buffer) != stream.str())
{
std::cout << std::fixed << "Failed to convert: " << std::setprecision(24) << x << " Precision:" << std::setprecision(0) << static_cast <int> (precision) << " String:" << std::string(buffer) << " Stream:" << stream.str() << std::endl;
@@ -355,7 +507,7 @@ std::string utilities::dtos(double x, unsigned char precision)
return buffer;
}
/*
-bool utilities::case_insensitive_compare_char(char& c1, char& c2)
+bool case_insensitive_compare_char(char& c1, char& c2)
{
if (c1 == c2)
return true;
@@ -366,10 +518,34 @@ bool utilities::case_insensitive_compare_char(char& c1, char& c2)
/*
* Case Insensitive String Comparision
-
-bool utilities::case_insensitive_compare(std::string& str1, std::string& str2)
+
+bool case_insensitive_compare(std::string& str1, std::string& str2)
{
- return ((str1.size() == str2.size()) && std::equal(str1.begin(), str1.end(), str2.begin(), &utilities::case_insensitive_compare_char));
+ return ((str1.size() == str2.size()) && std::equal(str1.begin(), str1.end(), str2.begin(), &case_insensitive_compare_char));
+}
+
+*/
+
+std::string utilities::replace(std::string subject, const std::string& search, const std::string& replace) {
+ size_t pos = 0;
+ while ((pos = subject.find(search, pos)) != std::string::npos) {
+ subject.replace(pos, search.length(), replace);
+ pos += replace.length();
+ }
+ return subject;
+}
+
+double utilities::rand_range(double min, double max) {
+ double f = (double)std::rand() / RAND_MAX;
+ return min + f * (max - min);
+}
+
+unsigned char utilities::rand_range(unsigned char min, unsigned char max) {
+ double f = (double)std::rand() / RAND_MAX;
+ return static_cast<unsigned char>(static_cast<double>(min) + f * (static_cast<double>(max) - static_cast<double>(min)));
}
-*/ \ No newline at end of file
+int utilities::rand_range(int min, int max) {
+ double f = (double)std::rand() / RAND_MAX;
+ return static_cast<int>(static_cast<double>(min) + f * (static_cast<double>(max) - static_cast<double>(min)));
+} \ No newline at end of file
diff --git a/GcodeProcessorLib/utilities.h b/GcodeProcessorLib/utilities.h
index cf3a97b..7e2f80b 100644
--- a/GcodeProcessorLib/utilities.h
+++ b/GcodeProcessorLib/utilities.h
@@ -20,83 +20,137 @@
// FormerLurker@pm.me
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-#pragma once
+#ifndef UTILITIES_H
+#define UTILITIES_H
#include <string>
#include <vector>
#include <set>
+#include <cmath>
+#include <sstream>
+#include <iostream>
+#include <iomanip>
+#include <algorithm>
+#include <cstring>
+#include "fpconv.h"
+
#define FPCONV_BUFFER_LENGTH 25
// Had to increase the zero tolerance because prusa slicer doesn't always
// retract enough while wiping.
#define ZERO_TOLERANCE 0.000005
#define PI_DOUBLE 3.14159265358979323846264338327950288
-class utilities{
-public:
- static bool is_zero(double x, double tolerance = ZERO_TOLERANCE);
- static int round_up_to_int(double x, double tolerance = ZERO_TOLERANCE);
- static bool is_equal(double x, double y, double tolerance = ZERO_TOLERANCE);
- static bool greater_than(double x, double y, double tolerance = ZERO_TOLERANCE);
- static bool greater_than_or_equal(double x, double y, double tolerance = ZERO_TOLERANCE);
- static bool less_than(double x, double y, double tolerance = ZERO_TOLERANCE);
- static bool less_than_or_equal(double x, double y, double tolerance = ZERO_TOLERANCE);
-
- static double get_cartesian_distance(double x1, double y1, double x2, double y2);
- static double get_cartesian_distance(double x1, double y1, double z1, double x2, double y2, double z2);
- static double get_arc_distance(double x1, double y1, double z1, double x2, double y2, double z2, double i, double j, double r, bool is_clockwise);
- /* Todo: Implement for gcode comment processor
- static bool case_insensitive_compare_char(char& c1, char& c2);
- static bool case_insensitive_compare(std::string& str1, std::string& str2);
- */
- static std::string to_string(double value);
- static std::string to_string(int value);
- static std::string ltrim(const std::string& s);
- static std::string rtrim(const std::string& s);
- static std::string trim(const std::string& s);
- static std::istream& safe_get_line(std::istream& is, std::string& t);
- static std::string center(std::string input, int width);
- static double get_percent_change(int v1, int v2);
- static double get_percent_change(double v1, double v2);
- static std::string get_percent_change_string(int v1, int v2, int precision);
- static int get_num_digits(int x);
- static int get_num_digits(double x);
- static int get_num_digits(double x, int precision);
-
- static std::vector<std::string> splitpath(const std::string& str);
- static bool get_file_path(const std::string& file_path, std::string& path);
- static bool get_temp_file_path_for_file(const std::string& file_path, std::string& temp_file_path);
- static std::string create_uuid();
- // Man I can't wait till I can drop python 2.7 support so I can stop doing everything myself. s
- // td::hypot doesn't work for msvc for python 2.7....
- static double hypot(double x, double y);
- static std::string dtos(double x, unsigned char precision);
-
- static double rand_range(double min, double max) {
- double f = (double)std::rand() / RAND_MAX;
- return min + f * (max - min);
- }
-
- static unsigned char rand_range(unsigned char min, unsigned char max) {
- double f = (double)std::rand() / RAND_MAX;
- return static_cast<unsigned char>(static_cast<double>(min) + f * (static_cast<double>(max) - static_cast<double>(min)));
- }
-
- static int rand_range(int min, int max) {
- double f = (double)std::rand() / RAND_MAX;
- return static_cast<int>(static_cast<double>(min) + f * (static_cast<double>(max) - static_cast<double>(min)));
- }
+namespace utilities{
+ extern const std::string WHITESPACE_;
+ extern const char GUID_RANGE[];
+ extern const bool GUID_DASHES[];
+
+ extern const char PATH_SEPARATOR_;
+ bool is_zero(double x, double tolerance);
+ bool is_zero(double x);
+
+ int round_up_to_int(double x, double tolerance);
+ int round_up_to_int(double x);
+
+ bool is_equal(double x, double y, double tolerance);
+
+ bool is_equal(double x, double y);
+
+ bool greater_than(double x, double y, double tolerance);
+
+ bool greater_than(double x, double y);
+
+ bool greater_than_or_equal(double x, double y, double tolerance);
+ bool greater_than_or_equal(double x, double y);
+
+ bool less_than(double x, double y, double tolerance);
+ bool less_than(double x, double y);
+
+ bool less_than_or_equal(double x, double y, double tolerance);
+
+ bool less_than_or_equal(double x, double y);
+
+ double get_cartesian_distance(double x1, double y1, double x2, double y2);
+
+ double get_cartesian_distance(double x1, double y1, double z1, double x2, double y2, double z2);
+
+ double get_arc_distance(double x1, double y1, double z1, double x2, double y2, double z2, double i, double j, double r, bool is_clockwise);
+ std::string to_string(double value);
+
+ std::string to_string(int value);
+ std::string ltrim(const std::string& s);
+
+ std::string rtrim(const std::string& s);
+
+ std::string trim(const std::string& s);
+ std::string join(const std::string* strings, size_t length, std::string sep);
+
+ std::string join(const std::vector<std::string> strings, std::string sep);
+
+ std::istream& safe_get_line(std::istream& is, std::string& t);
+
+ std::string center(std::string input, int width);
+ double get_percent_change(int v1, int v2);
+ double get_percent_change(double v1, double v2);
+ std::string get_percent_change_string(int v1, int v2, int precision);
+
+ int get_num_digits(int x);
+ int get_num_digits(double x, int precision);
+
+ int get_num_digits(double x);
+ // Nice utility function found here: https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path
+ std::vector<std::string> splitpath(const std::string& str);
+
+ bool get_file_path(const std::string& file_path, std::string& path);
+ std::string create_uuid();
+
+ bool get_temp_file_path_for_file(const std::string& file_path, std::string& temp_file_path);
+
+ double hypot(double x, double y);
+
+ double atan2(double y, double x);
+
+ double atan2f(double y, double x);
+
+ double floor(double x);
+
+ double floorf(double x);
+
+ double ceil(double x);
+
+ double cos(double x);
+
+ double sin(double x);
+
+ double cosf(double x);
+
+ double sinf(double x);
+
+ double abs(double x);
+
+ double fabs(double x);
+
+ double fabsf(double x);
+
+ double sqrt(double x);
+
+ double sqrtf(double x);
+
+ double pow(int e, double x);
+
+ double min(float x, float y);
+
+ void* memcpy(void* dest, const void* src, size_t n);
+
+ std::string dtos(double x, unsigned char precision);
-protected:
- static const std::string WHITESPACE_;
- static const char PATH_SEPARATOR_ =
-#ifdef _WIN32
- '\\';
-#else
- '/';
-#endif
- static const char GUID_RANGE[];
- static const bool GUID_DASHES[];
-private:
- utilities();
-
-};
+ std::string replace(std::string subject, const std::string& search, const std::string& replace);
+
+ double rand_range(double min, double max);
+ unsigned char rand_range(unsigned char min, unsigned char max);
+ int rand_range(int min, int max);
+
+
+
+}
+#endif \ No newline at end of file