2019-10-04 12:47:02 +00:00
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
|
2019-10-02 12:55:02 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <random>
|
2019-09-26 07:42:08 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
// Debug
|
|
|
|
#include <fstream>
|
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
#include "libslic3r/libslic3r.h"
|
|
|
|
#include "libslic3r/Format/OBJ.hpp"
|
|
|
|
#include "libslic3r/SLAPrint.hpp"
|
|
|
|
#include "libslic3r/TriangleMesh.hpp"
|
|
|
|
#include "libslic3r/SLA/SLAPad.hpp"
|
2019-09-26 07:42:08 +00:00
|
|
|
#include "libslic3r/SLA/SLASupportTreeBuilder.hpp"
|
2019-10-01 11:27:58 +00:00
|
|
|
#include "libslic3r/SLA/SLASupportTreeBuildsteps.hpp"
|
2019-09-24 13:24:47 +00:00
|
|
|
#include "libslic3r/SLA/SLAAutoSupports.hpp"
|
2019-10-01 11:27:58 +00:00
|
|
|
#include "libslic3r/SLA/SLARaster.hpp"
|
2019-09-24 13:24:47 +00:00
|
|
|
#include "libslic3r/MTUtils.hpp"
|
|
|
|
|
|
|
|
#include "libslic3r/SVG.hpp"
|
2019-10-03 12:23:03 +00:00
|
|
|
#include "libslic3r/Format/OBJ.hpp"
|
2019-09-24 13:24:47 +00:00
|
|
|
|
2019-10-02 12:55:02 +00:00
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
#define PATH_SEPARATOR R"(\)"
|
|
|
|
#else
|
|
|
|
#define PATH_SEPARATOR R"(/)"
|
2019-09-24 13:24:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
using namespace Slic3r;
|
|
|
|
|
|
|
|
TriangleMesh load_model(const std::string &obj_filename)
|
|
|
|
{
|
|
|
|
TriangleMesh mesh;
|
2019-10-02 12:55:02 +00:00
|
|
|
auto fpath = TEST_DATA_DIR PATH_SEPARATOR + obj_filename;
|
2019-09-24 13:24:47 +00:00
|
|
|
load_obj(fpath.c_str(), &mesh);
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum e_validity {
|
|
|
|
ASSUME_NO_EMPTY = 1,
|
|
|
|
ASSUME_MANIFOLD = 2,
|
|
|
|
ASSUME_NO_REPAIR = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
void check_validity(const TriangleMesh &input_mesh,
|
|
|
|
int flags = ASSUME_NO_EMPTY | ASSUME_MANIFOLD |
|
|
|
|
ASSUME_NO_REPAIR)
|
|
|
|
{
|
|
|
|
TriangleMesh mesh{input_mesh};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
if (flags & ASSUME_NO_EMPTY) {
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE_FALSE(mesh.empty());
|
2019-10-02 12:55:02 +00:00
|
|
|
} else if (mesh.empty())
|
2019-09-24 13:24:47 +00:00
|
|
|
return; // If it can be empty and it is, there is nothing left to do.
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(stl_validate(&mesh.stl));
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
bool do_update_shared_vertices = false;
|
|
|
|
mesh.repair(do_update_shared_vertices);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
if (flags & ASSUME_NO_REPAIR) {
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE_FALSE(mesh.needed_repair());
|
2019-09-24 13:24:47 +00:00
|
|
|
}
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
if (flags & ASSUME_MANIFOLD) {
|
|
|
|
mesh.require_shared_vertices();
|
|
|
|
if (!mesh.is_manifold()) mesh.WriteOBJFile("non_manifold.obj");
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(mesh.is_manifold());
|
2019-09-24 13:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PadByproducts
|
|
|
|
{
|
|
|
|
ExPolygons model_contours;
|
|
|
|
ExPolygons support_contours;
|
|
|
|
TriangleMesh mesh;
|
|
|
|
};
|
|
|
|
|
|
|
|
void test_pad(const std::string & obj_filename,
|
|
|
|
const sla::PadConfig &padcfg,
|
|
|
|
PadByproducts & out)
|
|
|
|
{
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(padcfg.validate().empty());
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
TriangleMesh mesh = load_model(obj_filename);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE_FALSE(mesh.empty());
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Create pad skeleton only from the model
|
|
|
|
Slic3r::sla::pad_blueprint(mesh, out.model_contours);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE_FALSE(out.model_contours.empty());
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
// Create the pad geometry for the model contours only
|
2019-09-24 13:24:47 +00:00
|
|
|
Slic3r::sla::create_pad({}, out.model_contours, out.mesh, padcfg);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
check_validity(out.mesh);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
auto bb = out.mesh.bounding_box();
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(bb.max.z() - bb.min.z() == Approx(padcfg.full_height()));
|
2019-09-24 13:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_pad(const std::string & obj_filename,
|
|
|
|
const sla::PadConfig &padcfg = {})
|
|
|
|
{
|
|
|
|
PadByproducts byproducts;
|
|
|
|
test_pad(obj_filename, padcfg, byproducts);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SupportByproducts
|
|
|
|
{
|
2019-10-04 16:14:19 +00:00
|
|
|
std::string obj_fname;
|
2019-09-24 13:24:47 +00:00
|
|
|
std::vector<float> slicegrid;
|
|
|
|
std::vector<ExPolygons> model_slices;
|
2019-09-26 07:42:08 +00:00
|
|
|
sla::SupportTreeBuilder supporttree;
|
2019-10-04 16:14:19 +00:00
|
|
|
TriangleMesh input_mesh;
|
2019-09-24 13:24:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const constexpr float CLOSING_RADIUS = 0.005f;
|
|
|
|
|
2019-09-26 07:42:08 +00:00
|
|
|
void check_support_tree_integrity(const sla::SupportTreeBuilder &stree,
|
|
|
|
const sla::SupportConfig &cfg)
|
|
|
|
{
|
|
|
|
double gnd = stree.ground_level;
|
|
|
|
double H1 = cfg.max_solo_pillar_height_mm;
|
|
|
|
double H2 = cfg.max_dual_pillar_height_mm;
|
2019-10-04 12:47:02 +00:00
|
|
|
|
2019-10-02 12:55:02 +00:00
|
|
|
for (const sla::Head &head : stree.heads()) {
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE((!head.is_valid() || head.pillar_id != sla::ID_UNSET ||
|
|
|
|
head.bridge_id != sla::ID_UNSET));
|
2019-10-02 12:55:02 +00:00
|
|
|
}
|
2019-09-26 07:42:08 +00:00
|
|
|
|
|
|
|
for (const sla::Pillar &pillar : stree.pillars()) {
|
|
|
|
if (std::abs(pillar.endpoint().z() - gnd) < EPSILON) {
|
|
|
|
double h = pillar.height;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
if (h > H1) REQUIRE(pillar.links >= 1);
|
|
|
|
else if(h > H2) { REQUIRE(pillar.links >= 2); }
|
2019-09-26 07:42:08 +00:00
|
|
|
}
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(pillar.links <= cfg.pillar_cascade_neighbors);
|
|
|
|
REQUIRE(pillar.bridges <= cfg.max_bridges_on_pillar);
|
2019-09-26 07:42:08 +00:00
|
|
|
}
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-26 07:42:08 +00:00
|
|
|
double max_bridgelen = 0.;
|
|
|
|
auto chck_bridge = [&cfg](const sla::Bridge &bridge, double &max_brlen) {
|
|
|
|
Vec3d n = bridge.endp - bridge.startp;
|
|
|
|
double d = sla::distance(n);
|
|
|
|
max_brlen = std::max(d, max_brlen);
|
|
|
|
|
|
|
|
double z = n.z();
|
|
|
|
double polar = std::acos(z / d);
|
|
|
|
double slope = -polar + PI / 2.;
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(std::abs(slope) >= cfg.bridge_slope - EPSILON);
|
2019-09-26 07:42:08 +00:00
|
|
|
};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-26 07:42:08 +00:00
|
|
|
for (auto &bridge : stree.bridges()) chck_bridge(bridge, max_bridgelen);
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(max_bridgelen <= cfg.max_bridge_length_mm);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-26 07:42:08 +00:00
|
|
|
max_bridgelen = 0;
|
|
|
|
for (auto &bridge : stree.crossbridges()) chck_bridge(bridge, max_bridgelen);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-26 07:42:08 +00:00
|
|
|
double md = cfg.max_pillar_link_distance_mm / std::cos(-cfg.bridge_slope);
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(max_bridgelen <= md);
|
2019-09-26 07:42:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
void test_supports(const std::string & obj_filename,
|
|
|
|
const sla::SupportConfig &supportcfg,
|
|
|
|
SupportByproducts & out)
|
|
|
|
{
|
|
|
|
using namespace Slic3r;
|
|
|
|
TriangleMesh mesh = load_model(obj_filename);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE_FALSE(mesh.empty());
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
TriangleMeshSlicer slicer{&mesh};
|
2019-09-26 07:42:08 +00:00
|
|
|
|
|
|
|
auto bb = mesh.bounding_box();
|
|
|
|
double zmin = bb.min.z();
|
|
|
|
double zmax = bb.max.z();
|
|
|
|
double gnd = zmin - supportcfg.object_elevation_mm;
|
|
|
|
auto layer_h = 0.05f;
|
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
out.slicegrid = grid(float(gnd), float(zmax), layer_h);
|
|
|
|
slicer.slice(out.slicegrid , CLOSING_RADIUS, &out.model_slices, []{});
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Create the special index-triangle mesh with spatial indexing which
|
|
|
|
// is the input of the support point and support mesh generators
|
|
|
|
sla::EigenMesh3D emesh{mesh};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Create the support point generator
|
|
|
|
sla::SLAAutoSupports::Config autogencfg;
|
|
|
|
autogencfg.head_diameter = float(2 * supportcfg.head_front_radius_mm);
|
|
|
|
sla::SLAAutoSupports point_gen{emesh, out.model_slices, out.slicegrid,
|
|
|
|
autogencfg, [] {}, [](int) {}};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Get the calculated support points.
|
|
|
|
std::vector<sla::SupportPoint> support_points = point_gen.output();
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
int validityflags = ASSUME_NO_REPAIR;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// If there is no elevation, support points shall be removed from the
|
|
|
|
// bottom of the object.
|
2019-10-03 12:23:03 +00:00
|
|
|
if (std::abs(supportcfg.object_elevation_mm) < EPSILON) {
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::remove_bottom_points(support_points, zmin,
|
2019-10-02 12:55:02 +00:00
|
|
|
supportcfg.base_height_mm);
|
2019-09-24 13:24:47 +00:00
|
|
|
} else {
|
|
|
|
// Should be support points at least on the bottom of the model
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE_FALSE(support_points.empty());
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Also the support mesh should not be empty.
|
|
|
|
validityflags |= ASSUME_NO_EMPTY;
|
|
|
|
}
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Generate the actual support tree
|
2019-09-26 07:42:08 +00:00
|
|
|
sla::SupportTreeBuilder treebuilder;
|
|
|
|
treebuilder.build(sla::SupportableMesh{emesh, support_points, supportcfg});
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-26 07:42:08 +00:00
|
|
|
check_support_tree_integrity(treebuilder, supportcfg);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-26 07:42:08 +00:00
|
|
|
const TriangleMesh &output_mesh = treebuilder.retrieve_mesh();
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
check_validity(output_mesh, validityflags);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Quick check if the dimensions and placement of supports are correct
|
|
|
|
auto obb = output_mesh.bounding_box();
|
2019-10-04 12:47:02 +00:00
|
|
|
|
2019-10-03 12:23:03 +00:00
|
|
|
double allowed_zmin = zmin - supportcfg.object_elevation_mm;
|
2019-10-04 12:47:02 +00:00
|
|
|
|
2019-10-03 12:23:03 +00:00
|
|
|
if (std::abs(supportcfg.object_elevation_mm) < EPSILON)
|
|
|
|
allowed_zmin = zmin - 2 * supportcfg.head_back_radius_mm;
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(obb.min.z() >= allowed_zmin);
|
|
|
|
REQUIRE(obb.max.z() <= zmax);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Move out the support tree into the byproducts, we can examine it further
|
|
|
|
// in various tests.
|
2019-10-04 16:14:19 +00:00
|
|
|
out.obj_fname = std::move(obj_filename);
|
2019-09-26 07:42:08 +00:00
|
|
|
out.supporttree = std::move(treebuilder);
|
2019-10-04 16:14:19 +00:00
|
|
|
out.input_mesh = std::move(mesh);
|
2019-09-24 13:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_supports(const std::string & obj_filename,
|
|
|
|
const sla::SupportConfig &supportcfg = {})
|
|
|
|
{
|
|
|
|
SupportByproducts byproducts;
|
|
|
|
test_supports(obj_filename, supportcfg, byproducts);
|
|
|
|
}
|
|
|
|
|
2019-10-04 16:14:19 +00:00
|
|
|
void export_failed_case(const std::vector<ExPolygons> &support_slices,
|
|
|
|
const SupportByproducts &byproducts)
|
|
|
|
{
|
|
|
|
for (size_t n = 0; n < support_slices.size(); ++n) {
|
|
|
|
const ExPolygons &sup_slice = support_slices[n];
|
|
|
|
const ExPolygons &mod_slice = byproducts.model_slices[n];
|
|
|
|
Polygons intersections = intersection(sup_slice, mod_slice);
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
if (!intersections.empty()) {
|
|
|
|
ss << byproducts.obj_fname << std::setprecision(4) << n << ".svg";
|
|
|
|
SVG svg(ss.str());
|
|
|
|
svg.draw(sup_slice, "green");
|
|
|
|
svg.draw(mod_slice, "blue");
|
|
|
|
svg.draw(intersections, "red");
|
|
|
|
svg.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TriangleMesh m;
|
|
|
|
byproducts.supporttree.retrieve_full_mesh(m);
|
|
|
|
m.merge(byproducts.input_mesh);
|
|
|
|
m.repair();
|
|
|
|
m.require_shared_vertices();
|
|
|
|
m.WriteOBJFile(byproducts.obj_fname.c_str());
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
void test_support_model_collision(
|
|
|
|
const std::string & obj_filename,
|
|
|
|
const sla::SupportConfig &input_supportcfg = {})
|
|
|
|
{
|
|
|
|
SupportByproducts byproducts;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::SupportConfig supportcfg = input_supportcfg;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Set head penetration to a small negative value which should ensure that
|
|
|
|
// the supports will not touch the model body.
|
2019-10-04 16:14:19 +00:00
|
|
|
supportcfg.head_penetration_mm = -0.15;
|
|
|
|
|
|
|
|
// TODO: currently, the tailheads penetrating into the model body do not
|
|
|
|
// respect the penetration parameter properly. No issues were reported so
|
|
|
|
// far but we should definitely fix this.
|
|
|
|
supportcfg.ground_facing_only = true;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
test_supports(obj_filename, supportcfg, byproducts);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Slice the support mesh given the slice grid of the model.
|
|
|
|
std::vector<ExPolygons> support_slices =
|
|
|
|
byproducts.supporttree.slice(byproducts.slicegrid, CLOSING_RADIUS);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// The slices originate from the same slice grid so the numbers must match
|
2019-10-04 16:14:19 +00:00
|
|
|
|
|
|
|
bool support_mesh_is_empty =
|
|
|
|
byproducts.supporttree.retrieve_mesh(sla::MeshType::Pad).empty() &&
|
|
|
|
byproducts.supporttree.retrieve_mesh(sla::MeshType::Support).empty();
|
|
|
|
|
|
|
|
if (support_mesh_is_empty)
|
|
|
|
REQUIRE(support_slices.empty());
|
|
|
|
else
|
|
|
|
REQUIRE(support_slices.size() == byproducts.model_slices.size());
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
bool notouch = true;
|
|
|
|
for (size_t n = 0; notouch && n < support_slices.size(); ++n) {
|
|
|
|
const ExPolygons &sup_slice = support_slices[n];
|
|
|
|
const ExPolygons &mod_slice = byproducts.model_slices[n];
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
Polygons intersections = intersection(sup_slice, mod_slice);
|
2019-10-04 12:47:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
notouch = notouch && intersections.empty();
|
|
|
|
}
|
2019-10-04 16:14:19 +00:00
|
|
|
|
|
|
|
if (!notouch) export_failed_case(support_slices, byproducts);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(notouch);
|
2019-09-24 13:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * const BELOW_PAD_TEST_OBJECTS[] = {
|
|
|
|
"20mm_cube.obj",
|
|
|
|
"V.obj",
|
|
|
|
};
|
|
|
|
|
|
|
|
const char * const AROUND_PAD_TEST_OBJECTS[] = {
|
|
|
|
"20mm_cube.obj",
|
|
|
|
"V.obj",
|
|
|
|
"frog_legs.obj",
|
|
|
|
"cube_with_concave_hole_enlarged.obj",
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *const SUPPORT_TEST_MODELS[] = {
|
2019-10-03 12:23:03 +00:00
|
|
|
"cube_with_concave_hole_enlarged_standing.obj",
|
2019-10-03 14:15:11 +00:00
|
|
|
"A_upsidedown.obj",
|
|
|
|
"extruder_idler.obj"
|
2019-09-24 13:24:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-10-02 12:55:02 +00:00
|
|
|
// Test pair hash for 'nums' random number pairs.
|
2019-09-26 07:42:08 +00:00
|
|
|
template <class I, class II> void test_pairhash()
|
|
|
|
{
|
2019-10-02 12:55:02 +00:00
|
|
|
const constexpr size_t nums = 1000;
|
|
|
|
I A[nums] = {0}, B[nums] = {0};
|
|
|
|
std::unordered_set<I> CH;
|
|
|
|
std::unordered_map<II, std::pair<I, I>> ints;
|
|
|
|
|
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 gen(rd());
|
|
|
|
|
|
|
|
const I Ibits = int(sizeof(I) * CHAR_BIT);
|
|
|
|
const II IIbits = int(sizeof(II) * CHAR_BIT);
|
|
|
|
const int bits = IIbits / 2 < Ibits ? Ibits / 2 : Ibits;
|
|
|
|
|
|
|
|
const I Imax = I(std::pow(2., bits) - 1);
|
|
|
|
std::uniform_int_distribution<I> dis(0, Imax);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < nums;) {
|
|
|
|
I a = dis(gen);
|
|
|
|
if (CH.find(a) == CH.end()) { CH.insert(a); A[i] = a; ++i; }
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < nums;) {
|
|
|
|
I b = dis(gen);
|
|
|
|
if (CH.find(b) == CH.end()) { CH.insert(b); B[i] = b; ++i; }
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < nums; ++i) {
|
|
|
|
I a = A[i], b = B[i];
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(a != b);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
|
|
|
II hash_ab = sla::pairhash<I, II>(a, b);
|
|
|
|
II hash_ba = sla::pairhash<I, II>(b, a);
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(hash_ab == hash_ba);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
|
|
|
auto it = ints.find(hash_ab);
|
|
|
|
|
|
|
|
if (it != ints.end()) {
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE((
|
2019-10-02 12:55:02 +00:00
|
|
|
(it->second.first == a && it->second.second == b) ||
|
2019-10-04 12:47:02 +00:00
|
|
|
(it->second.first == b && it->second.second == a)
|
|
|
|
));
|
2019-10-02 12:55:02 +00:00
|
|
|
} else
|
|
|
|
ints[hash_ab] = std::make_pair(a, b);
|
|
|
|
}
|
2019-09-26 07:42:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("Pillar pairhash should be unique", "[SLASupportGeneration]") {
|
2019-09-26 07:42:08 +00:00
|
|
|
test_pairhash<int, long>();
|
|
|
|
test_pairhash<unsigned, unsigned>();
|
|
|
|
test_pairhash<unsigned, unsigned long>();
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("Flat pad geometry is valid", "[SLASupportGeneration]") {
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::PadConfig padcfg;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Disable wings
|
|
|
|
padcfg.wall_height_mm = .0;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
for (auto &fname : BELOW_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("WingedPadGeometryIsValid", "[SLASupportGeneration]") {
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::PadConfig padcfg;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Add some wings to the pad to test the cavity
|
|
|
|
padcfg.wall_height_mm = 1.;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
for (auto &fname : BELOW_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("FlatPadAroundObjectIsValid", "[SLASupportGeneration]") {
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::PadConfig padcfg;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Add some wings to the pad to test the cavity
|
|
|
|
padcfg.wall_height_mm = 0.;
|
|
|
|
// padcfg.embed_object.stick_stride_mm = 0.;
|
|
|
|
padcfg.embed_object.enabled = true;
|
|
|
|
padcfg.embed_object.everywhere = true;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
for (auto &fname : AROUND_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("WingedPadAroundObjectIsValid", "[SLASupportGeneration]") {
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::PadConfig padcfg;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
// Add some wings to the pad to test the cavity
|
|
|
|
padcfg.wall_height_mm = 1.;
|
|
|
|
padcfg.embed_object.enabled = true;
|
|
|
|
padcfg.embed_object.everywhere = true;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
for (auto &fname : AROUND_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("ElevatedSupportGeometryIsValid", "[SLASupportGeneration]") {
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::SupportConfig supportcfg;
|
|
|
|
supportcfg.object_elevation_mm = 5.;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
for (auto fname : SUPPORT_TEST_MODELS) test_supports(fname);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("FloorSupportGeometryIsValid", "[SLASupportGeneration]") {
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::SupportConfig supportcfg;
|
|
|
|
supportcfg.object_elevation_mm = 0;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
for (auto &fname: SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("ElevatedSupportsDoNotPierceModel", "[SLASupportGeneration]") {
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-09-24 13:24:47 +00:00
|
|
|
sla::SupportConfig supportcfg;
|
|
|
|
|
|
|
|
for (auto fname : SUPPORT_TEST_MODELS)
|
|
|
|
test_support_model_collision(fname, supportcfg);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("FloorSupportsDoNotPierceModel", "[SLASupportGeneration]") {
|
|
|
|
|
2019-10-03 14:15:11 +00:00
|
|
|
sla::SupportConfig supportcfg;
|
|
|
|
supportcfg.object_elevation_mm = 0;
|
2019-10-04 12:47:02 +00:00
|
|
|
|
2019-10-03 14:15:11 +00:00
|
|
|
for (auto fname : SUPPORT_TEST_MODELS)
|
|
|
|
test_support_model_collision(fname, supportcfg);
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("DefaultRasterShouldBeEmpty", "[SLARasterOutput]") {
|
2019-10-01 11:27:58 +00:00
|
|
|
sla::Raster raster;
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(raster.empty());
|
2019-10-01 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("InitializedRasterShouldBeNONEmpty", "[SLARasterOutput]") {
|
2019-10-01 11:27:58 +00:00
|
|
|
// Default Prusa SL1 display parameters
|
|
|
|
sla::Raster::Resolution res{2560, 1440};
|
|
|
|
sla::Raster::PixelDim pixdim{120. / res.width_px, 68. / res.height_px};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
sla::Raster raster;
|
|
|
|
raster.reset(res, pixdim);
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE_FALSE(raster.empty());
|
|
|
|
REQUIRE(raster.resolution().width_px == res.width_px);
|
|
|
|
REQUIRE(raster.resolution().height_px == res.height_px);
|
|
|
|
REQUIRE(raster.pixel_dimensions().w_mm == Approx(pixdim.w_mm));
|
|
|
|
REQUIRE(raster.pixel_dimensions().h_mm == Approx(pixdim.h_mm));
|
2019-10-01 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using TPixel = uint8_t;
|
|
|
|
static constexpr const TPixel FullWhite = 255;
|
|
|
|
static constexpr const TPixel FullBlack = 0;
|
|
|
|
|
|
|
|
template <class A, int N> constexpr int arraysize(const A (&)[N]) { return N; }
|
|
|
|
|
|
|
|
static void check_raster_transformations(sla::Raster::Orientation o,
|
|
|
|
sla::Raster::TMirroring mirroring)
|
|
|
|
{
|
|
|
|
double disp_w = 120., disp_h = 68.;
|
|
|
|
sla::Raster::Resolution res{2560, 1440};
|
|
|
|
sla::Raster::PixelDim pixdim{disp_w / res.width_px, disp_h / res.height_px};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
auto bb = BoundingBox({0, 0}, {scaled(disp_w), scaled(disp_h)});
|
|
|
|
sla::Raster::Trafo trafo{o, mirroring};
|
|
|
|
trafo.origin_x = bb.center().x();
|
|
|
|
trafo.origin_y = bb.center().y();
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
sla::Raster raster{res, pixdim, trafo};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
// create box of size 32x32 pixels (not 1x1 to avoid antialiasing errors)
|
|
|
|
coord_t pw = 32 * coord_t(std::ceil(scaled<double>(pixdim.w_mm)));
|
|
|
|
coord_t ph = 32 * coord_t(std::ceil(scaled<double>(pixdim.h_mm)));
|
|
|
|
ExPolygon box;
|
|
|
|
box.contour.points = {{-pw, -ph}, {pw, -ph}, {pw, ph}, {-pw, ph}};
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
double tr_x = scaled<double>(20.), tr_y = tr_x;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
box.translate(tr_x, tr_y);
|
|
|
|
ExPolygon expected_box = box;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
// Now calculate the position of the translated box according to output
|
|
|
|
// trafo.
|
|
|
|
if (o == sla::Raster::Orientation::roPortrait) expected_box.rotate(PI / 2.);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
if (mirroring[X])
|
|
|
|
for (auto &p : expected_box.contour.points) p.x() = -p.x();
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
if (mirroring[Y])
|
|
|
|
for (auto &p : expected_box.contour.points) p.y() = -p.y();
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
raster.draw(box);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
Point expected_coords = expected_box.contour.bounding_box().center();
|
|
|
|
double rx = unscaled(expected_coords.x() + bb.center().x()) / pixdim.w_mm;
|
|
|
|
double ry = unscaled(expected_coords.y() + bb.center().y()) / pixdim.h_mm;
|
|
|
|
auto w = size_t(std::floor(rx));
|
|
|
|
auto h = res.height_px - size_t(std::floor(ry));
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE((w < res.width_px && h < res.height_px));
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
auto px = raster.read_pixel(w, h);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
if (px != FullWhite) {
|
|
|
|
sla::PNGImage img;
|
|
|
|
std::fstream outf("out.png", std::ios::out);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
|
|
|
outf << img.serialize(raster);
|
2019-10-01 11:27:58 +00:00
|
|
|
}
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(px == FullWhite);
|
2019-10-01 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("MirroringShouldBeCorrect", "[SLARasterOutput]") {
|
2019-10-01 11:27:58 +00:00
|
|
|
sla::Raster::TMirroring mirrorings[] = {sla::Raster::NoMirror,
|
|
|
|
sla::Raster::MirrorX,
|
|
|
|
sla::Raster::MirrorY,
|
|
|
|
sla::Raster::MirrorXY};
|
|
|
|
|
|
|
|
sla::Raster::Orientation orientations[] = {sla::Raster::roLandscape,
|
|
|
|
sla::Raster::roPortrait};
|
|
|
|
for (auto orientation : orientations)
|
|
|
|
for (auto &mirror : mirrorings)
|
|
|
|
check_raster_transformations(orientation, mirror);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExPolygon square_with_hole(double v)
|
|
|
|
{
|
|
|
|
ExPolygon poly;
|
|
|
|
coord_t V = scaled(v / 2.);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
poly.contour.points = {{-V, -V}, {V, -V}, {V, V}, {-V, V}};
|
|
|
|
poly.holes.emplace_back();
|
|
|
|
V = V / 2;
|
|
|
|
poly.holes.front().points = {{-V, V}, {V, V}, {V, -V}, {-V, -V}};
|
|
|
|
return poly;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double pixel_area(TPixel px, const sla::Raster::PixelDim &pxdim)
|
|
|
|
{
|
|
|
|
return (pxdim.h_mm * pxdim.w_mm) * px * 1. / (FullWhite - FullBlack);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double raster_white_area(const sla::Raster &raster)
|
|
|
|
{
|
|
|
|
if (raster.empty()) return std::nan("");
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
auto res = raster.resolution();
|
|
|
|
double a = 0;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
for (size_t x = 0; x < res.width_px; ++x)
|
|
|
|
for (size_t y = 0; y < res.height_px; ++y) {
|
|
|
|
auto px = raster.read_pixel(x, y);
|
|
|
|
a += pixel_area(px, raster.pixel_dimensions());
|
|
|
|
}
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double predict_error(const ExPolygon &p, const sla::Raster::PixelDim &pd)
|
|
|
|
{
|
|
|
|
auto lines = p.lines();
|
|
|
|
double pix_err = pixel_area(FullWhite, pd) / 2.;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
// Worst case is when a line is parallel to the shorter axis of one pixel,
|
|
|
|
// when the line will be composed of the max number of pixels
|
|
|
|
double pix_l = std::min(pd.h_mm, pd.w_mm);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
double error = 0.;
|
|
|
|
for (auto &l : lines)
|
|
|
|
error += (unscaled(l.length()) / pix_l) * pix_err;
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
TEST_CASE("RasterizedPolygonAreaShouldMatch", "[SLARasterOutput]") {
|
2019-10-01 11:27:58 +00:00
|
|
|
double disp_w = 120., disp_h = 68.;
|
|
|
|
sla::Raster::Resolution res{2560, 1440};
|
|
|
|
sla::Raster::PixelDim pixdim{disp_w / res.width_px, disp_h / res.height_px};
|
|
|
|
|
|
|
|
sla::Raster raster{res, pixdim};
|
|
|
|
auto bb = BoundingBox({0, 0}, {scaled(disp_w), scaled(disp_h)});
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
ExPolygon poly = square_with_hole(10.);
|
|
|
|
poly.translate(bb.center().x(), bb.center().y());
|
|
|
|
raster.draw(poly);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
double a = poly.area() / (scaled<double>(1.) * scaled(1.));
|
|
|
|
double ra = raster_white_area(raster);
|
|
|
|
double diff = std::abs(a - ra);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(diff <= predict_error(poly, pixdim));
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
raster.clear();
|
|
|
|
poly = square_with_hole(60.);
|
|
|
|
poly.translate(bb.center().x(), bb.center().y());
|
|
|
|
raster.draw(poly);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-01 11:27:58 +00:00
|
|
|
a = poly.area() / (scaled<double>(1.) * scaled(1.));
|
|
|
|
ra = raster_white_area(raster);
|
|
|
|
diff = std::abs(a - ra);
|
2019-10-02 12:55:02 +00:00
|
|
|
|
2019-10-04 12:47:02 +00:00
|
|
|
REQUIRE(diff <= predict_error(poly, pixdim));
|
2019-09-24 13:24:47 +00:00
|
|
|
}
|