From 0216448cfa77b72c3efa3313d6671bc9b083a3c6 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 25 Jan 2013 19:57:08 +0100 Subject: [PATCH] Read materials from OBJ files and store them in the TriangleMesh object. Now we need to use them... --- lib/Slic3r/Format/OBJ.pm | 36 ++++++++++++++++++++++++++++++++---- lib/Slic3r/Model.pm | 6 ++++++ lib/Slic3r/TriangleMesh.pm | 2 ++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/Slic3r/Format/OBJ.pm b/lib/Slic3r/Format/OBJ.pm index c5cc0855..bf065748 100644 --- a/lib/Slic3r/Format/OBJ.pm +++ b/lib/Slic3r/Format/OBJ.pm @@ -1,25 +1,53 @@ package Slic3r::Format::OBJ; use Moo; +use constant MERGE_DUPLICATE_VERTICES => 0; + sub read_file { my $self = shift; my ($file) = @_; Slic3r::open(\my $fh, '<', $file) or die "Failed to open $file\n"; - my $vertices = []; - my $facets = []; + my $vertices = []; + my %unique_vertices = (); # cache for merging duplicate vertices + my %vertices_map = (); # cache for merging duplicate vertices (obj_v_id => v_id) + my $facets = []; + my $facets_materials = []; + my %materials = (); # material_id => material_idx + my $cur_material_idx = undef; while (my $_ = <$fh>) { + chomp; if (/^v ([^ ]+)\s+([^ ]+)\s+([^ ]+)/) { push @$vertices, [$1, $2, $3]; + if (MERGE_DUPLICATE_VERTICES) { + my $key = "$1-$2-$3"; + if (!exists $unique_vertices{$key}) { + $unique_vertices{$key} = (scalar keys %vertices_map); + } + $vertices_map{$#$vertices} = $unique_vertices{$key}; + } } elsif (/^f (\d+).*? (\d+).*? (\d+).*?/) { - push @$facets, [ $1-1, $2-1, $3-1 ]; + if (MERGE_DUPLICATE_VERTICES) { + push @$facets, [ map $vertices_map{$_}, $1-1, $2-1, $3-1 ]; + } else { + push @$facets, [ $1-1, $2-1, $3-1 ]; + } + push @$facets_materials, $cur_material_idx; + } elsif (/^usemtl (.+)/) { + $materials{$1} //= scalar(keys %materials); + $cur_material_idx = $materials{$1}; } } close $fh; my $model = Slic3r::Model->new; my $object = $model->add_object(vertices => $vertices); - my $volume = $object->add_volume(facets => $facets); + my $volume = $object->add_volume( + facets => $facets, + facets_materials => $facets_materials, + materials => { reverse %materials }, + ); + $model->set_material($_) for keys %materials;use XXX; XXX $model; return $model; } diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index c05ee8ee..31d5657e 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -142,12 +142,18 @@ use Moo; has 'object' => (is => 'ro', weak_ref => 1, required => 1); has 'material_id' => (is => 'rw'); has 'facets' => (is => 'rw', default => sub { [] }); +has 'facets_materials' => (is => 'rw', default => sub { [] }); # facet_idx => material_idx + +# maps the material numeric index used in the facets hashref to the alphanumeric model-wise material_id +has 'materials' => (is => 'rw', default => sub { [] }); # material_idx => material_id sub mesh { my $self = shift; return Slic3r::TriangleMesh->new( vertices => $self->object->vertices, facets => $self->facets, + facets_materials => $self->facets_materials, + materials => $self->materials, ); } diff --git a/lib/Slic3r/TriangleMesh.pm b/lib/Slic3r/TriangleMesh.pm index 0f5df9a4..eb210d9c 100644 --- a/lib/Slic3r/TriangleMesh.pm +++ b/lib/Slic3r/TriangleMesh.pm @@ -7,6 +7,8 @@ use Slic3r::Geometry::Clipper qw(union_ex); # public has 'vertices' => (is => 'ro', required => 1); # id => [$x,$y,$z] has 'facets' => (is => 'ro', required => 1); # id => [ $v1_id, $v2_id, $v3_id ] +has 'materials' => (is => 'ro', default => sub { [] }); # material_idx => material_id +has 'facets_materials' => (is => 'ro'); # facet_idx => material_idx # private has 'edges' => (is => 'ro', default => sub { [] }); # id => [ $v1_id, $v2_id ]