ENABLE_ADAPTIVE_LAYER_HEIGHT_PROFILE -> Refactoring of SlicingAdaptive to account for volumes' transformation

This commit is contained in:
Enrico Turri 2019-11-13 13:53:02 +01:00
parent b77ba32bb2
commit 0001ce3dab
5 changed files with 104 additions and 41 deletions

View file

@ -1,16 +1,22 @@
#include "libslic3r.h"
#if ENABLE_ADAPTIVE_LAYER_HEIGHT_PROFILE
#include "Model.hpp"
#else
#include "TriangleMesh.hpp"
#endif // ENABLE_ADAPTIVE_LAYER_HEIGHT_PROFILE
#include "SlicingAdaptive.hpp"
namespace Slic3r
{
#if !ENABLE_ADAPTIVE_LAYER_HEIGHT_PROFILE
void SlicingAdaptive::clear()
{
m_meshes.clear();
m_meshes.clear();
m_faces.clear();
m_face_normal_z.clear();
}
#endif // !ENABLE_ADAPTIVE_LAYER_HEIGHT_PROFILE
std::pair<float, float> face_z_span(const stl_facet *f)
{
@ -21,21 +27,38 @@ std::pair<float, float> face_z_span(const stl_facet *f)
void SlicingAdaptive::prepare()
{
// 1) Collect faces of all meshes.
int nfaces_total = 0;
for (std::vector<const TriangleMesh*>::const_iterator it_mesh = m_meshes.begin(); it_mesh != m_meshes.end(); ++ it_mesh)
#if ENABLE_ADAPTIVE_LAYER_HEIGHT_PROFILE
if (m_object == nullptr)
return;
m_faces.clear();
m_face_normal_z.clear();
m_mesh = m_object->raw_mesh();
const ModelInstance* first_instance = m_object->instances.front();
m_mesh.transform(first_instance->get_matrix(), first_instance->is_left_handed());
// 1) Collect faces from mesh.
m_faces.reserve(m_mesh.stl.stats.number_of_facets);
for (const stl_facet& face : m_mesh.stl.facet_start)
m_faces.emplace_back(&face);
#else
// 1) Collect faces of all meshes.
int nfaces_total = 0;
for (std::vector<const TriangleMesh*>::const_iterator it_mesh = m_meshes.begin(); it_mesh != m_meshes.end(); ++ it_mesh)
nfaces_total += (*it_mesh)->stl.stats.number_of_facets;
m_faces.reserve(nfaces_total);
for (std::vector<const TriangleMesh*>::const_iterator it_mesh = m_meshes.begin(); it_mesh != m_meshes.end(); ++ it_mesh)
for (const stl_facet &face : (*it_mesh)->stl.facet_start)
m_faces.emplace_back(&face);
m_faces.reserve(nfaces_total);
for (std::vector<const TriangleMesh*>::const_iterator it_mesh = m_meshes.begin(); it_mesh != m_meshes.end(); ++ it_mesh)
for (const stl_facet& face : (*it_mesh)->stl.facet_start)
m_faces.emplace_back(&face);
#endif // ENABLE_ADAPTIVE_LAYER_HEIGHT_PROFILE
// 2) Sort faces lexicographically by their Z span.
std::sort(m_faces.begin(), m_faces.end(), [](const stl_facet *f1, const stl_facet *f2) {
std::pair<float, float> span1 = face_z_span(f1);
std::pair<float, float> span1 = face_z_span(f1);
std::pair<float, float> span2 = face_z_span(f2);
return span1 < span2;
});
return span1 < span2;
});
// 3) Generate Z components of the facet normals.
m_face_normal_z.assign(m_faces.size(), 0.f);
@ -45,14 +68,14 @@ void SlicingAdaptive::prepare()
float SlicingAdaptive::cusp_height(float z, float cusp_value, int &current_facet)
{
float height = m_slicing_params.max_layer_height;
float height = (float)m_slicing_params.max_layer_height;
bool first_hit = false;
// find all facets intersecting the slice-layer
int ordered_id = current_facet;
for (; ordered_id < int(m_faces.size()); ++ ordered_id) {
std::pair<float, float> zspan = face_z_span(m_faces[ordered_id]);
// facet's minimum is higher than slice_z -> end loop
std::pair<float, float> zspan = face_z_span(m_faces[ordered_id]);
// facet's minimum is higher than slice_z -> end loop
if (zspan.first >= z)
break;
// facet's maximum is higher than slice_z -> store the first event for next cusp_height call to begin at this point
@ -77,8 +100,8 @@ float SlicingAdaptive::cusp_height(float z, float cusp_value, int &current_facet
// check for sloped facets inside the determined layer and correct height if necessary
if (height > m_slicing_params.min_layer_height) {
for (; ordered_id < int(m_faces.size()); ++ ordered_id) {
std::pair<float, float> zspan = face_z_span(m_faces[ordered_id]);
// facet's minimum is higher than slice_z + height -> end loop
std::pair<float, float> zspan = face_z_span(m_faces[ordered_id]);
// facet's minimum is higher than slice_z + height -> end loop
if (zspan.first >= z + height)
break;
@ -122,19 +145,18 @@ float SlicingAdaptive::cusp_height(float z, float cusp_value, int &current_facet
float SlicingAdaptive::horizontal_facet_distance(float z)
{
for (size_t i = 0; i < m_faces.size(); ++ i) {
std::pair<float, float> zspan = face_z_span(m_faces[i]);
// facet's minimum is higher than max forward distance -> end loop
std::pair<float, float> zspan = face_z_span(m_faces[i]);
// facet's minimum is higher than max forward distance -> end loop
if (zspan.first > z + m_slicing_params.max_layer_height)
break;
// min_z == max_z -> horizontal facet
if (zspan.first > z && zspan.first == zspan.second)
if ((zspan.first > z) && (zspan.first == zspan.second))
return zspan.first - z;
}
// objects maximum?
return (z + m_slicing_params.max_layer_height > m_slicing_params.object_print_z_height()) ?
std::max<float>(m_slicing_params.object_print_z_height() - z, 0.f) :
m_slicing_params.max_layer_height;
return (z + (float)m_slicing_params.max_layer_height > (float)m_slicing_params.object_print_z_height()) ?
std::max((float)m_slicing_params.object_print_z_height() - z, 0.f) : (float)m_slicing_params.max_layer_height;
}
}; // namespace Slic3r