Faster loading of 3D preview and much less memory used

This commit is contained in:
Alessandro Ranellucci 2015-01-24 23:35:29 +01:00
parent 8cfd2e33d8
commit a5c0ffe963
8 changed files with 202 additions and 163 deletions

View File

@ -210,6 +210,7 @@ sub thread_cleanup {
*Slic3r::Polyline::DESTROY = sub {}; *Slic3r::Polyline::DESTROY = sub {};
*Slic3r::Polyline::Collection::DESTROY = sub {}; *Slic3r::Polyline::Collection::DESTROY = sub {};
*Slic3r::Print::DESTROY = sub {}; *Slic3r::Print::DESTROY = sub {};
*Slic3r::Print::Object::DESTROY = sub {};
*Slic3r::Print::Region::DESTROY = sub {}; *Slic3r::Print::Region::DESTROY = sub {};
*Slic3r::Surface::DESTROY = sub {}; *Slic3r::Surface::DESTROY = sub {};
*Slic3r::Surface::Collection::DESTROY = sub {}; *Slic3r::Surface::Collection::DESTROY = sub {};

View File

@ -844,7 +844,7 @@ sub draw_volumes {
} }
glCullFace(GL_BACK); glCullFace(GL_BACK);
if ($volume->quad_verts) { if ($volume->qverts) {
my ($min_offset, $max_offset); my ($min_offset, $max_offset);
if (defined $min_z) { if (defined $min_z) {
$min_offset = $volume->offsets->{$min_z}->[0]; $min_offset = $volume->offsets->{$min_z}->[0];
@ -853,14 +853,14 @@ sub draw_volumes {
$max_offset = $volume->offsets->{$max_z}->[0]; $max_offset = $volume->offsets->{$max_z}->[0];
} }
$min_offset //= 0; $min_offset //= 0;
$max_offset //= $volume->quad_verts->elements; $max_offset //= $volume->qverts->size;
glVertexPointer_p(3, $volume->quad_verts); glVertexPointer_c(3, GL_FLOAT, 0, $volume->qverts->verts_ptr);
glNormalPointer_p($volume->quad_norms); glNormalPointer_c(GL_FLOAT, 0, $volume->qverts->norms_ptr);
glDrawArrays(GL_QUADS, $min_offset / 3, ($max_offset-$min_offset) / 3); glDrawArrays(GL_QUADS, $min_offset / 3, ($max_offset-$min_offset) / 3);
} }
if ($volume->verts) { if ($volume->tverts) {
my ($min_offset, $max_offset); my ($min_offset, $max_offset);
if (defined $min_z) { if (defined $min_z) {
$min_offset = $volume->offsets->{$min_z}->[1]; $min_offset = $volume->offsets->{$min_z}->[1];
@ -869,10 +869,10 @@ sub draw_volumes {
$max_offset = $volume->offsets->{$max_z}->[1]; $max_offset = $volume->offsets->{$max_z}->[1];
} }
$min_offset //= 0; $min_offset //= 0;
$max_offset //= $volume->verts->elements; $max_offset //= $volume->tverts->size;
glVertexPointer_p(3, $volume->verts); glVertexPointer_c(3, GL_FLOAT, 0, $volume->tverts->verts_ptr);
glNormalPointer_p($volume->norms); glNormalPointer_c(GL_FLOAT, 0, $volume->tverts->norms_ptr);
glDrawArrays(GL_TRIANGLES, $min_offset / 3, ($max_offset-$min_offset) / 3); glDrawArrays(GL_TRIANGLES, $min_offset / 3, ($max_offset-$min_offset) / 3);
} }
@ -903,12 +903,10 @@ has 'hover' => (is => 'rw', default => sub { 0 });
has 'range' => (is => 'rw'); has 'range' => (is => 'rw');
# geometric data # geometric data
has 'quad_verts' => (is => 'rw'); # OpenGL::Array object has 'qverts' => (is => 'rw'); # GLVertexArray object
has 'quad_norms' => (is => 'rw'); # OpenGL::Array object has 'tverts' => (is => 'rw'); # GLVertexArray object
has 'verts' => (is => 'rw'); # OpenGL::Array object
has 'norms' => (is => 'rw'); # OpenGL::Array object
has 'mesh' => (is => 'rw'); # only required for cut contours has 'mesh' => (is => 'rw'); # only required for cut contours
has 'offsets' => (is => 'rw'); # [ z => [ quad_verts_idx, verts_idx ] ] has 'offsets' => (is => 'rw'); # [ z => [ qverts_idx, tverts_idx ] ]
sub transformed_bounding_box { sub transformed_bounding_box {
my ($self) = @_; my ($self) = @_;
@ -999,17 +997,10 @@ sub load_object {
} }
push @volumes_idx, my $scene_volume_idx = $#{$self->volumes}; push @volumes_idx, my $scene_volume_idx = $#{$self->volumes};
$self->_objects_by_volumes->{$scene_volume_idx} = [ $obj_idx, $volume_idx, $instance_idx ]; $self->_objects_by_volumes->{$scene_volume_idx} = [ $obj_idx, $volume_idx, $instance_idx ];
{ my $verts = Slic3r::GUI::_3DScene::GLVertexArray->new;
my $vertices = $mesh->vertices; $verts->load_mesh($mesh);
my @verts = map @{ $vertices->[$_] }, map @$_, @{$mesh->facets}; $v->tverts($verts);
$v->verts(OpenGL::Array->new_list(GL_FLOAT, @verts));
}
{
my @norms = map { @$_, @$_, @$_ } @{$mesh->normals};
$v->norms(OpenGL::Array->new_list(GL_FLOAT, @norms));
}
} }
} }
@ -1074,9 +1065,12 @@ sub load_print_object_slices {
sub load_print_object_toolpaths { sub load_print_object_toolpaths {
my ($self, $object) = @_; my ($self, $object) = @_;
my (@perim_qverts, @perim_qnorms, @perim_tverts, @perim_tnorms) = (); my $perim_qverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
my (@infill_qverts, @infill_qnorms, @infill_tverts, @infill_tnorms) = (); my $perim_tverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
my (@support_qverts, @support_qnorms, @support_tverts, @support_tnorms) = (); my $infill_qverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
my $infill_tverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
my $support_qverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
my $support_tverts = Slic3r::GUI::_3DScene::GLVertexArray->new;
my %perim_offsets = (); # print_z => [ qverts, tverts ] my %perim_offsets = (); # print_z => [ qverts, tverts ]
my %infill_offsets = (); my %infill_offsets = ();
@ -1091,13 +1085,13 @@ sub load_print_object_toolpaths {
if (!exists $perim_offsets{$top_z}) { if (!exists $perim_offsets{$top_z}) {
$perim_offsets{$top_z} = [ $perim_offsets{$top_z} = [
scalar(@perim_qverts), scalar(@perim_tverts), $perim_qverts->size, $perim_tverts->size,
]; ];
$infill_offsets{$top_z} = [ $infill_offsets{$top_z} = [
scalar(@infill_qverts), scalar(@infill_tverts), $infill_qverts->size, $infill_tverts->size,
]; ];
$support_offsets{$top_z} = [ $support_offsets{$top_z} = [
scalar(@support_qverts), scalar(@support_tverts), $support_qverts->size, $support_tverts->size,
]; ];
} }
@ -1105,21 +1099,21 @@ sub load_print_object_toolpaths {
foreach my $layerm (@{$layer->regions}) { foreach my $layerm (@{$layer->regions}) {
if ($object->step_done(STEP_PERIMETERS)) { if ($object->step_done(STEP_PERIMETERS)) {
$self->_extrusionentity_to_verts($layerm->perimeters, $top_z, $copy, $self->_extrusionentity_to_verts($layerm->perimeters, $top_z, $copy,
\@perim_qverts, \@perim_qnorms, \@perim_tverts, \@perim_tnorms); $perim_qverts, $perim_tverts);
} }
if ($object->step_done(STEP_INFILL)) { if ($object->step_done(STEP_INFILL)) {
$self->_extrusionentity_to_verts($layerm->fills, $top_z, $copy, $self->_extrusionentity_to_verts($layerm->fills, $top_z, $copy,
\@infill_qverts, \@infill_qnorms, \@infill_tverts, \@infill_tnorms); $infill_qverts, $infill_tverts);
} }
} }
if ($layer->isa('Slic3r::Layer::Support') && $object->step_done(STEP_SUPPORTMATERIAL)) { if ($layer->isa('Slic3r::Layer::Support') && $object->step_done(STEP_SUPPORTMATERIAL)) {
$self->_extrusionentity_to_verts($layer->support_fills, $top_z, $copy, $self->_extrusionentity_to_verts($layer->support_fills, $top_z, $copy,
\@support_qverts, \@support_qnorms, \@support_tverts, \@support_tnorms); $support_qverts, $support_tverts);
$self->_extrusionentity_to_verts($layer->support_interface_fills, $top_z, $copy, $self->_extrusionentity_to_verts($layer->support_interface_fills, $top_z, $copy,
\@support_qverts, \@support_qnorms, \@support_tverts, \@support_tnorms); $support_qverts, $support_tverts);
} }
} }
} }
@ -1136,30 +1130,24 @@ sub load_print_object_toolpaths {
push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new( push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new(
bounding_box => $bb, bounding_box => $bb,
color => COLORS->[0], color => COLORS->[0],
quad_verts => OpenGL::Array->new_list(GL_FLOAT, @perim_qverts), qverts => $perim_qverts,
quad_norms => OpenGL::Array->new_list(GL_FLOAT, @perim_qnorms), tverts => $perim_tverts,
verts => OpenGL::Array->new_list(GL_FLOAT, @perim_tverts),
norms => OpenGL::Array->new_list(GL_FLOAT, @perim_tnorms),
offsets => { %perim_offsets }, offsets => { %perim_offsets },
); );
push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new( push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new(
bounding_box => $bb, bounding_box => $bb,
color => COLORS->[1], color => COLORS->[1],
quad_verts => OpenGL::Array->new_list(GL_FLOAT, @infill_qverts), qverts => $infill_qverts,
quad_norms => OpenGL::Array->new_list(GL_FLOAT, @infill_qnorms), tverts => $infill_tverts,
verts => OpenGL::Array->new_list(GL_FLOAT, @infill_tverts),
norms => OpenGL::Array->new_list(GL_FLOAT, @infill_tnorms),
offsets => { %infill_offsets }, offsets => { %infill_offsets },
); );
push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new( push @{$self->volumes}, Slic3r::GUI::3DScene::Volume->new(
bounding_box => $bb, bounding_box => $bb,
color => COLORS->[2], color => COLORS->[2],
quad_verts => OpenGL::Array->new_list(GL_FLOAT, @support_qverts), qverts => $support_qverts,
quad_norms => OpenGL::Array->new_list(GL_FLOAT, @support_qnorms), tverts => $support_tverts,
verts => OpenGL::Array->new_list(GL_FLOAT, @support_tverts),
norms => OpenGL::Array->new_list(GL_FLOAT, @support_tnorms),
offsets => { %support_offsets }, offsets => { %support_offsets },
); );
} }
@ -1201,11 +1189,11 @@ sub _expolygons_to_verts {
} }
sub _extrusionentity_to_verts { sub _extrusionentity_to_verts {
my ($self, $entity, $top_z, $copy, $qverts, $qnorms, $tverts, $tnorms) = @_; my ($self, $entity, $top_z, $copy, $qverts, $tverts) = @_;
my ($lines, $widths, $heights, $closed); my ($lines, $widths, $heights, $closed);
if ($entity->isa('Slic3r::ExtrusionPath::Collection')) { if ($entity->isa('Slic3r::ExtrusionPath::Collection')) {
$self->_extrusionentity_to_verts($_, $top_z, $copy, $qverts, $qnorms, $tverts, $tnorms) $self->_extrusionentity_to_verts($_, $top_z, $copy, $qverts, $tverts)
for @$entity; for @$entity;
return; return;
} elsif ($entity->isa('Slic3r::ExtrusionPath')) { } elsif ($entity->isa('Slic3r::ExtrusionPath')) {
@ -1231,7 +1219,7 @@ sub _extrusionentity_to_verts {
} }
Slic3r::GUI::_3DScene::_extrusionentity_to_verts_do($lines, $widths, $heights, Slic3r::GUI::_3DScene::_extrusionentity_to_verts_do($lines, $widths, $heights,
$closed, $top_z, $copy, $qverts, $qnorms, $tverts, $tnorms); $closed, $top_z, $copy, $qverts, $tverts);
} }
sub object_idx { sub object_idx {

View File

@ -196,6 +196,9 @@ sub new {
return $self; return $self;
} }
package Slic3r::GUI::_3DScene::GLVertexArray;
sub CLONE_SKIP { 1 }
package main; package main;
for my $class (qw( for my $class (qw(
Slic3r::BridgeDetector Slic3r::BridgeDetector

View File

@ -5,7 +5,7 @@ namespace Slic3r {
void void
_3DScene::_extrusionentity_to_verts_do(const Lines &lines, const std::vector<double> &widths, _3DScene::_extrusionentity_to_verts_do(const Lines &lines, const std::vector<double> &widths,
const std::vector<double> &heights, bool closed, double top_z, const Point &copy, const std::vector<double> &heights, bool closed, double top_z, const Point &copy,
Pointf3s* qverts, Pointf3s* qnorms, Pointf3s* tverts, Pointf3s* tnorms) GLVertexArray* qverts, GLVertexArray* tverts)
{ {
Line prev_line; Line prev_line;
Pointf prev_b1, prev_b2; Pointf prev_b1, prev_b2;
@ -52,62 +52,64 @@ _3DScene::_extrusionentity_to_verts_do(const Lines &lines, const std::vector<dou
// if we're making a ccw turn, draw the triangles on the right side, otherwise draw them on the left side // if we're making a ccw turn, draw the triangles on the right side, otherwise draw them on the left side
double ccw = line.b.ccw(prev_line); double ccw = line.b.ccw(prev_line);
if (ccw > EPSILON) { if (ccw > EPSILON) {
tverts->reserve_more(6);
// top-right vertex triangle between previous line and this one // top-right vertex triangle between previous line and this one
{ {
// use the normal going to the right calculated for the previous line // use the normal going to the right calculated for the previous line
tnorms->push_back(prev_xy_right_normal); tverts->push_norm(prev_xy_right_normal);
tverts->push_back(Pointf3(prev_b1.x, prev_b1.y, middle_z)); tverts->push_vert(prev_b1.x, prev_b1.y, middle_z);
// use the normal going to the right calculated for this line // use the normal going to the right calculated for this line
tnorms->push_back(xy_right_normal); tverts->push_norm(xy_right_normal);
tverts->push_back(Pointf3(a1.x, a1.y, middle_z)); tverts->push_vert(a1.x, a1.y, middle_z);
// normal going upwards // normal going upwards
tnorms->push_back(Pointf3(0,0,1)); tverts->push_norm(0,0,1);
tverts->push_back(Pointf3(a.x, a.y, top_z)); tverts->push_vert(a.x, a.y, top_z);
} }
// bottom-right vertex triangle between previous line and this one // bottom-right vertex triangle between previous line and this one
{ {
// use the normal going to the right calculated for the previous line // use the normal going to the right calculated for the previous line
tnorms->push_back(prev_xy_right_normal); tverts->push_norm(prev_xy_right_normal);
tverts->push_back(Pointf3(prev_b1.x, prev_b1.y, middle_z)); tverts->push_vert(prev_b1.x, prev_b1.y, middle_z);
// normal going downwards // normal going downwards
tnorms->push_back(Pointf3(0,0,-1)); tverts->push_norm(0,0,-1);
tverts->push_back(Pointf3(a.x, a.y, bottom_z)); tverts->push_vert(a.x, a.y, bottom_z);
// use the normal going to the right calculated for this line // use the normal going to the right calculated for this line
tnorms->push_back(xy_right_normal); tverts->push_norm(xy_right_normal);
tverts->push_back(Pointf3(a1.x, a1.y, middle_z)); tverts->push_vert(a1.x, a1.y, middle_z);
} }
} else if (ccw < -EPSILON) { } else if (ccw < -EPSILON) {
tverts->reserve_more(6);
// top-left vertex triangle between previous line and this one // top-left vertex triangle between previous line and this one
{ {
// use the normal going to the left calculated for the previous line // use the normal going to the left calculated for the previous line
tnorms->push_back(prev_xy_left_normal); tverts->push_norm(prev_xy_left_normal);
tverts->push_back(Pointf3(prev_b2.x, prev_b2.y, middle_z)); tverts->push_vert(prev_b2.x, prev_b2.y, middle_z);
// normal going upwards // normal going upwards
tnorms->push_back(Pointf3(0,0,1)); tverts->push_norm(0,0,1);
tverts->push_back(Pointf3(a.x, a.y, top_z)); tverts->push_vert(a.x, a.y, top_z);
// use the normal going to the right calculated for this line // use the normal going to the right calculated for this line
tnorms->push_back(xy_left_normal); tverts->push_norm(xy_left_normal);
tverts->push_back(Pointf3(a2.x, a2.y, middle_z)); tverts->push_vert(a2.x, a2.y, middle_z);
} }
// bottom-left vertex triangle between previous line and this one // bottom-left vertex triangle between previous line and this one
{ {
// use the normal going to the left calculated for the previous line // use the normal going to the left calculated for the previous line
tnorms->push_back(prev_xy_left_normal); tverts->push_norm(prev_xy_left_normal);
tverts->push_back(Pointf3(prev_b2.x, prev_b2.y, middle_z)); tverts->push_vert(prev_b2.x, prev_b2.y, middle_z);
// use the normal going to the right calculated for this line // use the normal going to the right calculated for this line
tnorms->push_back(xy_left_normal); tverts->push_norm(xy_left_normal);
tverts->push_back(Pointf3(a2.x, a2.y, middle_z)); tverts->push_vert(a2.x, a2.y, middle_z);
// normal going downwards // normal going downwards
tnorms->push_back(Pointf3(0,0,-1)); tverts->push_norm(0,0,-1);
tverts->push_back(Pointf3(a.x, a.y, bottom_z)); tverts->push_vert(a.x, a.y, bottom_z);
} }
} }
} }
@ -124,97 +126,121 @@ _3DScene::_extrusionentity_to_verts_do(const Lines &lines, const std::vector<dou
if (!closed) { if (!closed) {
// terminate open paths with caps // terminate open paths with caps
if (i == 0) { if (i == 0) {
qverts->reserve_more(4);
// normal pointing downwards // normal pointing downwards
qnorms->push_back(Pointf3(0,0,-1)); qverts->push_norm(0,0,-1);
qverts->push_back(Pointf3(a.x, a.y, bottom_z)); qverts->push_vert(a.x, a.y, bottom_z);
// normal pointing to the right // normal pointing to the right
qnorms->push_back(xy_right_normal); qverts->push_norm(xy_right_normal);
qverts->push_back(Pointf3(a1.x, a1.y, middle_z)); qverts->push_vert(a1.x, a1.y, middle_z);
// normal pointing upwards // normal pointing upwards
qnorms->push_back(Pointf3(0,0,1)); qverts->push_norm(0,0,1);
qverts->push_back(Pointf3(a.x, a.y, top_z)); qverts->push_vert(a.x, a.y, top_z);
// normal pointing to the left // normal pointing to the left
qnorms->push_back(xy_left_normal); qverts->push_norm(xy_left_normal);
qverts->push_back(Pointf3(a2.x, a2.y, middle_z)); qverts->push_vert(a2.x, a2.y, middle_z);
} else if (i == lines.size()-1) { } else if (i == lines.size()-1) {
qverts->reserve_more(4);
// normal pointing downwards // normal pointing downwards
qnorms->push_back(Pointf3(0,0,-1)); qverts->push_norm(0,0,-1);
qverts->push_back(Pointf3(b.x, b.y, bottom_z)); qverts->push_vert(b.x, b.y, bottom_z);
// normal pointing to the left // normal pointing to the left
qnorms->push_back(xy_left_normal); qverts->push_norm(xy_left_normal);
qverts->push_back(Pointf3(b2.x, b2.y, middle_z)); qverts->push_vert(b2.x, b2.y, middle_z);
// normal pointing upwards // normal pointing upwards
qnorms->push_back(Pointf3(0,0,1)); qverts->push_norm(0,0,1);
qverts->push_back(Pointf3(b.x, b.y, top_z)); qverts->push_vert(b.x, b.y, top_z);
// normal pointing to the right // normal pointing to the right
qnorms->push_back(xy_right_normal); qverts->push_norm(xy_right_normal);
qverts->push_back(Pointf3(b1.x, b1.y, middle_z)); qverts->push_vert(b1.x, b1.y, middle_z);
} }
} }
qverts->reserve_more(16);
// bottom-right face // bottom-right face
{ {
// normal going downwards // normal going downwards
qnorms->push_back(Pointf3(0,0,-1)); qverts->push_norm(0,0,-1);
qnorms->push_back(Pointf3(0,0,-1)); qverts->push_norm(0,0,-1);
qverts->push_back(Pointf3(a.x, a.y, bottom_z)); qverts->push_vert(a.x, a.y, bottom_z);
qverts->push_back(Pointf3(b.x, b.y, bottom_z)); qverts->push_vert(b.x, b.y, bottom_z);
qnorms->push_back(xy_right_normal); qverts->push_norm(xy_right_normal);
qnorms->push_back(xy_right_normal); qverts->push_norm(xy_right_normal);
qverts->push_back(Pointf3(b1.x, b1.y, middle_z)); qverts->push_vert(b1.x, b1.y, middle_z);
qverts->push_back(Pointf3(a1.x, a1.y, middle_z)); qverts->push_vert(a1.x, a1.y, middle_z);
} }
// top-right face // top-right face
{ {
qnorms->push_back(xy_right_normal); qverts->push_norm(xy_right_normal);
qnorms->push_back(xy_right_normal); qverts->push_norm(xy_right_normal);
qverts->push_back(Pointf3(a1.x, a1.y, middle_z)); qverts->push_vert(a1.x, a1.y, middle_z);
qverts->push_back(Pointf3(b1.x, b1.y, middle_z)); qverts->push_vert(b1.x, b1.y, middle_z);
// normal going upwards // normal going upwards
qnorms->push_back(Pointf3(0,0,1)); qverts->push_norm(0,0,1);
qnorms->push_back(Pointf3(0,0,1)); qverts->push_norm(0,0,1);
qverts->push_back(Pointf3(b.x, b.y, top_z)); qverts->push_vert(b.x, b.y, top_z);
qverts->push_back(Pointf3(a.x, a.y, top_z)); qverts->push_vert(a.x, a.y, top_z);
} }
// top-left face // top-left face
{ {
qnorms->push_back(Pointf3(0,0,1)); qverts->push_norm(0,0,1);
qnorms->push_back(Pointf3(0,0,1)); qverts->push_norm(0,0,1);
qverts->push_back(Pointf3(a.x, a.y, top_z)); qverts->push_vert(a.x, a.y, top_z);
qverts->push_back(Pointf3(b.x, b.y, top_z)); qverts->push_vert(b.x, b.y, top_z);
qnorms->push_back(xy_left_normal); qverts->push_norm(xy_left_normal);
qnorms->push_back(xy_left_normal); qverts->push_norm(xy_left_normal);
qverts->push_back(Pointf3(b2.x, b2.y, middle_z)); qverts->push_vert(b2.x, b2.y, middle_z);
qverts->push_back(Pointf3(a2.x, a2.y, middle_z)); qverts->push_vert(a2.x, a2.y, middle_z);
} }
// bottom-left face // bottom-left face
{ {
qnorms->push_back(xy_left_normal); qverts->push_norm(xy_left_normal);
qnorms->push_back(xy_left_normal); qverts->push_norm(xy_left_normal);
qverts->push_back(Pointf3(a2.x, a2.y, middle_z)); qverts->push_vert(a2.x, a2.y, middle_z);
qverts->push_back(Pointf3(b2.x, b2.y, middle_z)); qverts->push_vert(b2.x, b2.y, middle_z);
// normal going downwards // normal going downwards
qnorms->push_back(Pointf3(0,0,-1)); qverts->push_norm(0,0,-1);
qnorms->push_back(Pointf3(0,0,-1)); qverts->push_norm(0,0,-1);
qverts->push_back(Pointf3(b.x, b.y, bottom_z)); qverts->push_vert(b.x, b.y, bottom_z);
qverts->push_back(Pointf3(a.x, a.y, bottom_z)); qverts->push_vert(a.x, a.y, bottom_z);
} }
first_done = true; first_done = true;
} }
} }
void
GLVertexArray::load_mesh(const TriangleMesh &mesh)
{
this->reserve_more(3 * mesh.facets_count());
for (int i = 0; i < mesh.stl.stats.number_of_facets; ++i) {
stl_facet &facet = mesh.stl.facet_start[i];
for (int j = 0; j <= 2; ++j) {
this->push_norm(facet.normal.x, facet.normal.y, facet.normal.z);
this->push_vert(facet.vertex[j].x, facet.vertex[j].y, facet.vertex[j].z);
}
}
}
#ifdef SLIC3RXS
REGISTER_CLASS(GLVertexArray, "GUI::_3DScene::GLVertexArray");
#endif
} }

View File

@ -4,15 +4,51 @@
#include <myinit.h> #include <myinit.h>
#include "../Point.hpp" #include "../Point.hpp"
#include "../Line.hpp" #include "../Line.hpp"
#include "../TriangleMesh.hpp"
namespace Slic3r { namespace Slic3r {
class GLVertexArray {
public:
std::vector<float> verts, norms;
void reserve(size_t len) {
this->verts.reserve(len);
this->norms.reserve(len);
};
void reserve_more(size_t len) {
len += this->verts.size();
this->reserve(len);
};
void push_vert(const Pointf3 &point) {
this->verts.push_back(point.x);
this->verts.push_back(point.y);
this->verts.push_back(point.z);
};
void push_vert(float x, float y, float z) {
this->verts.push_back(x);
this->verts.push_back(y);
this->verts.push_back(z);
};
void push_norm(const Pointf3 &point) {
this->norms.push_back(point.x);
this->norms.push_back(point.y);
this->norms.push_back(point.z);
};
void push_norm(float x, float y, float z) {
this->norms.push_back(x);
this->norms.push_back(y);
this->norms.push_back(z);
};
void load_mesh(const TriangleMesh &mesh);
};
class _3DScene class _3DScene
{ {
public: public:
static void _extrusionentity_to_verts_do(const Lines &lines, const std::vector<double> &widths, static void _extrusionentity_to_verts_do(const Lines &lines, const std::vector<double> &widths,
const std::vector<double> &heights, bool closed, double top_z, const Point &copy, const std::vector<double> &heights, bool closed, double top_z, const Point &copy,
Pointf3s* qverts, Pointf3s* qnorms, Pointf3s* tverts, Pointf3s* tnorms); GLVertexArray* qverts, GLVertexArray* tverts);
}; };
} }

View File

@ -3,47 +3,26 @@
#include <myinit.h> #include <myinit.h>
#include "libslic3r/GUI/3DScene.hpp" #include "libslic3r/GUI/3DScene.hpp"
%name{Slic3r::GUI::_3DScene::GLVertexArray} class GLVertexArray {
GLVertexArray();
~GLVertexArray();
void load_mesh(TriangleMesh* mesh) const
%code%{ THIS->load_mesh(*mesh); %};
size_t size() const
%code%{ RETVAL = THIS->verts.size(); %};
void* verts_ptr() const
%code%{ RETVAL = THIS->verts.empty() ? 0 : &THIS->verts.front(); %};
void* norms_ptr() const
%code%{ RETVAL = THIS->verts.empty() ? 0 : &THIS->norms.front(); %};
};
%package{Slic3r::GUI::_3DScene}; %package{Slic3r::GUI::_3DScene};
%{ %{
void void
_extrusionentity_to_verts_do(Lines lines, std::vector<double> widths, std::vector<double> heights, bool closed, double top_z, Point* copy, SV* qverts, SV* qnorms, SV* tverts, SV* tnorms) _extrusionentity_to_verts_do(Lines lines, std::vector<double> widths, std::vector<double> heights, bool closed, double top_z, Point* copy, GLVertexArray* qverts, GLVertexArray* tverts)
CODE: CODE:
Pointf3s _qverts, _qnorms, _tverts, _tnorms;
_3DScene::_extrusionentity_to_verts_do(lines, widths, heights, closed, _3DScene::_extrusionentity_to_verts_do(lines, widths, heights, closed,
top_z, *copy, &_qverts, &_qnorms, &_tverts, &_tnorms); top_z, *copy, qverts, tverts);
{
AV* av = (AV*)SvRV(qverts);
for (Pointf3s::const_iterator it = _qverts.begin(); it != _qverts.end(); ++it) {
av_push(av, newSVnv(it->x));
av_push(av, newSVnv(it->y));
av_push(av, newSVnv(it->z));
}
}
{
AV* av = (AV*)SvRV(qnorms);
for (Pointf3s::const_iterator it = _qnorms.begin(); it != _qnorms.end(); ++it) {
av_push(av, newSVnv(it->x));
av_push(av, newSVnv(it->y));
av_push(av, newSVnv(it->z));
}
}
{
AV* av = (AV*)SvRV(tverts);
for (Pointf3s::const_iterator it = _tverts.begin(); it != _tverts.end(); ++it) {
av_push(av, newSVnv(it->x));
av_push(av, newSVnv(it->y));
av_push(av, newSVnv(it->z));
}
}
{
AV* av = (AV*)SvRV(tnorms);
for (Pointf3s::const_iterator it = _tnorms.begin(); it != _tnorms.end(); ++it) {
av_push(av, newSVnv(it->x));
av_push(av, newSVnv(it->y));
av_push(av, newSVnv(it->z));
}
}
%} %}

View File

@ -182,6 +182,8 @@ BridgeDetector* O_OBJECT_SLIC3R
Ref<BridgeDetector> O_OBJECT_SLIC3R_T Ref<BridgeDetector> O_OBJECT_SLIC3R_T
Clone<BridgeDetector> O_OBJECT_SLIC3R_T Clone<BridgeDetector> O_OBJECT_SLIC3R_T
GLVertexArray* O_OBJECT_SLIC3R
ExtrusionLoopRole T_UV ExtrusionLoopRole T_UV
ExtrusionRole T_UV ExtrusionRole T_UV
FlowRole T_UV FlowRole T_UV
@ -325,13 +327,11 @@ T_ARRAYREF
if (SvROK($arg) && SvTYPE(SvRV($arg)) == SVt_PVAV) { if (SvROK($arg) && SvTYPE(SvRV($arg)) == SVt_PVAV) {
AV* av = (AV*)SvRV($arg); AV* av = (AV*)SvRV($arg);
const unsigned int len = av_len(av)+1; const unsigned int len = av_len(av)+1;
$type* tmp = new $type(len); $var.resize(len);
for (unsigned int i = 0; i < len; i++) { for (unsigned int i = 0; i < len; i++) {
SV** elem = av_fetch(av, i, 0); SV** elem = av_fetch(av, i, 0);
(*tmp)[i].from_SV_check(*elem); $var\[i].from_SV_check(*elem);
} }
$var = *tmp;
delete tmp;
} else } else
Perl_croak(aTHX_ \"%s: %s is not an array reference\", Perl_croak(aTHX_ \"%s: %s is not an array reference\",
${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]}, ${$ALIAS?\q[GvNAME(CvGV(cv))]:\qq[\"$pname\"]},
@ -392,6 +392,7 @@ T_STD_STRING
T_STD_VECTOR_STD_STRING T_STD_VECTOR_STD_STRING
AV* av = newAV(); AV* av = newAV();
$arg = newRV_noinc((SV*)av); $arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
const unsigned int len = $var.size(); const unsigned int len = $var.size();
if (len) if (len)
av_extend(av, len-1); av_extend(av, len-1);
@ -404,6 +405,7 @@ T_STD_VECTOR_STD_STRING
T_STD_VECTOR_INT T_STD_VECTOR_INT
AV* av = newAV(); AV* av = newAV();
$arg = newRV_noinc((SV*)av); $arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
const unsigned int len = $var.size(); const unsigned int len = $var.size();
if (len) if (len)
av_extend(av, len-1); av_extend(av, len-1);
@ -414,6 +416,7 @@ T_STD_VECTOR_INT
T_STD_VECTOR_UINT T_STD_VECTOR_UINT
AV* av = newAV(); AV* av = newAV();
$arg = newRV_noinc((SV*)av); $arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
const unsigned int len = $var.size(); const unsigned int len = $var.size();
if (len) if (len)
av_extend(av, len-1); av_extend(av, len-1);
@ -424,6 +427,7 @@ T_STD_VECTOR_UINT
T_STD_VECTOR_DOUBLE T_STD_VECTOR_DOUBLE
AV* av = newAV(); AV* av = newAV();
$arg = newRV_noinc((SV*)av); $arg = newRV_noinc((SV*)av);
sv_2mortal($arg);
const unsigned int len = $var.size(); const unsigned int len = $var.size();
if (len) if (len)
av_extend(av, len-1); av_extend(av, len-1);

View File

@ -13,6 +13,7 @@
%typemap{std::vector<unsigned int>*}; %typemap{std::vector<unsigned int>*};
%typemap{std::vector<std::string>}; %typemap{std::vector<std::string>};
%typemap{t_layer_height_ranges}; %typemap{t_layer_height_ranges};
%typemap{void*};
%typemap{SV*}; %typemap{SV*};
%typemap{AV*}; %typemap{AV*};
%typemap{Point*}; %typemap{Point*};
@ -164,6 +165,7 @@
%typemap{ModelInstancePtrs*}; %typemap{ModelInstancePtrs*};
%typemap{Ref<ModelInstancePtrs>}{simple}; %typemap{Ref<ModelInstancePtrs>}{simple};
%typemap{Clone<ModelInstancePtrs>}{simple}; %typemap{Clone<ModelInstancePtrs>}{simple};
%typemap{GLVertexArray*};
%typemap{PrintRegionPtrs*}; %typemap{PrintRegionPtrs*};
%typemap{PrintObjectPtrs*}; %typemap{PrintObjectPtrs*};