Return Surface objects by reference from SurfaceCollection objects and fix a bug in XS code causing some shell options to be ignored

This commit is contained in:
Alessandro Ranellucci 2013-09-06 18:36:38 +02:00
parent cb677c45de
commit e02ae0d18a
9 changed files with 68 additions and 47 deletions

View file

@ -181,6 +181,11 @@ sub clone {
);
}
package Slic3r::Surface::Ref;
our @ISA = 'Slic3r::Surface';
sub DESTROY {}
package Slic3r::Surface::Collection;
use overload
'@{}' => sub { $_[0]->arrayref },

18
xs/src/Surface.cpp Normal file
View file

@ -0,0 +1,18 @@
#include "Surface.hpp"
namespace Slic3r {
SV*
Surface::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Surface::Ref", (void*)this );
return sv;
}
double
Surface::area() const
{
return this->expolygon.area();
}
}

View file

@ -16,6 +16,8 @@ class Surface
unsigned short thickness_layers; // in layers
double bridge_angle;
unsigned short extra_perimeters;
SV* to_SV_ref();
double area() const;
};
typedef std::vector<Surface> Surfaces;

View file

@ -4,7 +4,7 @@ use strict;
use warnings;
use Slic3r::XS;
use Test::More tests => 13;
use Test::More tests => 14;
my $square = [ # ccw
[100, 100],
@ -58,8 +58,9 @@ is $surface->extra_perimeters, 2, 'extra_perimeters';
is scalar(@$collection), 1, 'append to collection';
my $item = $collection->[0];
isa_ok $item, 'Slic3r::Surface::Ref';
$item->surface_type(Slic3r::Surface::S_TYPE_INTERNAL);
isnt $item->surface_type, $collection->[0]->surface_type, 'collection returns copies of items';
is $item->surface_type, $collection->[0]->surface_type, 'collection returns items by reference';
}
__END__

View file

@ -13,6 +13,7 @@
%code{% RETVAL = THIS->thickness; %};
unsigned short thickness_layers()
%code{% RETVAL = THIS->thickness_layers; %};
double area();
%{
Surface*

View file

@ -31,9 +31,19 @@ SurfaceCollection::arrayref()
av_fill(av, THIS->surfaces.size()-1);
int i = 0;
for (Surfaces::iterator it = THIS->surfaces.begin(); it != THIS->surfaces.end(); ++it) {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Surface", new Surface(*it) );
av_store(av, i++, sv);
av_store(av, i++, (*it).to_SV_ref());
}
RETVAL = newRV_noinc((SV*)av);
OUTPUT:
RETVAL
SV*
SurfaceCollection::filter_by_type(surface_type)
SurfaceType surface_type;
CODE:
AV* av = newAV();
for (Surfaces::iterator it = THIS->surfaces.begin(); it != THIS->surfaces.end(); ++it) {
if ((*it).surface_type == surface_type) av_push(av, (*it).to_SV_ref());
}
RETVAL = newRV_noinc((SV*)av);
OUTPUT:
@ -43,6 +53,7 @@ void
SurfaceCollection::append(...)
CODE:
for (unsigned int i = 1; i < items; i++) {
// Note: a COPY of the input is stored
THIS->surfaces.push_back(*(Surface *)SvIV((SV*)SvRV( ST(i) )));
}