242{
244
245 auto plane_mesh = Eigen::Hyperplane<double, 3>(
m_plane.get_normal(), -
m_plane.distance(Vec3d::Zero())).transform(
m_trafo.get_matrix().inverse());
246 const Vec3d up = plane_mesh.normal();
247 const float height_mesh = -plane_mesh.offset();
248
249
250 MeshSlicingParams slicing_params;
251 slicing_params.trafo.rotate(Eigen::Quaternion<double, Eigen::DontAlign>::FromTwoVectors(up, Vec3d::UnitZ()));
252
254
258
261 expolys =
diff_ex(expolys, neg_expolys);
262 }
263 } else {
265 }
266
267
268
269 Eigen::Quaterniond q;
270 q.setFromTwoVectors(Vec3d::UnitZ(), up);
272 tr.rotate(q);
273 tr =
m_trafo.get_matrix() * tr;
274
276
278 {
279
280
281
283 const Vec3d normal_new = (tr.matrix().block<3,3>(0,0).transpose() * normal_old).normalized();
284
285
286
287 Vec3d pt = Vec3d::Zero();
289 if (std::abs(normal_old.z()) > 0.5)
290 pt.z() = - plane_offset / normal_old.z();
291 else if (std::abs(normal_old.y()) > 0.5)
292 pt.y() = - plane_offset / normal_old.y();
293 else
294 pt.x() = - plane_offset / normal_old.x();
295 pt = tr.inverse() * pt;
296 const double offset = -(normal_new.dot(pt));
297
298 if (std::abs(normal_old.dot(
m_plane.get_normal().normalized())) > 0.99) {
299
300 if (normal_old.dot(
m_plane.get_normal().normalized()) < 0.0 &&
offset < height_mesh)
301 expolys.clear();
302 } else {
303
304
305
306 const double len = std::hypot(normal_new.x(), normal_new.y());
307 if (len == 0.)
308 return;
309 const double a = normal_new.x() / len;
310 const double b = normal_new.y() / len;
311 const double e = (normal_new.z() * height_mesh +
offset) / len;
312
313
314 double angle = (b != 0.0) ? std::atan(-a / b) : ((a < 0.0) ? -0.5 *
M_PI : 0.5 *
M_PI);
315 if (b > 0)
317
318
319
320
321
322 const coord_t size = (double(std::numeric_limits<coord_t>::max()/2) -
scale_(std::max(std::abs(e * a), std::abs(e * b)))) / 4;
324 ep.front().rotate(
angle);
326 expolys =
diff_ex(expolys, ep);
327 }
328 }
329
330 tr.pretranslate(0.001 *
m_plane.get_normal().normalized());
332 tr2.pretranslate(0.002 *
m_plane.get_normal().normalized());
333
334
335 std::vector<Vec2f> triangles2d;
336
337 for (const ExPolygon& exp : expolys) {
338 triangles2d.clear();
339
342
345 GLModel::Geometry init_data;
347 init_data.reserve_vertices(triangles2d.size());
348 init_data.reserve_indices(triangles2d.size());
349
350
351 for (auto it = triangles2d.cbegin(); it != triangles2d.cend(); it = it + 3) {
355 const size_t idx = it - triangles2d.cbegin();
356 init_data.add_triangle((unsigned int)idx, (unsigned int)idx + 1, (unsigned int)idx + 2);
357 }
358
359 if (!init_data.is_empty())
360 isl.model.init_from(std::move(init_data));
361 }
362
364 triangles2d.clear();
365
366
367
368
369
371 t.translation() = Vec3d::Zero();
372 double scale_x = (t * Vec3d::UnitX()).norm();
373 double scale_y = (t * Vec3d::UnitY()).norm();
374
375
376 double extra_scale = 1.;
377 coord_t limit =
coord_t(std::min(
double(std::numeric_limits<coord_t>::max()) / (2. * std::max(1., scale_x)),
double(std::numeric_limits<coord_t>::max()) / (2. * std::max(1., scale_y))));
379 for (const Point& pt : exp.contour)
380 max_coord = std::max(max_coord, std::max(std::abs(pt.x()), std::abs(pt.y())));
382 extra_scale = 0.9 * double(limit) / max_coord;
383
384 ExPolygon exp_copy = exp;
385 if (extra_scale != 1.)
386 exp_copy.scale(extra_scale);
387 exp_copy.scale(scale_x, scale_y);
388
391
392 for (ExPolygon& e : expolys_exp) {
393 e.scale(1./scale_x, 1./scale_y);
394 if (extra_scale != 1.)
395 e.scale(1./extra_scale);
396 }
397
398
400 GLModel::Geometry init_data = GLModel::Geometry();
402 init_data.reserve_vertices(triangles2d.size());
403 init_data.reserve_indices(triangles2d.size());
404
405
406 for (auto it = triangles2d.cbegin(); it != triangles2d.cend(); it = it + 3) {
410 const size_t idx = it - triangles2d.cbegin();
411 init_data.add_triangle((unsigned short)idx, (unsigned short)idx + 1, (unsigned short)idx + 2);
412 }
413
414 if (!init_data.is_empty())
415 isl.model_expanded.init_from(std::move(init_data));
416 }
417
418 isl.expoly = std::move(exp);
420
421 Point centroid_scaled = isl.expoly.contour.centroid();
423 isl.hash = isl.expoly.contour.size() + size_t(std::abs(100.*centroid_world.x())) + size_t(std::abs(100.*centroid_world.y())) + size_t(std::abs(100.*centroid_world.z()));
424 }
425
426
427
429 return a.hash < b.hash;
430 });
431}
#define M_PI
Definition ExtrusionSimulator.cpp:31
static ClippingPlane ClipsNothing()
Definition MeshUtils.hpp:73
std::vector< csg::CSGPart > m_csgmesh
Definition MeshUtils.hpp:144
double m_contour_width
Definition MeshUtils.hpp:163
ClippingPlane m_plane
Definition MeshUtils.hpp:146
ClippingPlane m_limiting_plane
Definition MeshUtils.hpp:147
AnyPtr< const indexed_triangle_set > m_mesh
Definition MeshUtils.hpp:142
bool m_fill_cut
Definition MeshUtils.hpp:162
AnyPtr< const indexed_triangle_set > m_negative_mesh
Definition MeshUtils.hpp:143
Geometry::Transformation m_trafo
Definition MeshUtils.hpp:141
Definition MeshUtils.hpp:157
#define scale_(val)
Definition libslic3r.h:106
int64_t coord_t
Definition libslic3r.h:48
std::vector< ExPolygons > slice_csgmesh_ex(const Range< ItCSG > &csgrange, const std::vector< float > &slicegrid, const MeshSlicingParamsEx ¶ms, const std::function< void()> &throw_on_cancel=[] {})
Definition SliceCSGMesh.hpp:57
Slic3r::ExPolygons diff_ex(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset)
Definition ClipperUtils.cpp:831
std::vector< ExPolygon > ExPolygons
Definition ExPolygon.hpp:20
Eigen::Transform< double, 3, Eigen::Affine, Eigen::DontAlign > Transform3d
Definition Point.hpp:96
auto range(Cont &&cont)
Definition libslic3r.h:405
BoundingBox get_extents(const ExPolygonsWithIds &e)
Definition Emboss.cpp:1374
Eigen::Matrix< float, 3, 1, Eigen::DontAlign > Vec3f
Definition Point.hpp:65
std::vector< Polygon, PointsAllocator< Polygon > > Polygons
Definition Polygon.hpp:22
Slic3r::Polygons offset(const Slic3r::Polygon &polygon, const float delta, JoinType joinType, double miterLimit)
Definition ClipperUtils.cpp:582
double angle(const Eigen::MatrixBase< Derived > &v1, const Eigen::MatrixBase< Derived2 > &v2)
Definition Point.hpp:138
std::vector< Vec2f > triangulate_expolygons_2f(const ExPolygons &polys, bool flip)
Definition Tesselate.cpp:249
Slic3r::ExPolygons offset_ex(const Slic3r::Polygons &polygons, const float delta, JoinType joinType, double miterLimit)
Definition ClipperUtils.cpp:605
std::vector< Vec2f > triangulate_expolygon_2f(const ExPolygon &poly, bool flip)
Definition Tesselate.cpp:238
Slic3r::ExPolygons union_ex(const Slic3r::Polygons &subject, PolyFillType fill_type)
Definition ClipperUtils.cpp:1011
std::vector< Polygons > slice_mesh(const indexed_triangle_set &mesh, const std::vector< float > &zs, const MeshSlicingParams ¶ms, std::function< void()> throw_on_cancel)
Definition TriangleMeshSlicer.cpp:1997
auto cast(const BoundingBoxBase< Tin > &b)
Definition BoundingBox.hpp:299
Slic3r::Polygon Polygon
Definition Emboss.cpp:101
@ up
Definition MarchingSquares.hpp:192
@ P3N3
Definition GLModel.hpp:46
@ Triangles
Definition GLModel.hpp:32