Merge branch 'master' into fs_emboss

This commit is contained in:
Filip Sykala 2022-05-11 16:05:40 +02:00
commit 34304a40bf
42 changed files with 1155 additions and 695 deletions

View file

@ -4,6 +4,7 @@ add_executable(${_TEST_NAME}_tests
${_TEST_NAME}_tests.cpp
test_3mf.cpp
test_aabbindirect.cpp
test_kdtreeindirect.cpp
test_clipper_offset.cpp
test_clipper_utils.cpp
test_color.cpp
@ -28,6 +29,7 @@ add_executable(${_TEST_NAME}_tests
test_triangulation.cpp
test_emboss.cpp
test_indexed_triangle_set.cpp
test_astar.cpp
../libnest2d/printer_parts.cpp
)

View file

@ -0,0 +1,71 @@
#include <catch2/catch.hpp>
#include "libslic3r/BoundingBox.hpp"
#include "libslic3r/AStar.hpp"
#include "libslic3r/Execution/ExecutionSeq.hpp"
#include "libslic3r/PointGrid.hpp"
using namespace Slic3r;
struct PointGridTracer {
using Node = size_t;
const PointGrid<float> &grid;
size_t final;
PointGridTracer(const PointGrid<float> &g, size_t goal) :
grid{g}, final{goal} {}
template<class Fn>
void foreach_reachable(size_t from, Fn &&fn) const
{
Vec3i from_crd = grid.get_coord(from);
REQUIRE(grid.get_idx(from_crd) == from);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 1, 0, 0}); i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 0, 1, 0}); i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 0, 0, 1}); i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 1, 1, 0}); i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 0, 1, 1}); i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 1, 1, 1}); i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{-1, 0, 0}); from_crd.x() > 0 && i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 0, -1, 0}); from_crd.y() > 0 && i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 0, 0, -1}); from_crd.z() > 0 && i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{-1, -1, 0}); from_crd.x() > 0 && from_crd.y() > 0 && i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{ 0, -1, -1}); from_crd.y() > 0 && from_crd.z() && i < grid.point_count()) fn(i);
if (size_t i = grid.get_idx(from_crd + Vec3i{-1, -1, -1}); from_crd.x() > 0 && from_crd.y() > 0 && from_crd.z() && i < grid.point_count()) fn(i);
}
float distance(size_t a, size_t b) const
{
return (grid.get(a) - grid.get(b)).squaredNorm();
}
float goal_heuristic(size_t n) const
{
return n == final ? -1.f : (grid.get(n) - grid.get(final)).squaredNorm();
}
size_t unique_id(size_t n) const { return n; }
};
TEST_CASE("astar algorithm test over 3D point grid", "[AStar]") {
auto vol = BoundingBox3Base<Vec3f>{{0.f, 0.f, 0.f}, {1.f, 1.f, 1.f}};
auto pgrid = point_grid(ex_seq, vol, {0.1f, 0.1f, 0.1f});
size_t target = pgrid.point_count() - 1;
std::cout << "Tracing route to " << pgrid.get_coord(target).transpose() << std::endl;
PointGridTracer pgt{pgrid, pgrid.point_count() - 1};
std::vector<size_t> out;
bool found = astar::search_route(pgt, size_t(0), std::back_inserter(out));
std::cout << "Route taken: ";
for (size_t i : out) {
std::cout << "(" << pgrid.get_coord(i).transpose() << ") ";
}
std::cout << std::endl;
REQUIRE(found);
}

View file

@ -0,0 +1,142 @@
#include <catch2/catch.hpp>
#include "libslic3r/KDTreeIndirect.hpp"
#include "libslic3r/Execution/ExecutionSeq.hpp"
#include "libslic3r/BoundingBox.hpp"
#include "libslic3r/PointGrid.hpp"
using namespace Slic3r;
//template<class G>
//struct Within { // Wrapper for the `within` predicate that counts calls.
// kdtree::Within<G> pred;
// Within(G box): pred{box} {}
// // Number of times the predicate was called
// mutable size_t call_count = 0;
// std::pair<bool, unsigned int> operator() (const Vec3f &p, size_t dim)
// {
// ++call_count;
// return pred(p, dim);
// }
//};
static double volume(const BoundingBox3Base<Vec3f> &box)
{
auto sz = box.size();
return sz.x() * sz.y() * sz.z();
}
static double volume(const Eigen::AlignedBox<float, 3> &box)
{
return box.volume();
}
TEST_CASE("Test kdtree query for a Box", "[KDTreeIndirect]")
{
auto vol = BoundingBox3Base<Vec3f>{{0.f, 0.f, 0.f}, {10.f, 10.f, 10.f}};
auto pgrid = point_grid(ex_seq, vol, Vec3f{0.1f, 0.1f, 0.1f});
REQUIRE(!pgrid.empty());
auto coordfn = [&pgrid] (size_t i, size_t D) { return pgrid.get(i)(int(D)); };
KDTreeIndirect<3, float, decltype(coordfn)> tree{coordfn, pgrid.point_count()};
std::vector<size_t> out;
auto qbox = BoundingBox3Base{Vec3f{0.f, 0.f, 0.f}, Vec3f{.5f, .5f, .5f}};
size_t call_count = 0;
out = find_nearby_points(tree, qbox.min, qbox.max, [&call_count](size_t) {
call_count++;
return true;
});
// Output shall be non-empty
REQUIRE(!out.empty());
std::sort(out.begin(), out.end());
// No duplicates allowed in the output
auto it = std::unique(out.begin(), out.end());
REQUIRE(it == out.end());
// Test if inside points are in the output and outside points are not.
bool succ = true;
for (size_t i = 0; i < pgrid.point_count(); ++i) {
auto foundit = std::find(out.begin(), out.end(), i);
bool contains = qbox.contains(pgrid.get(i));
succ = succ && contains ? foundit != out.end() : foundit == out.end();
if (!succ) {
std::cout << "invalid point: " << i << " " << pgrid.get(i).transpose()
<< std::endl;
break;
}
}
REQUIRE(succ);
// Test for the expected cost of the query.
double gridvolume = volume(vol);
double queryvolume = volume(qbox);
double volratio = (queryvolume / gridvolume);
REQUIRE(call_count < 3 * volratio * pgrid.point_count());
REQUIRE(call_count < pgrid.point_count());
}
//TEST_CASE("Test kdtree query for a Sphere", "[KDTreeIndirect]") {
// auto vol = BoundingBox3Base<Vec3f>{{0.f, 0.f, 0.f}, {10.f, 10.f, 10.f}};
// auto pgrid = point_grid(ex_seq, vol, Vec3f{0.1f, 0.1f, 0.1f});
// REQUIRE(!pgrid.empty());
// auto coordfn = [&pgrid] (size_t i, size_t D) { return pgrid.get(i)(int(D)); };
// kdtree::KDTreeIndirect<3, float, decltype(coordfn)> tree{coordfn, pgrid.point_count()};
// std::vector<size_t> out;
// auto querysphere = kdtree::Sphere{Vec3f{5.f, 5.f, 5.f}, 2.f};
// auto pred = Within(querysphere);
// kdtree::query(tree, pred, std::back_inserter(out));
// // Output shall be non-empty
// REQUIRE(!out.empty());
// std::sort(out.begin(), out.end());
// // No duplicates allowed in the output
// auto it = std::unique(out.begin(), out.end());
// REQUIRE(it == out.end());
// // Test if inside points are in the output and outside points are not.
// bool succ = true;
// for (size_t i = 0; i < pgrid.point_count(); ++i) {
// auto foundit = std::find(out.begin(), out.end(), i);
// bool contains = (querysphere.center - pgrid.get(i)).squaredNorm() < pred.pred.r2;
// succ = succ && contains ? foundit != out.end() : foundit == out.end();
// if (!succ) {
// std::cout << "invalid point: " << i << " " << pgrid.get(i).transpose()
// << std::endl;
// break;
// }
// }
// REQUIRE(succ);
// // Test for the expected cost of the query.
// double gridvolume = volume(vol);
// double queryvolume = volume(querysphere);
// double volratio = (queryvolume / gridvolume);
// REQUIRE(pred.call_count < 3 * volratio * pgrid.point_count());
// REQUIRE(pred.call_count < pgrid.point_count());
//}

View file

@ -343,7 +343,7 @@ TEST_CASE("Mutable priority queue - reshedule first", "[MutableSkipHeapPriorityQ
TEST_CASE("Mutable priority queue - first pop", "[MutableSkipHeapPriorityQueue]")
{
struct MyValue{
int id;
size_t id;
float val;
};
size_t count = 50000;
@ -356,15 +356,15 @@ TEST_CASE("Mutable priority queue - first pop", "[MutableSkipHeapPriorityQueue]"
[](MyValue &l, MyValue &r) { return l.val < r.val; });
q.reserve(count);
for (size_t id = 0; id < count; id++) {
MyValue mv;
mv.id = id;
mv.val = rand();
MyValue mv{ id, rand() / 100.f };
q.push(mv);
}
MyValue it = q.top(); // copy
q.pop();
bool valid = (it.id != 0) && (idxs[0] < 3 * count);
CHECK(valid);
// is valid id (no initial value default value)
CHECK(it.id != 0);
// is first item in queue valid value
CHECK(idxs[0] != std::numeric_limits<size_t>::max());
}
TEST_CASE("Mutable priority queue complex", "[MutableSkipHeapPriorityQueue]")