OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::SketchConstraints Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/libslic3r/CAD/SketchConstraints.hpp>

Collaboration diagram for Slic3r::SketchConstraints:

Classes

struct  Con

Public Member Functions

int add_point (double x, double y)
void set_point (int id, double x, double y)
Vec2d get_point (int id) const
int point_count () const
void fix_point (int id)
void coincident (int a, int b)
void horizontal (int a, int b)
void vertical (int a, int b)
void distance (int a, int b, double d)
void lock_x (int id, double x)
void lock_y (int id, double y)
void equal_length (int a, int b, int c, int d)
void parallel (int a, int b, int c, int d)
void perpendicular (int a, int b, int c, int d)
void midpoint (int m, int a, int b)
void symmetric (int a, int b, int c, int d)
void angle (int a, int b, int c, int d, double radians)
void point_line_distance (int p, int a, int b, double dist)
bool solve (int max_iter=200, double tol=1e-10)
double residual_norm () const

Private Types

enum  ConType : int {
  FIX_POINT = 0 , COINCIDENT , HORIZONTAL , VERTICAL ,
  DISTANCE , LOCK_X , LOCK_Y , EQUAL_LENGTH ,
  PARALLEL , PERPENDICULAR , MIDPOINT , SYMMETRIC ,
  ANGLE , PT_LINE_DIST
}

Private Member Functions

Eigen::VectorXd residuals (const std::vector< double > &v) const
Eigen::MatrixXd jacobian (const std::vector< double > &v) const

Private Attributes

std::vector< double > m_vars
std::vector< Con > m_cons

Detailed Description


Class Documentation

◆ Slic3r::SketchConstraints::Con

struct Slic3r::SketchConstraints::Con
Class Members
int a
int b
int c
int d
double k0
double k1
int type

Member Enumeration Documentation

◆ ConType

Enumerator
FIX_POINT 
COINCIDENT 
HORIZONTAL 
VERTICAL 
DISTANCE 
LOCK_X 
LOCK_Y 
EQUAL_LENGTH 
PARALLEL 
PERPENDICULAR 
MIDPOINT 
SYMMETRIC 
ANGLE 
PT_LINE_DIST 
39 : int {
40 FIX_POINT = 0,
45 LOCK_X,
46 LOCK_Y,
52 ANGLE,
54 };
@ ANGLE
Definition SketchConstraints.hpp:52
@ VERTICAL
Definition SketchConstraints.hpp:43
@ LOCK_Y
Definition SketchConstraints.hpp:46
@ MIDPOINT
Definition SketchConstraints.hpp:50
@ DISTANCE
Definition SketchConstraints.hpp:44
@ SYMMETRIC
Definition SketchConstraints.hpp:51
@ PERPENDICULAR
Definition SketchConstraints.hpp:49
@ FIX_POINT
Definition SketchConstraints.hpp:40
@ EQUAL_LENGTH
Definition SketchConstraints.hpp:47
@ PARALLEL
Definition SketchConstraints.hpp:48
@ HORIZONTAL
Definition SketchConstraints.hpp:42
@ PT_LINE_DIST
Definition SketchConstraints.hpp:53
@ COINCIDENT
Definition SketchConstraints.hpp:41
@ LOCK_X
Definition SketchConstraints.hpp:45

Member Function Documentation

◆ add_point()

int Slic3r::SketchConstraints::add_point ( double x,
double y )
13{
14 m_vars.push_back(x);
15 m_vars.push_back(y);
16 return static_cast<int>(m_vars.size() / 2) - 1;
17}
std::vector< double > m_vars
Definition SketchConstraints.hpp:37

References m_vars.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ angle()

void Slic3r::SketchConstraints::angle ( int a,
int b,
int c,
int d,
double radians )
151{
152 Con con;
153 con.type = ANGLE;
154 con.a = a; con.b = b; con.c = c; con.d = d;
155 con.k0 = radians; con.k1 = 0;
156 m_cons.push_back(con);
157}
int type
Definition SketchConstraints.hpp:57
std::vector< Con > m_cons
Definition SketchConstraints.hpp:61
Definition SketchConstraints.hpp:56

References Slic3r::SketchConstraints::Con::a, ANGLE, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ coincident()

void Slic3r::SketchConstraints::coincident ( int a,
int b )
50{
51 Con c;
52 c.type = COINCIDENT;
53 c.a = a; c.b = b; c.c = c.d = 0;
54 c.k0 = c.k1 = 0;
55 m_cons.push_back(c);
56}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, COINCIDENT, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ distance()

void Slic3r::SketchConstraints::distance ( int a,
int b,
double d )
77{
78 Con c;
79 c.type = DISTANCE;
80 c.a = a; c.b = b; c.c = c.d = 0;
81 c.k0 = d; c.k1 = 0;
82 m_cons.push_back(c);
83}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, DISTANCE, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ equal_length()

void Slic3r::SketchConstraints::equal_length ( int a,
int b,
int c,
int d )
106{
107 Con con;
108 con.type = EQUAL_LENGTH;
109 con.a = a; con.b = b; con.c = c; con.d = d;
110 con.k0 = con.k1 = 0;
111 m_cons.push_back(con);
112}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, EQUAL_LENGTH, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ fix_point()

void Slic3r::SketchConstraints::fix_point ( int id)
38{
39 size_t idx = 2 * id;
40 Con c;
41 c.type = FIX_POINT;
42 c.a = id;
43 c.b = c.c = c.d = 0;
44 c.k0 = m_vars[idx];
45 c.k1 = m_vars[idx + 1];
46 m_cons.push_back(c);
47}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, FIX_POINT, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, m_vars, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ get_point()

Vec2d Slic3r::SketchConstraints::get_point ( int id) const
27{
28 size_t idx = 2 * id;
29 return Vec2d(m_vars[idx], m_vars[idx + 1]);
30}
Eigen::Matrix< double, 2, 1, Eigen::DontAlign > Vec2d
Definition Point.hpp:67

References m_vars.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ horizontal()

void Slic3r::SketchConstraints::horizontal ( int a,
int b )
59{
60 Con c;
61 c.type = HORIZONTAL;
62 c.a = a; c.b = b; c.c = c.d = 0;
63 c.k0 = c.k1 = 0;
64 m_cons.push_back(c);
65}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, HORIZONTAL, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ jacobian()

Eigen::MatrixXd Slic3r::SketchConstraints::jacobian ( const std::vector< double > & v) const
private
250{
251 int m = static_cast<int>(residuals(v).size());
252 int n = static_cast<int>(v.size());
253 Eigen::MatrixXd J(m, n);
254 const double eps = 1e-7;
255
256 std::vector<double> vp = v;
257 std::vector<double> vm = v;
258
259 for (int j = 0; j < n; ++j) {
260 vp[j] = v[j] + eps;
261 vm[j] = v[j] - eps;
262 Eigen::VectorXd rp = residuals(vp);
263 Eigen::VectorXd rm = residuals(vm);
264 vp[j] = v[j];
265 vm[j] = v[j];
266 J.col(j) = (rp - rm) / (2.0 * eps);
267 }
268
269 return J;
270}
Eigen::VectorXd residuals(const std::vector< double > &v) const
Definition SketchConstraints.cpp:168
@ J
Definition libslic3r.h:148

References Slic3r::J, and residuals().

Referenced by solve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock_x()

void Slic3r::SketchConstraints::lock_x ( int id,
double x )
86{
87 Con c;
88 c.type = LOCK_X;
89 c.a = id;
90 c.b = c.c = c.d = 0;
91 c.k0 = x; c.k1 = 0;
92 m_cons.push_back(c);
93}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, LOCK_X, m_cons, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ lock_y()

void Slic3r::SketchConstraints::lock_y ( int id,
double y )
96{
97 Con c;
98 c.type = LOCK_Y;
99 c.a = id;
100 c.b = c.c = c.d = 0;
101 c.k0 = y; c.k1 = 0;
102 m_cons.push_back(c);
103}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, LOCK_Y, m_cons, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ midpoint()

void Slic3r::SketchConstraints::midpoint ( int m,
int a,
int b )
133{
134 Con con;
135 con.type = MIDPOINT;
136 con.a = m; con.b = a; con.c = b; con.d = -1;
137 con.k0 = con.k1 = 0;
138 m_cons.push_back(con);
139}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, MIDPOINT, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ parallel()

void Slic3r::SketchConstraints::parallel ( int a,
int b,
int c,
int d )
115{
116 Con con;
117 con.type = PARALLEL;
118 con.a = a; con.b = b; con.c = c; con.d = d;
119 con.k0 = con.k1 = 0;
120 m_cons.push_back(con);
121}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, PARALLEL, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ perpendicular()

void Slic3r::SketchConstraints::perpendicular ( int a,
int b,
int c,
int d )
124{
125 Con con;
126 con.type = PERPENDICULAR;
127 con.a = a; con.b = b; con.c = c; con.d = d;
128 con.k0 = con.k1 = 0;
129 m_cons.push_back(con);
130}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, PERPENDICULAR, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ point_count()

int Slic3r::SketchConstraints::point_count ( ) const
33{
34 return static_cast<int>(m_vars.size() / 2);
35}

References m_vars.

◆ point_line_distance()

void Slic3r::SketchConstraints::point_line_distance ( int p,
int a,
int b,
double dist )
160{
161 Con con;
162 con.type = PT_LINE_DIST;
163 con.a = p; con.b = a; con.c = b; con.d = -1;
164 con.k0 = dist; con.k1 = 0;
165 m_cons.push_back(con);
166}
T dist(const boost::polygon::point_data< T > &p1, const boost::polygon::point_data< T > &p2)
Definition Geometry.cpp:298

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::Geometry::dist(), Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, PT_LINE_DIST, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ residual_norm()

double Slic3r::SketchConstraints::residual_norm ( ) const
308{
309 return residuals(m_vars).norm();
310}

References m_vars, and residuals().

Here is the call graph for this function:

◆ residuals()

Eigen::VectorXd Slic3r::SketchConstraints::residuals ( const std::vector< double > & v) const
private
169{
170 auto X = [&](int i) { return v[2 * i]; };
171 auto Y = [&](int i) { return v[2 * i + 1]; };
172
173 std::vector<double> res;
174 for (const auto& c : m_cons) {
175 switch (c.type) {
176 case FIX_POINT:
177 res.push_back(X(c.a) - c.k0);
178 res.push_back(Y(c.a) - c.k1);
179 break;
180 case COINCIDENT:
181 res.push_back(X(c.a) - X(c.b));
182 res.push_back(Y(c.a) - Y(c.b));
183 break;
184 case HORIZONTAL:
185 res.push_back(Y(c.a) - Y(c.b));
186 break;
187 case VERTICAL:
188 res.push_back(X(c.a) - X(c.b));
189 break;
190 case DISTANCE:
191 res.push_back(std::hypot(X(c.a) - X(c.b), Y(c.a) - Y(c.b)) - c.k0);
192 break;
193 case LOCK_X:
194 res.push_back(X(c.a) - c.k0);
195 break;
196 case LOCK_Y:
197 res.push_back(Y(c.a) - c.k0);
198 break;
199 case EQUAL_LENGTH:
200 res.push_back(std::hypot(X(c.a) - X(c.b), Y(c.a) - Y(c.b)) -
201 std::hypot(X(c.c) - X(c.d), Y(c.c) - Y(c.d)));
202 break;
203 case PARALLEL:
204 res.push_back((X(c.b) - X(c.a)) * (Y(c.d) - Y(c.c)) -
205 (Y(c.b) - Y(c.a)) * (X(c.d) - X(c.c)));
206 break;
207 case PERPENDICULAR:
208 res.push_back((X(c.b) - X(c.a)) * (X(c.d) - X(c.c)) +
209 (Y(c.b) - Y(c.a)) * (Y(c.d) - Y(c.c)));
210 break;
211 case MIDPOINT:
212 res.push_back(X(c.a) - 0.5 * (X(c.b) + X(c.c)));
213 res.push_back(Y(c.a) - 0.5 * (Y(c.b) + Y(c.c)));
214 break;
215 case SYMMETRIC: {
216 const double abx = X(c.b) - X(c.a), aby = Y(c.b) - Y(c.a);
217 const double cdx = X(c.d) - X(c.c), cdy = Y(c.d) - Y(c.c);
218 res.push_back(abx * cdx + aby * cdy);
219 const double mx = 0.5 * (X(c.a) + X(c.b));
220 const double my = 0.5 * (Y(c.a) + Y(c.b));
221 res.push_back((mx - X(c.c)) * cdy - (my - Y(c.c)) * cdx);
222 break;
223 }
224 case ANGLE: {
225 const double ux = X(c.b) - X(c.a), uy = Y(c.b) - Y(c.a);
226 const double wx = X(c.d) - X(c.c), wy = Y(c.d) - Y(c.c);
227 const double cross = ux * wy - uy * wx;
228 const double dot = ux * wx + uy * wy;
229 res.push_back(std::atan2(cross, dot) - c.k0);
230 break;
231 }
232 case PT_LINE_DIST: {
233 const double bx = X(c.b), by = Y(c.b);
234 const double cx = X(c.c), cy = Y(c.c);
235 const double L = std::hypot(cx - bx, cy - by);
236 const double num = (X(c.a) - bx) * (cy - by) - (Y(c.a) - by) * (cx - bx);
237 res.push_back((L > 1e-12 ? std::abs(num) / L : 0.0) - c.k0);
238 break;
239 }
240 }
241 }
242
243 Eigen::VectorXd r(static_cast<Eigen::Index>(res.size()));
244 for (size_t i = 0; i < res.size(); ++i)
245 r(static_cast<Eigen::Index>(i)) = res[i];
246 return r;
247}
#define L(s)
Definition ExtrusionEntity.cpp:25
T cross(const boost::geometry::model::d2::point_xy< T > &v1, const boost::geometry::model::d2::point_xy< T > &v2)
Definition ExtrusionSimulator.cpp:168
T dot(const boost::geometry::model::d2::point_xy< T > &v1, const boost::geometry::model::d2::point_xy< T > &v2)
Definition ExtrusionSimulator.cpp:154
@ Y
Definition libslic3r.h:142
@ X
Definition libslic3r.h:141

References ANGLE, COINCIDENT, Slic3r::cross(), DISTANCE, Slic3r::dot(), EQUAL_LENGTH, FIX_POINT, HORIZONTAL, L, LOCK_X, LOCK_Y, m_cons, MIDPOINT, PARALLEL, PERPENDICULAR, PT_LINE_DIST, SYMMETRIC, VERTICAL, Slic3r::X, and Slic3r::Y.

Referenced by jacobian(), residual_norm(), and solve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_point()

void Slic3r::SketchConstraints::set_point ( int id,
double x,
double y )
20{
21 size_t idx = 2 * id;
22 m_vars[idx] = x;
23 m_vars[idx + 1] = y;
24}

References m_vars.

◆ solve()

bool Slic3r::SketchConstraints::solve ( int max_iter = 200,
double tol = 1e-10 )
273{
274 if (m_cons.empty()) return true;
275 double lambda = 1e-3;
276 Eigen::VectorXd r = residuals(m_vars);
277 for (int it = 0; it < max_iter; ++it) {
278 double rn = r.norm();
279 if (rn < tol) return true;
280 Eigen::MatrixXd J = jacobian(m_vars);
281 Eigen::MatrixXd A = J.transpose() * J;
282 Eigen::VectorXd g = J.transpose() * r;
283 bool stepped = false;
284 for (int t = 0; t < 12; ++t) {
285 Eigen::MatrixXd Ad = A;
286 for (int i = 0; i < Ad.rows(); ++i)
287 Ad(i, i) += lambda * (1.0 + Ad(i, i));
288 Eigen::VectorXd dx = Ad.ldlt().solve(-g);
289 std::vector<double> cand = m_vars;
290 for (size_t i = 0; i < cand.size(); ++i)
291 cand[i] += dx[static_cast<Eigen::Index>(i)];
292 Eigen::VectorXd rc = residuals(cand);
293 if (rc.norm() < rn) {
294 m_vars = cand;
295 r = rc;
296 lambda = std::max(lambda * 0.4, 1e-12);
297 stepped = true;
298 break;
299 }
300 lambda *= 3.0;
301 }
302 if (!stepped) break;
303 }
304 return r.norm() < tol * 100;
305}
Eigen::MatrixXd jacobian(const std::vector< double > &v) const
Definition SketchConstraints.cpp:249

References Slic3r::J, jacobian(), m_cons, m_vars, and residuals().

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ symmetric()

void Slic3r::SketchConstraints::symmetric ( int a,
int b,
int c,
int d )
142{
143 Con con;
144 con.type = SYMMETRIC;
145 con.a = a; con.b = b; con.c = c; con.d = d;
146 con.k0 = con.k1 = 0;
147 m_cons.push_back(con);
148}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, SYMMETRIC, and Slic3r::SketchConstraints::Con::type.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

◆ vertical()

void Slic3r::SketchConstraints::vertical ( int a,
int b )
68{
69 Con c;
70 c.type = VERTICAL;
71 c.a = a; c.b = b; c.c = c.d = 0;
72 c.k0 = c.k1 = 0;
73 m_cons.push_back(c);
74}

References Slic3r::SketchConstraints::Con::a, Slic3r::SketchConstraints::Con::b, Slic3r::SketchConstraints::Con::c, Slic3r::SketchConstraints::Con::d, Slic3r::SketchConstraints::Con::k0, Slic3r::SketchConstraints::Con::k1, m_cons, Slic3r::SketchConstraints::Con::type, and VERTICAL.

Referenced by Slic3r::CadDocument::solve_sketch_feature().

Here is the caller graph for this function:

Member Data Documentation

◆ m_cons

◆ m_vars

std::vector<double> Slic3r::SketchConstraints::m_vars
private

The documentation for this class was generated from the following files: