<commit_before>package Slic3r::Model;

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

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";
    
    die "The supplied file couldn't be read because it's empty.\n"
        if $model->objects_count == 0;
    
    $_->set_input_file($input_file) for @{$model->objects};
    return $model;
}

sub merge {
    my $class = shift;
    my @models = @_;
    
    my $new_model = ref($class)
        ? $class
        : $class->new;
    
    $new_model->add_object($_) for map @{$_->objects}, @models;
    return $new_model;
}

sub add_object {
    my $self = shift;
    
    if (@_ == 1) {
        # we have a Model::Object
        my ($object) = @_;
        return $self->_add_object_clone($object);
    } else {
        my (%args) = @_;
        
        my $new_object = $self->_add_object;
        
        $new_object->set_name($args{name})
            if defined $args{name};
        $new_object->set_input_file($args{input_file})
            if defined $args{input_file};
        $new_object->config->apply($args{config})
            if defined $args{config};
        $new_object->set_layer_height_ranges($args{layer_height_ranges})
            if defined $args{layer_height_ranges};
        $new_object->set_origin_translation($args{origin_translation})
            if defined $args{origin_translation};
        
        return $new_object;
    }
}

sub set_material {
    my $self = shift;
    my ($material_id, $attributes) = @_;
    
    my $material = $self->add_material($material_id);
    $material->apply($attributes // {});
    return $material;
}

sub duplicate_objects_grid {
    my ($self, $grid, $distance) = @_;

    die "Grid duplication is not supported with multiple objects\n"
        if @{$self->objects} > 1;

    my $object = $self->objects->[0];
    $object->clear_instances;

    my $size = $object->bounding_box->size;
    for my $x_copy (1..$grid->[X]) {
        for my $y_copy (1..$grid->[Y]) {
            $object->add_instance(
                offset => Slic3r::Pointf->new(
                    ($size->[X] + $distance) * ($x_copy-1),
                    ($size->[Y] + $distance) * ($y_copy-1),
                ),
            );
        }
    }
}

# this will append more instances to each object
# and then automatically rearrange everything
sub duplicate_objects {
    my ($self, $copies_num, $distance, $bb) = @_;
    
    foreach my $object (@{$self->objects}) {
        my @instances = @{$object->instances};
        foreach my $instance (@instances) {
            $object->add_instance($instance) for 2..$copies_num;
        }
    }
    
    $self->arrange_objects($distance, $bb);
}

# arrange objects preserving their instance count
# but altering their instance positions
sub arrange_objects {
    my ($self, $distance, $bb) = @_;
    
    # get the (transformed) size of each instance so that we take
    # into account their different transformations when packing
    my @instance_sizes = ();
    foreach my $object (@{$self->objects}) {
        push @instance_sizes, map $object->instance_bounding_box($_)->size, 0..$#{$object->instances};
    }
    
    my @positions = $self->_arrange(\@instance_sizes, $distance, $bb);
    
    foreach my $object (@{$self->objects}) {
        $_->set_offset(Slic3r::Pointf->new(@{shift @positions})) for @{$object->instances};
        $object->update_bounding_box;
    }
}

# duplicate the entire model preserving instance relative positions
sub duplicate {
    my ($self, $copies_num, $distance, $bb) = @_;
    
    my $model_size = $self->bounding_box->size;
    my @positions = $self->_arrange([ map $model_size, 2..$copies_num ], $distance, $bb);
    
    # note that this will leave the object count unaltered
    
    foreach my $object (@{$self->objects}) {
        my @instances = @{$object->instances};  # store separately to avoid recursion from add_instance() below
        foreach my $instance (@instances) {
            foreach my $pos (@positions) {
                $object->add_instance(
                    offset          => Slic3r::Pointf->new($instance->offset->[X] + $pos->[X], $instance->offset->[Y] + $pos->[Y]),
                    rotation        => $instance->rotation,
                    scaling_factor  => $instance->scaling_factor,
                );
            }
        }
        $object->update_bounding_box;
    }
}

sub _arrange {
    my ($self, $sizes, $distance, $bb) = @_;
    
    # we supply unscaled data to arrange()
    return @{Slic3r::Geometry::arrange(
        scalar(@$sizes),                # number of parts
        Slic3r::Pointf->new(
            max(map $_->x, @$sizes),        # cell width
            max(map $_->y, @$sizes),        # cell height  ,
        ),<commit_msg>Fix failing test. #2806<commit_after>package Slic3r::Model;

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

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";
    
    die "The supplied file couldn't be read because it's empty.\n"
        if $model->objects_count == 0;
    
    $_->set_input_file($input_file) for @{$model->objects};
    return $model;
}

sub merge {
    my $class = shift;
    my @models = @_;
    
    my $new_model = ref($class)
        ? $class
        : $class->new;
    
    $new_model->add_object($_) for map @{$_->objects}, @models;
    return $new_model;
}

sub add_object {
    my $self = shift;
    
    if (@_ == 1) {
        # we have a Model::Object
        my ($object) = @_;
        return $self->_add_object_clone($object);
    } else {
        my (%args) = @_;
        
        my $new_object = $self->_add_object;
        
        $new_object->set_name($args{name})
            if defined $args{name};
        $new_object->set_input_file($args{input_file})
            if defined $args{input_file};
        $new_object->config->apply($args{config})
            if defined $args{config};
        $new_object->set_layer_height_ranges($args{layer_height_ranges})
            if defined $args{layer_height_ranges};
        $new_object->set_origin_translation($args{origin_translation})
            if defined $args{origin_translation};
        
        return $new_object;
    }
}

sub set_material {
    my $self = shift;
    my ($material_id, $attributes) = @_;
    
    my $material = $self->add_material($material_id);
    $material->apply($attributes // {});
    return $material;
}

sub duplicate_objects_grid {
    my ($self, $grid, $distance) = @_;

    die "Grid duplication is not supported with multiple objects\n"
        if @{$self->objects} > 1;

    my $object = $self->objects->[0];
    $object->clear_instances;

    my $size = $object->bounding_box->size;
    for my $x_copy (1..$grid->[X]) {
        for my $y_copy (1..$grid->[Y]) {
            $object->add_instance(
                offset => Slic3r::Pointf->new(
                    ($size->[X] + $distance) * ($x_copy-1),
                    ($size->[Y] + $distance) * ($y_copy-1),
                ),
            );
        }
    }
}

# this will append more instances to each object
# and then automatically rearrange everything
sub duplicate_objects {
    my ($self, $copies_num, $distance, $bb) = @_;
    
    foreach my $object (@{$self->objects}) {
        my @instances = @{$object->instances};
        foreach my $instance (@instances) {
            $object->add_instance($instance) for 2..$copies_num;
        }
    }
    
    $self->arrange_objects($distance, $bb);
}

# arrange objects preserving their instance count
# but altering their instance positions
sub arrange_objects {
    my ($self, $distance, $bb) = @_;
    
    # get the (transformed) size of each instance so that we take
    # into account their different transformations when packing
    my @instance_sizes = ();
    foreach my $object (@{$self->objects}) {
        push @instance_sizes, map $object->instance_bounding_box($_)->size, 0..$#{$object->instances};
    }
    
    my @positions = $self->_arrange(\@instance_sizes, $distance, $bb);
    
    foreach my $object (@{$self->objects}) {
        $_->set_offset(Slic3r::Pointf->new(@{shift @positions})) for @{$object->instances};
        $object->update_bounding_box;
    }
}

# duplicate the entire model preserving instance relative positions
sub duplicate {
    my ($self, $copies_num, $distance, $bb) = @_;
    
    my $model_size = $self->bounding_box->size;
    my @positions = $self->_arrange([ map $model_size, 2..$copies_num ], $distance, $bb);
    
    # note that this will leave the object count unaltered
    
    foreach my $object (@{$self->objects}) {
        my @instances = @{$object->instances};  # store separately to avoid recursion from add_instance() below
        foreach my $instance (@instances) {
            foreach my $pos (@positions) {
                $object->add_instance(
                    offset          => Slic3r::Pointf->new($instance->offset->[X] + $pos->[X], $instance->offset->[Y] + $pos->[Y]),
                    rotation        => $instance->rotation,
                    scaling_factor  => $instance->scaling_factor,
                );
            }
        }
        $object->update_bounding_box;
    }
}

sub _arrange {
    my ($self, $sizes, $distance, $bb) = @_;

    $bb //= Slic3r::Geometry::BoundingBoxf->new;
    
    # we supply unscaled data to arrange()
    return @{Slic3r::Geometry::arrange(
        scalar(@$sizes),                # number of parts
        Slic3r::Pointf->new(
            max(map $_->x, @$sizes),        # cell width
            max(map $_->y, @$sizes),        # cell height  ,
        ),
{'fraction_non_alphanumeric': 0.15154718431674033, 'fraction_numerical': 0.0045166250240246016, 'mean_word_length': 2.291271347248577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 142, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5352978', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3260, 'n_words': 747}