Algorithm-SpatialIndex

 view release on metacpan or  search on metacpan

lib/Algorithm/SpatialIndex.pm  view on Meta::CPAN

package Algorithm::SpatialIndex;
use 5.008001;
use strict;
use warnings;
use Carp qw(croak);

our $VERSION = '0.06';

use Module::Pluggable (
  sub_name    => 'strategies',
  search_path => [__PACKAGE__ . "::Strategy"],
  require     => 1,
  inner       => 0,
);

use Module::Pluggable (
  sub_name    => 'storage_backends',
  search_path => [__PACKAGE__ . "::Storage"],
  require     => 1,
  inner       => 0,
);

use Algorithm::SpatialIndex::Node;
use Algorithm::SpatialIndex::Bucket;
use Algorithm::SpatialIndex::Strategy;
use Algorithm::SpatialIndex::Storage;

use Class::XSAccessor {
  getters => [qw(
    strategy
    storage
    limit_x_low
    limit_x_up
    limit_y_low
    limit_y_up
    limit_z_low
    limit_z_up
    bucket_size
    max_depth
  )],
};

sub new {
  my $class = shift;
  my %opt = @_;

  my $self = bless {
    limit_x_low => -100,
    limit_x_up  => 100,
    limit_y_low => -100,
    limit_y_up  => 100,
    limit_z_low => -100,
    limit_z_up  => 100,
    bucket_size => 100,
    max_depth   => 20,
    %opt,
  } => $class;

  $self->_init_strategy(\%opt);
  $self->_init_storage(\%opt);
  $self->strategy->_set_storage($self->storage);
  $self->strategy->_super_init_storage();

  return $self;
}

sub _init_strategy {
  my $self = shift;
  my $opt = shift;
  my $strategy = $opt->{strategy};

  croak("Need strategy") if not defined $strategy;
  my @strategies = grep /\b\Q$strategy\E$/, $self->strategies;
  if (@strategies == 0) {
    croak("Could not find specified strategy '$strategy'. Available strategies: " . join(', ', @strategies));
  }
  elsif (@strategies > 1) {
    croak("Found multiple matching strategy for '$strategy': " . join(', ', @strategies));
  }
  $strategy = shift @strategies;
  $self->{strategy} = $strategy->new(%$opt, index => $self);
}

sub _init_storage {
  my $self = shift;
  my $opt = shift;
  my $storage = $opt->{storage};

  croak("Need storage") if not defined $storage;
  my @storage_backends = grep /\b\Q$storage\E$/, $self->storage_backends;
  if (@storage_backends == 0) {
    croak("Could not find specified storage backends '$storage'");
  }
  elsif (@storage_backends > 1) {
    croak("Found multiple matching storage backends for '$storage': " . join(', ', @storage_backends));
  }
  $storage = shift @storage_backends;
  $self->{storage} = $storage->new(index => $self, opt => $opt);
}

sub insert {
  my $self = shift;
  return $self->{strategy}->insert(@_);
}

sub get_items_in_rect {
  my ($self, @rect) = @_;



( run in 0.574 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )