Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/pstack/calc/stacker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct stack_state {
util::mdarray<Bool, 3> space;
std::vector<std::shared_ptr<const part>> ordered_parts;
stack_result result;
std::size_t total_parts;
std::size_t total_placed;
};

void place(const util::mdspan<Bool, 3> space, const int index, const util::mdspan<const int, 3> obj, const int x, const int y, const int z) {
Expand Down Expand Up @@ -100,6 +102,9 @@ std::size_t try_place(const stack_parameters& params, stack_state& state, const
new_piece.translation += translation;
place(state.space, bit_index, state.voxels[part_index], x, y, z); // Mark voxels as occupied
++placed;
++state.total_placed;
params.set_progress(state.total_placed, state.total_parts);
params.display_mesh(state.result.mesh, max);
if (to_place == placed) { // All instances of this part placed, move to next part
return placed;
} else { // Move to next instance of part
Expand All @@ -125,10 +130,11 @@ std::optional<stack_result> stack_impl(const stack_parameters& params, const std

double triangles = 0;
const double scale_factor = 1 / params.resolution;
int total_parts = 0;
state.total_parts = 0;
state.total_placed = 0;
for (const std::shared_ptr<const part> part : state.ordered_parts) {
triangles += part->triangle_count * rotation_sets[part->rotation_index].size();
total_parts += part->quantity;
state.total_parts += part->quantity;
}

double progress = 0;
Expand Down Expand Up @@ -232,7 +238,6 @@ std::optional<stack_result> stack_impl(const stack_parameters& params, const std

params.set_progress(0, 1);

std::size_t total_placed = 0;
for (std::size_t part_index = 0; part_index != state.ordered_parts.size(); ++part_index) {
std::size_t to_place = state.ordered_parts[part_index]->quantity;
while (to_place > 0) {
Expand All @@ -241,9 +246,6 @@ std::optional<stack_result> stack_impl(const stack_parameters& params, const std
}
const std::size_t placed = try_place(params, state, part_index, to_place, { max_x, max_y, max_z });
to_place -= placed;
total_placed += placed;
params.set_progress(total_placed, total_parts);
params.display_mesh(state.result.mesh, max_x, max_y, max_z);

// If we have not placed a part, it means there are no more ways to place an instance of the current part in the box: it must be enlarged
if (placed == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/pstack/calc/stacker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct stack_parameters {
std::vector<std::shared_ptr<const part>> parts;

std::function<void(double, double)> set_progress;
std::function<void(const mesh&, int, int, int)> display_mesh;
std::function<void(const mesh&, const geo::point3<int>)> display_mesh;
std::function<void(stack_result, std::chrono::system_clock::duration)> on_success;
std::function<void()> on_failure;
std::function<void()> on_finish;
Expand Down
4 changes: 2 additions & 2 deletions src/pstack/gui/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ void main_window::on_stacking_start() {
_controls.progress_bar->SetValue(static_cast<int>(100 * progress / total));
});
},
.display_mesh = [this](const calc::mesh& mesh, int max_x, int max_y, int max_z) {
.display_mesh = [this](const calc::mesh& mesh, const geo::point3<int> max) {
CallAfter([=] {
// Make a copy of `mesh`, otherwise we encounter a data race
_viewport->set_mesh(mesh, { max_x / 2.0f, max_y / 2.0f, max_z / 2.0f });
_viewport->set_mesh(mesh, { max.x / 2.0f, max.y / 2.0f, max.z / 2.0f });
});
},
.on_success = [this](calc::stack_result result, const std::chrono::system_clock::duration elapsed) {
Expand Down