Algorithm-BreakOverlappingRectangles
view release on metacpan or search on metacpan
lib/Algorithm/BreakOverlappingRectangles.pm view on Meta::CPAN
package Algorithm::BreakOverlappingRectangles;
use strict;
use warnings;
BEGIN {
our $VERSION = '0.01';
require XSLoader;
XSLoader::load('Algorithm::BreakOverlappingRectangles', $VERSION);
}
use constant X0 => 0;
use constant Y0 => 1;
use constant X1 => 2;
use constant Y1 => 3;
our $verbose = 0;
use constant NVSIZE => length pack F => 1.0;
use constant IDOFFSET => NVSIZE * 4;
sub new {
my $class = shift;
my $self = { rects => [],
name2id => {},
names => [],
n => 0 };
bless $self, $class;
}
sub add_rectangle {
my ($self, $x0, $y0, $x1, $y1, @names) = @_;
($x0, $x1) = ($x1, $x0) if $x0 > $x1;
($y0, $y1) = ($y1, $y0) if $y0 > $y1;
my @ids;
for (@names) {
my $id = $self->{name2id}{$_};
unless (defined $id) {
$id = $self->{name2id}{$_} = @{$self->{names}};
push @{$self->{names}}, $_;
}
push @ids, $id;
}
push @{$self->{rects}}, pack 'F4L*' => $x0, $y0, $x1, $y1, @ids;
delete $self->{broken};
++($self->{n});
}
sub _do_break {
my $self = shift;
_break_rectangles $self->{rects};
$self->{broken} = 1;
$self->{iter} = 0;
}
* _brute_force_break = \&_brute_force_break_xs;
sub dump {
my $self = shift;
$self->_do_break unless $self->{broken};
for (@{$self->{rects}}) {
my ($x0, $y0, $x1, $y1, @ids) = unpack 'F4L*' => $_;
my @names = map $self->{names}[$_], @ids;
# my @names = @ids;
print "[$x0 $y0 $x1 $y1 | @names]\n";
}
print "$self->{n} rectangles broken into ".(scalar @{$self->{rects}})."\n";
}
sub dump_stats {
my $self = shift;
$self->_do_break unless $self->{broken};
print "$self->{n} rectangles broken into ".(scalar @{$self->{rects}})."\n";
}
( run in 0.442 second using v1.01-cache-2.11-cpan-0bd6704ced7 )