PrusaSlicer-NonPlainar/tests/libslic3r/test_mutable_polygon.cpp
Vojtech Bubnik 5276bd98d7 WIP: MutablePolygon - linked list based polygon implementation
allowing rapid insertion and removal of points.
WIP: porting smooth_outward() from Cura.
2021-03-01 18:41:46 +01:00

146 lines
5.5 KiB
C++

#include <catch2/catch.hpp>
#include "libslic3r/MutablePolygon.hpp"
using namespace Slic3r;
SCENARIO("Iterators", "[MutablePolygon]") {
GIVEN("Polygon with three points") {
Slic3r::MutablePolygon p({ { 0, 0 }, { 0, 1 }, { 1, 0 } });
WHEN("Iterating upwards") {
auto begin = p.begin();
auto end = p.end();
auto it = begin;
THEN("++ it is not equal to begin") {
REQUIRE(++ it != begin);
} THEN("++ it is not equal to end") {
REQUIRE(++ it != end);
} THEN("++ (++ it) is not equal to begin") {
REQUIRE(++ (++ it) != begin);
} THEN("++ (++ it) is equal to end") {
REQUIRE(++ (++ it) == end);
} THEN("++ (++ (++ it)) is equal to begin") {
REQUIRE(++ (++ (++ it)) == begin);
} THEN("++ (++ (++ it)) is not equal to end") {
REQUIRE(++ (++ (++ it)) != end);
}
}
WHEN("Iterating downwards") {
auto begin = p.begin();
auto end = p.end();
auto it = begin;
THEN("-- it is not equal to begin") {
REQUIRE(-- it != begin);
} THEN("-- it is equal to end") {
REQUIRE(-- it == end);
} THEN("-- (-- it) is not equal to begin") {
REQUIRE(-- (-- it) != begin);
} THEN("-- (-- it) is not equal to end") {
REQUIRE(-- (-- it) != end);
} THEN("-- (-- (-- it)) is equal to begin") {
REQUIRE(-- (-- (-- it)) == begin);
} THEN("-- (-- (-- it)) is not equal to end") {
REQUIRE(-- (-- (-- it)) != end);
}
}
WHEN("Deleting 1st point") {
auto it_2nd = p.begin().next();
auto it_3rd = p.end();
auto it = p.begin().remove();
THEN("Size is 2") {
REQUIRE(p.size() == 2);
} THEN("p.begin().remove() == it_2nd") {
REQUIRE(it == it_2nd);
} THEN("it_2nd == new begin()") {
REQUIRE(it_2nd == p.begin());
}
}
WHEN("Deleting 2nd point") {
auto it_1st = p.begin();
auto it_2nd = it_1st.next();
auto it_3rd = p.end();
auto it = it_2nd.remove();
THEN("Size is 2") {
REQUIRE(p.size() == 2);
REQUIRE(! p.empty());
} THEN("it_2nd.remove() == it_3rd") {
REQUIRE(it == it_2nd);
} THEN("it_1st == new begin()") {
REQUIRE(it_1st == p.begin());
}
}
WHEN("Deleting two points") {
p.begin().remove().remove();
THEN("Size is 1") {
REQUIRE(p.size() == 1);
} THEN("p.begin().next() == p.begin()") {
REQUIRE(p.begin().next() == p.begin());
} THEN("p.begin().prev() == p.begin()") {
REQUIRE(p.begin().prev() == p.begin());
}
}
WHEN("Deleting all points") {
auto it = p.begin().remove().remove().remove();
THEN("Size is 0") {
REQUIRE(p.size() == 0);
REQUIRE(p.empty());
} THEN("! p.begin().valid()") {
REQUIRE(!p.begin().valid());
} THEN("last iterator not valid") {
REQUIRE(! it.valid());
}
}
WHEN("Inserting a point at the beginning") {
p.insert(p.begin(), { 3, 4 });
THEN("Polygon content is ok") {
REQUIRE(p == MutablePolygon{ { 0, 0 }, { 0, 1 }, { 1, 0 }, { 3, 4 } });
}
}
WHEN("Inserting a point at the 2nd position") {
p.insert(++ p.begin(), { 3, 4 });
THEN("Polygon content is ok") {
REQUIRE(p == MutablePolygon{ { 0, 0 }, { 3, 4 }, { 0, 1 }, { 1, 0 } });
}
} WHEN("Inserting a point after a point was removed") {
size_t capacity = p.capacity();
THEN("Initial capacity is 3") {
REQUIRE(capacity == 3);
}
p.begin().remove();
THEN("After removal of the 1st point the capacity is still 3") {
REQUIRE(p.capacity() == 3);
}
THEN("After removal of the 1st point the content is ok") {
REQUIRE(p == MutablePolygon{ { 0, 1 }, { 1, 0 } });
}
p.insert(p.begin(), { 5, 6 });
THEN("After insertion at head position the polygon content is ok") {
REQUIRE(p == MutablePolygon{ { 0, 1 }, { 1, 0 }, { 5, 6 } });
} THEN("and the capacity is still 3") {
REQUIRE(p.capacity() == 3);
}
}
}
}
SCENARIO("Remove degenerate points from MutablePolygon", "[MutablePolygon]") {
GIVEN("Polygon with duplicate points"){
Slic3r::MutablePolygon p({
{ 0, 0 },
{ 0, 100 }, { 0, 100 }, { 0, 100 },
{ 0, 150 },
{ 0, 200 },
{ 200, 200 },
{ 180, 200 }, { 180, 200 },
{ 180, 20 },
{ 180, 0 },
});
WHEN("Duplicate points are removed") {
remove_duplicates(p);
THEN("Polygon content is ok") {
REQUIRE(p == Slic3r::MutablePolygon{ { 0, 0 }, { 0, 100 }, { 0, 150 }, { 0, 200 }, { 200, 200 }, { 180, 200 }, { 180, 20 }, { 180, 0 } });
}
}
}
}