From f3a9d41c709011827578fe9ccfeb3a7689d93f47 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 23 Jun 2013 17:07:12 +0200 Subject: [PATCH] Cache a Z table for layer range search --- lib/Slic3r/Print/Object.pm | 10 +++++++--- xs/src/myinit.h | 21 +++++++++++++++++++++ xs/t/02_object.t | 4 ++-- xs/xsp/Object.xsp | 35 ++++++++++++++++++++++------------- xs/xsp/my.map | 3 ++- xs/xsp/typemap.xspt | 1 + 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm index 8d60d523e..3ae166e64 100644 --- a/lib/Slic3r/Print/Object.pm +++ b/lib/Slic3r/Print/Object.pm @@ -16,6 +16,7 @@ has 'copies' => (is => 'rw', trigger => 1); # in scaled coordinates has 'layers' => (is => 'rw', default => sub { [] }); has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ] has 'fill_maker' => (is => 'lazy'); +has '_z_table' => (is => 'lazy'); sub BUILD { my $self = shift; @@ -83,6 +84,11 @@ sub _build_fill_maker { return Slic3r::Fill->new(object => $self); } +sub _build__z_table { + my $self = shift; + return Slic3r::Object::XS::ZTable->new([ map $_->slice_z, @{$self->layers} ]); +} + # This should be probably moved in Print.pm at the point where we sort Layer objects sub _trigger_copies { my $self = shift; @@ -99,9 +105,7 @@ sub layer_count { sub get_layer_range { my $self = shift; - my ($min_z, $max_z) = @_; - - return @{ Slic3r::Object::XS::get_layer_range([ map $_->slice_z, @{$self->layers} ], $min_z, $max_z) }; + return @{ $self->_z_table->get_range(@_) }; } sub bounding_box { diff --git a/xs/src/myinit.h b/xs/src/myinit.h index e69de29bb..f2104209e 100644 --- a/xs/src/myinit.h +++ b/xs/src/myinit.h @@ -0,0 +1,21 @@ +#ifndef _myinit_h_ +#define _myinit_h_ + +#include + +typedef std::vector Ztable2; + +class ZTable +{ + public: + ZTable(std::vector* z_array); + std::vector get_range(unsigned int min_z, unsigned int max_z); + std::vector z; +}; + +ZTable::ZTable(std::vector* ztable) : + z(*ztable) +{ +} + +#endif diff --git a/xs/t/02_object.t b/xs/t/02_object.t index d9b2b85dc..4ceddeda3 100644 --- a/xs/t/02_object.t +++ b/xs/t/02_object.t @@ -6,7 +6,7 @@ use warnings; use Slic3r::XS; use Test::More tests => 1; -is_deeply Slic3r::Object::XS::get_layer_range([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ], 39, 69), - [2, 6], 'get_layer_range'; +my $table = Slic3r::Object::XS::ZTable->new([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]); +is_deeply $table->get_range(39, 69), [2, 6], 'get_layer_range'; __END__ diff --git a/xs/xsp/Object.xsp b/xs/xsp/Object.xsp index 9a8168d4f..d6071977b 100644 --- a/xs/xsp/Object.xsp +++ b/xs/xsp/Object.xsp @@ -1,49 +1,58 @@ %module{Slic3r::XS}; -%package{Slic3r::Object::XS}; - -#include -#include %{ -PROTOTYPES: DISABLE +#include +#include +%} +%name{Slic3r::Object::XS::ZTable} class ZTable { + ZTable(std::vector* z_array); + +%{ std::vector -get_layer_range(z_array, min_z, max_z) - std::vector* z_array; +get_range(THIS, min_z, max_z) + ZTable* THIS; unsigned int min_z; unsigned int max_z; CODE: RETVAL.resize(2); unsigned int bottom = 0; - unsigned int top = z_array->size()-1; + unsigned int top = THIS->z.size()-1; while (1) { unsigned int mid = bottom + floor((top - bottom)/2.0); if (mid == top || mid == bottom) { RETVAL[0] = mid; break; } - if ((*z_array)[mid] > min_z) { + if (THIS->z[mid] > min_z) { top = mid; } else { bottom = mid; } } - top = z_array->size()-1; + top = THIS->z.size()-1; while (1) { unsigned int mid = bottom + ceil((top - bottom)/2.0); if (mid == top || mid == bottom) { RETVAL[1] = mid; break; } - if ((*z_array)[mid] < max_z) { + if (THIS->z[mid] < max_z) { bottom = mid; } else { top = mid; } } - delete z_array; OUTPUT: RETVAL - +%} + +}; + +%package{Slic3r::Object::XS}; + +%{ +#include +#include %} diff --git a/xs/xsp/my.map b/xs/xsp/my.map index ad7868c92..6ce884228 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -1 +1,2 @@ -std::map* T_PTR +ZTable* O_OBJECT +std::vector< unsigned int >* T_STD_VECTOR_UINT_PTR diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 15a95c239..cbb66c436 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -1 +1,2 @@ %typemap{std::string}; +%typemap{std::vector< unsigned int >*}; \ No newline at end of file