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

STL.pm « Slic3r « lib - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6fc7ff80cd3bd1fc810b7fc32886557887e5bf33 (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
package Slic3r::STL;
use Moo;

use Math::Clipper qw(integerize_coordinate_sets is_counter_clockwise);
use Slic3r::Geometry qw(X Y Z three_points_aligned longest_segment);
use XXX;

use constant MIN => 0;
use constant MAX => 1;

sub parse_file {
    my $self = shift;
    my ($file) = @_;
    
    # open STL file
    my $facets = $self->read_file($file);
    
    if ($Slic3r::rotate > 0) {
        my $deg = Slic3r::Geometry::deg2rad($Slic3r::rotate);
        foreach my $facet (@$facets) {
            my ($normal, @vertices) = @$facet;
            foreach my $vertex (@vertices) {
                @$vertex = (@{ +(Slic3r::Geometry::rotate_points($deg, undef, [ $vertex->[X], $vertex->[Y] ]))[0] }, $vertex->[Z]);
            }
        }
    }
    
    # we only want to work with positive coordinates, so let's 
    # find our object extents to calculate coordinate displacements
    my @extents = (map [99999999999, -99999999999], X,Y,Z);
    foreach my $facet (@$facets) {
        my ($normal, @vertices) = @$facet;
        foreach my $vertex (@vertices) {
            for (X,Y,Z) {
                $extents[$_][MIN] = $vertex->[$_] if $vertex->[$_] < $extents[$_][MIN];
                $extents[$_][MAX] = $vertex->[$_] if $vertex->[$_] > $extents[$_][MAX];
            }
        }
    }
    
    # scale extents
    for (X,Y,Z) {
        $extents[$_][MIN] *= $Slic3r::scale;
        $extents[$_][MAX] *= $Slic3r::scale;
    }
    
    # duplicate object
    my @duplicate_offset = (
        (($extents[X][MAX] - $extents[X][MIN]) + $Slic3r::duplicate_distance),
        (($extents[Y][MAX] - $extents[Y][MIN]) + $Slic3r::duplicate_distance),
    );
    $extents[X][MAX] += $duplicate_offset[X] * ($Slic3r::duplicate_x-1);
    $extents[Y][MAX] += $duplicate_offset[Y] * ($Slic3r::duplicate_y-1);
    my @copies = ();
    for (my $i = 0; $i < $Slic3r::duplicate_x; $i++) {
        for (my $j = 0; $j < $Slic3r::duplicate_y; $j++) {
            push @copies, [ $duplicate_offset[X] * $i, $duplicate_offset[Y] * $j ];
        }
    }
    
    # initialize print job
    my $print = Slic3r::Print->new(
        x_length => ($extents[X][MAX] - $extents[X][MIN]) / $Slic3r::resolution,
        y_length => ($extents[Y][MAX] - $extents[Y][MIN]) / $Slic3r::resolution,
    );
    
    # calculate the displacements needed to 
    # have lowest value for each axis at coordinate 0
    my @shift = map sprintf('%.0f', -$extents[$_][MIN] / $Slic3r::resolution), X,Y,Z;
    
    # process facets
    foreach my $facet (@$facets) {
        
        # transform vertex coordinates
        my ($normal, @vertices) = @$facet;
        foreach my $vertex (@vertices) {
            $vertex->[$_] = ($Slic3r::scale * $vertex->[$_] / $Slic3r::resolution) + $shift[$_]
                for X,Y,Z;
        }
        
        foreach my $copy (@copies) {
            my @copy_vertices = map [ @$_ ], @vertices;  # clone vertices
            foreach my $vertex (@copy_vertices) {
                $vertex->[$_] += $copy->[$_] / $Slic3r::resolution for X,Y;
            }
            $self->_facet($print, $normal, @copy_vertices);
        }
    }
    
    # remove last layer if empty
    # (we might have created it because of the $max_layer = ... + 1 code below)
    pop @{$print->layers} if !@{$print->layers->[-1]->surfaces} && !@{$print->layers->[-1]->lines};
    
    return $print;
}

sub _facet {
    my $self = shift;
    my ($print, $normal, @vertices) = @_;
    Slic3r::debugf "\n==> FACET (%f,%f,%f - %f,%f,%f - %f,%f,%f):\n", map @$_, @vertices
        if $Slic3r::debug;
    
    # find the vertical extents of the facet
    my ($min_z, $max_z) = (99999999999, -99999999999);
    foreach my $vertex (@vertices) {
        $min_z = $vertex->[Z] if $vertex->[Z] < $min_z;
        $max_z = $vertex->[Z] if $vertex->[Z] > $max_z;
    }
    Slic3r::debugf "z: min = %.0f, max = %.0f\n", $min_z, $max_z;
    
    if ($min_z == $max_z) {
        Slic3r::debugf "Facet is horizontal; ignoring\n";
        return;
    }
    
    # calculate the layer extents
    # (the -1 and +1 here are used as a quick and dirty replacement for some
    # complex calculation of the first layer height ratio logic)
    my $min_layer = int($min_z * $Slic3r::resolution / $Slic3r::layer_height) - 1;
    $min_layer = 0 if $min_layer < 0;
    my $max_layer = int($max_z * $Slic3r::resolution / $Slic3r::layer_height) + 1;
    Slic3r::debugf "layers: min = %s, max = %s\n", $min_layer, $max_layer;
    
    # reorder vertices so that the first one is the one with lowest Z
    # this is needed to get all intersection lines in a consistent order
    # (external on the right of the line)
    {
        my @z_order = sort { $vertices[$a][Z] <=> $vertices[$b][Z] } 0..2;
        @vertices = (splice(@vertices, $z_order[0]), splice(@vertices, 0, $z_order[0]));
    }
    
    for (my $layer_id = $min_layer; $layer_id <= $max_layer; $layer_id++) {
        my $layer = $print->layer($layer_id);
        $layer->add_line($_) for $self->intersect_facet(\@vertices, $layer->slice_z);
    }
}

sub intersect_facet {
    my $self = shift;
    my ($vertices, $z) = @_;
    printf "Slicing at $z\n";
    # build the three segments of the triangle facet
    my @edges = (
        [ $vertices->[0], $vertices->[1] ],
        [ $vertices->[1], $vertices->[2] ],
        [ $vertices->[2], $vertices->[0] ],
    );
    
    my (@lines, @intersection_points) = ();
        
    foreach my $edge (@edges) {
        my ($a, $b) = @$edge;
        #printf "Az = %d, Bz = %d, z = %d\n", $a->[Z], $b->[Z], $z;
        
        if ($a->[Z] == $b->[Z] && $a->[Z] == $z) {
            # edge is horizontal and belongs to the current layer
            my $edge_type = (grep $_->[Z] > $z, @$vertices) ? 'Bottom' : 'Top';
            ($a, $b) = ($b, $a) if $edge_type eq 'Bottom';
            push @lines, "Slic3r::Line::FacetEdge::$edge_type"->new(
                [$a->[X], $a->[Y]], [$b->[X], $b->[Y]],
            );
            #print "Horizontal edge at $z!\n";
            
        } elsif (($a->[Z] < $z && $b->[Z] > $z) || ($b->[Z] < $z && $a->[Z] > $z)) {
            # edge intersects the current layer; calculate intersection
            push @intersection_points, [
                $b->[X] + ($a->[X] - $b->[X]) * ($z - $b->[Z]) / ($a->[Z] - $b->[Z]),
                $b->[Y] + ($a->[Y] - $b->[Y]) * ($z - $b->[Z]) / ($a->[Z] - $b->[Z]),
            ];
            #print "Intersects at $z!\n";
            
        } elsif ($a->[Z] == $z) {
            #print "A point on plane $z!\n";
            push @intersection_points, [ $a->[X], $a->[Y] ];
            
        } elsif ($b->[Z] == $z) {
            #print "B point on plane $z!\n";
            push @intersection_points, [ $b->[X], $b->[Y] ];
        }
    }
    
    Slic3r::Geometry::remove_coinciding_points(\@intersection_points);
    
    if (@intersection_points > 1 && !@lines) {
        
        # remove coinciding points
        
        # defensive programming:
        die "Facets must intersect each plane 0 or 2 times" if @intersection_points != 2;
        
        # check whether the two points coincide due to resolution rounding
        #if ($intersection_points[0]->coincides_with($intersection_points[1])) {
        #    Slic3r::debugf "Points coincide; removing\n";
        #    return;
        #}
        
        # connect points:
        push @lines, Slic3r::Line->new(@intersection_points);
        #printf "  intersection points = %f,%f - %f,%f\n", map @$_, @intersection_points;
    }
    
    return @lines;
}

sub read_file {
    my $self = shift;
    my ($file) = @_;
    
    open my $fh, '<', $file or die "Failed to open $file\n";
    my $facets = [];
    
    # let's detect whether file is ASCII or binary
    my $mode;
    {
        my $size = +(stat $fh)[7];
        $mode = 'ascii' if $size < 80 + 4;
        
        # skip binary header
        seek $fh, 80, 0;
        read $fh, my $buf, 4;
        my $triangle_count = unpack 'L', $buf;
        my $expected_size =
            + 80 # header
            +  4 # count
            + $triangle_count * (
                + 4   # normal, pt,pt,pt (vectors)
                  * 4   # bytes per value
                  * 3   # values per vector
                + 2 # the trailing 'short'
            );
        $mode = ($size == $expected_size) ? 'binary' : 'ascii';
    }
    
    $mode eq 'ascii'
        ? _read_ascii($fh, $facets)
        : _read_binary($fh, $facets);
    
    close $fh;
    return $facets;
}

sub _read_ascii {
    my ($fh, $facets) = @_;
    
    my $point_re = qr/([^ ]+)\s+([^ ]+)\s+([^ ]+)$/;
    
    my $facet;
    seek $fh, 0, 0;
    while (<$fh>) {
        chomp;
        if (!$facet) {
            /^\s*facet\s+normal\s+$point_re/ or next;
            $facet = [ [$1, $2, $3] ];
        } else {
            if (/^\s*endfacet/) {
                push @$facets, $facet;
                undef $facet;
            } else {
                /^\s*vertex\s+$point_re/ or next;
                push @$facet, [$1, $2, $3];
            }
        }
    }
    if ($facet) {
        die "STL file seems invalid\n";
    }
}

sub _read_binary {
    my ($fh, $facets) = @_;
    
    die "bigfloat" unless length(pack "f", 1) == 4;
    
    binmode $fh;
    seek $fh, 80 + 4, 0;
    while (read $fh, $_, 4*4*3+2) {
        my @v = unpack '(f<3)4';
        push @$facets, [ [@v[0..2]], [@v[3..5]], [@v[6..8]], [@v[9..11]] ];
    }
}

1;