Do not store normals and save a lot of memory

This commit is contained in:
Alessandro Ranellucci 2012-05-20 11:05:16 +02:00
parent 74c00cdb7a
commit 3c8dbcef4a
4 changed files with 21 additions and 21 deletions

View file

@ -74,7 +74,7 @@ sub write_file {
foreach my $facet (@{$mesh->facets}) { foreach my $facet (@{$mesh->facets}) {
printf $fh qq{ <triangle>\n}; printf $fh qq{ <triangle>\n};
printf $fh qq{ <v%d>%d</v%d>\n}, $_, $facet->[$_] + $vertices_offset{$mesh}, $_ printf $fh qq{ <v%d>%d</v%d>\n}, $_, $facet->[$_] + $vertices_offset{$mesh}, $_
for 1..3; for -3..-1;
printf $fh qq{ </triangle>\n}; printf $fh qq{ </triangle>\n};
} }
printf $fh qq{ </volume>\n}; printf $fh qq{ </volume>\n};

View file

@ -26,9 +26,9 @@ sub start_element {
$self->{_volume_materialid} = $self->_get_attribute($data, 'materialid') || '_'; $self->{_volume_materialid} = $self->_get_attribute($data, 'materialid') || '_';
$self->{_volume} = []; $self->{_volume} = [];
} elsif ($data->{LocalName} eq 'triangle') { } elsif ($data->{LocalName} eq 'triangle') {
$self->{_triangle} = [[], "", "", ""]; # empty normal $self->{_triangle} = ["", "", ""];
} elsif ($self->{_triangle} && $data->{LocalName} =~ /^v([123])$/ && $self->{_tree}[-1] eq 'triangle') { } elsif ($self->{_triangle} && $data->{LocalName} =~ /^v([123])$/ && $self->{_tree}[-1] eq 'triangle') {
$self->{_vertex_idx} = $1; $self->{_vertex_idx} = $1-1;
} elsif ($data->{LocalName} eq 'material') { } elsif ($data->{LocalName} eq 'material') {
$self->{_material_id} = $self->_get_attribute($data, 'id') || '_'; $self->{_material_id} = $self->_get_attribute($data, 'id') || '_';
$self->{_material} = {}; $self->{_material} = {};

View file

@ -43,7 +43,7 @@ sub read_file {
my %vertices_map = (); # given a vertex's coordinates, what's its index? my %vertices_map = (); # given a vertex's coordinates, what's its index?
my @vertices_facets = (); # given a vertex index, what are the indexes of its tangent facets? my @vertices_facets = (); # given a vertex index, what are the indexes of its tangent facets?
for (my $f = 0; $f <= $#$facets; $f++) { for (my $f = 0; $f <= $#$facets; $f++) {
for (1..3) { for (-3..-1) {
my $point_id = join ',', @{$facets->[$f][$_]}; my $point_id = join ',', @{$facets->[$f][$_]};
if (exists $vertices_map{$point_id}) { if (exists $vertices_map{$point_id}) {
$facets->[$f][$_] = $vertices_map{$point_id}; $facets->[$f][$_] = $vertices_map{$point_id};
@ -74,11 +74,11 @@ sub read_file {
CYCLE: while (@facets_indexes && @this_f) { CYCLE: while (@facets_indexes && @this_f) {
# look for a facet that is connected to $this_f[-1] and whose common line contains $v # look for a facet that is connected to $this_f[-1] and whose common line contains $v
my @other_vertices_indexes = grep $_ != $v, @{$facets->[$this_f[-1]]}[1..3]; my @other_vertices_indexes = grep $_ != $v, @{$facets->[$this_f[-1]]}[-3..-1];
OTHER: for my $other_f (@facets_indexes) { OTHER: for my $other_f (@facets_indexes) {
# facet is connected if it shares one more point # facet is connected if it shares one more point
for (grep $_ != $v, @{$facets->[$other_f]}[1..3]) { for (grep $_ != $v, @{$facets->[$other_f]}[-3..-1]) {
if ($_ ~~ @other_vertices_indexes) { if ($_ ~~ @other_vertices_indexes) {
#printf "facet %d is connected to $other_f (sharing vertices $v and $_)\n", $this_f[-1]; #printf "facet %d is connected to $other_f (sharing vertices $v and $_)\n", $this_f[-1];
@ -107,7 +107,7 @@ sub read_file {
Slic3r::debugf " more than one vertex in the same point\n"; Slic3r::debugf " more than one vertex in the same point\n";
push @$vertices, $vertices->[$v]; push @$vertices, $vertices->[$v];
for my $f (@this_f) { for my $f (@this_f) {
$facets->[$f][$_] = $#$vertices for grep $facets->[$f][$_] == $v, 1..3; $facets->[$f][$_] = $#$vertices for grep $facets->[$f][$_] == $v, -3..-1;
} }
} }
} }
@ -128,7 +128,7 @@ sub _read_ascii {
while (my $_ = <$fh>) { while (my $_ = <$fh>) {
if (!$facet) { if (!$facet) {
/^\s*facet\s+normal\s+$point_re/ or next; /^\s*facet\s+normal\s+$point_re/ or next;
$facet = [ [$1, $2, $3] ]; $facet = []; # ignore normal: [$1, $2, $3]
} else { } else {
if (/^\s*endfacet/) { if (/^\s*endfacet/) {
push @$facets, $facet; push @$facets, $facet;
@ -153,7 +153,7 @@ sub _read_binary {
seek $fh, 80 + 4, 0; seek $fh, 80 + 4, 0;
while (read $fh, my $_, 4*4*3+2) { while (read $fh, my $_, 4*4*3+2) {
my @v = unpack '(f<3)4'; my @v = unpack '(f<3)4';
push @$facets, [ [@v[0..2]], [@v[3..5]], [@v[6..8]], [@v[9..11]] ]; push @$facets, [ [@v[3..5]], [@v[6..8]], [@v[9..11]] ]; # ignore normal: [@v[0..2]]
} }
} }

View file

@ -5,7 +5,7 @@ use Slic3r::Geometry qw(X Y Z A B unscale same_point);
# public # public
has 'vertices' => (is => 'ro', required => 1); # id => [$x,$y,$z] has 'vertices' => (is => 'ro', required => 1); # id => [$x,$y,$z]
has 'facets' => (is => 'ro', required => 1); # id => [ $normal, $v1_id, $v2_id, $v3_id ] has 'facets' => (is => 'ro', required => 1); # id => [ $v1_id, $v2_id, $v3_id ]
# private # private
has 'edges' => (is => 'ro', default => sub { [] }); # id => [ $v1_id, $v2_id ] has 'edges' => (is => 'ro', default => sub { [] }); # id => [ $v1_id, $v2_id ]
@ -36,15 +36,15 @@ sub BUILD {
my %table = (); # edge_coordinates => edge_id my %table = (); # edge_coordinates => edge_id
for (my $facet_id = 0; $facet_id <= $#{$self->facets}; $facet_id++) { for (my $facet_id = 0; $facet_id <= $#{$self->facets}; $facet_id++) {
my $facet = $self->facets->[$facet_id]; my $facet = $self->facets->[$facet_id];##use Devel::Size qw(total_size); printf "total_size = %d\n", total_size($facet);exit;
$self->facets_edges->[$facet_id] = []; $self->facets_edges->[$facet_id] = [];
# reorder vertices so that the first one is the one with lowest Z # reorder vertices so that the first one is the one with lowest Z
# this is needed to get all intersection lines in a consistent order # this is needed to get all intersection lines in a consistent order
# (external on the right of the line) # (external on the right of the line)
{ {
my @z_order = sort { $self->vertices->[$facet->[$a]][Z] <=> $self->vertices->[$facet->[$b]][Z] } 1..3; my @z_order = sort { $self->vertices->[$facet->[$a]][Z] <=> $self->vertices->[$facet->[$b]][Z] } -3..-1;
@$facet[1..3] = (@$facet[$z_order[0]..3], @$facet[1..($z_order[0]-1)]); @$facet[-3..-1] = (@$facet[$z_order[0]..-1], @$facet[-3..($z_order[0]-1)]);
} }
# ignore the normal if provided # ignore the normal if provided
@ -83,9 +83,9 @@ sub _facet_edges {
my $facet = $self->facets->[$facet_id]; my $facet = $self->facets->[$facet_id];
return ( return (
[ $facet->[1], $facet->[2] ], [ $facet->[-3], $facet->[-2] ],
[ $facet->[2], $facet->[3] ], [ $facet->[-2], $facet->[-1] ],
[ $facet->[3], $facet->[1] ], [ $facet->[-1], $facet->[-3] ],
); );
} }
@ -198,7 +198,7 @@ sub make_loops {
# choice) # choice)
# The "// ''" on the next line avoids uninitialized value errors mentioned in issue #357 but these # The "// ''" on the next line avoids uninitialized value errors mentioned in issue #357 but these
# errors occur on fixed models so the root cause still needs to be found # errors occur on fixed models so the root cause still needs to be found
if (@lines_starting_here == 2 && join('', sort map $_->[I_FACET_EDGE] // '', @lines_starting_here) eq FE_TOP.FE_BOTTOM) { if (@lines_starting_here == 2 && join('', sort map $_->[I_FACET_EDGE] // '', @lines_starting_here) eq FE_TOP.FE_BOTTOM) { #/
my @to_remove = grep $_->[I_FACET_EDGE] == FE_TOP, @lines_starting_here; my @to_remove = grep $_->[I_FACET_EDGE] == FE_TOP, @lines_starting_here;
while (!grep defined $_->[I_B_ID] && $_->[I_B_ID] == $to_remove[-1]->[I_B_ID] && $_ ne $to_remove[-1], @lines) { while (!grep defined $_->[I_B_ID] && $_->[I_B_ID] == $to_remove[-1]->[I_B_ID] && $_ ne $to_remove[-1], @lines) {
push @to_remove, grep defined $_->[I_A_ID] && $_->[I_A_ID] == $to_remove[-1]->[I_B_ID], @lines; push @to_remove, grep defined $_->[I_A_ID] && $_->[I_A_ID] == $to_remove[-1]->[I_B_ID], @lines;
@ -359,7 +359,7 @@ sub size {
sub slice_facet { sub slice_facet {
my $self = shift; my $self = shift;
my ($print_object, $facet_id) = @_; my ($print_object, $facet_id) = @_;
my ($normal, @vertices) = @{$self->facets->[$facet_id]}; my @vertices = @{$self->facets->[$facet_id]}[-3..-1];
Slic3r::debugf "\n==> FACET %d (%f,%f,%f - %f,%f,%f - %f,%f,%f):\n", Slic3r::debugf "\n==> FACET %d (%f,%f,%f - %f,%f,%f - %f,%f,%f):\n",
$facet_id, map @{$self->vertices->[$_]}, @vertices $facet_id, map @{$self->vertices->[$_]}, @vertices
if $Slic3r::debug; if $Slic3r::debug;
@ -398,7 +398,7 @@ sub intersect_facet {
my $self = shift; my $self = shift;
my ($facet_id, $z) = @_; my ($facet_id, $z) = @_;
my @vertices_ids = @{$self->facets->[$facet_id]}[1..3]; my @vertices_ids = @{$self->facets->[$facet_id]}[-3..-1];
my @edge_ids = @{$self->facets_edges->[$facet_id]}; my @edge_ids = @{$self->facets_edges->[$facet_id]};
my @edge_vertices_ids = $self->_facet_edges($facet_id); my @edge_vertices_ids = $self->_facet_edges($facet_id);
@ -534,11 +534,11 @@ sub split_mesh {
$self->facets->[$facet_id] = undef; $self->facets->[$facet_id] = undef;
} }
my %vertices = map { $_ => 1 } map @$_[1,2,3], @facets; my %vertices = map { $_ => 1 } map @$_[-3..-1], @facets;
my @new_vertices = keys %vertices; my @new_vertices = keys %vertices;
my %new_vertices = map { $new_vertices[$_] => $_ } 0..$#new_vertices; my %new_vertices = map { $new_vertices[$_] => $_ } 0..$#new_vertices;
foreach my $facet (@facets) { foreach my $facet (@facets) {
$facet->[$_] = $new_vertices{$facet->[$_]} for 1,2,3; $facet->[$_] = $new_vertices{$facet->[$_]} for -3..-1;
} }
push @meshes, Slic3r::TriangleMesh->new( push @meshes, Slic3r::TriangleMesh->new(
facets => \@facets, facets => \@facets,