From cb5e4e29b30a14b432cc4427b0cfdac228867eaa Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Mon, 30 May 2022 22:43:49 +0200 Subject: [PATCH] Fix translation issue after cut surface --- src/slic3r/GUI/Jobs/EmbossJob.cpp | 73 ++++++++++--------------------- 1 file changed, 22 insertions(+), 51 deletions(-) diff --git a/src/slic3r/GUI/Jobs/EmbossJob.cpp b/src/slic3r/GUI/Jobs/EmbossJob.cpp index 76e3f964c..73f4758b5 100644 --- a/src/slic3r/GUI/Jobs/EmbossJob.cpp +++ b/src/slic3r/GUI/Jobs/EmbossJob.cpp @@ -61,15 +61,13 @@ static double get_shape_scale(const FontProp &fp, const Emboss::FontFile &ff); /// Create projection for cut surface from mesh /// /// Volume transformation in object -/// Configuration of embossig -/// Font file for size --> unit per em -/// Bounding box 2d of shape to center result -/// volume Bounding box 3d of model volume for -/// projection ranges Orthogonal cut_projection +/// Convert shape to milimeters +/// Bounding box 2d of shape to center result +/// Bounding box 3d of model volume for projection ranges +/// Orthogonal cut_projection static std::unique_ptr create_projection_for_cut( Transform3d tr, - const TextConfiguration &tc, - const Emboss::FontFile &ff, + double shape_scale, const BoundingBox &shape_bb, const std::pair &z_range); @@ -77,15 +75,12 @@ static std::unique_ptr create_projection_for_cut( /// Create tranformation for emboss Cutted surface /// /// True .. raise, False .. engrave -/// Text configuration +/// Depth of embossing /// Text voliume transformation inside object /// Cutted surface from model /// Projection static std::unique_ptr create_emboss_projection( - bool is_outside, - const TextConfiguration &tc, - Transform3d tr, - SurfaceCut &cut); + bool is_outside, float emboss, Transform3d tr, SurfaceCut &cut); } @@ -312,11 +307,15 @@ void UseSurfaceJob::process(Ctl &ctl) { Transform3d mesh_tr_inv = m_input.mesh_tr.inverse(); Transform3d cut_projection_tr = mesh_tr_inv * m_input.text_tr; - BoundingBoxf3 mesh_bb_tr = m_input.mesh_bb.transformed(cut_projection_tr.inverse()); + Transform3d emboss_tr = cut_projection_tr.inverse(); + BoundingBoxf3 mesh_bb_tr = m_input.mesh_bb.transformed(emboss_tr); std::pair z_range{mesh_bb_tr.min.z(), mesh_bb_tr.max.z()}; const Emboss::FontFile &ff = *m_input.font_file.font_file; - auto cut_projection = priv::create_projection_for_cut(cut_projection_tr, tc, ff, bb, z_range); + double shape_scale = priv::get_shape_scale(fp, ff); + auto cut_projection = priv::create_projection_for_cut(cut_projection_tr, + shape_scale, bb, + z_range); if (cut_projection == nullptr) return; // Use CGAL to cut surface from triangle mesh @@ -325,26 +324,19 @@ void UseSurfaceJob::process(Ctl &ctl) { if (was_canceled()) return; // !! Projection needs to transform cut - auto projection = priv::create_emboss_projection(m_input.is_outside, tc, cut_projection_tr, cut); + auto projection = priv::create_emboss_projection(m_input.is_outside, fp.emboss, emboss_tr, cut); if (projection == nullptr) return; indexed_triangle_set new_its = cut2model(cut, *projection); if (was_canceled()) return; //its_write_obj(new_its, "C:/data/temp/projected.obj"); // only debug - m_result = TriangleMesh(std::move(new_its)); - Vec3d shift = -m_result.bounding_box().center(); - // do not center in emboss direction - shift.z() = 0; - m_result.translate(shift.cast()); } void UseSurfaceJob::finalize(bool canceled, std::exception_ptr &) { if (canceled || m_input.cancel->load()) return; priv::update_volume(std::move(m_result), m_input); - // TODO: use fix matrix to compensate uncentered position - } //////////////////////////// @@ -466,8 +458,7 @@ static double priv::get_shape_scale(const FontProp &fp, const Emboss::FontFile & std::unique_ptr priv::create_projection_for_cut( Transform3d tr, - const TextConfiguration &tc, - const Emboss::FontFile &ff, + double shape_scale, const BoundingBox &shape_bb, const std::pair &z_range) { @@ -491,43 +482,23 @@ std::unique_ptr priv::create_projection_for_cut( // Projection is in direction from far plane tr.translate(Vec3d(0., 0., min_z)); - tr.scale(get_shape_scale(tc.font_item.prop, ff)); + tr.scale(shape_scale); // Text alignemnt to center 2D Vec2d move = -(shape_bb.max + shape_bb.min).cast() / 2.; + //Vec2d move = -shape_bb.center().cast(); // not precisse tr.translate(Vec3d(move.x(), move.y(), 0.)); return std::make_unique(tr, project_direction); } std::unique_ptr priv::create_emboss_projection( - bool is_outside, - const TextConfiguration &tc, - Transform3d tr, - SurfaceCut &cut) + bool is_outside, float emboss, Transform3d tr, SurfaceCut &cut) { // Offset of clossed side to model const float surface_offset = 1e-3f; // [in mm] - - const FontProp &fp = tc.font_item.prop; - float front_move, back_move; - if (is_outside) { - front_move = fp.emboss; - back_move = -surface_offset; - } else { - front_move = surface_offset; - back_move = -fp.emboss; - } - Matrix3d rot_i = tr.linear().inverse(); - its_transform(cut, rot_i); - - BoundingBoxf3 bb = Slic3r::bounding_box(cut); - float z_move = -bb.max.z(); - float x_center = (bb.max.x() + bb.min.x()) / 2; - float y_center = (bb.max.y() + bb.min.y()) / 2; - // move to front distance - Vec3f move(x_center, y_center, front_move + z_move); - - its_translate(cut, move); - + float + front_move = (is_outside) ? emboss : surface_offset, + back_move = -(is_outside) ? surface_offset : emboss; + its_transform(cut, tr.pretranslate(Vec3d(0., 0., front_move))); Vec3f from_front_to_back(0.f, 0.f, back_move - front_move); return std::make_unique(from_front_to_back); }