Skip to content

Commit

Permalink
Read materials from OBJ files and store them in the TriangleMesh obje…
Browse files Browse the repository at this point in the history
…ct. Now we need to use them...
  • Loading branch information
alranel committed Jan 25, 2013
1 parent 18280da commit 0216448
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
36 changes: 32 additions & 4 deletions 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;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Slic3r/Model.pm
Expand Up @@ -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,
);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Slic3r/TriangleMesh.pm
Expand Up @@ -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 ]
Expand Down

0 comments on commit 0216448

Please sign in to comment.