Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Model.pm « Slic3r « lib - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba873da9d557f1c0955b2756a28832f293ddccc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package Slic3r::Model;
use Moo;

use List::Util qw(first max);
use Slic3r::Geometry qw(X Y Z MIN move_points);

has 'materials' => (is => 'ro', default => sub { {} });
has 'objects'   => (is => 'ro', default => sub { [] });
has '_bounding_box' => (is => 'rw');

sub read_from_file {
    my $class = shift;
    my ($input_file) = @_;
    
    my $model = $input_file =~ /\.stl$/i            ? Slic3r::Format::STL->read_file($input_file)
              : $input_file =~ /\.obj$/i            ? Slic3r::Format::OBJ->read_file($input_file)
              : $input_file =~ /\.amf(\.xml)?$/i    ? Slic3r::Format::AMF->read_file($input_file)
              : die "Input file must have .stl, .obj or .amf(.xml) extension\n";
    
    $_->input_file($input_file) for @{$model->objects};
    return $model;
}

sub merge {
    my $class = shift;
    my @models = @_;
    
    my $new_model = $class->new;
    foreach my $model (@models) {
        # merge material attributes (should we rename them in case of duplicates?)
        $new_model->set_material($_, { %{$model->materials->{$_}}, %{$model->materials->{$_} || {}} })
            for keys %{$model->materials};
        
        foreach my $object (@{$model->objects}) {
            my $new_object = $new_model->add_object(
                input_file          => $object->input_file,
                vertices            => $object->vertices,
                layer_height_ranges => $object->layer_height_ranges,
            );
            
            $new_object->add_volume(
                material_id         => $_->material_id,
                facets              => $_->facets,
            ) for @{$object->volumes};
            
            $new_object->add_instance(
                offset              => $_->offset,
                rotation            => $_->rotation,
            ) for @{ $object->instances // [] };
        }
    }
    
    return $new_model;
}

sub add_object {
    my $self = shift;
    
    my $object = Slic3r::Model::Object->new(model => $self, @_);
    push @{$self->objects}, $object;
    $self->_bounding_box(undef);
    return $object;
}

sub set_material {
    my $self = shift;
    my ($material_id, $attributes) = @_;
    
    return $self->materials->{$material_id} = Slic3r::Model::Region->new(
        model       => $self,
        attributes  => $attributes || {},
    );
}

sub arrange_objects {
    my $self = shift;
    my ($config) = @_;
    
    # do we have objects with no position?
    if (first { !defined $_->instances } @{$self->objects}) {
        # we shall redefine positions for all objects
        
        my ($copies, @positions) = $self->_arrange(
            config  => $config,
            items   => $self->objects,
        );
        
        # apply positions to objects
        foreach my $object (@{$self->objects}) {
            $object->align_to_origin;
            
            $object->instances([]);
            $object->add_instance(
                offset      => $_,
                rotation    => 0,
            ) for splice @positions, 0, $copies;
        }
        
    } else {
        # we only have objects with defined position
        
        # align the whole model to origin as it is
        $self->align_to_origin;
        
        # arrange this model as a whole
        my ($copies, @positions) = $self->_arrange(
            config  => $config,
            items   => [$self],
        );
        
        # apply positions to objects by translating the current positions
        foreach my $object (@{$self->objects}) {
            my @old_instances = @{$object->instances};
            $object->instances([]);
            foreach my $instance (@old_instances) {
                $object->add_instance(
                    offset      => $_,
                    rotation    => $instance->rotation,
                    scaling_factor => $instance->scaling_factor,
                ) for move_points($instance->offset, @positions);
            }
        }
    }
}

sub _arrange {
    my $self = shift;
    my %params = @_;
    
    my $config  = $params{config};
    my @items   = @{$params{items}};  # can be Model or Object objects, they have to implement size()
    
    if ($config->duplicate_grid->[X] > 1 || $config->duplicate_grid->[Y] > 1) {
        if (@items > 1) {
            die "Grid duplication is not supported with multiple objects\n";
        }
        my @positions = ();
        my $size = $items[0]->size;
        my $dist = $config->duplicate_distance;
        for my $x_copy (1..$config->duplicate_grid->[X]) {
            for my $y_copy (1..$config->duplicate_grid->[Y]) {
                push @positions, [
                    ($size->[X] + $dist) * ($x_copy-1),
                    ($size->[Y] + $dist) * ($y_copy-1),
                ];
            }
        }
        return ($config->duplicate_grid->[X] * $config->duplicate_grid->[Y]), @positions;
    } else {
        my $total_parts = $config->duplicate * @items;
        my @sizes = map $_->size, @items;
        my $partx = max(map $_->[X], @sizes);
        my $party = max(map $_->[Y], @sizes);
        return $config->duplicate,
            Slic3r::Geometry::arrange
                ($total_parts, $partx, $party, (map $_, @{$config->bed_size}),
                $config->min_object_distance, $config);
    }
}

sub vertices {
    my $self = shift;
    return [ map @{$_->vertices}, @{$self->objects} ];
}

sub used_vertices {
    my $self = shift;
    return [ map @{$_->used_vertices}, @{$self->objects} ];
}

sub size {
    my $self = shift;
    return $self->bounding_box->size;
}

sub bounding_box {
    my $self = shift;
    
    if (!defined $self->_bounding_box) {
        $self->_bounding_box(Slic3r::Geometry::BoundingBox->new_from_points_3D($self->used_vertices));
    }
    return $self->_bounding_box;
}

sub align_to_origin {
    my $self = shift;
    
    # calculate the displacements needed to 
    # have lowest value for each axis at coordinate 0
    {
        my $bb = $self->bounding_box;
        $self->move(map -$bb->extents->[$_][MIN], X,Y,Z);
    }
    
    # align all instances to 0,0 as well
    {
        my @instances = map @{$_->instances}, @{$self->objects};
        my @extents = Slic3r::Geometry::bounding_box_3D([ map $_->offset, @instances ]);
        $_->offset->translate(-$extents[X][MIN], -$extents[Y][MIN]) for @instances;
    }
}

sub scale {
    my $self = shift;
    $_->scale(@_) for @{$self->objects};
    $self->_bounding_box->scale(@_) if defined $self->_bounding_box;
}

sub move {
    my $self = shift;
    my @shift = @_;
    
    $_->move(@shift) for @{$self->objects};
    $self->_bounding_box->translate(@shift) if defined $self->_bounding_box;
}

# flattens everything to a single mesh
sub mesh {
    my $self = shift;
    
    my @meshes = ();
    foreach my $object (@{$self->objects}) {
        my @instances = $object->instances ? @{$object->instances} : (undef);
        foreach my $instance (@instances) {
            my $mesh = $object->mesh->clone;
            if ($instance) {
                $mesh->rotate($instance->rotation);
                $mesh->scale($instance->scaling_factor);
                $mesh->align_to_origin;
                $mesh->move(@{$instance->offset});
            }
            push @meshes, $mesh;
        }
    }
    
    return Slic3r::TriangleMesh->merge(@meshes);
}

# this method splits objects into multiple distinct objects by walking their meshes
sub split_meshes {
    my $self = shift;
    
    my @objects = @{$self->objects};
    @{$self->objects} = ();
    
    foreach my $object (@objects) {
        if (@{$object->volumes} > 1) {
            # We can't split meshes if there's more than one material, because
            # we can't group the resulting meshes by object afterwards
            push @{$self->objects}, $object;
            next;
        }
        
        my $volume = $object->volumes->[0];
        foreach my $mesh ($volume->mesh->split_mesh) {
            my $new_object = $self->add_object(
                input_file          => $object->input_file,
                layer_height_ranges => $object->layer_height_ranges,
            );
            $new_object->add_volume(
                vertices    => $mesh->vertices,
                facets      => $mesh->facets,
                material_id => $volume->material_id,
            );
            
            # let's now align the new object to the origin and put its displacement
            # (extents) in the instances info
            my $bb = $mesh->bounding_box;
            $new_object->align_to_origin;
            
            # add one instance per original instance applying the displacement
            $new_object->add_instance(
                offset      => [ $_->offset->[X] + $bb->x_min, $_->offset->[Y] + $bb->y_min ],
                rotation    => $_->rotation,
                scaling_factor => $_->scaling_factor,
            ) for @{ $object->instances // [] };
        }
    }
}

package Slic3r::Model::Region;
use Moo;

has 'model'         => (is => 'ro', weak_ref => 1, required => 1);
has 'attributes'    => (is => 'rw', default => sub { {} });

package Slic3r::Model::Object;
use Moo;

use List::Util qw(first);
use Slic3r::Geometry qw(X Y Z MIN MAX move_points move_points_3D);
use Storable qw(dclone);

has 'input_file' => (is => 'rw');
has 'model'     => (is => 'ro', weak_ref => 1, required => 1);
has 'vertices'  => (is => 'ro', default => sub { [] });
has 'volumes'   => (is => 'ro', default => sub { [] });
has 'instances' => (is => 'rw');
has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ]
has 'mesh_stats' => (is => 'rw');
has '_bounding_box' => (is => 'rw');

sub add_volume {
    my $self = shift;
    my %args = @_;
    
    if (my $vertices = delete $args{vertices}) {
        my $v_offset = @{$self->vertices};
        push @{$self->vertices}, @$vertices;
        
        @{$args{facets}} = map {
            my $f = [@$_];
            $f->[$_] += $v_offset for -3..-1;
            $f;
        } @{$args{facets}};
    }
    
    my $volume = Slic3r::Model::Volume->new(object => $self, %args);
    push @{$self->volumes}, $volume;
    $self->_bounding_box(undef);
    $self->model->_bounding_box(undef);
    return $volume;
}

sub add_instance {
    my $self = shift;
    
    $self->instances([]) if !defined $self->instances;
    push @{$self->instances}, Slic3r::Model::Instance->new(object => $self, @_);
    $self->model->_bounding_box(undef);
    return $self->instances->[-1];
}

sub mesh {
    my $self = shift;
    
    # this mesh won't be suitable for check_manifoldness as multiple
    # facets from different volumes may use the same vertices
    return Slic3r::TriangleMesh->new(
        vertices => $self->vertices,
        facets   => [ map @{$_->facets}, @{$self->volumes} ],
    );
}

sub used_vertices {
    my $self = shift;
    return [ map $self->vertices->[$_], map @$_, map @{$_->facets}, @{$self->volumes} ];
}

sub size {
    my $self = shift;
    return $self->bounding_box->size;
}

sub center {
    my $self = shift;
    return $self->bounding_box->center;
}

sub bounding_box {
    my $self = shift;
    
    if (!defined $self->_bounding_box) {
        $self->_bounding_box(Slic3r::Geometry::BoundingBox->new_from_points_3D($self->used_vertices));
    }
    return $self->_bounding_box;
}

sub align_to_origin {
    my $self = shift;
    
    # calculate the displacements needed to 
    # have lowest value for each axis at coordinate 0
    my $bb = $self->bounding_box;
    my @shift = map -$bb->extents->[$_][MIN], X,Y,Z;
    $self->move(@shift);
    return @shift;
}

sub move {
    my $self = shift;
    my @shift = @_;
    
    @{$self->vertices} = move_points_3D([ @shift ], @{$self->vertices});
    $self->_bounding_box->translate(@shift) if defined $self->_bounding_box;
}

sub scale {
    my $self = shift;
    my ($factor) = @_;
    return if $factor == 1;
    
    # transform vertex coordinates
    foreach my $vertex (@{$self->vertices}) {
        $vertex->[$_] *= $factor for X,Y,Z;
    }
    
    $self->_bounding_box->scale($factor) if defined $self->_bounding_box;
}

sub rotate {
    my $self = shift;
    my ($deg) = @_;
    return if $deg == 0;
    
    my $rad = Slic3r::Geometry::deg2rad($deg);
    
    # transform vertex coordinates
    foreach my $vertex (@{$self->vertices}) {
        @$vertex = (@{ +(Slic3r::Geometry::rotate_points($rad, undef, [ $vertex->[X], $vertex->[Y] ]))[0] }, $vertex->[Z]);
    }
    
    $self->_bounding_box(undef);
}

sub materials_count {
    my $self = shift;
    
    my %materials = map { $_->material_id // '_default' => 1 } @{$self->volumes};
    return scalar keys %materials;
}

sub check_manifoldness {
    my $self = shift;
    return (first { !$_->mesh->check_manifoldness } @{$self->volumes}) ? 0 : 1;
}

sub clone { dclone($_[0]) }

package Slic3r::Model::Volume;
use Moo;

has 'object'        => (is => 'ro', weak_ref => 1, required => 1);
has 'material_id'   => (is => 'rw');
has 'facets'        => (is => 'rw', default => sub { [] });

sub mesh {
    my $self = shift;
    return Slic3r::TriangleMesh->new(
        vertices => $self->object->vertices,
        facets   => $self->facets,
    );
}

package Slic3r::Model::Instance;
use Moo;

has 'object'    => (is => 'ro', weak_ref => 1, required => 1);
has 'rotation'  => (is => 'rw', default => sub { 0 });  # around mesh center point
has 'scaling_factor' => (is => 'rw', default => sub { 1 });
has 'offset'    => (is => 'rw');  # must be Slic3r::Point object

1;