Move definitons to header in the SupportSpotsGenerator.

Moving the definitions to a header file will enable testing the
functions involved.
This commit is contained in:
Martin Šach 2023-09-25 13:20:01 +02:00 committed by SachCZ
parent ea69deef24
commit 13579fff45
3 changed files with 290 additions and 236 deletions

View File

@ -55,43 +55,26 @@
#include "libslic3r/Color.hpp"
#endif
namespace Slic3r {
namespace Slic3r::SupportSpotsGenerator {
class ExtrusionLine
{
public:
ExtrusionLine() : a(Vec2f::Zero()), b(Vec2f::Zero()), len(0.0), origin_entity(nullptr) {}
ExtrusionLine(const Vec2f &a, const Vec2f &b, float len, const ExtrusionEntity *origin_entity)
ExtrusionLine::ExtrusionLine() : a(Vec2f::Zero()), b(Vec2f::Zero()), len(0.0), origin_entity(nullptr) {}
ExtrusionLine::ExtrusionLine(const Vec2f &a, const Vec2f &b, float len, const ExtrusionEntity *origin_entity)
: a(a), b(b), len(len), origin_entity(origin_entity)
{}
ExtrusionLine(const Vec2f &a, const Vec2f &b)
ExtrusionLine::ExtrusionLine(const Vec2f &a, const Vec2f &b)
: a(a), b(b), len((a-b).norm()), origin_entity(nullptr)
{}
bool is_external_perimeter() const
bool ExtrusionLine::is_external_perimeter() const
{
assert(origin_entity != nullptr);
return origin_entity->role().is_external_perimeter();
}
Vec2f a;
Vec2f b;
float len;
const ExtrusionEntity *origin_entity;
std::optional<SupportSpotsGenerator::SupportPointCause> support_point_generated = {};
float form_quality = 1.0f;
float curled_up_height = 0.0f;
static const constexpr int Dim = 2;
using Scalar = Vec2f::Scalar;
};
auto get_a(ExtrusionLine &&l) { return l.a; }
auto get_b(ExtrusionLine &&l) { return l.b; }
namespace SupportSpotsGenerator {
using LD = AABBTreeLines::LinesDistancer<ExtrusionLine>;
@ -151,14 +134,7 @@ public:
}
};
struct SliceConnection
{
float area{};
Vec3f centroid_accumulator = Vec3f::Zero();
Vec2f second_moment_of_area_accumulator = Vec2f::Zero();
float second_moment_of_area_covariance_accumulator{};
void add(const SliceConnection &other)
void SliceConnection::add(const SliceConnection &other)
{
this->area += other.area;
this->centroid_accumulator += other.centroid_accumulator;
@ -166,7 +142,7 @@ struct SliceConnection
this->second_moment_of_area_covariance_accumulator += other.second_moment_of_area_covariance_accumulator;
}
void print_info(const std::string &tag) const
void SliceConnection::print_info(const std::string &tag) const
{
Vec3f centroid = centroid_accumulator / area;
Vec2f variance = (second_moment_of_area_accumulator / area - centroid.head<2>().cwiseProduct(centroid.head<2>()));
@ -177,7 +153,6 @@ struct SliceConnection
std::cout << "variance: " << variance.x() << " " << variance.y() << std::endl;
std::cout << "covariance: " << covariance << std::endl;
}
};
Integrals::Integrals (const Polygons& polygons) {
for (const Polygon &polygon : polygons) {
@ -479,18 +454,7 @@ float compute_second_moment(
return moment_at_0_0 - area * distance;
}
class ObjectPart
{
public:
float volume{};
Vec3f volume_centroid_accumulator = Vec3f::Zero();
float sticking_area{};
Vec3f sticking_centroid_accumulator = Vec3f::Zero();
Vec2f sticking_second_moment_of_area_accumulator = Vec2f::Zero();
float sticking_second_moment_of_area_covariance_accumulator{};
bool connected_to_bed = false;
ObjectPart(
ObjectPart::ObjectPart(
const std::vector<const ExtrusionEntityCollection*>& extrusion_collections,
const bool connected_to_bed,
const coordf_t print_head_z,
@ -533,7 +497,7 @@ public:
}
}
void add(const ObjectPart &other)
void ObjectPart::add(const ObjectPart &other)
{
this->connected_to_bed = this->connected_to_bed || other.connected_to_bed;
this->volume_centroid_accumulator += other.volume_centroid_accumulator;
@ -544,7 +508,7 @@ public:
this->sticking_second_moment_of_area_covariance_accumulator += other.sticking_second_moment_of_area_covariance_accumulator;
}
void add_support_point(const Vec3f &position, float sticking_area)
void ObjectPart::add_support_point(const Vec3f &position, float sticking_area)
{
this->sticking_area += sticking_area;
this->sticking_centroid_accumulator += sticking_area * position;
@ -553,7 +517,7 @@ public:
}
float compute_elastic_section_modulus(
float ObjectPart::compute_elastic_section_modulus(
const Vec2f &line_dir,
const Vec3f &extreme_point,
const Integrals& integrals
@ -577,7 +541,7 @@ public:
return elastic_section_modulus;
}
std::tuple<float, SupportPointCause> is_stable_while_extruding(const SliceConnection &connection,
std::tuple<float, SupportPointCause> ObjectPart::is_stable_while_extruding(const SliceConnection &connection,
const ExtrusionLine &extruded_line,
const Vec3f &extreme_point,
float layer_z,
@ -694,7 +658,6 @@ public:
return {conn_total_torque / conn_conflict_torque_arm, SupportPointCause::WeakObjectPart};
}
}
};
std::vector<const ExtrusionEntityCollection*> gather_extrusions(const LayerSlice& slice, const Layer* layer) {
// TODO reserve might be good, benchmark
@ -1352,4 +1315,3 @@ std::vector<std::pair<SupportPointCause, bool>> gather_issues(const SupportPoint
}
} // namespace SupportSpotsGenerator
} // namespace Slic3r

View File

@ -174,6 +174,77 @@ float compute_second_moment(
const Vec2f& axis_direction
);
class ExtrusionLine
{
public:
ExtrusionLine();
ExtrusionLine(const Vec2f &a, const Vec2f &b, float len, const ExtrusionEntity *origin_entity);
ExtrusionLine(const Vec2f &a, const Vec2f &b);
bool is_external_perimeter() const;
Vec2f a;
Vec2f b;
float len;
const ExtrusionEntity *origin_entity;
std::optional<SupportSpotsGenerator::SupportPointCause> support_point_generated = {};
float form_quality = 1.0f;
float curled_up_height = 0.0f;
static const constexpr int Dim = 2;
using Scalar = Vec2f::Scalar;
};
struct SliceConnection
{
float area{};
Vec3f centroid_accumulator = Vec3f::Zero();
Vec2f second_moment_of_area_accumulator = Vec2f::Zero();
float second_moment_of_area_covariance_accumulator{};
void add(const SliceConnection &other);
void print_info(const std::string &tag) const;
};
class ObjectPart
{
public:
float volume{};
Vec3f volume_centroid_accumulator = Vec3f::Zero();
float sticking_area{};
Vec3f sticking_centroid_accumulator = Vec3f::Zero();
Vec2f sticking_second_moment_of_area_accumulator = Vec2f::Zero();
float sticking_second_moment_of_area_covariance_accumulator{};
bool connected_to_bed = false;
ObjectPart(
const std::vector<const ExtrusionEntityCollection*>& extrusion_collections,
const bool connected_to_bed,
const coordf_t print_head_z,
const coordf_t layer_height,
const std::optional<Polygons>& brim
);
void add(const ObjectPart &other);
void add_support_point(const Vec3f &position, float sticking_area);
float compute_elastic_section_modulus(
const Vec2f &line_dir,
const Vec3f &extreme_point,
const Integrals& integrals
) const;
std::tuple<float, SupportPointCause> is_stable_while_extruding(const SliceConnection &connection,
const ExtrusionLine &extruded_line,
const Vec3f &extreme_point,
float layer_z,
const Params &params) const;
};
using PartialObjects = std::vector<PartialObject>;
// Both support points and partial objects are sorted from the lowest z to the highest

View File

@ -95,3 +95,24 @@ TEST_CASE("Moments calculation for rotated axis.", "[SupportSpotsGenerator]") {
CHECK(moment_calculated_then_rotated == Approx(moment_rotated_polygon));
}
TEST_CASE("TODO", "[SupportSpotsGenerator]") {
const Polyline polyline{
Point{scaled(Vec2f{0, 0})},
Point{scaled(Vec2f{1, 0})},
};
ExtrusionAttributes attributes;
attributes.width = 0.1;
const ExtrusionPath path{polyline, attributes};
ExtrusionEntityCollection collection;
collection.append(path);
std::vector<const ExtrusionEntityCollection*> collections{&collection};
Polygons polygons = path.polygons_covered_by_width();
for (const Polygon& polygon : polygons) {
std::cout << "Polygon: " << std::endl;
for (const Line& line : polygon.lines()) {
std::cout << "(" << line.a.x() << ", " << line.a.y() << ")" << std::endl;
}
}
}