cosmethics

Comments and cosmethics
This commit is contained in:
tamasmeszaros 2020-09-01 20:08:42 +02:00
parent c10ff4f503
commit b4b9af4100
3 changed files with 26 additions and 21 deletions

View File

@ -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

View File

@ -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<size_t N>
constexpr long num_iter(const std::array<size_t, N> &idx, size_t gridsz)
long num_iter(const std::array<size_t, N> &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<int D, size_t N, class Fn, class Cmp>
bool run(std::array<size_t, N> &idx,
Result<N> &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<N> 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<D - 1>(idx, result, bounds, std::forward<Fn>(fn),
std::forward<Cmp>(cmp))) return false;
std::forward<Cmp>(cmp)))
return false;
}
}
@ -90,7 +102,7 @@ struct AlgBurteForce {
}
};
} // namespace bruteforce_detail
} // namespace detail
using AlgBruteForce = detail::AlgBurteForce;

View File

@ -6,14 +6,11 @@
#include <libslic3r/SLA/Rotfinder.hpp>
#include <libslic3r/SLA/Concurrency.hpp>
#include <libslic3r/SLA/SupportTree.hpp>
#include <libslic3r/SLA/SupportPointGenerator.hpp>
#include <libslic3r/SimplifyMesh.hpp>
#include "Model.hpp"
#include <thread>
namespace Slic3r {
namespace sla {
namespace Slic3r { namespace sla {
using VertexFaceMap = std::vector<std::vector<size_t>>;
@ -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<double, 2> 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<double, 2> 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<opt::AlgBruteForce> 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<double, 2> find_best_rotation(const ModelObject& modelobj,
return rot;
}
}
}
}} // namespace Slic3r::sla