1 #ifndef _libslic3r_h_
2 #define _libslic3r_h_
3
4 #include "libslic3r_version.h"
5 #define GCODEVIEWER_APP_NAME "PrusaSlicer G-code Viewer"
6 #define GCODEVIEWER_APP_KEY "PrusaSlicerGcodeViewer"
7 #define GCODEVIEWER_BUILD_ID std::string("PrusaSlicer G-code Viewer-") + std::string(SLIC3R_VERSION) + std::string("-UNKNOWN")
8
9 // this needs to be included early for MSVC (listing it in Build.PL is not enough)
10 #include <memory>
11 #include <array>
12 #include <algorithm>
13 #include <ostream>
14 #include <iostream>
15 #include <math.h>
16 #include <queue>
17 #include <sstream>
18 #include <cstdio>
19 #include <stdint.h>
20 #include <stdarg.h>
21 #include <vector>
22 #include <cassert>
23 #include <cmath>
24 #include <type_traits>
25
26 #include "Technologies.hpp"
27 #include "Semver.hpp"
28
29 #if 1
30 // Saves around 32% RAM after slicing step, 6.7% after G-code export (tested on PrusaSlicer 2.2.0 final).
31 using coord_t = int32_t;
32 #else
33 //FIXME At least FillRectilinear2 and std::boost Voronoi require coord_t to be 32bit.
34 typedef int64_t coord_t;
35 #endif
36
37 using coordf_t = double;
38
39 //FIXME This epsilon value is used for many non-related purposes:
40 // For a threshold of a squared Euclidean distance,
41 // for a trheshold in a difference of radians,
42 // for a threshold of a cross product of two non-normalized vectors etc.
43 static constexpr double EPSILON = 1e-4;
44 // Scaling factor for a conversion from coord_t to coordf_t: 10e-6
45 // This scaling generates a following fixed point representation with for a 32bit integer:
46 // 0..4294mm with 1nm resolution
47 // int32_t fits an interval of (-2147.48mm, +2147.48mm)
48 // with int64_t we don't have to worry anymore about the size of the int.
49 static constexpr double SCALING_FACTOR = 0.000001;
50 // RESOLUTION, SCALED_RESOLUTION: Used as an error threshold for a Douglas-Peucker polyline simplification algorithm.
51 static constexpr double RESOLUTION = 0.0125;
52 #define SCALED_RESOLUTION (RESOLUTION / SCALING_FACTOR)
53 static constexpr double PI = 3.141592653589793238;
54 // When extruding a closed loop, the loop is interrupted and shortened a bit to reduce the seam.
55 static constexpr double LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER = 0.15;
56 // Maximum perimeter length for the loop to apply the small perimeter speed.
57 #define SMALL_PERIMETER_LENGTH ((6.5 / SCALING_FACTOR) * 2 * PI)
58 static constexpr double INSET_OVERLAP_TOLERANCE = 0.4;
59 // 3mm ring around the top / bottom / bridging areas.
60 //FIXME This is quite a lot.
61 static constexpr double EXTERNAL_INFILL_MARGIN = 3.;
62 //FIXME Better to use an inline function with an explicit return type.
63 //inline coord_t scale_(coordf_t v) { return coord_t(floor(v / SCALING_FACTOR + 0.5f)); }
64 #define scale_(val) ((val) / SCALING_FACTOR)
65
66 #define SCALED_EPSILON scale_(EPSILON)
67
68 #define SLIC3R_DEBUG_OUT_PATH_PREFIX "out/"
69
debug_out_path(const char * name,...)70 inline std::string debug_out_path(const char *name, ...)
71 {
72 char buffer[2048];
73 va_list args;
74 va_start(args, name);
75 std::vsprintf(buffer, name, args);
76 va_end(args);
77 return std::string(SLIC3R_DEBUG_OUT_PATH_PREFIX) + std::string(buffer);
78 }
79
80 #ifndef UNUSED
81 #define UNUSED(x) (void)(x)
82 #endif /* UNUSED */
83
84 // Write slices as SVG images into out directory during the 2D processing of the slices.
85 // #define SLIC3R_DEBUG_SLICE_PROCESSING
86
87 namespace Slic3r {
88
89 extern Semver SEMVER;
90
91 template<typename T, typename Q>
unscale(Q v)92 inline T unscale(Q v) { return T(v) * T(SCALING_FACTOR); }
93
94 enum Axis {
95 X=0,
96 Y,
97 Z,
98 E,
99 F,
100 NUM_AXES,
101 // For the GCodeReader to mark a parsed axis, which is not in "XYZEF", it was parsed correctly.
102 UNKNOWN_AXIS = NUM_AXES,
103 NUM_AXES_WITH_UNKNOWN,
104 };
105
106 template <typename T>
append(std::vector<T> & dest,const std::vector<T> & src)107 inline void append(std::vector<T>& dest, const std::vector<T>& src)
108 {
109 if (dest.empty())
110 dest = src;
111 else
112 dest.insert(dest.end(), src.begin(), src.end());
113 }
114
115 template <typename T>
append(std::vector<T> & dest,std::vector<T> && src)116 inline void append(std::vector<T>& dest, std::vector<T>&& src)
117 {
118 if (dest.empty())
119 dest = std::move(src);
120 else {
121 dest.reserve(dest.size() + src.size());
122 std::move(std::begin(src), std::end(src), std::back_inserter(dest));
123 }
124 src.clear();
125 src.shrink_to_fit();
126 }
127
128 // Append the source in reverse.
129 template <typename T>
append_reversed(std::vector<T> & dest,const std::vector<T> & src)130 inline void append_reversed(std::vector<T>& dest, const std::vector<T>& src)
131 {
132 if (dest.empty())
133 dest = src;
134 else
135 dest.insert(dest.end(), src.rbegin(), src.rend());
136 }
137
138 // Append the source in reverse.
139 template <typename T>
append_reversed(std::vector<T> & dest,std::vector<T> && src)140 inline void append_reversed(std::vector<T>& dest, std::vector<T>&& src)
141 {
142 if (dest.empty())
143 dest = std::move(src);
144 else {
145 dest.reserve(dest.size() + src.size());
146 std::move(std::rbegin(src), std::rend(src), std::back_inserter(dest));
147 }
148 src.clear();
149 src.shrink_to_fit();
150 }
151
152 // Casting an std::vector<> from one type to another type without warnings about a loss of accuracy.
153 template<typename T_TO, typename T_FROM>
cast(const std::vector<T_FROM> & src)154 std::vector<T_TO> cast(const std::vector<T_FROM> &src)
155 {
156 std::vector<T_TO> dst;
157 dst.reserve(src.size());
158 for (const T_FROM &a : src)
159 dst.emplace_back((T_TO)a);
160 return dst;
161 }
162
163 template <typename T>
remove_nulls(std::vector<T * > & vec)164 inline void remove_nulls(std::vector<T*> &vec)
165 {
166 vec.erase(
167 std::remove_if(vec.begin(), vec.end(), [](const T *ptr) { return ptr == nullptr; }),
168 vec.end());
169 }
170
171 template <typename T>
sort_remove_duplicates(std::vector<T> & vec)172 inline void sort_remove_duplicates(std::vector<T> &vec)
173 {
174 std::sort(vec.begin(), vec.end());
175 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
176 }
177
178 // Older compilers do not provide a std::make_unique template. Provide a simple one.
179 template<typename T, typename... Args>
make_unique(Args &&...args)180 inline std::unique_ptr<T> make_unique(Args&&... args) {
181 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
182 }
183
184 // Variant of std::lower_bound() with compare predicate, but without the key.
185 // This variant is very useful in case that the T type is large or it does not even have a public constructor.
186 template<class ForwardIt, class LowerThanKeyPredicate>
lower_bound_by_predicate(ForwardIt first,ForwardIt last,LowerThanKeyPredicate lower_than_key)187 ForwardIt lower_bound_by_predicate(ForwardIt first, ForwardIt last, LowerThanKeyPredicate lower_than_key)
188 {
189 ForwardIt it;
190 typename std::iterator_traits<ForwardIt>::difference_type count, step;
191 count = std::distance(first, last);
192
193 while (count > 0) {
194 it = first;
195 step = count / 2;
196 std::advance(it, step);
197 if (lower_than_key(*it)) {
198 first = ++it;
199 count -= step + 1;
200 }
201 else
202 count = step;
203 }
204 return first;
205 }
206
207 // from https://en.cppreference.com/w/cpp/algorithm/lower_bound
208 template<class ForwardIt, class T, class Compare=std::less<>>
209 ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp={})
210 {
211 // Note: BOTH type T and the type after ForwardIt is dereferenced
212 // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
213 // This is stricter than lower_bound requirement (see above)
214
215 first = std::lower_bound(first, last, value, comp);
216 return first != last && !comp(value, *first) ? first : last;
217 }
218
219 // from https://en.cppreference.com/w/cpp/algorithm/lower_bound
220 template<class ForwardIt, class LowerThanKeyPredicate, class EqualToKeyPredicate>
binary_find_by_predicate(ForwardIt first,ForwardIt last,LowerThanKeyPredicate lower_thank_key,EqualToKeyPredicate equal_to_key)221 ForwardIt binary_find_by_predicate(ForwardIt first, ForwardIt last, LowerThanKeyPredicate lower_thank_key, EqualToKeyPredicate equal_to_key)
222 {
223 // Note: BOTH type T and the type after ForwardIt is dereferenced
224 // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
225 // This is stricter than lower_bound requirement (see above)
226
227 first = lower_bound_by_predicate(first, last, lower_thank_key);
228 return first != last && equal_to_key(*first) ? first : last;
229 }
230
231 template<typename T>
sqr(T x)232 static inline T sqr(T x)
233 {
234 return x * x;
235 }
236
237 template <typename T>
clamp(const T low,const T high,const T value)238 static inline T clamp(const T low, const T high, const T value)
239 {
240 return std::max(low, std::min(high, value));
241 }
242
243 template <typename T, typename Number>
lerp(const T & a,const T & b,Number t)244 static inline T lerp(const T& a, const T& b, Number t)
245 {
246 assert((t >= Number(-EPSILON)) && (t <= Number(1) + Number(EPSILON)));
247 return (Number(1) - t) * a + t * b;
248 }
249
250 template <typename Number>
is_approx(Number value,Number test_value)251 static inline bool is_approx(Number value, Number test_value)
252 {
253 return std::fabs(double(value) - double(test_value)) < double(EPSILON);
254 }
255
256 // A meta-predicate which is true for integers wider than or equal to coord_t
257 template<class I> struct is_scaled_coord
258 {
259 static const constexpr bool value =
260 std::is_integral<I>::value &&
261 std::numeric_limits<I>::digits >=
262 std::numeric_limits<coord_t>::digits;
263 };
264
265 // Meta predicates for floating, 'scaled coord' and generic arithmetic types
266 // Can be used to restrict templates to work for only the specified set of types.
267 // parameter T is the type we want to restrict
268 // parameter O (Optional defaults to T) is the type that the whole expression
269 // will be evaluated to.
270 // e.g. template<class T> FloatingOnly<T, bool> is_nan(T val);
271 // The whole template will be defined only for floating point types and the
272 // return type will be bool.
273 // For more info how to use, see docs for std::enable_if
274 //
275 template<class T, class O = T>
276 using FloatingOnly = std::enable_if_t<std::is_floating_point<T>::value, O>;
277
278 template<class T, class O = T>
279 using ScaledCoordOnly = std::enable_if_t<is_scaled_coord<T>::value, O>;
280
281 template<class T, class O = T>
282 using IntegerOnly = std::enable_if_t<std::is_integral<T>::value, O>;
283
284 template<class T, class O = T>
285 using ArithmeticOnly = std::enable_if_t<std::is_arithmetic<T>::value, O>;
286
287 template<class T, class O = T>
288 using IteratorOnly = std::enable_if_t<
289 !std::is_same_v<typename std::iterator_traits<T>::value_type, void>, O
290 >;
291
292 template<class T, class I, class... Args> // Arbitrary allocator can be used
reserve_vector(I capacity)293 IntegerOnly<I, std::vector<T, Args...>> reserve_vector(I capacity)
294 {
295 std::vector<T, Args...> ret;
296 if (capacity > I(0)) ret.reserve(size_t(capacity));
297
298 return ret;
299 }
300
301 } // namespace Slic3r
302
303 #endif
304