OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::TextureDisplacementPrepareJob Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Jobs/TextureDisplacementPrepareJob.hpp>

Inheritance diagram for Slic3r::GUI::TextureDisplacementPrepareJob:
Collaboration diagram for Slic3r::GUI::TextureDisplacementPrepareJob:

Public Types

enum  JobPrepareState { PREPARE_STATE_DEFAULT = 0 , PREPARE_STATE_MENU = 1 }

Public Member Functions

 TextureDisplacementPrepareJob (TextureDisplacementPrepareInput &&input, std::function< void(TextureDisplacementPrepareOutcome)> on_finished)
void process (Ctl &ctl) override
void finalize (bool canceled, std::exception_ptr &eptr) override

Private Attributes

TextureDisplacementPrepareInput m_input
std::vector< TextureDisplacementPrepareResult > m_results
std::function< void(TextureDisplacementPrepareOutcome)> m_on_finished

Detailed Description

Member Enumeration Documentation

◆ JobPrepareState

Enumerator
PREPARE_STATE_DEFAULT 
PREPARE_STATE_MENU 
22 {
25 };
@ PREPARE_STATE_MENU
Definition Job.hpp:24
@ PREPARE_STATE_DEFAULT
Definition Job.hpp:23

Constructor & Destructor Documentation

◆ TextureDisplacementPrepareJob()

Slic3r::GUI::TextureDisplacementPrepareJob::TextureDisplacementPrepareJob ( TextureDisplacementPrepareInput && input,
std::function< void(TextureDisplacementPrepareOutcome)> on_finished )
32 : m_input(std::move(input)), m_on_finished(std::move(on_finished))
33{
34}
std::function< void(TextureDisplacementPrepareOutcome)> m_on_finished
Definition TextureDisplacementPrepareJob.hpp:78
TextureDisplacementPrepareInput m_input
Definition TextureDisplacementPrepareJob.hpp:76

References m_input, and m_on_finished.

Member Function Documentation

◆ finalize()

void Slic3r::GUI::TextureDisplacementPrepareJob::finalize ( bool canceled,
std::exception_ptr & eptr )
overridevirtual

Reimplemented from Slic3r::GUI::Job.

76{
78 struct OnExit
79 {
80 std::function<void(TextureDisplacementPrepareOutcome)> fn;
82 ~OnExit() { if (fn) fn(*outcome); }
83 } on_exit{m_on_finished, &outcome};
84
85 if (canceled || eptr)
86 return;
87 // One part losing its paint stops the whole prepare, exactly as it stops a single-part one: the
88 // promise is that a prepare which cannot carry the paint changes nothing, and committing the other
89 // parts would break it halfway. The message that follows tells the user to remesh by hand instead.
90 for (const TextureDisplacementPrepareResult &result : m_results)
91 if (result.paint_lost) {
93 return;
94 }
95
96 Plater *plater = wxGetApp().plater();
97
98 // Resolved before the snapshot is opened, so a run whose parts have all gone away leaves no empty
99 // undo step behind. The lookup doubles as the staleness check: anything that replaces a volume's
100 // mesh also gives it a new id (set_new_unique_id()), so a prepare computed from a mesh that has
101 // since been replaced - by an undo, another bake, or a boolean - simply fails to find its volume.
102 std::vector<std::pair<size_t, ModelVolume *>> to_commit;
103 for (size_t i = 0; i < m_results.size(); ++i) {
104 if (m_results[i].mesh.indices.empty())
105 continue;
106 if (ModelVolume *volume = get_model_volume(m_input.volumes[i].volume_id, plater->model().objects);
107 volume != nullptr && volume->get_object() != nullptr)
108 to_commit.emplace_back(i, volume);
109 }
110 if (to_commit.empty()) {
111 // Nothing needed doing anywhere, which is not an error - every mesh already met the criteria.
113 return;
114 }
115
116 ModelObject *object = to_commit.front().second->get_object();
117 {
118 Plater::TakeSnapshot snapshot(plater, m_input.snapshot_name, UndoRedo::SnapshotType::GizmoAction);
119
120 for (const auto &[i, volume] : to_commit) {
121 // The four standard paint channels ride across on ModelVolume's own spatial remap. The eight
122 // texture-displacement masks do not go through it - prepare_mesh() has already carried them,
123 // exactly, from the source triangles the refinement records - so they are put back after
124 // restore_painting(), which resets every extra facet before remapping the channels it knows.
125 std::optional<TriangleSelector::SavedPainting> saved_painting = volume->save_painting();
126 volume->set_mesh(TriangleMesh(std::move(m_results[i].mesh)));
127 volume->set_new_unique_id();
128 volume->calculate_convex_hull();
129 volume->restore_painting(saved_painting);
130 for (int slot = 0; slot < int(TEXTURE_DISPLACEMENT_MAX_LAYERS); ++slot)
131 volume->texture_displacement_facet(slot).set_data(std::move(m_results[i].masks[size_t(slot)]));
132 // The model's own UV channel, resampled onto the new mesh by the run. Also put back after
133 // restore_painting(), which resets it along with the extra facets for the same reason: it is
134 // indexed by triangle and the triangles have changed.
135 volume->texture_uv = std::move(m_results[i].uv);
136 }
137
138 if (ObjectList *obj_list = wxGetApp().obj_list()) {
139 const ModelObjectPtrs &objs = plater->model().objects;
140 auto it = std::find(objs.begin(), objs.end(), object);
141 if (it != objs.end())
142 obj_list->update_info_items(size_t(it - objs.begin()));
143 }
144 plater->changed_object(*object);
145 }
147}
void update_info_items(size_t obj_idx, wxDataViewItemArray *selections=nullptr, bool added_object=false, bool color_mode_changed=false)
Definition GUI_ObjectList.cpp:4039
void changed_object(ModelObject &object)
Definition Plater.cpp:22227
const Model & model() const
Definition Plater.cpp:16047
std::vector< TextureDisplacementPrepareResult > m_results
Definition TextureDisplacementPrepareJob.hpp:77
ModelObject * get_object() const
Definition Model.hpp:1022
ModelObjectPtrs objects
Definition Model.hpp:1702
static ObjectList * obj_list()
Definition GUI_Factories.cpp:405
static Plater * plater()
Definition GUI_Factories.cpp:400
static ModelVolume * get_model_volume(const Selection &selection, Model &model)
Definition GLGizmoBrimEars.cpp:66
TextureDisplacementPrepareOutcome
Definition TextureDisplacementPrepareJob.hpp:33
@ Unchanged
Definition TextureDisplacementPrepareJob.hpp:35
@ PaintLost
Definition TextureDisplacementPrepareJob.hpp:36
@ Failed
Definition TextureDisplacementPrepareJob.hpp:37
@ Committed
Definition TextureDisplacementPrepareJob.hpp:34
@ ObjectList
Definition Shortcuts.hpp:21
@ Plater
Definition Shortcuts.hpp:21
@ GizmoAction
Definition UndoRedo.hpp:36
static constexpr size_t TEXTURE_DISPLACEMENT_MAX_LAYERS
Definition TextureDisplacement.hpp:44
std::vector< ModelObject * > ModelObjectPtrs
Definition CutUtils.hpp:13

References Slic3r::GUI::Committed, Slic3r::GUI::Failed, finalize(), Slic3r::GUI::get_model_volume(), Slic3r::UndoRedo::GizmoAction, m_input, m_on_finished, m_results, Slic3r::GUI::obj_list(), Slic3r::GUI::PaintLost, Slic3r::GUI::plater(), Slic3r::TEXTURE_DISPLACEMENT_MAX_LAYERS, and Slic3r::GUI::Unchanged.

Referenced by finalize().

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

◆ process()

void Slic3r::GUI::TextureDisplacementPrepareJob::process ( Ctl & ctl)
overridevirtual

Implements Slic3r::GUI::Job.

37{
38 const std::string status = _u8L("Preparing mesh for texture displacement");
39 ctl.update_status(1, status);
40
41 // Only ever touches m_input (captured by value before this job was queued) and local state - never
42 // the live Model - so this is safe to run concurrently with the UI thread.
43 //
44 // The progress hook is not just cosmetic: the framework's notification only grows a close button
45 // once it reaches 100%, and below that it shows Cancel, which is wired through here. Reporting is
46 // throttled to whole percentage points because every call repaints the notification and wakes the
47 // idle loop.
48 // Each part gets its own slice of the range, in order, so one bar covers them all.
49 const size_t count = m_input.volumes.size();
50 m_results.resize(count);
51 int last_reported = 1;
52 for (size_t i = 0; i < count; ++i) {
53 const TextureDisplacementPrepareVolume &volume = m_input.volumes[i];
54 m_results[i] = GLGizmoTextureDisplacement::prepare_mesh(volume.base_mesh, volume.masks, volume.layers,
55 volume.model_uv, volume.texture_frame,
56 m_input.params, m_input.color,
57 [&ctl, &status, &last_reported, i, count](int percent) {
58 if (ctl.was_canceled())
59 return false;
60 const int overall =
61 int((i * 100 + size_t(std::max(0, percent))) / count);
62 if (overall > last_reported) {
63 last_reported = overall;
64 ctl.update_status(overall, status);
65 }
66 return true;
67 });
68 if (ctl.was_canceled())
69 break;
70 }
71
72 ctl.update_status(100, status); // always finish at 100: this is what closes the notification
73}
static unsigned int count
Definition AuxiliaryDataViewModel.cpp:246
static TextureDisplacementPrepareResult prepare_mesh(const indexed_triangle_set &base, const TextureDisplacementFacetsData &masks, const std::vector< TextureDisplacementLayer > &layers, const std::vector< Vec2f > &model_uv, const Transform3d &texture_frame, const TextureDisplacementPrepareParams &params, const TextureColorSettings &color_settings, const DisplacementProgressFn &progress, BakeStageRecorder *debug=nullptr)
Definition GLGizmoTextureDisplacement.cpp:6071
#define _u8L(s)
Definition I18N.hpp:11

References _u8L, Slic3r::GUI::TextureDisplacementPrepareVolume::base_mesh, count, Slic3r::GUI::TextureDisplacementPrepareVolume::layers, m_input, m_results, Slic3r::GUI::TextureDisplacementPrepareVolume::masks, Slic3r::GUI::TextureDisplacementPrepareVolume::model_uv, Slic3r::GUI::GLGizmoTextureDisplacement::prepare_mesh(), Slic3r::GUI::TextureDisplacementPrepareVolume::texture_frame, Slic3r::GUI::Job::Ctl::update_status(), and Slic3r::GUI::Job::Ctl::was_canceled().

Here is the call graph for this function:

Member Data Documentation

◆ m_input

TextureDisplacementPrepareInput Slic3r::GUI::TextureDisplacementPrepareJob::m_input
private

◆ m_on_finished

std::function<void(TextureDisplacementPrepareOutcome)> Slic3r::GUI::TextureDisplacementPrepareJob::m_on_finished
private

◆ m_results

std::vector<TextureDisplacementPrepareResult> Slic3r::GUI::TextureDisplacementPrepareJob::m_results
private

Referenced by finalize(), and process().


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