From b4b9af410037fff27cfa0682e1deeb5744515d86 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Tue, 1 Sep 2020 20:08:42 +0200 Subject: [PATCH] cosmethics Comments and cosmethics --- src/libslic3r/CMakeLists.txt | 1 + .../Optimize/BruteforceOptimizer.hpp | 26 ++++++++++++++----- src/libslic3r/SLA/Rotfinder.cpp | 20 +++++--------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index 263920ecb..e30811133 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -217,6 +217,7 @@ add_library(libslic3r STATIC MarchingSquares.hpp Optimize/Optimizer.hpp Optimize/NLoptOptimizer.hpp + Optimize/BruteforceOptimizer.hpp ${OpenVDBUtils_SOURCES} SLA/Pad.hpp SLA/Pad.cpp diff --git a/src/libslic3r/Optimize/BruteforceOptimizer.hpp b/src/libslic3r/Optimize/BruteforceOptimizer.hpp index 960676e7b..2daef538e 100644 --- a/src/libslic3r/Optimize/BruteforceOptimizer.hpp +++ b/src/libslic3r/Optimize/BruteforceOptimizer.hpp @@ -8,14 +8,18 @@ namespace Slic3r { namespace opt { namespace detail { // Implementing a bruteforce optimizer +// Return the number of iterations needed to reach a specific grid position (idx) template -constexpr long num_iter(const std::array &idx, size_t gridsz) +long num_iter(const std::array &idx, size_t gridsz) { long ret = 0; for (size_t i = 0; i < N; ++i) ret += idx[i] * std::pow(gridsz, i); return ret; } +// Implementation of a grid search where the search interval is sampled in +// equidistant points for each dimension. Grid size determines the number of +// samples for one dimension so the number of function calls is gridsize ^ dimension. struct AlgBurteForce { bool to_min; StopCriteria stc; @@ -23,6 +27,11 @@ struct AlgBurteForce { AlgBurteForce(const StopCriteria &cr, size_t gs): stc{cr}, gridsz{gs} {} + // This function is called recursively for each dimension and generates + // the grid values for the particular dimension. If D is less than zero, + // the object function input values are generated for each dimension and it + // can be evaluated. The current best score is compared with the newly + // returned score and changed appropriately. template bool run(std::array &idx, Result &result, @@ -32,11 +41,12 @@ struct AlgBurteForce { { if (stc.stop_condition()) return false; - if constexpr (D < 0) { + if constexpr (D < 0) { // Let's evaluate fn Input inp; auto max_iter = stc.max_iterations(); - if (max_iter && num_iter(idx, gridsz) >= max_iter) return false; + if (max_iter && num_iter(idx, gridsz) >= max_iter) + return false; for (size_t d = 0; d < N; ++d) { const Bound &b = bounds[d]; @@ -45,12 +55,13 @@ struct AlgBurteForce { } auto score = fn(inp); - if (cmp(score, result.score)) { + if (cmp(score, result.score)) { // Change current score to the new double absdiff = std::abs(score - result.score); result.score = score; result.optimum = inp; + // Check if the required precision is reached. if (absdiff < stc.abs_score_diff() || absdiff < stc.rel_score_diff() * std::abs(score)) return false; @@ -58,9 +69,10 @@ struct AlgBurteForce { } else { for (size_t i = 0; i < gridsz; ++i) { - idx[D] = i; + idx[D] = i; // Mark the current grid position and dig down if (!run(idx, result, bounds, std::forward(fn), - std::forward(cmp))) return false; + std::forward(cmp))) + return false; } } @@ -90,7 +102,7 @@ struct AlgBurteForce { } }; -} // namespace bruteforce_detail +} // namespace detail using AlgBruteForce = detail::AlgBurteForce; diff --git a/src/libslic3r/SLA/Rotfinder.cpp b/src/libslic3r/SLA/Rotfinder.cpp index b7bed1c91..e8cc7df1b 100644 --- a/src/libslic3r/SLA/Rotfinder.cpp +++ b/src/libslic3r/SLA/Rotfinder.cpp @@ -6,14 +6,11 @@ #include #include #include -#include -#include #include "Model.hpp" #include -namespace Slic3r { -namespace sla { +namespace Slic3r { namespace sla { using VertexFaceMap = std::vector>; @@ -51,7 +48,6 @@ double find_ground_level(const TriangleMesh &mesh, // Try to guess the number of support points needed to support a mesh double calculate_model_supportedness(const TriangleMesh & mesh, -// const VertexFaceMap &vmap, const Transform3d & tr) { static constexpr double POINTS_PER_UNIT_AREA = 1.; @@ -111,8 +107,6 @@ std::array find_best_rotation(const ModelObject& modelobj, // rotations TriangleMesh mesh = modelobj.raw_mesh(); mesh.require_shared_vertices(); -// auto vmap = create_vertex_face_map(mesh); -// simplify_mesh(mesh); // For current iteration number unsigned status = 0; @@ -147,21 +141,20 @@ std::array find_best_rotation(const ModelObject& modelobj, return score; }; - // Firing up the genetic optimizer. For now it uses the nlopt library. - + // Firing up the optimizer. opt::Optimizer solver(opt::StopCriteria{} .max_iterations(max_tries) .rel_score_diff(1e-6) .stop_condition(stopcond), - 100 /*grid size*/); + std::sqrt(max_tries)/*grid size*/); // We are searching rotations around only two axes x, y. Thus the // problem becomes a 2 dimensional optimization task. // We can specify the bounds for a dimension in the following way: auto b = opt::Bound{-PI, PI}; - // Now we start the optimization process with initial angles (0, 0, 0) - auto result = solver.to_min().optimize(objfunc, opt::initvals({0.0, 0.0}), + // Now we start the optimization process with initial angles (0, 0) + auto result = solver.to_min().optimize(objfunc, opt::initvals({0., 0.}), opt::bounds({b, b})); // Save the result and fck off @@ -171,5 +164,4 @@ std::array find_best_rotation(const ModelObject& modelobj, return rot; } -} -} +}} // namespace Slic3r::sla