From 93b0cd436a8ca8c48ac8b39a59e49d951fa28930 Mon Sep 17 00:00:00 2001 From: Samir55 Date: Thu, 12 Jul 2018 09:34:47 +0200 Subject: [PATCH] Porting test 1. --- src/CMakeLists.txt | 6 +- src/test/libslic3r/test_support_material.cpp | 98 ++++++++++++++++ xs/src/libslic3r/SupportMaterial.cpp | 9 +- xs/src/libslic3r/SupportMaterial.hpp | 117 +++---------------- 4 files changed, 123 insertions(+), 107 deletions(-) create mode 100644 src/test/libslic3r/test_support_material.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4fc3ad07b..0f2dbe99e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -84,7 +84,7 @@ include_directories(${LIBDIR}/poly2tri/common) add_library(ZipArchive STATIC ${LIBDIR}/Zip/ZipArchive.cpp ) - +target_compile_features(ZipArchive PUBLIC cxx_std_11) add_library(libslic3r STATIC ${LIBDIR}/libslic3r/BoundingBox.cpp @@ -165,6 +165,8 @@ add_library(expat STATIC ${LIBDIR}/expat/xmlrole.c ${LIBDIR}/expat/xmltok.c ) +target_compile_features(clipper PUBLIC cxx_std_11) + add_library(polypartition STATIC ${LIBDIR}/polypartition.cpp) add_library(poly2tri STATIC ${LIBDIR}/poly2tri/common/shapes.cc @@ -191,7 +193,7 @@ set(UI_TEST_SOURCES set(SLIC3R_TEST_SOURCES ${TESTDIR}/test_harness.cpp ${TESTDIR}/libslic3r/test_trianglemesh.cpp -) + test/libslic3r/test_support_material.cpp) add_executable(slic3r slic3r.cpp) #set_target_properties(slic3r PROPERTIES LINK_SEARCH_START_STATIC 1) #set_target_properties(slic3r PROPERTIES LINK_SEARCH_END_STATIC 1) diff --git a/src/test/libslic3r/test_support_material.cpp b/src/test/libslic3r/test_support_material.cpp new file mode 100644 index 000000000..29d888e0b --- /dev/null +++ b/src/test/libslic3r/test_support_material.cpp @@ -0,0 +1,98 @@ +#include +//#include "/home/ahmedsamir/Work/SamirSlic3r/Slic3r/build/external/Catch/include/catch.hpp" + +#include "libslic3r.h" +#include "TriangleMesh.hpp" +#include "Model.hpp" +#include "SupportMaterial.hpp" + +using namespace std; +using namespace Slic3r; + +SCENARIO("SupportMaterial: support_layers_z and contact_distance") +{ + GIVEN("A print object having one modelObject") { + // Create a mesh & modelObject. + TriangleMesh mesh = TriangleMesh::make_cube(20, 20, 20); + + // Create modelObject. + Model model = Model(); + ModelObject *object = model.add_object(); + object->add_volume(mesh); + model.add_default_instances(); + + // Align to origin. + model.align_instances_to_origin(); + + // Create Print. + Print print = Print(); + print.default_object_config.set_deserialize("support_material", "1"); + + vector contact_z = {1.9}; + vector top_z = {1.1}; + + WHEN("Layer height = 0.2 and, first layer height = 0.3") { + + print.default_object_config.set_deserialize("layer_height", "0.2"); + print.default_object_config.set_deserialize("first_layer_height", "0.3"); + + print.add_model_object(model.objects[0]); + print.objects.front()->_slice(); + + SupportMaterial support = SupportMaterial(&print.config, &print.objects.front()->config); + + vector + support_z = support.support_layers_z(contact_z, top_z, print.default_object_config.layer_height.value); + + THEN("First layer height is honored") { + REQUIRE((support_z[0] == print.default_object_config.first_layer_height.value)); + } + + THEN("No null or negative support layers") { + bool check = true; + for (size_t i = 1; i < support_z.size(); ++i) + if (support_z[i] - support_z[i - 1] <= 0) check = false; + REQUIRE(check); + } + + THEN("No layers thicker than nozzle diameter") { + bool check = true; + for (size_t i = 1; i < support_z.size(); ++i) + if (support_z[i] - support_z[i - 1] > print.config.nozzle_diameter.get_at(0) + EPSILON) + check = false; + REQUIRE(check); + } + + THEN("Layers above top surfaces are spaced correctly") { + coordf_t expected_top_spacing = support + .contact_distance(print.default_object_config.layer_height, print.config.nozzle_diameter.get_at(0)); + + bool wrong_top_spacing = 0; + for (coordf_t top_z_el : top_z) { + // find layer index of this top surface. + int layer_id = -1; + for (int i = 0; i < support_z.size(); i++) { + if (abs(support_z[i] - top_z_el) < EPSILON) { + layer_id = i; + i = static_cast(support_z.size()); + } + } + + // check that first support layer above this top surface (or the next one) is spaced with nozzle diameter + if (abs(support_z[layer_id + 1] - support_z[layer_id] - expected_top_spacing) > EPSILON + && abs(support_z[layer_id + 2] - support_z[layer_id] - expected_top_spacing) > EPSILON) { + wrong_top_spacing = 1; + } + REQUIRE(!wrong_top_spacing); + } + } + } +// /* Test Also with this +// $config->set('first_layer_height', 0.4); +// $test->(); +// +// $config->set('layer_height', $config->nozzle_diameter->[0]); +// $test->(); +// */ + } +} diff --git a/xs/src/libslic3r/SupportMaterial.cpp b/xs/src/libslic3r/SupportMaterial.cpp index 5d76a951b..e23e0c71b 100644 --- a/xs/src/libslic3r/SupportMaterial.cpp +++ b/xs/src/libslic3r/SupportMaterial.cpp @@ -162,12 +162,11 @@ SupportMaterial::support_layers_z(vector contact_z, } // Create other layers (skip raft layers as they're already done and use thicker layers). - for (size_t i = z.size(); i >= object_config->raft_layers; i--) { + for (int i = static_cast(z.size()); i >= object_config->raft_layers.value; i--) { coordf_t target_height = support_material_height; if (i > 0 && is_top[z[i - 1]]) { target_height = nozzle_diameter; } - // Enforce first layer height. if ((i == 0 && z[i] > target_height + first_layer_height) || (z[i] - z[i - 1] > target_height + EPSILON)) { @@ -179,10 +178,10 @@ SupportMaterial::support_layers_z(vector contact_z, // Remove duplicates and make sure all 0.x values have the leading 0. { set s; - for (auto el : z) - s.insert(roundf(static_cast((el * 100)) / 100)); // round it to 2 decimal places. + for (coordf_t el : z) + s.insert(int(el * 100) / 100.0); // round it to 2 decimal places. z = vector(); - for (auto el : s) + for (coordf_t el : s) z.push_back(el); } diff --git a/xs/src/libslic3r/SupportMaterial.hpp b/xs/src/libslic3r/SupportMaterial.hpp index 7f375892b..923dadc26 100644 --- a/xs/src/libslic3r/SupportMaterial.hpp +++ b/xs/src/libslic3r/SupportMaterial.hpp @@ -28,6 +28,7 @@ constexpr coordf_t PILLAR_SIZE = 2.5; constexpr coordf_t PILLAR_SPACING = 10; +/// Struct for carrying the toolpaths parameters needed for each thread. struct toolpaths_params { int contact_loops; @@ -36,11 +37,11 @@ struct toolpaths_params Polygon circle; SupportMaterialPattern pattern; vector angles; - double interface_angle; - double interface_spacing; - float interface_density; - double support_spacing; - double support_density; + double interface_angle{}; + double interface_spacing{}; + float interface_density{}; + double support_spacing{}; + double support_density{}; toolpaths_params(int contact_loops = 0, coordf_t circle_radius = 0, @@ -60,11 +61,11 @@ struct toolpaths_params class SupportMaterial { public: - PrintConfig *config; ///< - PrintObjectConfig *object_config; ///< - Flow *flow; ///< - Flow *first_layer_flow; ///< - Flow *interface_flow; ///< + PrintConfig *config; ///< The print config + PrintObjectConfig *object_config; ///< The object print config. + Flow *flow; ///< The intermediate layers print flow. + Flow *first_layer_flow; ///< The first (base) layers print flow. + Flow *interface_flow; ///< The interface layers print flow. SupportMaterial(PrintConfig *print_config, PrintObjectConfig *print_object_config, @@ -79,14 +80,17 @@ public: object(nullptr) {} + /// Generate the extrusions paths for the support matterial generated for the given print object. void generate_toolpaths(PrintObject *object, map overhang, map contact, map interface, map base); + /// Generate support material for the given print object. void generate(PrintObject *object); + /// Generate the support layers z coordinates. (TODO Dicuss more). vector support_layers_z(vector contact_z, vector top_z, coordf_t max_object_layer_height); @@ -129,10 +133,13 @@ public: void process_layer(int layer_id, toolpaths_params params); private: + // Get the maximum layer height given a print object. coordf_t get_max_layer_height(PrintObject *object); + // (Deprecated) use append_to instead TODO @Samir55. void append_polygons(Polygons &dst, Polygons &src); + // Return polygon vector given a vector of surfaces. Polygons p(SurfacesPtr &surfaces); vector get_keys_sorted(map _map); @@ -148,96 +155,6 @@ private: }; -// To Be converted to catch. -// Supports Material tests. -class SupportMaterialTests -{ -public: - bool test_1() - { - // Create a mesh & modelObject. - TriangleMesh mesh = TriangleMesh::make_cube(20, 20, 20); - - // Create modelObject. - Model model = Model(); - ModelObject *object = model.add_object(); - object->add_volume(mesh); - model.add_default_instances(); - - // Align to origin. - model.align_instances_to_origin(); - - // Create Print. - Print print = Print(); - - // Configure the printObjectConfig. - print.default_object_config.set_deserialize("support_material", "1"); - print.default_object_config.set_deserialize("layer_height", "0.2"); - print.config.set_deserialize("first_layer_height", "0.3"); - - vector contact_z; - vector top_z; - contact_z.push_back(1.9); - top_z.push_back(1.9); - - // Add the modelObject. - print.add_model_object(model.objects[0]); - - // Create new supports. - SupportMaterial support = SupportMaterial(&print.config, &print.objects.front()->config); - - vector - support_z = support.support_layers_z(contact_z, top_z, print.default_object_config.layer_height); - -// bool -// is_1 = (support_z[0] == print.default_object_config.first_layer_height); // 'first layer height is honored'. -// -// bool is_2 = false; // 'no null or negative support layers'. -// for (int i = 1; i < support_z.size(); ++i) { -// if (support_z[i] - support_z[i - 1] <= 0) is_2 = true; -// } -// -// bool is_3 = false; // 'no layers thicker than nozzle diameter'. -// for (int i = 1; i < support_z.size(); ++i) { -// if (support_z[i] - support_z[i - 1] > print.config.nozzle_diameter.get_at(0) + EPSILON) is_2 = true; -// } -// -// coordf_t expected_top_spacing = -// support.contact_distance(print.default_object_config.layer_height, print.config.nozzle_diameter.get_at(0)); -// coordf_t wrong_top_spacing = 0; -// for (auto top_z_el : top_z) { -// // find layer index of this top surface. -// int layer_id = -1; -// for (int i = 0; i < support_z.size(); i++) { -// if (abs(support_z[i] - top_z_el) < EPSILON) { -// layer_id = i; -// i = static_cast(support_z.size()); -// } -// } -// -// // check that first support layer above this top surface (or the next one) is spaced with nozzle diameter -// if ((support_z[layer_id + 1] - support_z[layer_id]) != expected_top_spacing -// && (support_z[layer_id + 2] - support_z[layer_id]) != expected_top_spacing) -// wrong_top_spacing = 1; -// } -// bool is_4 = !wrong_top_spacing; // 'layers above top surfaces are spaced correctly' - - /* Test Also with this - $config->set('first_layer_height', 0.4); - $test->(); - - $config->set('layer_height', $config->nozzle_diameter->[0]); - $test->(); - */ - } - - bool test_2() - { - - } - -}; - } #endif