Merge branch 'tm_rotcalipers'

This commit is contained in:
tamasmeszaros 2019-06-19 13:11:18 +02:00
commit 1e7b5c5a81
25 changed files with 1329 additions and 898 deletions

View file

@ -39,7 +39,12 @@
#include "libslic3r/SLA/SLARotfinder.hpp"
#include "libslic3r/Utils.hpp"
#include "libnest2d/optimizers/nlopt/genetic.hpp"
//#include "libslic3r/ClipperUtils.hpp"
// #include "libnest2d/optimizers/nlopt/genetic.hpp"
// #include "libnest2d/backends/clipper/geometries.hpp"
// #include "libnest2d/utils/rotcalipers.hpp"
#include "libslic3r/MinAreaBoundingBox.hpp"
#include "GUI.hpp"
#include "GUI_App.hpp"
@ -2468,8 +2473,9 @@ void Plater::priv::ExclusiveJobGroup::RotoptimizeJob::process()
},
[this]() { return was_canceled(); });
const auto *bed_shape_opt = plater().config->opt<ConfigOptionPoints>(
"bed_shape");
const auto *bed_shape_opt =
plater().config->opt<ConfigOptionPoints>("bed_shape");
assert(bed_shape_opt);
auto & bedpoints = bed_shape_opt->values;
@ -2478,70 +2484,40 @@ void Plater::priv::ExclusiveJobGroup::RotoptimizeJob::process()
for (auto &v : bedpoints) bed.append(Point::new_scale(v(0), v(1)));
double mindist = 6.0; // FIXME
double offs = mindist / 2.0 - EPSILON;
if (!was_canceled()) // wasn't canceled
for (ModelInstance *oi : o->instances) {
if (!was_canceled()) {
for(ModelInstance * oi : o->instances) {
oi->set_rotation({r[X], r[Y], r[Z]});
auto trchull = o->convex_hull_2d(
oi->get_transformation().get_matrix());
namespace opt = libnest2d::opt;
opt::StopCriteria stopcr;
stopcr.relative_score_difference = 0.01;
stopcr.max_iterations = 10000;
stopcr.stop_score = 0.0;
opt::GeneticOptimizer solver(stopcr);
Polygon pbed(bed);
auto bin = pbed.bounding_box();
double binw = bin.size()(X) * SCALING_FACTOR - offs;
double binh = bin.size()(Y) * SCALING_FACTOR - offs;
auto result = solver.optimize_min(
[&trchull, binw, binh](double rot) {
auto chull = trchull;
chull.rotate(rot);
auto bb = chull.bounding_box();
double bbw = bb.size()(X) * SCALING_FACTOR;
double bbh = bb.size()(Y) * SCALING_FACTOR;
auto wdiff = bbw - binw;
auto hdiff = bbh - binh;
double diff = 0;
if (wdiff < 0 && hdiff < 0) diff = wdiff + hdiff;
if (wdiff > 0) diff += wdiff;
if (hdiff > 0) diff += hdiff;
return diff;
},
opt::initvals(0.0),
opt::bound(-PI / 2, PI / 2));
double r = std::get<0>(result.optimum);
Vec3d rt = oi->get_rotation();
rt(Z) += r;
oi->set_rotation(rt);
arr::WipeTowerInfo wti; // useless in SLA context
arr::find_new_position(plater().model,
o->instances,
coord_t(mindist / SCALING_FACTOR),
bed,
wti);
// Correct the z offset of the object which was corrupted be
// the rotation
o->ensure_on_bed();
auto trmatrix = oi->get_transformation().get_matrix();
Polygon trchull = o->convex_hull_2d(trmatrix);
update_status(100, _(L("Orientation found.")));
MinAreaBoundigBox rotbb(trchull, MinAreaBoundigBox::pcConvex);
double r = rotbb.angle_to_X();
// The box should be landscape
if(rotbb.width() < rotbb.height()) r += PI / 2;
Vec3d rt = oi->get_rotation(); rt(Z) += r;
oi->set_rotation(rt);
}
else {
update_status(100, _(L("Orientation search canceled.")));
arr::WipeTowerInfo wti; // useless in SLA context
arr::find_new_position(plater().model,
o->instances,
coord_t(mindist / SCALING_FACTOR),
bed,
wti);
// Correct the z offset of the object which was corrupted be
// the rotation
o->ensure_on_bed();
}
update_status(100,
was_canceled() ? _(L("Orientation search canceled."))
: _(L("Orientation found.")));
}
void Plater::priv::split_object()