mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-05 04:42:05 +00:00
Adds a Planar mode to the fill tool of color, support, fuzzy skin and painted-modifier painting. It fills every face parallel to the clicked one and within the plane offset (0 to 10 mm) of its plane, connected or not. "Include connected faces" limits the fill to the parallel faces connected to the clicked one, the faces between them, and the parallel faces they enclose. In color painting, the "Edge detection" checkbox is replaced by a Connected / Smart / Planar switch. Connected is the old unchecked box, and Smart, the default, is the old checked box. Ctrl + mouse wheel changes the plane offset in Planar mode and zooms in Connected mode.
645 lines
29 KiB
C++
645 lines
29 KiB
C++
#include <catch2/catch_all.hpp>
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/catch_message.hpp>
|
|
#include <catch2/generators/catch_generators.hpp>
|
|
#include "libslic3r/TriangleSelector.hpp"
|
|
#include "libslic3r/TriangleMesh.hpp"
|
|
#include "libslic3r/Geometry.hpp"
|
|
#include "libslic3r/Point.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cmath>
|
|
#include "libslic3r/libslic3r.h"
|
|
#include "libslic3r/Point.hpp"
|
|
#include <vector>
|
|
#include <initializer_list>
|
|
#include <memory>
|
|
#include <cstddef>
|
|
#include <string>
|
|
|
|
using namespace Slic3r;
|
|
|
|
// A sphere gives well over ExtruderMax original facets, so every extruder state can be assigned
|
|
// to a facet of its own without any splitting getting in the way.
|
|
static TriangleMesh test_mesh() { return make_sphere(5., 2 * PI / 24); }
|
|
|
|
// Read the nibble_idx-th 4-bit group of a serialized bitstream, least significant bit first.
|
|
static int nibble_at(const std::vector<bool> &bitstream, size_t nibble_idx)
|
|
{
|
|
int n = 0;
|
|
for (size_t bit = 0; bit < 4; ++bit)
|
|
n |= int(bitstream[nibble_idx * 4 + bit]) << bit;
|
|
return n;
|
|
}
|
|
|
|
TEST_CASE("Every extruder state survives a serialize/deserialize round trip", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = test_mesh();
|
|
const int max_state = int(EnforcerBlockerType::ExtruderMax);
|
|
REQUIRE(int(mesh.its.indices.size()) >= max_state);
|
|
|
|
TriangleSelector selector(mesh);
|
|
for (int state = 1; state <= max_state; ++state)
|
|
selector.set_facet(state - 1, EnforcerBlockerType(state));
|
|
|
|
TriangleSelector restored(mesh);
|
|
restored.deserialize(selector.serialize());
|
|
|
|
for (int state = 1; state <= max_state; ++state) {
|
|
INFO("Extruder " << state);
|
|
REQUIRE(restored.has_facets(EnforcerBlockerType(state)));
|
|
REQUIRE(restored.num_facets(EnforcerBlockerType(state)) == 1);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Serialized data reports the extruder states it uses", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = test_mesh();
|
|
TriangleSelector selector(mesh);
|
|
selector.set_facet(0, EnforcerBlockerType::Extruder16);
|
|
selector.set_facet(1, EnforcerBlockerType::Extruder32);
|
|
|
|
const TriangleSelector::TriangleSplittingData data = selector.serialize();
|
|
|
|
REQUIRE(data.used_states.size() == size_t(EnforcerBlockerType::ExtruderMax) + 1);
|
|
REQUIRE(data.used_states[size_t(EnforcerBlockerType::Extruder16)]);
|
|
REQUIRE(data.used_states[size_t(EnforcerBlockerType::Extruder32)]);
|
|
REQUIRE_FALSE(data.used_states[size_t(EnforcerBlockerType::Extruder17)]);
|
|
|
|
SECTION("used_states recomputed from the bitstream agrees") {
|
|
TriangleSelector::TriangleSplittingData recomputed = data;
|
|
recomputed.reset_used_states();
|
|
recomputed.update_used_states(0);
|
|
REQUIRE(recomputed.used_states == data.used_states);
|
|
}
|
|
|
|
SECTION("has_facets on the raw data agrees") {
|
|
REQUIRE(TriangleSelector::has_facets(data, EnforcerBlockerType::Extruder32));
|
|
REQUIRE_FALSE(TriangleSelector::has_facets(data, EnforcerBlockerType::Extruder17));
|
|
}
|
|
}
|
|
|
|
// States 3..17 must keep the pre-existing encoding ("11" prefix plus one nibble of state-3) so
|
|
// projects written by older builds stay readable and newly written ones stay readable by them.
|
|
TEST_CASE("Extruder states up to 17 keep the single-nibble encoding", "[TriangleSelector]")
|
|
{
|
|
const int state = GENERATE(3, 8, 16, 17);
|
|
|
|
TriangleSelector selector(test_mesh());
|
|
selector.set_facet(0, EnforcerBlockerType(state));
|
|
const std::vector<bool> bitstream = selector.serialize().bitstream;
|
|
|
|
INFO("Extruder " << state);
|
|
// Two nibbles: the "11"-prefixed leaf code, then the state itself.
|
|
REQUIRE(bitstream.size() == 8);
|
|
REQUIRE(nibble_at(bitstream, 0) == 0b1100);
|
|
REQUIRE(nibble_at(bitstream, 1) == state - 3);
|
|
}
|
|
|
|
// States 18 and above set the state nibble to 0b1111 and carry (state-18) in one more nibble.
|
|
TEST_CASE("Extruder states above 17 are encoded in a second nibble", "[TriangleSelector]")
|
|
{
|
|
const int state = GENERATE(18, 25, 32);
|
|
|
|
TriangleSelector selector(test_mesh());
|
|
selector.set_facet(0, EnforcerBlockerType(state));
|
|
const std::vector<bool> bitstream = selector.serialize().bitstream;
|
|
|
|
INFO("Extruder " << state);
|
|
REQUIRE(bitstream.size() == 12);
|
|
REQUIRE(nibble_at(bitstream, 0) == 0b1100);
|
|
REQUIRE(nibble_at(bitstream, 1) == 0b1111);
|
|
REQUIRE(nibble_at(bitstream, 2) == state - 18);
|
|
}
|
|
|
|
// Model.cpp writes these hex strings into the 3MF for colored mesh imports; the selector must
|
|
// decode exactly the states CONST_FILAMENTS assigns to them.
|
|
TEST_CASE("Extruder states match the CONST_FILAMENTS hex encoding", "[TriangleSelector]")
|
|
{
|
|
struct Case { const char *hex; int state; };
|
|
const auto c = GENERATE(values<Case>({
|
|
{"8", 2}, {"0C", 3}, {"DC", 16}, {"EC", 17}, {"0FC", 18}, {"EFC", 32},
|
|
}));
|
|
|
|
// get_triangle_as_string emits the nibbles most significant first, so read the hex backwards.
|
|
const std::string hex = c.hex;
|
|
std::vector<bool> bitstream;
|
|
for (auto it = hex.rbegin(); it != hex.rend(); ++it) {
|
|
const int nibble = *it >= 'A' ? (*it - 'A' + 10) : (*it - '0');
|
|
for (int bit = 0; bit < 4; ++bit)
|
|
bitstream.push_back((nibble >> bit) & 1);
|
|
}
|
|
|
|
TriangleSelector::TriangleSplittingData data;
|
|
data.triangles_to_split.emplace_back(0, 0);
|
|
data.bitstream = bitstream;
|
|
|
|
INFO("Hex " << c.hex << " -> extruder " << c.state);
|
|
REQUIRE(TriangleSelector::has_facets(data, EnforcerBlockerType(c.state)));
|
|
}
|
|
|
|
// Pack 4-bit codes into a bitstream, least significant bit first, in the order the decoder reads them.
|
|
static std::vector<bool> pack_nibbles(const std::vector<int> &nibbles)
|
|
{
|
|
std::vector<bool> bitstream;
|
|
for (const int nibble : nibbles)
|
|
for (int bit = 0; bit < 4; ++bit)
|
|
bitstream.push_back((nibble >> bit) & 1);
|
|
return bitstream;
|
|
}
|
|
|
|
TEST_CASE("A valid paint stream with nested splits round-trips bit for bit", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = test_mesh();
|
|
|
|
TriangleSelector::TriangleSplittingData data;
|
|
data.triangles_to_split.emplace_back(0, 0);
|
|
// A three-side split whose children, in stream order, are: a one-side split (side 2) into two
|
|
// leaves, a two-side split (side 1) into leaves of states 20, 0 and 8, then two plain leaves.
|
|
const std::vector<int> triangle_0 = {0b0011,
|
|
0b1001, 0b1000, 0b0100,
|
|
0b0110, 0b1100, 0b1111, 20 - 18, 0b0000, 0b1100, 8 - 3,
|
|
0b1000,
|
|
0b0100};
|
|
data.bitstream = pack_nibbles(triangle_0);
|
|
data.triangles_to_split.emplace_back(5, int(data.bitstream.size()));
|
|
const std::vector<bool> triangle_5 = pack_nibbles({0b1100, 3 - 3});
|
|
data.bitstream.insert(data.bitstream.end(), triangle_5.begin(), triangle_5.end());
|
|
data.reset_used_states();
|
|
REQUIRE(data.update_used_states(0));
|
|
|
|
TriangleSelector restored(mesh);
|
|
restored.deserialize(data);
|
|
|
|
REQUIRE(restored.num_facets(EnforcerBlockerType::Extruder20) == 1);
|
|
REQUIRE(restored.num_facets(EnforcerBlockerType::Extruder3) == 1);
|
|
REQUIRE(restored.serialize() == data);
|
|
}
|
|
|
|
TEST_CASE("A truncated or malformed paint stream drops only the damaged triangle", "[TriangleSelector][Regression]")
|
|
{
|
|
struct Case { const char *name; std::vector<int> nibbles; };
|
|
const auto c = GENERATE(values<Case>({
|
|
{"three-side split missing two children", {0b0011, 0b1000, 0b1000}},
|
|
{"leaf missing its state nibble", {0b1100}},
|
|
{"leaf missing its second state nibble", {0b1100, 0b1111}},
|
|
{"splits nested past the end", {0b0011, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF,
|
|
0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF, 0xF}},
|
|
{"one-side split of the nonexistent side 3", {0b1101, 0b1000, 0b1000}},
|
|
}));
|
|
INFO(c.name);
|
|
|
|
const TriangleMesh mesh = test_mesh();
|
|
TriangleSelector intact(mesh);
|
|
intact.set_facet(0, EnforcerBlockerType::Extruder2);
|
|
|
|
// Triangle 0 stays intact, triangle 1 carries the damaged stream.
|
|
TriangleSelector::TriangleSplittingData data = intact.serialize();
|
|
data.triangles_to_split.emplace_back(1, int(data.bitstream.size()));
|
|
const std::vector<bool> damaged = pack_nibbles(c.nibbles);
|
|
data.bitstream.insert(data.bitstream.end(), damaged.begin(), damaged.end());
|
|
|
|
TriangleSelector restored(mesh);
|
|
REQUIRE_NOTHROW(restored.deserialize(data));
|
|
// Triangle 1 unwinds completely, so the selector holds exactly the intact paint.
|
|
REQUIRE(restored.serialize() == intact.serialize());
|
|
|
|
REQUIRE_NOTHROW(TriangleSelector::has_facets(data, EnforcerBlockerType::Extruder3));
|
|
|
|
TriangleSelector::TriangleSplittingData recomputed = data;
|
|
recomputed.reset_used_states();
|
|
REQUIRE_FALSE(recomputed.update_used_states(0));
|
|
REQUIRE(std::none_of(recomputed.used_states.begin(), recomputed.used_states.end(), [](bool used) { return used; }));
|
|
}
|
|
|
|
// Adds rectangles of two facets each to a mesh. Faces that meet share their vertices, which is what makes them neighbors.
|
|
struct PlanarFillMeshBuilder
|
|
{
|
|
indexed_triangle_set its;
|
|
|
|
int vertex(const Vec3f &v)
|
|
{
|
|
const auto it = std::find(its.vertices.begin(), its.vertices.end(), v);
|
|
if (it != its.vertices.end())
|
|
return int(it - its.vertices.begin());
|
|
its.vertices.emplace_back(v);
|
|
return int(its.vertices.size()) - 1;
|
|
}
|
|
|
|
void add_face(const Vec3f &a, const Vec3f &b, const Vec3f &c, const Vec3f &d)
|
|
{
|
|
its.indices.emplace_back(vertex(a), vertex(b), vertex(c));
|
|
its.indices.emplace_back(vertex(a), vertex(c), vertex(d));
|
|
}
|
|
};
|
|
|
|
// Faces of the steps test part, in the order planar_fill_steps() adds them. Each is a 10 x 10 rectangle.
|
|
// Slope, Top10, Wall and Top11 share their edges in that order; the other faces touch nothing.
|
|
enum PlanarFillFace {
|
|
Top10, // facing up at z = 10
|
|
Top10Apart, // facing up at z = 10, not connected to Top10
|
|
Top11, // facing up at z = 11
|
|
Top12, // facing up at z = 12
|
|
Wall, // vertical, from Top10 up to Top11
|
|
Slope, // rising from Top10 to z = 10.5, touching no other face
|
|
};
|
|
|
|
// The part can be lowered, as centering it on load does. Its heights are then no longer exact as floats.
|
|
static TriangleMesh planar_fill_steps(double lowered_by = 0.)
|
|
{
|
|
PlanarFillMeshBuilder mesh;
|
|
auto z = [lowered_by](double height) { return float(height - lowered_by); };
|
|
auto add_top = [&mesh](float x, float z) { mesh.add_face({x, 0, z}, {x + 10, 0, z}, {x + 10, 10, z}, {x, 10, z}); };
|
|
add_top(0, z(10));
|
|
add_top(30, z(10));
|
|
add_top(10, z(11));
|
|
add_top(50, z(12));
|
|
mesh.add_face({10, 0, z(10)}, {10, 0, z(11)}, {10, 10, z(11)}, {10, 10, z(10)});
|
|
mesh.add_face({0, 0, z(10)}, {0, 10, z(10)}, {-10, 10, z(10.5)}, {-10, 0, z(10.5)});
|
|
return TriangleMesh(mesh.its);
|
|
}
|
|
|
|
// Faces of the plate test part, in the order planar_fill_plate() adds them: a 30 x 30 plate with its top at z = 10 and
|
|
// a square groove, 1 deep, around an island, like a debossed "0". All but Island and Shelf are bands of four rectangles.
|
|
enum PlanarFillPlateFace {
|
|
PlateTop, // facing up at z = 10, around the groove
|
|
GrooveWall, // from PlateTop down to GrooveFloor
|
|
GrooveFloor, // facing up at z = 9, around the island
|
|
IslandWall, // from GrooveFloor up to Island
|
|
Island, // facing up at z = 10, inside the groove
|
|
PlateSide, // from PlateTop down to z = 0
|
|
Trench, // one rectangle, from the bottom of a PlateSide up to Shelf
|
|
Shelf, // one rectangle, facing up at z = 10 next to the plate
|
|
};
|
|
|
|
static TriangleMesh planar_fill_plate()
|
|
{
|
|
PlanarFillMeshBuilder mesh;
|
|
// Corners of a square around the middle of the plate, counter-clockwise seen from above.
|
|
auto square = [](float half_size, float z) {
|
|
return std::array<Vec3f, 4>{Vec3f(15 - half_size, 15 - half_size, z), Vec3f(15 + half_size, 15 - half_size, z),
|
|
Vec3f(15 + half_size, 15 + half_size, z), Vec3f(15 - half_size, 15 + half_size, z)};
|
|
};
|
|
auto add_band = [&mesh](const std::array<Vec3f, 4> &from, const std::array<Vec3f, 4> &to) {
|
|
for (int i = 0; i < 4; ++i)
|
|
mesh.add_face(from[i], from[(i + 1) % 4], to[(i + 1) % 4], to[i]);
|
|
};
|
|
const std::array<Vec3f, 4> plate = square(15, 10), groove = square(5, 10), floor = square(5, 9), foot = square(2, 9), island = square(2, 10), bottom = square(15, 0);
|
|
add_band(plate, groove);
|
|
add_band(groove, floor);
|
|
add_band(floor, foot);
|
|
add_band(foot, island);
|
|
mesh.add_face(island[0], island[1], island[2], island[3]);
|
|
for (int i = 0; i < 4; ++i)
|
|
mesh.add_face(plate[(i + 1) % 4], plate[i], bottom[i], bottom[(i + 1) % 4]);
|
|
mesh.add_face(bottom[1], bottom[0], {0, -5, 10}, {30, -5, 10});
|
|
mesh.add_face({30, -5, 10}, {0, -5, 10}, {0, -15, 10}, {30, -15, 10});
|
|
return TriangleMesh(mesh.its);
|
|
}
|
|
|
|
// Faces of the ramp test part, in the order planar_fill_ramp() adds them: three segments, each 20 x 20, rising at 20, 30 and
|
|
// 45 degrees one after the other. The middle and the right one have a square pocket, 1 deep, like a debossed mark.
|
|
enum PlanarFillRampFace {
|
|
LeftSegment, // one rectangle
|
|
MiddleSegment, // band of four rectangles around the pocket
|
|
MiddlePocketWall, // band of four rectangles, from MiddleSegment down to MiddlePocketFloor
|
|
MiddlePocketFloor, // one rectangle, parallel to MiddleSegment
|
|
RightSegment, // as the middle one
|
|
RightPocketWall,
|
|
RightPocketFloor,
|
|
};
|
|
|
|
static TriangleMesh planar_fill_ramp()
|
|
{
|
|
PlanarFillMeshBuilder mesh;
|
|
// The segment from one line across the ramp to the next, both given by their (x, z). Returns the end of the segment.
|
|
auto add_segment = [&mesh](const Vec2f &from, float slope_deg, bool pocket) {
|
|
const Vec2f dir(std::cos(Geometry::deg2rad(slope_deg)), std::sin(Geometry::deg2rad(slope_deg)));
|
|
const Vec2f to = from + 20.f * dir;
|
|
const std::array<Vec3f, 4> outline = {Vec3f(from.x(), 0, from.y()), Vec3f(to.x(), 0, to.y()), Vec3f(to.x(), 20, to.y()), Vec3f(from.x(), 20, from.y())};
|
|
if (!pocket) {
|
|
mesh.add_face(outline[0], outline[1], outline[2], outline[3]);
|
|
return to;
|
|
}
|
|
// Corners of a square around the middle of the segment, in its plane or below it.
|
|
const Vec3f along(dir.x(), 0, dir.y()), across(0, 1, 0), normal = along.cross(across);
|
|
const Vec3f middle = 0.25f * (outline[0] + outline[1] + outline[2] + outline[3]);
|
|
auto square = [&](float depth) {
|
|
return std::array<Vec3f, 4>{middle - 4.f * along - 4.f * across - depth * normal, middle + 4.f * along - 4.f * across - depth * normal,
|
|
middle + 4.f * along + 4.f * across - depth * normal, middle - 4.f * along + 4.f * across - depth * normal};
|
|
};
|
|
auto add_band = [&mesh](const std::array<Vec3f, 4> &outer, const std::array<Vec3f, 4> &inner) {
|
|
for (int i = 0; i < 4; ++i)
|
|
mesh.add_face(outer[i], outer[(i + 1) % 4], inner[(i + 1) % 4], inner[i]);
|
|
};
|
|
const std::array<Vec3f, 4> rim = square(0.f), floor = square(1.f);
|
|
add_band(outline, rim);
|
|
add_band(rim, floor);
|
|
mesh.add_face(floor[0], floor[1], floor[2], floor[3]);
|
|
return to;
|
|
};
|
|
Vec2f at(0.f, 0.f);
|
|
at = add_segment(at, 20.f, false);
|
|
at = add_segment(at, 30.f, true);
|
|
at = add_segment(at, 45.f, true);
|
|
return TriangleMesh(mesh.its);
|
|
}
|
|
|
|
// Facets of the given faces of the steps part.
|
|
static std::vector<int> facets_of(std::initializer_list<PlanarFillFace> faces)
|
|
{
|
|
std::vector<int> facets;
|
|
for (const PlanarFillFace face : faces) {
|
|
facets.push_back(2 * face);
|
|
facets.push_back(2 * face + 1);
|
|
}
|
|
std::sort(facets.begin(), facets.end());
|
|
return facets;
|
|
}
|
|
|
|
// Facets of the given faces of a part whose face i has the facets from first_facet[i] up to first_facet[i + 1].
|
|
template<typename Face> static std::vector<int> facets_of(std::initializer_list<Face> faces, const int *first_facet)
|
|
{
|
|
std::vector<int> facets;
|
|
for (const Face face : faces)
|
|
for (int facet = first_facet[face]; facet < first_facet[face + 1]; ++facet)
|
|
facets.push_back(facet);
|
|
std::sort(facets.begin(), facets.end());
|
|
return facets;
|
|
}
|
|
|
|
// Facets of the given faces of the plate part.
|
|
static std::vector<int> facets_of(std::initializer_list<PlanarFillPlateFace> faces)
|
|
{
|
|
static const int first_facet[] = {0, 8, 16, 24, 32, 34, 42, 44, 46};
|
|
return facets_of(faces, first_facet);
|
|
}
|
|
|
|
// Facets of the given faces of the ramp part.
|
|
static std::vector<int> facets_of(std::initializer_list<PlanarFillRampFace> faces)
|
|
{
|
|
static const int first_facet[] = {0, 2, 10, 18, 20, 28, 36, 38};
|
|
return facets_of(faces, first_facet);
|
|
}
|
|
|
|
// Facets with paint on them, whole or in part.
|
|
static std::vector<int> painted_facets(const TriangleSelector &selector)
|
|
{
|
|
std::vector<int> facets;
|
|
for (const TriangleSelector::TriangleBitStreamMapping &mapping : selector.serialize().triangles_to_split)
|
|
facets.push_back(mapping.triangle_idx);
|
|
std::sort(facets.begin(), facets.end());
|
|
return facets;
|
|
}
|
|
|
|
// Planar fill preview with the cursor in the middle of a facet, as hovering it does.
|
|
static void planar_preview(TriangleSelector &selector, const TriangleMesh &mesh, int facet, float plane_offset, bool connected = false)
|
|
{
|
|
const stl_triangle_vertex_indices &indices = mesh.its.indices[facet];
|
|
const Vec3f hit = (mesh.its.vertices[indices[0]] + mesh.its.vertices[indices[1]] + mesh.its.vertices[indices[2]]) / 3.f;
|
|
selector.coplanar_select_triangles(hit, facet, Transform3d::Identity(), plane_offset, connected, 0.f, Vec3f::UnitZ());
|
|
}
|
|
|
|
// Planar fill as clicking a facet does. Returns the facets with paint on them.
|
|
static std::vector<int> planar_paint(TriangleSelector &selector, int facet, float plane_offset, bool connected = false, const Transform3d &trafo = Transform3d::Identity(),
|
|
const TriangleSelector::ClippingPlane &clp = TriangleSelector::ClippingPlane())
|
|
{
|
|
selector.coplanar_paint_triangles(facet, trafo, clp, plane_offset, connected, 0.f, Vec3f::UnitZ(), EnforcerBlockerType::ENFORCER);
|
|
return painted_facets(selector);
|
|
}
|
|
|
|
TEST_CASE("Planar fill takes the faces on the clicked plane, connected or not", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps();
|
|
TriangleSelector selector(mesh);
|
|
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 0.f) == facets_of({Top10, Top10Apart}));
|
|
}
|
|
|
|
TEST_CASE("Planar fill takes parallel faces within the plane offset", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps();
|
|
TriangleSelector selector(mesh);
|
|
|
|
SECTION("in mm") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 1.5f) == facets_of({Top10, Top10Apart, Top11}));
|
|
}
|
|
|
|
// Scaled to twice the size, Top11 is 2 mm above Top10.
|
|
Transform3d twice = Transform3d::Identity();
|
|
twice.scale(2.);
|
|
|
|
SECTION("in mm of the scaled part, too small") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 1.5f, false, twice) == facets_of({Top10, Top10Apart}));
|
|
}
|
|
|
|
SECTION("in mm of the scaled part, large enough") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 3.f, false, twice) == facets_of({Top10, Top10Apart, Top11}));
|
|
}
|
|
}
|
|
|
|
// Lowered by 6.2, the steps at 10 and 11 are at 3.8f and 4.8f, which are 1.00000024 apart.
|
|
TEST_CASE("Planar fill takes a face exactly at the plane offset, whatever the rounding", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps(6.2);
|
|
TriangleSelector selector(mesh);
|
|
REQUIRE(mesh.its.vertices[mesh.its.indices[2 * Top11][0]].z() - mesh.its.vertices[mesh.its.indices[2 * Top10][0]].z() > 1.f);
|
|
|
|
SECTION("at the offset") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 1.f) == facets_of({Top10, Top10Apart, Top11}));
|
|
}
|
|
|
|
SECTION("not with the offset one step of the slider lower") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 0.99f) == facets_of({Top10, Top10Apart}));
|
|
}
|
|
|
|
SECTION("with connected faces, the wall up to it too") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 1.f, true) == facets_of({Top10, Top11, Wall}));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Connected planar fill takes parallel faces connected to the clicked one and the faces between them", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps();
|
|
TriangleSelector selector(mesh);
|
|
|
|
SECTION("not a face on the same plane that is apart") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 0.f, true) == facets_of({Top10}));
|
|
}
|
|
|
|
SECTION("the wall between two parallel faces, not the slope that leads to none") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 1.5f, true) == facets_of({Top10, Top11, Wall}));
|
|
}
|
|
|
|
SECTION("not past a face that is out of the plane offset") {
|
|
REQUIRE(planar_paint(selector, 2 * Top10, 0.75f, true) == facets_of({Top10}));
|
|
}
|
|
|
|
SECTION("the preview shows the same faces") {
|
|
planar_preview(selector, mesh, 2 * Top10, 1.5f, true);
|
|
selector.seed_fill_apply_on_triangles(EnforcerBlockerType::ENFORCER);
|
|
REQUIRE(painted_facets(selector) == facets_of({Top10, Top11, Wall}));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Connected planar fill takes the parallel faces it encloses", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_plate();
|
|
TriangleSelector selector(mesh);
|
|
|
|
SECTION("the island inside a groove that is out of the plane offset, not the shelf outside the plate") {
|
|
REQUIRE(planar_paint(selector, 0, 0.f, true) == facets_of({PlateTop, Island}));
|
|
}
|
|
|
|
SECTION("the groove too once it is within the plane offset") {
|
|
REQUIRE(planar_paint(selector, 0, 1.5f, true) == facets_of({PlateTop, GrooveWall, GrooveFloor, IslandWall, Island}));
|
|
}
|
|
|
|
SECTION("the plate from its island") {
|
|
REQUIRE(planar_paint(selector, facets_of({Island}).front(), 1.5f, true) == facets_of({PlateTop, GrooveWall, GrooveFloor, IslandWall, Island}));
|
|
}
|
|
|
|
SECTION("without connected, the shelf as well") {
|
|
REQUIRE(planar_paint(selector, 0, 0.f, false) == facets_of({PlateTop, Island, Shelf}));
|
|
}
|
|
|
|
// From the island the plate is neither connected nor enclosed, so the preview made on the plate must not be kept there.
|
|
SECTION("the preview follows the cursor from the plate onto the island") {
|
|
planar_preview(selector, mesh, 0, 0.f, true);
|
|
planar_preview(selector, mesh, facets_of({Island}).front(), 0.f, true);
|
|
selector.seed_fill_apply_on_triangles(EnforcerBlockerType::ENFORCER);
|
|
REQUIRE(painted_facets(selector) == facets_of({Island}));
|
|
}
|
|
}
|
|
|
|
// The segments next to the clicked one are at another angle and touch no other face parallel to it, so they lead nowhere.
|
|
TEST_CASE("Connected planar fill stays on the clicked segment of a ramp", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_ramp();
|
|
TriangleSelector selector(mesh);
|
|
const int clicked = facets_of({MiddleSegment}).front();
|
|
const float plane_offset = 7.f;
|
|
|
|
// The whole ramp is within the plane offset of the clicked segment, so it is not the offset that keeps the other segments out.
|
|
const stl_triangle_vertex_indices &corners = mesh.its.indices[clicked];
|
|
const Vec3f &corner = mesh.its.vertices[corners[0]];
|
|
const Vec3f normal = (mesh.its.vertices[corners[1]] - corner).cross(mesh.its.vertices[corners[2]] - corner).normalized();
|
|
float furthest = 0.f;
|
|
for (const Vec3f &vertex : mesh.its.vertices)
|
|
furthest = std::max(furthest, std::abs(normal.dot(vertex - corner)));
|
|
REQUIRE(furthest < plane_offset);
|
|
|
|
SECTION("with its pocket, without the segments next to it and their pocket") {
|
|
REQUIRE(planar_paint(selector, clicked, plane_offset, true) == facets_of({MiddleSegment, MiddlePocketWall, MiddlePocketFloor}));
|
|
}
|
|
|
|
SECTION("the preview shows the same faces") {
|
|
planar_preview(selector, mesh, clicked, plane_offset, true);
|
|
selector.seed_fill_apply_on_triangles(EnforcerBlockerType::ENFORCER);
|
|
REQUIRE(painted_facets(selector) == facets_of({MiddleSegment, MiddlePocketWall, MiddlePocketFloor}));
|
|
}
|
|
|
|
SECTION("without connected, the segment and the floor of its pocket") {
|
|
REQUIRE(planar_paint(selector, clicked, plane_offset, false) == facets_of({MiddleSegment, MiddlePocketFloor}));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Planar fill on overhangs only takes overhanging faces", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps();
|
|
TriangleSelector selector(mesh);
|
|
const TriangleSelector::ClippingPlane no_clp;
|
|
|
|
SECTION("nothing from a face that is not an overhang") {
|
|
selector.coplanar_paint_triangles(2 * Top10, Transform3d::Identity(), no_clp, 1.5f, true, 45.f, Vec3f::UnitZ(), EnforcerBlockerType::ENFORCER);
|
|
REQUIRE(painted_facets(selector).empty());
|
|
}
|
|
|
|
// Upside down the tops are overhangs. The wall is not, so it does not lead to Top11.
|
|
SECTION("each face at another angle is tested") {
|
|
selector.coplanar_paint_triangles(2 * Top10, Transform3d::Identity(), no_clp, 1.5f, true, 45.f, -Vec3f::UnitZ(), EnforcerBlockerType::ENFORCER);
|
|
REQUIRE(painted_facets(selector) == facets_of({Top10}));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Planar fill paints only what the section view shows", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps();
|
|
TriangleSelector selector(mesh);
|
|
|
|
// Everything at x > 5 is hidden: half of Top10, and all of Top10Apart.
|
|
const TriangleSelector::ClippingPlane clp({1.f, 0.f, 0.f, 5.f});
|
|
REQUIRE(planar_paint(selector, 2 * Top10 + 1, 0.f, false, Transform3d::Identity(), clp) == facets_of({Top10}));
|
|
|
|
// The visible half of Top10 has an area of 50, less what the triangles along the cut leave out.
|
|
const indexed_triangle_set painted = selector.get_facets(EnforcerBlockerType::ENFORCER);
|
|
double area = 0.;
|
|
for (const stl_triangle_vertex_indices &indices : painted.indices) {
|
|
const Vec3f &a = painted.vertices[indices[0]], &b = painted.vertices[indices[1]], &c = painted.vertices[indices[2]];
|
|
REQUIRE(std::max({a.x(), b.x(), c.x()}) <= 5.001f);
|
|
area += 0.5 * double((b - a).cross(c - a).norm());
|
|
}
|
|
REQUIRE(area > 48.);
|
|
}
|
|
|
|
TEST_CASE("Planar fill previews the whole fill again after painting", "[TriangleSelector]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps();
|
|
TriangleSelector selector(mesh);
|
|
|
|
// The section view hides half of Top10 and all of Top10Apart from painting, but not from the preview.
|
|
const TriangleSelector::ClippingPlane clp({1.f, 0.f, 0.f, 5.f});
|
|
planar_paint(selector, 2 * Top10 + 1, 0.f, false, Transform3d::Identity(), clp);
|
|
selector.seed_fill_apply_on_triangles(EnforcerBlockerType::BLOCKER);
|
|
REQUIRE(painted_facets(selector) == facets_of({Top10, Top10Apart}));
|
|
REQUIRE(selector.num_facets(EnforcerBlockerType::ENFORCER) == 0);
|
|
}
|
|
|
|
// With a plane offset, a previewed facet of another plane gives a different selection than the previewed one.
|
|
TEST_CASE("Planar fill preview follows the cursor to a previewed facet of another plane", "[TriangleSelector][Regression]")
|
|
{
|
|
const TriangleMesh mesh = planar_fill_steps();
|
|
TriangleSelector selector(mesh);
|
|
|
|
planar_preview(selector, mesh, 2 * Top10, 1.5f); // previews Top10, Top10Apart and Top11
|
|
planar_preview(selector, mesh, 2 * Top11, 1.5f); // Top12 is within the offset of Top11
|
|
selector.seed_fill_apply_on_triangles(EnforcerBlockerType::ENFORCER);
|
|
REQUIRE(painted_facets(selector) == facets_of({Top10, Top10Apart, Top11, Top12}));
|
|
}
|
|
|
|
// Paints a sphere of the given radius at the middle of the top face of a cube of the given size,
|
|
// shown at the given scale, and returns how many painted triangles that left.
|
|
static size_t painted_triangles(float size, float scale, float radius_world)
|
|
{
|
|
const TriangleMesh mesh = make_cube(size, size, size);
|
|
TriangleSelector selector(mesh);
|
|
Transform3d trafo = Transform3d::Identity();
|
|
trafo.scale(double(scale));
|
|
const Vec3f center(0.5f * size, 0.5f * size, size);
|
|
int top_facet = -1;
|
|
for (int i = 0; i < int(mesh.its.indices.size()) && top_facet < 0; ++i) {
|
|
const auto &f = mesh.its.indices[i];
|
|
const Vec3f a = mesh.its.vertices[f[0]], b = mesh.its.vertices[f[1]], c = mesh.its.vertices[f[2]];
|
|
if (a.z() == size && b.z() == size && c.z() == size)
|
|
top_facet = i;
|
|
}
|
|
REQUIRE(top_facet >= 0);
|
|
selector.select_patch(top_facet,
|
|
std::make_unique<TriangleSelector::Sphere>(center, center + Vec3f(0.f, 0.f, size), radius_world, trafo,
|
|
TriangleSelector::ClippingPlane()),
|
|
EnforcerBlockerType::ENFORCER, trafo, true);
|
|
return selector.get_facets(EnforcerBlockerType::ENFORCER).indices.size();
|
|
}
|
|
|
|
TEST_CASE("A brush splits a scaled-down mesh as finely as the same mesh at full size", "[TriangleSelector][Regression]")
|
|
{
|
|
// A 1 mm cube, and a 100 mm cube shown at 1%: the same part in the world.
|
|
const size_t full_size = painted_triangles(1.f, 1.f, 0.25f);
|
|
const size_t scaled_down = painted_triangles(100.f, 0.01f, 0.25f);
|
|
CAPTURE(full_size, scaled_down);
|
|
REQUIRE(full_size > 0);
|
|
CHECK(scaled_down < 2 * full_size);
|
|
}
|