<commit_before>use Test::More tests => 4;
use strict;
use warnings;

BEGIN {
    use FindBin;
    use lib "$FindBin::Bin/../lib";
}

use List::Util qw(first);
use Slic3r;
use Slic3r::Geometry qw(scale epsilon rad2deg PI);
use Slic3r::Test;

my $flow = Slic3r::Flow->new(width => 0.5, spacing => 0.45, nozzle_diameter => 0.5);
my $bd = Slic3r::Layer::BridgeDetector->new(
    lower_slices    => [],
    perimeter_flow  => $flow,
    infill_flow     => $flow,
);

{
    my $test = sub {
        my ($bridge_size, $expected_angle) = @_;
        
        my ($x, $y) = @$bridge_size;
        my $lower = Slic3r::ExPolygon->new(
            Slic3r::Polygon->new_scale([-2,-2], [$x+2,-2], [$x+2,$y+2], [-2,$y+2]),
            Slic3r::Polygon->new_scale([0,0], [0,$y], [$x,$y], [$x,0]),
        );
        $lower->translate(scale 20, scale 20); # avoid negative coordinates for easier SVG preview
        my $bridge = $lower->[1]->clone;
        $bridge->reverse;
        $bridge = Slic3r::ExPolygon->new($bridge);
        $bd->lower_slices([$lower]);
        
        ok check_angle($bd->detect_angle($bridge), $expected_angle), 'correct bridge angle for O-shaped overhang';
    };
    
    $test->([20,10], 90);
    $test->([10,20],  0);
}

{
    my $bridge = Slic3r::ExPolygon->new(
        Slic3r::Polygon->new_scale([0,0], [20,0], [20,10], [0,10]),
    );
    my $lower = [
        Slic3r::ExPolygon->new(
            Slic3r::Polygon->new_scale([-2,0], [0,0], [0,10], [-2,10]),
        ),
    ];
    $_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
    
    $lower->[1] = $lower->[0]->clone;
    $lower->[1]->translate(scale 22, 0);
    
    $bd->lower_slices($lower);
    ok check_angle($bd->detect_angle($bridge), 0), 'correct bridge angle for two-sided bridge';
}

{
    my $bridge = Slic3r::ExPolygon->new(
        Slic3r::Polygon->new_scale([0,0], [20,0], [10,10], [0,10]),
    );
    my $lower = [
        Slic3r::ExPolygon->new(
            Slic3r::Polygon->new_scale([0,0], [0,10], [10,10], [10,12], [-2,12], [-2,-2], [22,-2], [22,0]),
        ),
    ];
    $_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
    
    $bd->lower_slices($lower);
    ok check_angle($bd->detect_angle($bridge), 135), 'correct bridge angle for C-shaped overhang';
}

sub check_angle {
    my ($result, $expected) = @_;
    
    # our epsilon is equal to the steps used by the bridge detection algorithm
    ###use XXX; YYY [ rad2deg($result), $expected ];<commit_msg>Test bridges when infill flow is larger than perimeter flow. #1917<commit_after>use Test::More tests => 8;
use strict;
use warnings;

BEGIN {
    use FindBin;
    use lib "$FindBin::Bin/../lib";
}

use List::Util qw(first);
use Slic3r;
use Slic3r::Geometry qw(scale epsilon rad2deg PI);
use Slic3r::Test;

my $full_test = sub {
    my ($bd) = @_;
    {
        my $test = sub {
            my ($bridge_size, $expected_angle) = @_;
        
            my ($x, $y) = @$bridge_size;
            my $lower = Slic3r::ExPolygon->new(
                Slic3r::Polygon->new_scale([-2,-2], [$x+2,-2], [$x+2,$y+2], [-2,$y+2]),
                Slic3r::Polygon->new_scale([0,0], [0,$y], [$x,$y], [$x,0]),
            );
            $lower->translate(scale 20, scale 20); # avoid negative coordinates for easier SVG preview
            my $bridge = $lower->[1]->clone;
            $bridge->reverse;
            $bridge = Slic3r::ExPolygon->new($bridge);
            $bd->lower_slices([$lower]);
        
            ok check_angle($bd, $bridge, $expected_angle), 'correct bridge angle for O-shaped overhang';
        };
    
        $test->([20,10], 90);
        $test->([10,20],  0);
    }

    {
        my $bridge = Slic3r::ExPolygon->new(
            Slic3r::Polygon->new_scale([0,0], [20,0], [20,10], [0,10]),
        );
        my $lower = [
            Slic3r::ExPolygon->new(
                Slic3r::Polygon->new_scale([-2,0], [0,0], [0,10], [-2,10]),
            ),
        ];
        $_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
    
        $lower->[1] = $lower->[0]->clone;
        $lower->[1]->translate(scale 22, 0);
    
        $bd->lower_slices($lower);
        ok check_angle($bd, $bridge, 0), 'correct bridge angle for two-sided bridge';
    }

    {
        my $bridge = Slic3r::ExPolygon->new(
            Slic3r::Polygon->new_scale([0,0], [20,0], [10,10], [0,10]),
        );
        my $lower = [
            Slic3r::ExPolygon->new(
                Slic3r::Polygon->new_scale([0,0], [0,10], [10,10], [10,12], [-2,12], [-2,-2], [22,-2], [22,0]),
            ),
        ];
        $_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
    
        $bd->lower_slices($lower);
        ok check_angle($bd, $bridge, 135), 'correct bridge angle for C-shaped overhang';
    }
};

my $flow = Slic3r::Flow->new(width => 0.5, spacing => 0.45, nozzle_diameter => 0.5);
my $bd = Slic3r::Layer::BridgeDetector->new(
    lower_slices    => [],
    perimeter_flow  => $flow,
    infill_flow     => $flow,
);

$full_test->($bd);

# infill flow larger than perimeter flow
$bd->infill_flow(Slic3r::Flow->new(width => 0.9, spacing => 0.85, nozzle_diameter => 0.5));
$full_test->($bd);


sub check_angle {
    my ($bd, $bridge, $expected) = @_;
    
    my $result = $bd->detect_angle($bridge);
    
    # our epsilon is equal to the steps used by the bridge detection algorithm
    ###use XXX; YYY [ rad2deg($result), $expected ];
{'fraction_non_alphanumeric': 0.19461291473421335, 'fraction_numerical': 0.048876204067070994, 'mean_word_length': 2.5965362411802437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2164698', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2019, 'n_words': 442}