2014-08-03 17:42:29 +00:00
|
|
|
#ifndef _libslic3r_h_
|
|
|
|
#define _libslic3r_h_
|
|
|
|
|
2018-09-24 09:53:05 +00:00
|
|
|
#include "libslic3r_version.h"
|
2020-10-07 14:02:01 +00:00
|
|
|
#define GCODEVIEWER_APP_NAME "PrusaSlicer G-code Viewer"
|
|
|
|
#define GCODEVIEWER_APP_KEY "PrusaSlicerGcodeViewer"
|
|
|
|
#define GCODEVIEWER_BUILD_ID std::string("PrusaSlicer G-code Viewer-") + std::string(SLIC3R_VERSION) + std::string("-UNKNOWN")
|
2018-09-24 09:53:05 +00:00
|
|
|
|
2014-08-03 17:42:29 +00:00
|
|
|
// this needs to be included early for MSVC (listing it in Build.PL is not enough)
|
2019-01-16 11:22:17 +00:00
|
|
|
#include <memory>
|
2020-06-26 14:58:53 +00:00
|
|
|
#include <array>
|
2019-01-16 11:22:17 +00:00
|
|
|
#include <algorithm>
|
2014-08-03 17:42:29 +00:00
|
|
|
#include <ostream>
|
|
|
|
#include <iostream>
|
2016-11-27 14:25:22 +00:00
|
|
|
#include <math.h>
|
2016-11-26 15:07:36 +00:00
|
|
|
#include <queue>
|
2014-08-03 17:42:29 +00:00
|
|
|
#include <sstream>
|
2016-10-20 16:34:33 +00:00
|
|
|
#include <cstdio>
|
2016-10-20 16:38:44 +00:00
|
|
|
#include <stdint.h>
|
2016-10-20 15:44:46 +00:00
|
|
|
#include <stdarg.h>
|
2016-11-26 15:07:36 +00:00
|
|
|
#include <vector>
|
2019-01-16 11:22:17 +00:00
|
|
|
#include <cassert>
|
2019-01-29 11:09:40 +00:00
|
|
|
#include <cmath>
|
2020-03-27 08:05:26 +00:00
|
|
|
#include <type_traits>
|
2014-08-03 17:42:29 +00:00
|
|
|
|
2018-09-17 13:12:13 +00:00
|
|
|
#include "Technologies.hpp"
|
2019-08-09 15:01:37 +00:00
|
|
|
#include "Semver.hpp"
|
2018-09-17 13:12:13 +00:00
|
|
|
|
2020-03-25 13:34:51 +00:00
|
|
|
#if 1
|
|
|
|
// Saves around 32% RAM after slicing step, 6.7% after G-code export (tested on PrusaSlicer 2.2.0 final).
|
2020-05-21 15:45:00 +00:00
|
|
|
using coord_t = int32_t;
|
2020-03-25 13:34:51 +00:00
|
|
|
#else
|
2020-06-16 12:39:08 +00:00
|
|
|
//FIXME At least FillRectilinear2 and std::boost Voronoi require coord_t to be 32bit.
|
2020-03-25 10:07:26 +00:00
|
|
|
typedef int64_t coord_t;
|
2020-03-25 13:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-21 15:45:00 +00:00
|
|
|
using coordf_t = double;
|
2017-05-18 14:53:19 +00:00
|
|
|
|
2016-09-13 11:30:00 +00:00
|
|
|
//FIXME This epsilon value is used for many non-related purposes:
|
|
|
|
// For a threshold of a squared Euclidean distance,
|
|
|
|
// for a trheshold in a difference of radians,
|
|
|
|
// for a threshold of a cross product of two non-normalized vectors etc.
|
2020-05-21 15:45:00 +00:00
|
|
|
static constexpr double EPSILON = 1e-4;
|
2016-09-13 11:30:00 +00:00
|
|
|
// Scaling factor for a conversion from coord_t to coordf_t: 10e-6
|
|
|
|
// This scaling generates a following fixed point representation with for a 32bit integer:
|
|
|
|
// 0..4294mm with 1nm resolution
|
2016-10-18 14:44:05 +00:00
|
|
|
// int32_t fits an interval of (-2147.48mm, +2147.48mm)
|
2020-03-25 10:07:26 +00:00
|
|
|
// with int64_t we don't have to worry anymore about the size of the int.
|
2020-05-21 15:45:00 +00:00
|
|
|
static constexpr double SCALING_FACTOR = 0.000001;
|
2016-09-13 11:30:00 +00:00
|
|
|
// RESOLUTION, SCALED_RESOLUTION: Used as an error threshold for a Douglas-Peucker polyline simplification algorithm.
|
2020-05-21 15:45:00 +00:00
|
|
|
static constexpr double RESOLUTION = 0.0125;
|
|
|
|
#define SCALED_RESOLUTION (RESOLUTION / SCALING_FACTOR)
|
|
|
|
static constexpr double PI = 3.141592653589793238;
|
2016-09-13 11:30:00 +00:00
|
|
|
// When extruding a closed loop, the loop is interrupted and shortened a bit to reduce the seam.
|
2020-05-21 15:45:00 +00:00
|
|
|
static constexpr double LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER = 0.15;
|
2016-09-13 11:30:00 +00:00
|
|
|
// Maximum perimeter length for the loop to apply the small perimeter speed.
|
2020-05-21 15:58:07 +00:00
|
|
|
#define SMALL_PERIMETER_LENGTH ((6.5 / SCALING_FACTOR) * 2 * PI)
|
2020-05-21 15:45:00 +00:00
|
|
|
static constexpr double INSET_OVERLAP_TOLERANCE = 0.4;
|
2016-09-26 10:42:44 +00:00
|
|
|
// 3mm ring around the top / bottom / bridging areas.
|
|
|
|
//FIXME This is quite a lot.
|
2020-05-21 15:45:00 +00:00
|
|
|
static constexpr double EXTERNAL_INFILL_MARGIN = 3.;
|
2017-05-18 14:53:19 +00:00
|
|
|
//FIXME Better to use an inline function with an explicit return type.
|
|
|
|
//inline coord_t scale_(coordf_t v) { return coord_t(floor(v / SCALING_FACTOR + 0.5f)); }
|
2015-12-19 13:49:29 +00:00
|
|
|
#define scale_(val) ((val) / SCALING_FACTOR)
|
2019-06-14 16:10:16 +00:00
|
|
|
|
2019-06-17 08:05:46 +00:00
|
|
|
#define SCALED_EPSILON scale_(EPSILON)
|
|
|
|
|
|
|
|
#define SLIC3R_DEBUG_OUT_PATH_PREFIX "out/"
|
|
|
|
|
2016-10-20 15:44:46 +00:00
|
|
|
inline std::string debug_out_path(const char *name, ...)
|
|
|
|
{
|
|
|
|
char buffer[2048];
|
|
|
|
va_list args;
|
|
|
|
va_start(args, name);
|
2016-10-20 16:34:33 +00:00
|
|
|
std::vsprintf(buffer, name, args);
|
2016-10-20 15:44:46 +00:00
|
|
|
va_end(args);
|
2016-10-21 08:18:01 +00:00
|
|
|
return std::string(SLIC3R_DEBUG_OUT_PATH_PREFIX) + std::string(buffer);
|
2016-10-20 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 08:39:43 +00:00
|
|
|
#ifndef UNUSED
|
|
|
|
#define UNUSED(x) (void)(x)
|
|
|
|
#endif /* UNUSED */
|
|
|
|
|
2016-09-26 11:56:24 +00:00
|
|
|
// Write slices as SVG images into out directory during the 2D processing of the slices.
|
2016-09-30 13:23:18 +00:00
|
|
|
// #define SLIC3R_DEBUG_SLICE_PROCESSING
|
2016-09-26 11:56:24 +00:00
|
|
|
|
2016-11-26 15:07:36 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2019-08-09 15:01:37 +00:00
|
|
|
extern Semver SEMVER;
|
|
|
|
|
2018-08-21 15:43:05 +00:00
|
|
|
template<typename T, typename Q>
|
|
|
|
inline T unscale(Q v) { return T(v) * T(SCALING_FACTOR); }
|
|
|
|
|
2020-03-21 09:09:33 +00:00
|
|
|
enum Axis {
|
|
|
|
X=0,
|
|
|
|
Y,
|
|
|
|
Z,
|
|
|
|
E,
|
|
|
|
F,
|
|
|
|
NUM_AXES,
|
|
|
|
// For the GCodeReader to mark a parsed axis, which is not in "XYZEF", it was parsed correctly.
|
|
|
|
UNKNOWN_AXIS = NUM_AXES,
|
|
|
|
NUM_AXES_WITH_UNKNOWN,
|
|
|
|
};
|
2016-11-26 15:07:36 +00:00
|
|
|
|
2017-02-15 10:03:19 +00:00
|
|
|
template <typename T>
|
2017-05-15 09:32:59 +00:00
|
|
|
inline void append(std::vector<T>& dest, const std::vector<T>& src)
|
2017-02-15 10:03:19 +00:00
|
|
|
{
|
|
|
|
if (dest.empty())
|
|
|
|
dest = src;
|
|
|
|
else
|
2017-02-15 10:16:39 +00:00
|
|
|
dest.insert(dest.end(), src.begin(), src.end());
|
2017-02-15 10:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-05-15 09:32:59 +00:00
|
|
|
inline void append(std::vector<T>& dest, std::vector<T>&& src)
|
2017-02-15 10:03:19 +00:00
|
|
|
{
|
|
|
|
if (dest.empty())
|
|
|
|
dest = std::move(src);
|
2020-11-20 10:56:40 +00:00
|
|
|
else {
|
2020-11-29 12:58:36 +00:00
|
|
|
dest.reserve(dest.size() + src.size());
|
2017-02-15 10:16:39 +00:00
|
|
|
std::move(std::begin(src), std::end(src), std::back_inserter(dest));
|
2020-11-20 10:56:40 +00:00
|
|
|
}
|
2017-02-15 10:03:19 +00:00
|
|
|
src.clear();
|
|
|
|
src.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:32:40 +00:00
|
|
|
// Append the source in reverse.
|
|
|
|
template <typename T>
|
|
|
|
inline void append_reversed(std::vector<T>& dest, const std::vector<T>& src)
|
|
|
|
{
|
|
|
|
if (dest.empty())
|
|
|
|
dest = src;
|
|
|
|
else
|
|
|
|
dest.insert(dest.end(), src.rbegin(), src.rend());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the source in reverse.
|
|
|
|
template <typename T>
|
|
|
|
inline void append_reversed(std::vector<T>& dest, std::vector<T>&& src)
|
|
|
|
{
|
|
|
|
if (dest.empty())
|
|
|
|
dest = std::move(src);
|
|
|
|
else {
|
|
|
|
dest.reserve(dest.size() + src.size());
|
|
|
|
std::move(std::rbegin(src), std::rend(src), std::back_inserter(dest));
|
|
|
|
}
|
2017-02-15 10:03:19 +00:00
|
|
|
src.clear();
|
|
|
|
src.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
// Casting an std::vector<> from one type to another type without warnings about a loss of accuracy.
|
|
|
|
template<typename T_TO, typename T_FROM>
|
|
|
|
std::vector<T_TO> cast(const std::vector<T_FROM> &src)
|
|
|
|
{
|
|
|
|
std::vector<T_TO> dst;
|
|
|
|
dst.reserve(src.size());
|
|
|
|
for (const T_FROM &a : src)
|
|
|
|
dst.emplace_back((T_TO)a);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:35:09 +00:00
|
|
|
template <typename T>
|
2017-05-15 09:32:59 +00:00
|
|
|
inline void remove_nulls(std::vector<T*> &vec)
|
2017-03-22 14:35:09 +00:00
|
|
|
{
|
|
|
|
vec.erase(
|
|
|
|
std::remove_if(vec.begin(), vec.end(), [](const T *ptr) { return ptr == nullptr; }),
|
|
|
|
vec.end());
|
|
|
|
}
|
|
|
|
|
2017-05-15 09:32:59 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void sort_remove_duplicates(std::vector<T> &vec)
|
|
|
|
{
|
|
|
|
std::sort(vec.begin(), vec.end());
|
|
|
|
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
|
|
|
|
}
|
|
|
|
|
2017-05-05 07:59:56 +00:00
|
|
|
// Older compilers do not provide a std::make_unique template. Provide a simple one.
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline std::unique_ptr<T> make_unique(Args&&... args) {
|
|
|
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
2020-01-10 10:26:52 +00:00
|
|
|
// Variant of std::lower_bound() with compare predicate, but without the key.
|
|
|
|
// This variant is very useful in case that the T type is large or it does not even have a public constructor.
|
|
|
|
template<class ForwardIt, class LowerThanKeyPredicate>
|
2020-11-20 10:56:40 +00:00
|
|
|
ForwardIt lower_bound_by_predicate(ForwardIt first, ForwardIt last, LowerThanKeyPredicate lower_than_key)
|
2020-01-10 10:26:52 +00:00
|
|
|
{
|
|
|
|
ForwardIt it;
|
|
|
|
typename std::iterator_traits<ForwardIt>::difference_type count, step;
|
|
|
|
count = std::distance(first, last);
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
it = first;
|
|
|
|
step = count / 2;
|
|
|
|
std::advance(it, step);
|
2020-11-20 10:56:40 +00:00
|
|
|
if (lower_than_key(*it)) {
|
2020-01-10 10:26:52 +00:00
|
|
|
first = ++it;
|
|
|
|
count -= step + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
count = step;
|
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
|
|
template<class ForwardIt, class T, class Compare=std::less<>>
|
|
|
|
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp={})
|
|
|
|
{
|
|
|
|
// Note: BOTH type T and the type after ForwardIt is dereferenced
|
|
|
|
// must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
|
|
|
|
// This is stricter than lower_bound requirement (see above)
|
|
|
|
|
|
|
|
first = std::lower_bound(first, last, value, comp);
|
|
|
|
return first != last && !comp(value, *first) ? first : last;
|
|
|
|
}
|
|
|
|
|
|
|
|
// from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
|
|
template<class ForwardIt, class LowerThanKeyPredicate, class EqualToKeyPredicate>
|
|
|
|
ForwardIt binary_find_by_predicate(ForwardIt first, ForwardIt last, LowerThanKeyPredicate lower_thank_key, EqualToKeyPredicate equal_to_key)
|
|
|
|
{
|
|
|
|
// Note: BOTH type T and the type after ForwardIt is dereferenced
|
|
|
|
// must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
|
|
|
|
// This is stricter than lower_bound requirement (see above)
|
|
|
|
|
|
|
|
first = lower_bound_by_predicate(first, last, lower_thank_key);
|
|
|
|
return first != last && equal_to_key(*first) ? first : last;
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:34:22 +00:00
|
|
|
template<typename ContainerType, typename ValueType> inline bool contains(const ContainerType &c, const ValueType &v)
|
|
|
|
{ return std::find(c.begin(), c.end(), v) != c.end(); }
|
|
|
|
template<typename T> inline bool contains(const std::initializer_list<T> &il, const T &v)
|
|
|
|
{ return std::find(il.begin(), il.end(), v) != il.end(); }
|
|
|
|
|
|
|
|
template<typename ContainerType, typename ValueType> inline bool one_of(const ValueType &v, const ContainerType &c)
|
|
|
|
{ return contains(c, v); }
|
|
|
|
template<typename T> inline bool one_of(const T& v, const std::initializer_list<T>& il)
|
|
|
|
{ return contains(il, v); }
|
|
|
|
|
2017-07-27 08:39:43 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline T sqr(T x)
|
|
|
|
{
|
|
|
|
return x * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T clamp(const T low, const T high, const T value)
|
|
|
|
{
|
|
|
|
return std::max(low, std::min(high, value));
|
|
|
|
}
|
|
|
|
|
2018-01-08 12:44:10 +00:00
|
|
|
template <typename T, typename Number>
|
|
|
|
static inline T lerp(const T& a, const T& b, Number t)
|
|
|
|
{
|
|
|
|
assert((t >= Number(-EPSILON)) && (t <= Number(1) + Number(EPSILON)));
|
|
|
|
return (Number(1) - t) * a + t * b;
|
|
|
|
}
|
2017-07-27 08:39:43 +00:00
|
|
|
|
2019-01-29 10:26:35 +00:00
|
|
|
template <typename Number>
|
|
|
|
static inline bool is_approx(Number value, Number test_value)
|
|
|
|
{
|
2019-01-29 10:38:51 +00:00
|
|
|
return std::fabs(double(value) - double(test_value)) < double(EPSILON);
|
2019-02-04 15:57:11 +00:00
|
|
|
}
|
2019-01-29 10:26:35 +00:00
|
|
|
|
2020-03-27 08:05:26 +00:00
|
|
|
// A meta-predicate which is true for integers wider than or equal to coord_t
|
|
|
|
template<class I> struct is_scaled_coord
|
|
|
|
{
|
|
|
|
static const constexpr bool value =
|
|
|
|
std::is_integral<I>::value &&
|
|
|
|
std::numeric_limits<I>::digits >=
|
|
|
|
std::numeric_limits<coord_t>::digits;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Meta predicates for floating, 'scaled coord' and generic arithmetic types
|
|
|
|
// Can be used to restrict templates to work for only the specified set of types.
|
|
|
|
// parameter T is the type we want to restrict
|
|
|
|
// parameter O (Optional defaults to T) is the type that the whole expression
|
|
|
|
// will be evaluated to.
|
|
|
|
// e.g. template<class T> FloatingOnly<T, bool> is_nan(T val);
|
|
|
|
// The whole template will be defined only for floating point types and the
|
|
|
|
// return type will be bool.
|
|
|
|
// For more info how to use, see docs for std::enable_if
|
|
|
|
//
|
|
|
|
template<class T, class O = T>
|
|
|
|
using FloatingOnly = std::enable_if_t<std::is_floating_point<T>::value, O>;
|
|
|
|
|
|
|
|
template<class T, class O = T>
|
|
|
|
using ScaledCoordOnly = std::enable_if_t<is_scaled_coord<T>::value, O>;
|
|
|
|
|
|
|
|
template<class T, class O = T>
|
|
|
|
using IntegerOnly = std::enable_if_t<std::is_integral<T>::value, O>;
|
|
|
|
|
|
|
|
template<class T, class O = T>
|
|
|
|
using ArithmeticOnly = std::enable_if_t<std::is_arithmetic<T>::value, O>;
|
|
|
|
|
2020-08-05 13:49:36 +00:00
|
|
|
template<class T, class O = T>
|
|
|
|
using IteratorOnly = std::enable_if_t<
|
|
|
|
!std::is_same_v<typename std::iterator_traits<T>::value_type, void>, O
|
|
|
|
>;
|
|
|
|
|
2020-08-13 07:00:58 +00:00
|
|
|
template<class T, class I, class... Args> // Arbitrary allocator can be used
|
|
|
|
IntegerOnly<I, std::vector<T, Args...>> reserve_vector(I capacity)
|
|
|
|
{
|
|
|
|
std::vector<T, Args...> ret;
|
|
|
|
if (capacity > I(0)) ret.reserve(size_t(capacity));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:50:46 +00:00
|
|
|
// Borrowed from C++20
|
|
|
|
template<class T>
|
|
|
|
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
|
|
|
|
2016-11-26 15:07:36 +00:00
|
|
|
} // namespace Slic3r
|
|
|
|
|
2014-08-03 17:42:29 +00:00
|
|
|
#endif
|