libslic3r
Library for generating gcode from 3d models
libslic3r.h
Go to the documentation of this file.
1 #ifndef _libslic3r_h_
2 #define _libslic3r_h_
3 
4 // this needs to be included early for MSVC (listing it in Build.PL is not enough)
5 #include <ostream>
6 #include <iostream>
7 #define _USE_MATH_DEFINES
8 #include <math.h>
9 #include <queue>
10 #include <sstream>
11 #include <vector>
12 #include <boost/thread.hpp>
13 #include <cstdint>
14 
15 #ifdef _MSC_VER
16 #include <limits>
17 #define NOMINMAX
18 #endif
19 /* Implementation of CONFESS("foo"): */
20 #ifdef _MSC_VER
21  #define CONFESS(...) confess_at(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
22 #else
23  #define CONFESS(...) confess_at(__FILE__, __LINE__, __func__, __VA_ARGS__)
24 #endif
25 void confess_at(const char *file, int line, const char *func, const char *pat, ...);
26 /* End implementation of CONFESS("foo"): */
27 
28 // Which C++ version is supported?
29 // For example, could optimized functions with move semantics be used?
30 #if __cplusplus==201402L
31  #define SLIC3R_CPPVER 14
32  #define STDMOVE(WHAT) std::move(WHAT)
33 #elif __cplusplus==201103L
34  #define SLIC3R_CPPVER 11
35  #define STDMOVE(WHAT) std::move(WHAT)
36 #else
37  #define SLIC3R_CPPVER 0
38  #define STDMOVE(WHAT) (WHAT)
39 #endif
40 
41 // dummy macro to mark strings for translation for gettext/poedit
42 #define __TRANS(s) s
43 namespace Slic3r {
44 
45 constexpr auto SLIC3R_VERSION = "1.3.1-dev";
46 
47 
48 #ifndef SLIC3R_BUILD_COMMIT
49 #define SLIC3R_BUILD_COMMIT (Unknown revision)
50 #endif
51 #define VER1_(x) #x
52 #define VER_(x) VER1_(x)
53 #define BUILD_COMMIT VER_(SLIC3R_BUILD_COMMIT)
54 
55 const auto SLIC3R_GIT_STR = std::string(BUILD_COMMIT);
56 const auto SLIC3R_GIT = SLIC3R_GIT_STR.c_str();
57 
58 #ifdef _WIN32
59 typedef int64_t coord_t;
60 typedef double coordf_t;
61 #else
62 typedef long coord_t;
63 typedef double coordf_t;
64 #endif
65 
66 // Scaling factor for a conversion from coord_t to coordf_t: 10e-6
67 // This scaling generates a following fixed point representation with for a 32bit integer:
68 // 0..4294mm with 1nm resolution
69 constexpr auto SCALING_FACTOR = 0.000001;
70 inline constexpr coord_t scale_(const coordf_t &val) { return val / SCALING_FACTOR; }
71 inline constexpr coordf_t unscale(const coord_t &val) { return val * SCALING_FACTOR; }
72 
73 //FIXME This epsilon value is used for many non-related purposes:
74 // For a threshold of a squared Euclidean distance,
75 // for a trheshold in a difference of radians,
76 // for a threshold of a cross product of two non-normalized vectors etc.
77 constexpr auto EPSILON = 1e-4;
78 constexpr auto SCALED_EPSILON = scale_(EPSILON);
79 // RESOLUTION, SCALED_RESOLUTION: Used as an error threshold for a Douglas-Peucker polyline simplification algorithm.
80 constexpr auto RESOLUTION = 0.0125;
81 constexpr auto SCALED_RESOLUTION = scale_(RESOLUTION);
82 constexpr auto PI = 3.141592653589793238;
83 // When extruding a closed loop, the loop is interrupted and shortened a bit to reduce the seam.
85 // Maximum perimeter length for the loop to apply the small perimeter speed.
86 constexpr coord_t SMALL_PERIMETER_LENGTH = scale_(6.5) * 2 * PI;
87 constexpr coordf_t INSET_OVERLAP_TOLERANCE = 0.4;
88 constexpr coordf_t EXTERNAL_INFILL_MARGIN = 3;
89 constexpr coord_t SCALED_EXTERNAL_INFILL_MARGIN = scale_(EXTERNAL_INFILL_MARGIN);
90 
91 constexpr float CLIPPER_OFFSET_SCALE = 100000.0;
92 
93 enum Axis { X=0, Y, Z };
94 
95 template <class T>
96 inline void append_to(std::vector<T> &dst, const std::vector<T> &src)
97 {
98  dst.insert(dst.end(), src.begin(), src.end());
99 }
100 
101 template <class T> void
102 _parallelize_do(std::queue<T>* queue, boost::mutex* queue_mutex, boost::function<void(T)> func)
103 {
104  //std::cout << "THREAD STARTED: " << boost::this_thread::get_id() << std::endl;
105  while (true) {
106  T i;
107  {
108  boost::lock_guard<boost::mutex> l(*queue_mutex);
109  if (queue->empty()) return;
110  i = queue->front();
111  queue->pop();
112  }
113  //std::cout << " Thread " << boost::this_thread::get_id() << " processing item " << i << std::endl;
114  func(i);
115  boost::this_thread::interruption_point();
116  }
117 }
118 
119 template <class T> void
120 parallelize(std::queue<T> queue, boost::function<void(T)> func,
121  int threads_count = boost::thread::hardware_concurrency())
122 {
123  if (threads_count == 0) threads_count = 2;
124  boost::mutex queue_mutex;
125  boost::thread_group workers;
126  for (int i = 0; i < std::min(threads_count, (int)queue.size()); i++)
127  workers.add_thread(new boost::thread(&_parallelize_do<T>, &queue, &queue_mutex, func));
128  workers.join_all();
129 }
130 
131 template <class T> void
132 parallelize(T start, T end, boost::function<void(T)> func,
133  int threads_count = boost::thread::hardware_concurrency())
134 {
135  std::queue<T> queue;
136  for (T i = start; i <= end; ++i) queue.push(i);
137  parallelize(queue, func, threads_count);
138 }
139 
140 } // namespace Slic3r
141 
142 using namespace Slic3r;
143 
144 #endif
constexpr auto SLIC3R_VERSION
Definition: libslic3r.h:45
void confess_at(const char *file, int line, const char *func, const char *pat,...)
Definition: utils.cpp:12
Definition: libslic3r.h:93
constexpr auto EPSILON
Definition: libslic3r.h:77
long coord_t
Definition: libslic3r.h:62
const auto SLIC3R_GIT_STR
Definition: libslic3r.h:55
void append_to(std::vector< T > &dst, const std::vector< T > &src)
Definition: libslic3r.h:96
constexpr auto SCALING_FACTOR
Definition: libslic3r.h:69
constexpr coord_t SMALL_PERIMETER_LENGTH
Definition: libslic3r.h:86
constexpr coordf_t INSET_OVERLAP_TOLERANCE
Definition: libslic3r.h:87
constexpr coordf_t unscale(const coord_t &val)
Definition: libslic3r.h:71
constexpr auto SCALED_RESOLUTION
Definition: libslic3r.h:81
constexpr coord_t scale_(const coordf_t &val)
Definition: libslic3r.h:70
Definition: libslic3r.h:93
constexpr auto RESOLUTION
Definition: libslic3r.h:80
double coordf_t
Definition: libslic3r.h:63
Definition: BoundingBox.cpp:4
constexpr coordf_t EXTERNAL_INFILL_MARGIN
Definition: libslic3r.h:88
constexpr auto PI
Definition: libslic3r.h:82
constexpr auto SCALED_EPSILON
Definition: libslic3r.h:78
Definition: libslic3r.h:93
constexpr coord_t SCALED_EXTERNAL_INFILL_MARGIN
Definition: libslic3r.h:89
constexpr float CLIPPER_OFFSET_SCALE
Definition: libslic3r.h:91
#define BUILD_COMMIT
Definition: libslic3r.h:53
const auto SLIC3R_GIT
Definition: libslic3r.h:56
Axis
Definition: libslic3r.h:93
constexpr auto LOOP_CLIPPING_LENGTH_OVER_NOZZLE_DIAMETER
Definition: libslic3r.h:84
void _parallelize_do(std::queue< T > *queue, boost::mutex *queue_mutex, boost::function< void(T)> func)
Definition: libslic3r.h:102
void parallelize(std::queue< T > queue, boost::function< void(T)> func, int threads_count=boost::thread::hardware_concurrency())
Definition: libslic3r.h:120