Copy-constructor for LayerHeightSpline to use it for interactive interface

This commit is contained in:
Florens Wasserfall 2017-03-07 12:20:37 +01:00
parent dd46432e4f
commit ece3a6e0ad
7 changed files with 110 additions and 32 deletions

View File

@ -226,6 +226,7 @@ sub thread_cleanup {
*Slic3r::Geometry::BoundingBoxf::DESTROY = sub {}; *Slic3r::Geometry::BoundingBoxf::DESTROY = sub {};
*Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {}; *Slic3r::Geometry::BoundingBoxf3::DESTROY = sub {};
*Slic3r::Layer::PerimeterGenerator::DESTROY = sub {}; *Slic3r::Layer::PerimeterGenerator::DESTROY = sub {};
*Slic3r::LayerHeightSpline::DESTROY = sub {};
*Slic3r::Line::DESTROY = sub {}; *Slic3r::Line::DESTROY = sub {};
*Slic3r::Linef3::DESTROY = sub {}; *Slic3r::Linef3::DESTROY = sub {};
*Slic3r::Model::DESTROY = sub {}; *Slic3r::Model::DESTROY = sub {};

View File

@ -79,33 +79,74 @@ sub repaint {
# draw original layers as lines # draw original layers as lines
my $last_z = 0.0; my $last_z = 0.0;
my @points = (); my @points = ();
foreach my $z (@{$self->{original_interpolated_layers}}) { # foreach my $z (@{$self->{original_interpolated_layers}}) {
my $layer_h = $z - $last_z; # my $layer_h = $z - $last_z;
$dc->SetPen($self->{original_pen}); # $dc->SetPen($self->{original_pen});
my $pl = $self->point_to_pixel(0, $z); # my $pl = $self->point_to_pixel(0, $z);
my $pr = $self->point_to_pixel($layer_h, $z); # my $pr = $self->point_to_pixel($layer_h, $z);
#$dc->DrawLine($pl->x, $pl->y, $pr->x, $pr->y); # #$dc->DrawLine($pl->x, $pl->y, $pr->x, $pr->y);
push (@points, $pr); # push (@points, $pr);
$last_z = $z; # $last_z = $z;
} # }
#
$dc->DrawSpline(\@points); # $dc->DrawSpline(\@points);
if($self->{original_height_spline}) {
# # draw interactive (user modified) layers as lines
$last_z = 0.0; $last_z = 0.0;
@points = (); @points = ();
if($self->{interactive_heights}) { #draw spline
foreach my $i (0..@{$self->{interactive_heights}}-1) { $dc->SetPen($self->{original_pen});
my $z = $self->{original_layers}[$i]; @points = ();
my $layer_h = $self->{interactive_heights}[$i]; foreach my $pixel (0..$size[1]) {
my @z = $self->pixel_to_point(Wx::Point->new(0, $pixel));
my $h = $self->{original_height_spline}->getLayerHeightAt($z[1]);
my $p = $self->point_to_pixel($h, $z[1]);
push (@points, $p);
}
$dc->DrawLines(\@points);
}
# # draw interactive (user modified) layers as lines
# $last_z = 0.0;
# @points = ();
# if($self->{interactive_heights}) {
# foreach my $i (0..@{$self->{interactive_heights}}-1) {
# my $z = $self->{original_layers}[$i];
# my $layer_h = $self->{interactive_heights}[$i];
# $dc->SetPen($self->{interactive_pen});
# my $pl = $self->point_to_pixel(0, $z);
# my $pr = $self->point_to_pixel($layer_h, $z);
# $dc->DrawLine($pl->x, $pl->y, $pr->x, $pr->y);
# push (@points, $pr);
# }
#
# $dc->DrawSpline(\@points);
# }
if($self->{interactive_height_spline}) {
$last_z = 0.0;
@points = ();
# draw layer lines
foreach my $z (@{$self->{interactive_height_spline}->getInterpolatedLayers}) {
my $layer_h = $z - $last_z;
$dc->SetPen($self->{interactive_pen}); $dc->SetPen($self->{interactive_pen});
my $pl = $self->point_to_pixel(0, $z); my $pl = $self->point_to_pixel(0, $z);
my $pr = $self->point_to_pixel($layer_h, $z); my $pr = $self->point_to_pixel($layer_h, $z);
$dc->DrawLine($pl->x, $pl->y, $pr->x, $pr->y); $dc->DrawLine($pl->x, $pl->y, $pr->x, $pr->y);
push (@points, $pr); $last_z = $z;
} }
$dc->DrawSpline(\@points); #draw spline
$dc->SetPen($self->{interactive_pen});
@points = ();
foreach my $pixel (0..$size[1]) {
my @z = $self->pixel_to_point(Wx::Point->new(0, $pixel));
my $h = $self->{interactive_height_spline}->getLayerHeightAt($z[1]);
my $p = $self->point_to_pixel($h, $z[1]);
push (@points, $p);
}
$dc->DrawLines(\@points);
} }
@ -152,10 +193,12 @@ sub mouse_event {
if ($event->LeftDown) { if ($event->LeftDown) {
# start dragging # start dragging
$self->{left_drag_start_pos} = $pos; $self->{left_drag_start_pos} = $pos;
$self->{interactive_height_spline} = $self->{object}->layer_height_spline->clone;
} }
if ($event->RightDown) { if ($event->RightDown) {
# start dragging # start dragging
$self->{right_drag_start_pos} = $pos; $self->{right_drag_start_pos} = $pos;
$self->{interactive_height_spline} = $self->{object}->layer_height_spline->clone;
} }
} elsif ($event->LeftUp) { } elsif ($event->LeftUp) {
if($self->{left_drag_start_pos}) { if($self->{left_drag_start_pos}) {
@ -171,6 +214,7 @@ sub mouse_event {
$self->Refresh; $self->Refresh;
$self->{object}->layer_height_spline->suppressUpdate; $self->{object}->layer_height_spline->suppressUpdate;
$self->{on_layer_update}->(@{$self->{interpolated_layers}}); $self->{on_layer_update}->(@{$self->{interpolated_layers}});
$self->{interactive_height_spline} = undef;
} }
$self->{left_drag_start_pos} = undef; $self->{left_drag_start_pos} = undef;
} elsif ($event->RightUp) { } elsif ($event->RightUp) {
@ -187,6 +231,7 @@ sub mouse_event {
$self->Refresh; $self->Refresh;
$self->{object}->layer_height_spline->suppressUpdate; $self->{object}->layer_height_spline->suppressUpdate;
$self->{on_layer_update}->(@{$self->{interpolated_layers}}); $self->{on_layer_update}->(@{$self->{interpolated_layers}});
$self->{interactive_height_spline} = undef;
} }
$self->{right_drag_start_pos} = undef; $self->{right_drag_start_pos} = undef;
} elsif ($event->Dragging) { } elsif ($event->Dragging) {
@ -197,6 +242,10 @@ sub mouse_event {
# compute updated interactive layer heights # compute updated interactive layer heights
$self->_interactive_quadratic_curve($start_pos[1], $obj_pos[0], $range); $self->_interactive_quadratic_curve($start_pos[1], $obj_pos[0], $range);
unless($self->{interactive_height_spline}->updateLayerHeights($self->{interactive_heights})) {
die "Unable to update interactive interpolated layers!\n";
}
$self->Refresh; $self->Refresh;
} elsif($self->{right_drag_start_pos}) { } elsif($self->{right_drag_start_pos}) {
my @start_pos = $self->pixel_to_point($self->{right_drag_start_pos}); my @start_pos = $self->pixel_to_point($self->{right_drag_start_pos});
@ -204,6 +253,9 @@ sub mouse_event {
# compute updated interactive layer heights # compute updated interactive layer heights
$self->_interactive_linear_curve($start_pos[1], $obj_pos[0], $range); $self->_interactive_linear_curve($start_pos[1], $obj_pos[0], $range);
unless($self->{interactive_height_spline}->updateLayerHeights($self->{interactive_heights})) {
die "Unable to update interactive interpolated layers!\n";
}
$self->Refresh; $self->Refresh;
} }
} elsif ($event->Moving) { } elsif ($event->Moving) {
@ -236,6 +288,7 @@ sub update {
my $self = shift; my $self = shift;
if($self->{object}->layer_height_spline->layersUpdated) { if($self->{object}->layer_height_spline->layersUpdated) {
$self->{original_height_spline} = $self->{object}->layer_height_spline->clone;
$self->{original_layers} = $self->{object}->layer_height_spline->getOriginalLayers; $self->{original_layers} = $self->{object}->layer_height_spline->getOriginalLayers;
$self->{original_interpolated_layers} = $self->{object}->layer_height_spline->getInterpolatedLayers; $self->{original_interpolated_layers} = $self->{object}->layer_height_spline->getInterpolatedLayers;
$self->{interpolated_layers} = $self->{object}->layer_height_spline->getInterpolatedLayers; # Initialize to current values $self->{interpolated_layers} = $self->{object}->layer_height_spline->getInterpolatedLayers; # Initialize to current values

View File

@ -21,6 +21,23 @@ LayerHeightSpline::LayerHeightSpline(coordf_t object_height)
this->_cusp_value = -1; this->_cusp_value = -1;
} }
LayerHeightSpline::LayerHeightSpline(const LayerHeightSpline &other)
: _object_height(other._object_height), _layer_height_spline(NULL)
{
this->_original_layers = other._original_layers;
this->_internal_layers = other._internal_layers;
this->_internal_layer_heights = other._internal_layer_heights;
this->_is_valid = other._is_valid;
this->_update_required = other._update_required;
this->_layers_updated = other._layers_updated;
this->_layer_heights_updated = other._layer_heights_updated;
this->_cusp_value = other._cusp_value;
if(this->_is_valid) {
this->_updateBSpline();
}
}
LayerHeightSpline::~LayerHeightSpline() LayerHeightSpline::~LayerHeightSpline()
{ {
if (this->_layer_height_spline) { if (this->_layer_height_spline) {
@ -80,9 +97,9 @@ bool LayerHeightSpline::setLayers(std::vector<coordf_t> layers)
// add 0-values at both ends to achieve correct boundary conditions // add 0-values at both ends to achieve correct boundary conditions
this->_internal_layers = this->_original_layers; this->_internal_layers = this->_original_layers;
this->_internal_layers.insert(this->_internal_layers.begin(), 0); // add z = 0 to the front this->_internal_layers.insert(this->_internal_layers.begin(), 0); // add z = 0 to the front
this->_internal_layers.push_back(this->_internal_layers.back()+1); // and object_height + 1 to the end //this->_internal_layers.push_back(this->_internal_layers.back()+1); // and object_height + 1 to the end
this->_internal_layer_heights.insert(this->_internal_layer_heights.begin(), 0); this->_internal_layer_heights.insert(this->_internal_layer_heights.begin(), this->_internal_layer_heights[0]);
this->_internal_layer_heights.push_back(0); //this->_internal_layer_heights.push_back(0);
this->_layers_updated = true; this->_layers_updated = true;
this->_layer_heights_updated = false; this->_layer_heights_updated = false;
@ -102,12 +119,14 @@ bool LayerHeightSpline::updateLayerHeights(std::vector<coordf_t> heights)
bool result = false; bool result = false;
// do we receive the correct number of values? // do we receive the correct number of values?
if(heights.size() == this->_internal_layers.size()-2) { if(heights.size() == this->_internal_layers.size()-1) {
this->_internal_layer_heights = heights; this->_internal_layer_heights = heights;
// add leading an trailing 0-value // add leading and trailing 0-value
this->_internal_layer_heights.insert(this->_internal_layer_heights.begin(), 0); this->_internal_layer_heights.insert(this->_internal_layer_heights.begin(), this->_internal_layer_heights[0]);
this->_internal_layer_heights.push_back(0); //this->_internal_layer_heights.push_back(0);
result = this->_updateBSpline(); result = this->_updateBSpline();
}else{
std::cerr << "Unable to update layer heights. You provided " << heights.size() << " layers, but " << this->_internal_layers.size()-1 << " expected" << std::endl;
} }
this->_layers_updated = false; this->_layers_updated = false;
@ -186,7 +205,7 @@ bool LayerHeightSpline::_updateBSpline()
this->_internal_layers.size(), this->_internal_layers.size(),
&this->_internal_layer_heights[0], &this->_internal_layer_heights[0],
0, 0,
0, 1,
0); 0);
if (this->_layer_height_spline->ok()) { if (this->_layer_height_spline->ok()) {

View File

@ -12,6 +12,7 @@ class LayerHeightSpline
{ {
public: public:
LayerHeightSpline(coordf_t object_height); LayerHeightSpline(coordf_t object_height);
LayerHeightSpline(const LayerHeightSpline &other);
~LayerHeightSpline(); ~LayerHeightSpline();
bool hasData(); // indicate that we have valid data bool hasData(); // indicate that we have valid data
bool updateRequired(); // indicate whether we want to generate a new spline from the layers bool updateRequired(); // indicate whether we want to generate a new spline from the layers

View File

@ -6,7 +6,10 @@
%} %}
%name{Slic3r::LayerHeightSpline} class LayerHeightSpline { %name{Slic3r::LayerHeightSpline} class LayerHeightSpline {
// owned by PrintObject, no constructor/destructor LayerHeightSpline(double object_height);
~LayerHeightSpline();
Clone<LayerHeightSpline> clone()
%code%{ RETVAL = THIS; %};
bool hasData(); bool hasData();
bool updateRequired(); bool updateRequired();
@ -21,7 +24,6 @@
std::vector<double> getOriginalLayers(); std::vector<double> getOriginalLayers();
std::vector<double> getInterpolatedLayers(); std::vector<double> getInterpolatedLayers();
coordf_t getLayerHeightAt(coordf_t height); coordf_t getLayerHeightAt(coordf_t height);
//%code%{ RETVAL = THIS->upper_layer; %};
void setCuspValue(coordf_t cusp_value); void setCuspValue(coordf_t cusp_value);
coordf_t getCuspValue(); coordf_t getCuspValue();

View File

@ -180,6 +180,7 @@ Ref<SupportLayer> O_OBJECT_SLIC3R_T
LayerHeightSpline* O_OBJECT_SLIC3R LayerHeightSpline* O_OBJECT_SLIC3R
Ref<LayerHeightSpline> O_OBJECT_SLIC3R_T Ref<LayerHeightSpline> O_OBJECT_SLIC3R_T
Clone<LayerHeightSpline> O_OBJECT_SLIC3R_T
PlaceholderParser* O_OBJECT_SLIC3R PlaceholderParser* O_OBJECT_SLIC3R
Ref<PlaceholderParser> O_OBJECT_SLIC3R_T Ref<PlaceholderParser> O_OBJECT_SLIC3R_T

View File

@ -135,6 +135,7 @@
%typemap{LayerHeightSpline*}; %typemap{LayerHeightSpline*};
%typemap{Ref<LayerHeightSpline>}{simple}; %typemap{Ref<LayerHeightSpline>}{simple};
%typemap{Clone<LayerHeightSpline>}{simple};
%typemap{SupportLayer*}; %typemap{SupportLayer*};
%typemap{Ref<SupportLayer>}{simple}; %typemap{Ref<SupportLayer>}{simple};