mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-05-05 23:05:10 +08:00
Memory (and maybe speed?) optimization in STL reader
This commit is contained in:
parent
4121a7ece5
commit
25bafe7ff1
@ -33,26 +33,11 @@ sub read_file {
|
|||||||
}
|
}
|
||||||
|
|
||||||
my $facets = [];
|
my $facets = [];
|
||||||
$mode eq 'ascii'
|
|
||||||
? _read_ascii($fh, $facets)
|
|
||||||
: _read_binary($fh, $facets);
|
|
||||||
close $fh;
|
|
||||||
|
|
||||||
my $vertices = [];
|
my $vertices = [];
|
||||||
{
|
$mode eq 'ascii'
|
||||||
my %vertices_map = (); # given a vertex's coordinates, what's its index?
|
? _read_ascii($fh, $facets, $vertices)
|
||||||
for (my $f = 0; $f <= $#$facets; $f++) {
|
: _read_binary($fh, $facets, $vertices);
|
||||||
for (-3..-1) {
|
close $fh;
|
||||||
my $point_id = join ',', @{$facets->[$f][$_]};
|
|
||||||
if (exists $vertices_map{$point_id}) {
|
|
||||||
$facets->[$f][$_] = $vertices_map{$point_id};
|
|
||||||
} else {
|
|
||||||
push @$vertices, $facets->[$f][$_];
|
|
||||||
$facets->[$f][$_] = $vertices_map{$point_id} = $#$vertices;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
my $model = Slic3r::Model->new;
|
my $model = Slic3r::Model->new;
|
||||||
my $object = $model->add_object(vertices => $vertices);
|
my $object = $model->add_object(vertices => $vertices);
|
||||||
@ -61,11 +46,12 @@ sub read_file {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub _read_ascii {
|
sub _read_ascii {
|
||||||
my ($fh, $facets) = @_;
|
my ($fh, $facets, $vertices) = @_;
|
||||||
|
|
||||||
my $point_re = qr/([^ ]+)\s+([^ ]+)\s+([^ ]+)/;
|
my $point_re = qr/([^ ]+)\s+([^ ]+)\s+([^ ]+)/;
|
||||||
|
|
||||||
my $facet;
|
my $facet;
|
||||||
|
my %vertices_map = ();
|
||||||
seek $fh, 0, 0;
|
seek $fh, 0, 0;
|
||||||
while (my $_ = <$fh>) {
|
while (my $_ = <$fh>) {
|
||||||
if (!$facet) {
|
if (!$facet) {
|
||||||
@ -77,7 +63,15 @@ sub _read_ascii {
|
|||||||
undef $facet;
|
undef $facet;
|
||||||
} else {
|
} else {
|
||||||
/^\s*vertex\s+$point_re/o or next;
|
/^\s*vertex\s+$point_re/o or next;
|
||||||
push @$facet, [map $_ * 1, $1, $2, $3];
|
my $vertex_id = join ',', $1, $2, $3;
|
||||||
|
my $vertex_idx;
|
||||||
|
if (exists $vertices_map{$vertex_id}) {
|
||||||
|
$vertex_idx = $vertices_map{$vertex_id};
|
||||||
|
} else {
|
||||||
|
push @$vertices, [map $_ * 1, $1, $2, $3];
|
||||||
|
$vertex_idx = $vertices_map{$vertex_id} = $#$vertices;
|
||||||
|
}
|
||||||
|
push @$facet, $vertex_idx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,15 +81,25 @@ sub _read_ascii {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub _read_binary {
|
sub _read_binary {
|
||||||
my ($fh, $facets) = @_;
|
my ($fh, $facets, $vertices) = @_;
|
||||||
|
|
||||||
die "bigfloat" unless length(pack "f", 1) == 4;
|
die "bigfloat" unless length(pack "f", 1) == 4;
|
||||||
|
|
||||||
|
my %vertices_map = ();
|
||||||
binmode $fh;
|
binmode $fh;
|
||||||
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';
|
push @$facets, my $facet = [];
|
||||||
push @$facets, [ [@v[3..5]], [@v[6..8]], [@v[9..11]] ]; # ignore normal: [@v[0..2]]
|
for (unpack 'x[f3](a[f3])3') { # ignore normal
|
||||||
|
my $vertex_idx;
|
||||||
|
if (exists $vertices_map{$_}) {
|
||||||
|
$vertex_idx = $vertices_map{$_};
|
||||||
|
} else {
|
||||||
|
push @$vertices, [ unpack 'f<3', $_ ];
|
||||||
|
$vertex_idx = $vertices_map{$_} = $#$vertices;
|
||||||
|
}
|
||||||
|
push @$facet, $vertex_idx;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user