From b91b98b21e342783b3c8989e0275294a808dd6ca Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Sun, 27 Nov 2016 19:15:27 -0600 Subject: [PATCH] Added prototype make_cylinder() --- lib/Slic3r/GUI/Plater/LambdaObjectDialog.pm | 2 +- lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm | 13 ++++--- xs/src/libslic3r/TriangleMesh.cpp | 40 ++++++++++++++++++++- xs/src/libslic3r/TriangleMesh.hpp | 3 ++ xs/xsp/TriangleMesh.xsp | 2 ++ 5 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/Slic3r/GUI/Plater/LambdaObjectDialog.pm b/lib/Slic3r/GUI/Plater/LambdaObjectDialog.pm index d6d4c3b09..2f815c27e 100644 --- a/lib/Slic3r/GUI/Plater/LambdaObjectDialog.pm +++ b/lib/Slic3r/GUI/Plater/LambdaObjectDialog.pm @@ -60,7 +60,7 @@ sub new { }, label_width => 100, ); - my @options = ("box"); + my @options = ("box", "cylinder"); $self->{type} = Wx::ComboBox->new($self, 1, "box", wxDefaultPosition, wxDefaultSize, \@options, wxCB_READONLY); $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new( diff --git a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm index 63a1f3f0c..31d693e78 100644 --- a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm +++ b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm @@ -342,14 +342,19 @@ sub on_btn_lambda { } my $params = $dlg->ObjectParameter; my $name = "lambda-".$params->{"type"}; + my $mesh = Slic3r::TriangleMesh->new(); - my $new_volume = $self->{model_object}->add_volume(mesh => Slic3r::Test::mesh($params->{"type"}, dim=>$params->{"dim"}), material_id=>"generic"); + #TODO support non-boxes + if ($name eq "box") { + $mesh = $mesh->cube($params->{"dim"}[0], $params->{"dim"}[1], $params->{"dim"}[2]); + } elsif ($name eq "cylinder") { + $mesh = $mesh->cylinder($params->{"dim"}[0], $params->{"dim"}[1]); + } + + my $new_volume = $self->{model_object}->add_volume(mesh => $mesh); $new_volume->set_modifier($is_modifier); $new_volume->set_name($name); - # apply the same translation we applied to the object - $new_volume->mesh->translate(@{$self->{model_object}->origin_translation}); - # set a default extruder value, since user can't add it manually $new_volume->config->set_ifndef('extruder', 0); diff --git a/xs/src/libslic3r/TriangleMesh.cpp b/xs/src/libslic3r/TriangleMesh.cpp index 968ce371e..0950ee26f 100644 --- a/xs/src/libslic3r/TriangleMesh.cpp +++ b/xs/src/libslic3r/TriangleMesh.cpp @@ -1140,7 +1140,7 @@ TriangleMeshSlicer::~TriangleMeshSlicer() { if (this->v_scaled_shared != NULL) free(this->v_scaled_shared); } - +// Generate the vertex list for a cube solid of arbitrary size in X/Y/Z. TriangleMesh make_cube(double x, double y, double z) { Pointf3 pv[8] = { Pointf3(x, y, 0), Pointf3(x, 0, 0), Pointf3(0, 0, 0), @@ -1160,4 +1160,42 @@ TriangleMesh make_cube(double x, double y, double z) { TriangleMesh mesh(vertices ,facets); return mesh; } + +// Generate the mesh for a cylinder and return it, using +// the generated angle to calculate the top mesh triangles. +TriangleMesh make_cylinder(double r, double h, double fa) { + Pointf3s vertices; + std::vector facets; + + // 2 special vertices, top and bottom center, rest are relative to this + vertices.push_back(Pointf3(0.0, 0.0, 0.0)); + vertices.push_back(Pointf3(0.0, 0.0, h)); + + // adjust via rounding + double angle = (2*PI / floor(2*PI / fa)); + + // for each line along the polygon approximating the top/bottom of the + // circle, generate four points and four facets (2 for the wall, 2 for the + // top and bottom. + // Special case: Last line shares 2 vertices with the first line. + unsigned id = 3; + vertices.push_back(Pointf3(sin(0) * r , cos(0) * r, 0)); + vertices.push_back(Pointf3(sin(0) * r , cos(0) * r, h)); + for (double i = angle; i < 2*PI; i+=angle) { + vertices.push_back(Pointf3(sin(i) * r , cos(i) * r, 0)); + vertices.push_back(Pointf3(sin(i) * r , cos(i) * r, h)); + id += 2; + facets.push_back(Point3(0, id - 1, id - 3)); + facets.push_back(Point3(1, id, id - 2)); + facets.push_back(Point3(id, id - 1, id - 3)); + facets.push_back(Point3(id - 3, id - 2, id)); + } + facets.push_back(Point3(0, 2, id -1)); + facets.push_back(Point3(1, 3, id)); + facets.push_back(Point3(id - 1, 2, 3)); + facets.push_back(Point3(id - 1, 3, id)); + + TriangleMesh mesh(vertices ,facets); + return mesh; +} } diff --git a/xs/src/libslic3r/TriangleMesh.hpp b/xs/src/libslic3r/TriangleMesh.hpp index 4c2ad2d5e..9320d2a16 100644 --- a/xs/src/libslic3r/TriangleMesh.hpp +++ b/xs/src/libslic3r/TriangleMesh.hpp @@ -111,6 +111,9 @@ class TriangleMeshSlicer TriangleMesh make_cube(double x, double y, double z); +// Generate a TriangleMesh of a cylinder +TriangleMesh make_cylinder(double r, double h, double fa=(2*PI/360)); + } #endif diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index 24d1b5f88..32425d6d4 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -39,6 +39,8 @@ void reset_repair_stats(); Clone cube(double x, double y, double z) %code{% RETVAL = make_cube(x, y, z); %}; + Clone cylinder(double r, double h) + %code{% RETVAL = make_cylinder(r, h); %}; %{ void