From 3772b3d0b5279689ffe0dc4bacc887fb70df0463 Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Tue, 22 Mar 2022 17:49:45 +0100 Subject: [PATCH] Remove unnecessary ctors of data types, no more need to use unique ptr to data --- src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp | 31 +++-- src/slic3r/GUI/Jobs/EmbossJob.cpp | 64 +++++----- src/slic3r/GUI/Jobs/EmbossJob.hpp | 160 ++++++++---------------- 3 files changed, 102 insertions(+), 153 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp index 0fd61c883d..720a9a2d2b 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoEmboss.cpp @@ -153,10 +153,12 @@ void GLGizmoEmboss::create_volume(ModelVolumeType volume_type, const Vec2d& mous const Camera &camera = plater->get_camera(); const Pointfs &bed_shape = plater->build_volume().bed_shape(); auto &worker = plater->get_ui_job_worker(); - auto data = std::make_unique( - m_font_manager.get_font().font_file_with_cache, - create_configuration(), create_volume_name(), - screen_coor, camera, bed_shape); + EmbossDataCreateObject data{m_font_manager.get_font().font_file_with_cache, + create_configuration(), + create_volume_name(), + screen_coor, + camera, + bed_shape}; queue_job(worker, std::make_unique(std::move(data))); } @@ -656,11 +658,16 @@ bool GLGizmoEmboss::start_volume_creation(ModelVolumeType volume_type, // create volume auto &worker = plater->get_ui_job_worker(); - auto data = std::make_unique( - m_font_manager.get_font().font_file_with_cache, - create_configuration(), create_volume_name(), volume_type, - screen_coor, object_idx, camera, *hit, hit_object_trmat, - hit_instance_trmat); + EmbossDataCreateVolume data{m_font_manager.get_font().font_file_with_cache, + create_configuration(), + create_volume_name(), + volume_type, + screen_coor, + object_idx_signed, + camera, + *hit, + hit_object_trmat, + hit_instance_trmat}; queue_job(worker, std::make_unique(std::move(data))); return true; } @@ -741,9 +748,9 @@ bool GLGizmoEmboss::process() // IMPROVE: Cancel only EmbossUpdateJob no others worker.cancel(); - auto data = std::make_unique( - font, create_configuration(), create_volume_name(), - m_volume->id(), m_update_job_cancel); + EmbossDataUpdate data{font, create_configuration(), create_volume_name(), + m_volume->id(), m_update_job_cancel}; + queue_job(worker, std::make_unique(std::move(data))); // notification is removed befor object is changed by job diff --git a/src/slic3r/GUI/Jobs/EmbossJob.cpp b/src/slic3r/GUI/Jobs/EmbossJob.cpp index 4b49d0e6b2..36d920b2a9 100644 --- a/src/slic3r/GUI/Jobs/EmbossJob.cpp +++ b/src/slic3r/GUI/Jobs/EmbossJob.cpp @@ -44,17 +44,17 @@ static TriangleMesh create_default_mesh(); ///////////////// /// Update Volume -EmbossUpdateJob::EmbossUpdateJob(std::unique_ptr input) +EmbossUpdateJob::EmbossUpdateJob(EmbossDataUpdate&& input) : m_input(std::move(input)) {} void EmbossUpdateJob::process(Ctl &ctl) { // check if exist valid font - if (!m_input->font_file.has_value()) return; + if (!m_input.font_file.has_value()) return; - const TextConfiguration &cfg = m_input->text_configuration; - m_result = priv::create_mesh(cfg.text.c_str(), m_input->font_file, + const TextConfiguration &cfg = m_input.text_configuration; + m_result = priv::create_mesh(cfg.text.c_str(), m_input.font_file, cfg.font_item.prop, ctl); if (m_result.its.empty()) return; if (ctl.was_canceled()) return; @@ -66,7 +66,7 @@ void EmbossUpdateJob::process(Ctl &ctl) void EmbossUpdateJob::finalize(bool canceled, std::exception_ptr &) { - if (canceled || *m_input->cancel) return; + if (canceled || *m_input.cancel) return; // for sure that some object is created from shape if (m_result.its.indices.empty()) return; @@ -80,14 +80,14 @@ void EmbossUpdateJob::finalize(bool canceled, std::exception_ptr &) // Check emboss gizmo is still open if (manager.get_current_type() != GLGizmosManager::Emboss) return; - std::string snap_name = GUI::format(_L("Text: %1%"), m_input->text_configuration.text); + std::string snap_name = GUI::format(_L("Text: %1%"), m_input.text_configuration.text); Plater::TakeSnapshot snapshot(plater, snap_name, UndoRedo::SnapshotType::GizmoAction); ModelVolume *volume = nullptr; Model &model = plater->model(); for (auto obj : model.objects) for (auto vol : obj->volumes) - if (vol->id() == m_input->volume_id) { + if (vol->id() == m_input.volume_id) { volume = vol; break; } @@ -101,8 +101,8 @@ void EmbossUpdateJob::finalize(bool canceled, std::exception_ptr &) volume->set_new_unique_id(); volume->calculate_convex_hull(); volume->get_object()->invalidate_bounding_box(); - volume->name = m_input->volume_name; - volume->text_configuration = m_input->text_configuration; + volume->name = m_input.volume_name; + volume->text_configuration = m_input.text_configuration; // update volume in right panel( volume / object name) const Selection &selection = canvas->get_selection(); @@ -124,29 +124,28 @@ void EmbossUpdateJob::finalize(bool canceled, std::exception_ptr &) ///////////////// /// Create Volume -EmbossCreateVolumeJob::EmbossCreateVolumeJob( - std::unique_ptr input) +EmbossCreateVolumeJob::EmbossCreateVolumeJob(EmbossDataCreateVolume &&input) : m_input(std::move(input)) {} void EmbossCreateVolumeJob::process(Ctl &ctl) { // It is neccessary to create some shape // Emboss text window is opened by creation new emboss text object - const char *text = m_input->text_configuration.text.c_str(); - FontProp &prop = m_input->text_configuration.font_item.prop; + const char *text = m_input.text_configuration.text.c_str(); + FontProp &prop = m_input.text_configuration.font_item.prop; - m_result = priv::create_mesh(text, m_input->font_file, prop, ctl); + m_result = priv::create_mesh(text, m_input.font_file, prop, ctl); if (m_result.its.empty()) m_result = priv::create_default_mesh(); if (ctl.was_canceled()) return; // Create new volume inside of object - const FontProp &font_prop = m_input->text_configuration.font_item.prop; + const FontProp &font_prop = m_input.text_configuration.font_item.prop; Transform3d surface_trmat = Emboss::create_transformation_onto_surface( - m_input->hit.position, m_input->hit.normal); + m_input.hit.position, m_input.hit.normal); Emboss::apply_transformation(font_prop, surface_trmat); - m_transformation = m_input->hit_instance_tr.inverse() * - m_input->hit_object_tr * surface_trmat; + m_transformation = m_input.hit_instance_tr.inverse() * + m_input.hit_object_tr * surface_trmat; } void EmbossCreateVolumeJob::finalize(bool canceled, std::exception_ptr &) { @@ -159,14 +158,14 @@ void EmbossCreateVolumeJob::finalize(bool canceled, std::exception_ptr &) { Model &model = plater->model(); // create volume in object - size_t object_idx = m_input->object_idx; + size_t object_idx = m_input.object_idx; assert(model.objects.size() > object_idx); if (model.objects.size() <= object_idx) return; Plater::TakeSnapshot snapshot(plater, _L("Add Emboss text Volume")); ModelObject *obj = model.objects[object_idx]; - ModelVolumeType type = m_input->volume_type; + ModelVolumeType type = m_input.volume_type; ModelVolume *volume = obj->add_volume(std::move(m_result), type); // set a default extruder value, since user can't add it manually @@ -175,8 +174,8 @@ void EmbossCreateVolumeJob::finalize(bool canceled, std::exception_ptr &) { // do not allow model reload from disk volume->source.is_from_builtin_objects = true; - volume->name = m_input->volume_name; - volume->text_configuration = std::move(m_input->text_configuration); + volume->name = m_input.volume_name; + volume->text_configuration = std::move(m_input.text_configuration); volume->set_transformation(m_transformation); // update volume name in object list @@ -209,8 +208,7 @@ void EmbossCreateVolumeJob::finalize(bool canceled, std::exception_ptr &) { ///////////////// /// Create Object -EmbossCreateObjectJob::EmbossCreateObjectJob( - std::unique_ptr input) +EmbossCreateObjectJob::EmbossCreateObjectJob(EmbossDataCreateObject &&input) : m_input(std::move(input)) {} @@ -218,10 +216,10 @@ void EmbossCreateObjectJob::process(Ctl &ctl) { // It is neccessary to create some shape // Emboss text window is opened by creation new emboss text object - const char *text = m_input->text_configuration.text.c_str(); - FontProp &prop = m_input->text_configuration.font_item.prop; + const char *text = m_input.text_configuration.text.c_str(); + FontProp &prop = m_input.text_configuration.font_item.prop; - m_result = priv::create_mesh(text, m_input->font_file, prop, ctl); + m_result = priv::create_mesh(text, m_input.font_file, prop, ctl); if (m_result.its.empty()) m_result = priv::create_default_mesh(); if (ctl.was_canceled()) return; @@ -230,19 +228,19 @@ void EmbossCreateObjectJob::process(Ctl &ctl) // calculate X,Y offset position for lay on platter in place of // mouse click Vec2d bed_coor = CameraUtils::get_z0_position( - m_input->camera, m_input->screen_coor); + m_input.camera, m_input.screen_coor); // check point is on build plate: Points bed_shape_; - bed_shape_.reserve(m_input->bed_shape.size()); - for (const Vec2d &p : m_input->bed_shape) + bed_shape_.reserve(m_input.bed_shape.size()); + for (const Vec2d &p : m_input.bed_shape) bed_shape_.emplace_back(p.cast()); Polygon bed(bed_shape_); if (!bed.contains(bed_coor.cast())) // mouse pose is out of build plate so create object in center of plate bed_coor = bed.centroid().cast(); - double z = m_input->text_configuration.font_item.prop.emboss / 2; + double z = m_input.text_configuration.font_item.prop.emboss / 2; Vec3d offset(bed_coor.x(), bed_coor.y(), z); offset -= m_result.center(); Transform3d::TranslationType tt(offset.x(), offset.y(), offset.z()); @@ -262,8 +260,8 @@ void EmbossCreateObjectJob::finalize(bool canceled, std::exception_ptr &) // Create new object and change selection bool center = false; - obj_list->load_mesh_object(std::move(m_result), m_input->volume_name, - center, &m_input->text_configuration, + obj_list->load_mesh_object(std::move(m_result), m_input.volume_name, + center, &m_input.text_configuration, &m_transformation); // When add new object selection is empty. diff --git a/src/slic3r/GUI/Jobs/EmbossJob.hpp b/src/slic3r/GUI/Jobs/EmbossJob.hpp index 3e5a80d929..1e7d1ad2f8 100644 --- a/src/slic3r/GUI/Jobs/EmbossJob.hpp +++ b/src/slic3r/GUI/Jobs/EmbossJob.hpp @@ -14,63 +14,6 @@ class TriangleMesh; namespace Slic3r::GUI { -struct EmbossDataUpdate; -struct EmbossDataCreateVolume; -struct EmbossDataCreateObject; - -/// -/// Update text shape in existing text volume -/// -class EmbossUpdateJob : public Job -{ - std::unique_ptr m_input; - TriangleMesh m_result; - -public: - EmbossUpdateJob(std::unique_ptr input); - void process(Ctl &ctl) override; - - /// - /// Update volume - change object_id - /// - /// Was process canceled. - /// NOTE: Be carefull it doesn't care about - /// time between finished process and started finalize part. - /// unused - void finalize(bool canceled, std::exception_ptr &) override; -}; - - -/// -/// Create new TextVolume on the surface of ModelObject -/// -class EmbossCreateVolumeJob : public Job -{ - std::unique_ptr m_input; - TriangleMesh m_result; - Transform3d m_transformation; - -public: - EmbossCreateVolumeJob(std::unique_ptr input); - void process(Ctl &ctl) override; - void finalize(bool canceled, std::exception_ptr &) override; -}; - -/// -/// Create new TextObject on the platter -/// -class EmbossCreateObjectJob : public Job -{ - std::unique_ptr m_input; - TriangleMesh m_result; - Transform3d m_transformation; - -public: - EmbossCreateObjectJob(std::unique_ptr input); - void process(Ctl &ctl) override; - void finalize(bool canceled, std::exception_ptr &) override; -}; - /// /// Base data holder for embossing /// @@ -82,13 +25,6 @@ struct EmbossDataBase TextConfiguration text_configuration; // new volume name created from text std::string volume_name; - EmbossDataBase(Emboss::FontFileWithCache font_file, - TextConfiguration text_configuration, - std::string volume_name) - : font_file(font_file) - , text_configuration(text_configuration) - , volume_name(volume_name) - {} }; /// @@ -102,19 +38,6 @@ struct EmbossDataUpdate : public EmbossDataBase // flag that job is canceled // for time after process. std::shared_ptr cancel; - - // unique identifier of volume to change - // Change of volume change id, last change could disapear - // ObjectID volume_id; - EmbossDataUpdate(Emboss::FontFileWithCache font_file, - TextConfiguration text_configuration, - std::string volume_name, - ObjectID volume_id, - std::shared_ptr cancel) - : EmbossDataBase(font_file, text_configuration, volume_name) - , volume_id(volume_id) - , cancel(std::move(cancel)) - {} }; /// @@ -139,26 +62,6 @@ struct EmbossDataCreateVolume : public EmbossDataBase RaycastManager::SurfacePoint hit; Transform3d hit_object_tr; Transform3d hit_instance_tr; - - EmbossDataCreateVolume(Emboss::FontFileWithCache font_file, - const TextConfiguration &text_configuration, - const std::string &volume_name, - ModelVolumeType volume_type, - Vec2d screen_coor, - int object_idx, - const Camera &camera, - const RaycastManager::SurfacePoint &hit, - const Transform3d &hit_object_tr, - const Transform3d &hit_instance_tr) - : EmbossDataBase(font_file, text_configuration, volume_name) - , volume_type(volume_type) - , screen_coor(screen_coor) - , object_idx(object_idx) - , camera(camera) - , hit(hit) - , hit_object_tr(hit_object_tr) - , hit_instance_tr(hit_instance_tr) - {} }; /// @@ -176,18 +79,59 @@ struct EmbossDataCreateObject : public EmbossDataBase // shape of bed in case of create volume on bed std::vector bed_shape; +}; - EmbossDataCreateObject(Emboss::FontFileWithCache font_file, - const TextConfiguration &text_configuration, - const std::string &volume_name, - Vec2d screen_coor, - const Camera &camera, - const std::vector &bed_shape) - : EmbossDataBase(font_file, text_configuration, volume_name) - , screen_coor(screen_coor) - , camera(camera) - , bed_shape(bed_shape) - {} +/// +/// Update text shape in existing text volume +/// +class EmbossUpdateJob : public Job +{ + EmbossDataUpdate m_input; + TriangleMesh m_result; + +public: + EmbossUpdateJob(EmbossDataUpdate&& input); + void process(Ctl &ctl) override; + + /// + /// Update volume - change object_id + /// + /// Was process canceled. + /// NOTE: Be carefull it doesn't care about + /// time between finished process and started finalize part. + /// unused + void finalize(bool canceled, std::exception_ptr &) override; +}; + + +/// +/// Create new TextVolume on the surface of ModelObject +/// +class EmbossCreateVolumeJob : public Job +{ + EmbossDataCreateVolume m_input; + TriangleMesh m_result; + Transform3d m_transformation; + +public: + EmbossCreateVolumeJob(EmbossDataCreateVolume&& input); + void process(Ctl &ctl) override; + void finalize(bool canceled, std::exception_ptr &) override; +}; + +/// +/// Create new TextObject on the platter +/// +class EmbossCreateObjectJob : public Job +{ + EmbossDataCreateObject m_input; + TriangleMesh m_result; + Transform3d m_transformation; + +public: + EmbossCreateObjectJob(EmbossDataCreateObject&& input); + void process(Ctl &ctl) override; + void finalize(bool canceled, std::exception_ptr &) override; }; } // namespace Slic3r::GUI