Memory optimization in PreviewCanvas: don't keep additional meshes in memory when not needed

This commit is contained in:
Alessandro Ranellucci 2015-01-09 01:18:47 +01:00
parent 64c9e3af4b
commit 4c7d9dfef5
3 changed files with 23 additions and 16 deletions

View file

@ -8,7 +8,9 @@ use Slic3r::Geometry qw();
use Slic3r::Geometry::Clipper qw();
use Wx qw(:misc :pen :brush :sizer :font :cursor wxTAB_TRAVERSAL);
use Wx::Event qw();
use base 'Slic3r::GUI::PreviewCanvas';
use base qw(Slic3r::GUI::PreviewCanvas Class::Accessor);
__PACKAGE__->mk_accessors(qw(_volumes _volumes_inv));
sub new {
my $class = shift;
@ -29,8 +31,8 @@ sub new {
my $obj_idx = undef;
if ($volume_idx != -1) {
$obj_idx = $self->{_volumes_inv}{$volume_idx};
$self->volumes->[$_]->selected(1) for @{$self->{_volumes}{$obj_idx}};
$obj_idx = $self->_volumes_inv->{$volume_idx};
$self->volumes->[$_]->selected(1) for @{$self->_volumes->{$obj_idx}};
$self->Refresh;
}
$self->{on_select_object}->($obj_idx)
@ -39,14 +41,14 @@ sub new {
$self->on_hover(sub {
my ($volume_idx) = @_;
my $obj_idx = $self->{_volumes_inv}{$volume_idx};
$self->volumes->[$_]->hover(1) for @{$self->{_volumes}{$obj_idx}};
my $obj_idx = $self->_volumes_inv->{$volume_idx};
$self->volumes->[$_]->hover(1) for @{$self->_volumes->{$obj_idx}};
});
$self->on_move(sub {
my ($volume_idx) = @_;
my $volume = $self->volumes->[$volume_idx];
my $obj_idx = $self->{_volumes_inv}{$volume_idx};
my $obj_idx = $self->_volumes_inv->{$volume_idx};
my $model_object = $self->{model}->get_object($obj_idx);
$model_object
->instances->[$volume->instance_idx]
@ -84,8 +86,8 @@ sub set_on_instance_moved {
sub update {
my ($self) = @_;
$self->{_volumes} = {}; # obj_idx => [ volume_idx, volume_idx ]
$self->{_volumes_inv} = {}; # volume_idx => obj_idx
$self->_volumes({}); # obj_idx => [ volume_idx, volume_idx ]
$self->_volumes_inv({}); # volume_idx => obj_idx
$self->reset_objects;
$self->update_bed_size;
@ -95,8 +97,8 @@ sub update {
my @volume_idxs = $self->load_object($model_object, 1);
# store mapping between canvas volumes and model objects
$self->{_volumes}{$obj_idx} = [ @volume_idxs ];
$self->{_volumes_inv}{$_} = $obj_idx for @volume_idxs;
$self->_volumes->{$obj_idx} = [ @volume_idxs ];
$self->_volumes_inv->{$_} = $obj_idx for @volume_idxs;
if ($self->{objects}[$obj_idx]->selected) {
$self->select_volume($_) for @volume_idxs;

View file

@ -87,6 +87,7 @@ sub new {
my $canvas;
if ($Slic3r::GUI::have_OpenGL) {
$canvas = $self->{canvas} = Slic3r::GUI::PreviewCanvas->new($self);
$canvas->enable_cutting(1);
$canvas->load_object($self->{model_object});
$canvas->set_auto_bed_shape;
$canvas->SetSize([500,500]);

View file

@ -13,6 +13,7 @@ use Slic3r::Geometry::Clipper qw(offset_ex intersection_pl);
use Wx::GLCanvas qw(:all);
__PACKAGE__->mk_accessors( qw(_quat _dirty init
enable_cutting
enable_picking
enable_moving
on_hover
@ -256,7 +257,7 @@ sub zoom_to_volume {
my ($self, $volume_idx) = @_;
my $volume = $self->volumes->[$volume_idx];
my $bb = $volume->bounding_box;
my $bb = $volume->transformed_bounding_box;
$self->zoom_to_bounding_box($bb);
}
@ -269,7 +270,7 @@ sub volumes_bounding_box {
my ($self) = @_;
my $bb = Slic3r::Geometry::BoundingBoxf3->new;
$bb->merge($_->bounding_box) for @{$self->volumes};
$bb->merge($_->transformed_bounding_box) for @{$self->volumes};
return $bb;
}
@ -375,11 +376,12 @@ sub load_object {
my $color = [ @{COLORS->[ $color_idx % scalar(@{&COLORS}) ]} ];
push @$color, $volume->modifier ? 0.5 : 1;
push @{$self->volumes}, my $v = Slic3r::GUI::PreviewCanvas::Volume->new(
bounding_box => $mesh->bounding_box,
group_id => $group_id,
instance_idx => $instance_idx,
mesh => $mesh,
color => $color,
);
$v->mesh($mesh) if $self->enable_cutting;
push @volumes_idx, $#{$self->volumes};
{
@ -419,6 +421,7 @@ sub SetCuttingPlane {
my @verts = ();
foreach my $volume (@{$self->volumes}) {
foreach my $volume (@{$self->volumes}) {
next if !$volume->mesh;
my $expolygons = $volume->mesh->slice([ $z - $volume->origin->z ])->[0];
$expolygons = offset_ex([ map @$_, @$expolygons ], scale 0.1);
@ -940,7 +943,8 @@ sub draw_volumes {
package Slic3r::GUI::PreviewCanvas::Volume;
use Moo;
has 'mesh' => (is => 'ro', required => 1);
has 'mesh' => (is => 'rw', required => 0); # only required for cut contours
has 'bounding_box' => (is => 'ro', required => 1);
has 'color' => (is => 'ro', required => 1);
has 'group_id' => (is => 'ro', required => 1);
has 'instance_idx' => (is => 'ro', default => sub { 0 });
@ -950,10 +954,10 @@ has 'norms' => (is => 'rw');
has 'selected' => (is => 'rw', default => sub { 0 });
has 'hover' => (is => 'rw', default => sub { 0 });
sub bounding_box {
sub transformed_bounding_box {
my ($self) = @_;
my $bb = $self->mesh->bounding_box;
my $bb = $self->bounding_box;
$bb->translate(@{$self->origin});
return $bb;
}