mirror of https://github.com/vitalif/Slic3r
Read materials from OBJ files and store them in the TriangleMesh object. Now we need to use them...
parent
18280da1b3
commit
0216448cfa
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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 ]
|
||||
|
|
Loading…
Reference in New Issue