Skip to content

Commit

Permalink
Add specific generator for mesh insets
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Dec 9, 2024
1 parent 118d217 commit 5d3d530
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 52 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ set(engine_SRCS # Except main.cpp.

src/feature_generation/FeatureGenerator.cpp include/feature_generation/FeatureGenerator.h
src/feature_generation/MeshFeatureGenerator.cpp include/feature_generation/MeshFeatureGenerator.h
src/feature_generation/MeshInsetsGenerator.cpp include/feature_generation/MeshInsetsGenerator.h
src/feature_generation/SkirtBrimAppender.cpp include/feature_generation/SkirtBrimAppender.h

src/infill/ImageBasedDensityProvider.cpp
Expand Down
3 changes: 2 additions & 1 deletion include/feature_generation/FeatureGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace cura
{

struct LayerIndex;
class SliceDataStorage;

class FeatureGenerator
{
Expand All @@ -19,7 +20,7 @@ class FeatureGenerator

virtual bool isActive() const = 0;

virtual void generateFeatures(const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans) const = 0;
virtual void generateFeatures(const SliceDataStorage& storage, const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans) const = 0;
};

} // namespace cura
10 changes: 9 additions & 1 deletion include/feature_generation/MeshFeatureGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace cura
{

class SliceMeshStorage;
class SliceLayerPart;

class MeshFeatureGenerator : public FeatureGenerator
{
Expand All @@ -17,7 +18,14 @@ class MeshFeatureGenerator : public FeatureGenerator

bool isActive() const override;

void generateFeatures(const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans) const override;
void generateFeatures(const SliceDataStorage& storage, const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans) const final;

protected:
std::shared_ptr<SliceMeshStorage> getMesh() const;

virtual void
generateFeatures(const SliceDataStorage& storage, const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans, const SliceLayerPart& part) const
= 0;

private:
std::shared_ptr<SliceMeshStorage> mesh_;
Expand Down
25 changes: 25 additions & 0 deletions include/feature_generation/MeshInsetsGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#pragma once

#include "feature_generation/MeshFeatureGenerator.h"

namespace cura
{

class SliceLayerPart;

class MeshInsetsGenerator : public MeshFeatureGenerator
{
public:
explicit MeshInsetsGenerator(const std::shared_ptr<SliceMeshStorage>& mesh);

bool isActive() const override;

protected:
void generateFeatures(const SliceDataStorage& storage, const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans, const SliceLayerPart& part)
const override;
};

} // namespace cura
3 changes: 3 additions & 0 deletions include/print_operation/ExtruderPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include "print_operation/ExtruderPlanPtr.h"
#include "print_operation/PrintOperationSequence.h"

namespace cura
Expand All @@ -22,6 +23,8 @@ class ExtruderPlan : public PrintOperationSequence

void appendFeatureExtrusion(const std::shared_ptr<FeatureExtrusion>& feature_extrusion, const bool check_non_empty = true);

static ExtruderPlanPtr find(const std::vector<ExtruderPlanPtr>& extruder_plans, const size_t extruder_nr);

private:
const size_t extruder_nr_;
#warning use a shared_ptr
Expand Down
5 changes: 3 additions & 2 deletions include/print_operation/ExtrusionMove.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ namespace cura
class ExtrusionMove : public ExtruderMove
{
public:
explicit ExtrusionMove(const Point3LL& position, const Ratio& line_width_ratio = 1.0_r);
explicit ExtrusionMove(const Point3LL& position, const coord_t line_width_start, const std::optional<coord_t>& line_width_end = std::nullopt);

void write(PlanExporter& exporter) const override;

private:
Ratio line_width_ratio_{ 1.0 };
coord_t line_width_start_{ 0 };
coord_t line_width_end_{ 0 };
};

} // namespace cura
Expand Down
84 changes: 42 additions & 42 deletions src/FffGcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "bridge.h"
#include "communication/Communication.h" //To send layer view data.
#include "feature_generation/FeatureGenerator.h"
#include "feature_generation/MeshFeatureGenerator.h"
#include "feature_generation/MeshInsetsGenerator.h"
#include "geometry/LinesSet.h"
#include "geometry/OpenPolyline.h"
#include "geometry/PointMatrix.h"
Expand Down Expand Up @@ -110,20 +110,20 @@ void FffGcodeWriter::writeGCode(SliceDataStorage& storage, TimeKeeper& time_keep

setConfigRetractionAndWipe(storage);

if (scene.current_mesh_group == scene.mesh_groups.begin())
{
auto should_prime_extruder = gcode.initializeExtruderTrains(storage, start_extruder_nr);

if (! should_prime_extruder)
{
// set to most negative number so that layer processing never primes this extruder anymore.
extruder_prime_layer_nr[start_extruder_nr] = std::numeric_limits<int>::min();
}
}
else
{
processNextMeshGroupCode(storage);
}
// if (scene.current_mesh_group == scene.mesh_groups.begin())
// {
// auto should_prime_extruder = gcode.initializeExtruderTrains(storage, start_extruder_nr);
//
// if (! should_prime_extruder)
// {
// // set to most negative number so that layer processing never primes this extruder anymore.
// extruder_prime_layer_nr[start_extruder_nr] = std::numeric_limits<int>::min();
// }
// }
// else
// {
// processNextMeshGroupCode(storage);
// }

size_t total_layers = 0;
for (std::shared_ptr<SliceMeshStorage>& mesh_ptr : storage.meshes)
Expand All @@ -144,34 +144,34 @@ void FffGcodeWriter::writeGCode(SliceDataStorage& storage, TimeKeeper& time_keep

setSupportAngles(storage);

gcode.writeLayerCountComment(total_layers);

{ // calculate the mesh order for each extruder
const size_t extruder_count = Application::getInstance().current_slice_->scene.extruders.size();
mesh_order_per_extruder.clear(); // Might be not empty in case of sequential printing.
mesh_order_per_extruder.reserve(extruder_count);
for (size_t extruder_nr = 0; extruder_nr < extruder_count; extruder_nr++)
{
mesh_order_per_extruder.push_back(calculateMeshOrder(storage, extruder_nr));
}
}
const auto extruder_settings = Application::getInstance().current_slice_->scene.extruders[gcode.getExtruderNr()].settings_;
// gcode.writeLayerCountComment(total_layers);
//
// { // calculate the mesh order for each extruder
// const size_t extruder_count = Application::getInstance().current_slice_->scene.extruders.size();
// mesh_order_per_extruder.clear(); // Might be not empty in case of sequential printing.
// mesh_order_per_extruder.reserve(extruder_count);
// for (size_t extruder_nr = 0; extruder_nr < extruder_count; extruder_nr++)
// {
// mesh_order_per_extruder.push_back(calculateMeshOrder(storage, extruder_nr));
// }
// }
// const auto extruder_settings = Application::getInstance().current_slice_->scene.extruders[gcode.getExtruderNr()].settings_;
// in case the prime blob is enabled the brim already starts from the closest start position which is blob location
// also in case of one at a time printing the first move of every object shouldn't be start position of machine
if (! extruder_settings.get<bool>("prime_blob_enable") and ! (extruder_settings.get<std::string>("print_sequence") == "one_at_a_time"))
{
// Setting first travel move of the first extruder to the machine start position
Point3LL p(extruder_settings.get<coord_t>("machine_extruder_start_pos_x"), extruder_settings.get<coord_t>("machine_extruder_start_pos_y"), gcode.getPositionZ());
gcode.writeTravel(p, extruder_settings.get<Velocity>("speed_travel"));
}

calculateExtruderOrderPerLayer(storage);
calculatePrimeLayerPerExtruder(storage);
// if (! extruder_settings.get<bool>("prime_blob_enable") and ! (extruder_settings.get<std::string>("print_sequence") == "one_at_a_time"))
// {
// // Setting first travel move of the first extruder to the machine start position
// Point3LL p(extruder_settings.get<coord_t>("machine_extruder_start_pos_x"), extruder_settings.get<coord_t>("machine_extruder_start_pos_y"), gcode.getPositionZ());
// gcode.writeTravel(p, extruder_settings.get<Velocity>("speed_travel"));
// }

if (scene.current_mesh_group->settings.get<bool>("magic_spiralize"))
{
findLayerSeamsForSpiralize(storage, total_layers);
}
// calculateExtruderOrderPerLayer(storage);
// calculatePrimeLayerPerExtruder(storage);
//
// if (scene.current_mesh_group->settings.get<bool>("magic_spiralize"))
// {
// findLayerSeamsForSpiralize(storage, total_layers);
// }

int process_layer_starting_layer_nr = 0;
// const bool has_raft = scene.current_mesh_group->settings.get<EPlatformAdhesion>("adhesion_type") == EPlatformAdhesion::RAFT;
Expand All @@ -193,10 +193,10 @@ void FffGcodeWriter::writeGCode(SliceDataStorage& storage, TimeKeeper& time_keep
// Add all possible feature generators
for (const std::shared_ptr<SliceMeshStorage>& mesh : storage.meshes)
{
feature_generators_.push_back(std::make_shared<MeshFeatureGenerator>(mesh));
feature_generators_.push_back(std::make_shared<MeshInsetsGenerator>(mesh));
}

// Filter out generators that are actually useless in this context
// Filter out generators that are actually useless in this context. Not highly useful, but helps for debugging.
ranges::remove_if(
feature_generators_,
[](const std::shared_ptr<FeatureGenerator>& generator)
Expand Down
35 changes: 33 additions & 2 deletions src/feature_generation/MeshFeatureGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,42 @@ MeshFeatureGenerator::MeshFeatureGenerator(const std::shared_ptr<SliceMeshStorag

bool MeshFeatureGenerator::isActive() const
{
return ! mesh_->layers.empty();
if (mesh_->layers.empty())
{
return false;
}

const Settings& mesh_settings = mesh_->settings;
if (mesh_settings.get<bool>("anti_overhang_mesh") || mesh_settings.get<bool>("support_mesh"))
{
return false;
}

return true;
}

void MeshFeatureGenerator::generateFeatures(const SliceDataStorage& storage, const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans) const
{
if (layer_index > mesh_->layer_nr_max_filled_layer)
{
return;
}

const SliceLayer& layer = mesh_->layers[layer_index];
for (const SliceLayerPart& part : layer)
{
if (part.outline.empty())
{
continue;
}

generateFeatures(storage, layer_index, extruder_plans, part);
}
}

void MeshFeatureGenerator::generateFeatures(const LayerIndex& layer_index, const std::vector<ExtruderPlanPtr>& extruder_plans) const
std::shared_ptr<SliceMeshStorage> MeshFeatureGenerator::getMesh() const
{
return mesh_;
}

} // namespace cura
93 changes: 93 additions & 0 deletions src/feature_generation/MeshInsetsGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#include "feature_generation/MeshInsetsGenerator.h"

#include <ExtruderTrain.h>
#include <WallToolPaths.h>

#include <range/v3/algorithm/find_if.hpp>

#include "print_operation/ContinuousExtruderMoveSequence.h"
#include "print_operation/ExtruderPlan.h"
#include "print_operation/FeatureExtrusion.h"
#include "sliceDataStorage.h"

namespace cura
{

MeshInsetsGenerator::MeshInsetsGenerator(const std::shared_ptr<SliceMeshStorage>& mesh)
: MeshFeatureGenerator(mesh)
{
}
bool MeshInsetsGenerator::isActive() const
{
if (! MeshFeatureGenerator::isActive())
{
return false;
}

if (getMesh()->settings.get<size_t>("wall_line_count") == 0)
{
return false;
}

return true;
}

void MeshInsetsGenerator::generateFeatures(
const SliceDataStorage& storage,
const LayerIndex& layer_index,
const std::vector<ExtruderPlanPtr>& extruder_plans,
const SliceLayerPart& part) const
{
// if (extruder_nr != mesh.settings.get<ExtruderTrain&>("wall_0_extruder_nr").extruder_nr_ && extruder_nr !=
// mesh.settings.get<ExtruderTrain&>("wall_x_extruder_nr").extruder_nr_)
// {
// return added_something;
// }

const Settings mesh_settings = getMesh()->settings;

const size_t extruder_nr_outer_walls = mesh_settings.get<ExtruderTrain&>("wall_0_extruder_nr").extruder_nr_;
ExtruderPlanPtr extruder_plan_outer_walls = ExtruderPlan::find(extruder_plans, extruder_nr_outer_walls);
assert(extruder_plan_outer_walls && "Unable to find extruder plan for outer walls");
auto feature_extrusion_outer_walls = std::make_shared<FeatureExtrusion>();

const size_t extruder_nr_inner_walls = mesh_settings.get<ExtruderTrain&>("wall_x_extruder_nr").extruder_nr_;
ExtruderPlanPtr extruder_plan_inner_walls = ExtruderPlan::find(extruder_plans, extruder_nr_inner_walls);
assert(extruder_plan_inner_walls && "Unable to find extruder plan for inner walls");
auto feature_extrusion_inner_walls = std::make_shared<FeatureExtrusion>();

for (const VariableWidthLines& toolpath : part.wall_toolpaths)
{
for (const ExtrusionLine& extrusion_line : toolpath)
{
auto move_sequence = std::make_shared<ContinuousExtruderMoveSequence>(extrusion_line.is_closed_);

for (const ExtrusionJunction& extrusion_junction : extrusion_line)
{
}

if (extrusion_line.is_outer_wall())
{
feature_extrusion_outer_walls->appendExtruderMoveSequence(move_sequence);
}
else
{
feature_extrusion_inner_walls->appendExtruderMoveSequence(move_sequence);
}
}
}

extruder_plan_outer_walls->appendFeatureExtrusion(feature_extrusion_outer_walls);
extruder_plan_inner_walls->appendFeatureExtrusion(feature_extrusion_inner_walls);

// if (mesh.settings.get<ESurfaceMode>("magic_mesh_surface_mode") != ESurfaceMode::NORMAL && extruder_nr ==
// mesh.settings.get<ExtruderTrain&>("wall_0_extruder_nr").extruder_nr_)
// {
// addMeshOpenPolyLinesToGCode(mesh, mesh_config, gcode_layer);
// }
}

} // namespace cura
2 changes: 1 addition & 1 deletion src/feature_generation/SkirtBrimAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void SkirtBrimAppender::process(PrintPlan* print_plan)
{
used_extruders.insert(extruder_plan->getExtruderNr());
}
return false;
return false; // Loop through all operations, we don't actually search something specific
},
PrintOperationSequence::SearchOrder::Forward,
1);
Expand Down
Loading

0 comments on commit 5d3d530

Please sign in to comment.