mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-11-02 12:41:18 -07:00
Ported Slic3r::ExPolygon::Collection to XS
This commit is contained in:
parent
7f4dc4e248
commit
b1ad466189
10 changed files with 173 additions and 68 deletions
|
|
@ -26,4 +26,10 @@ sub rotate {
|
|||
$self->_rotate($angle, $center);
|
||||
}
|
||||
|
||||
package Slic3r::ExPolygon::Collection;
|
||||
use overload
|
||||
'@{}' => sub { $_[0]->arrayref };
|
||||
|
||||
sub clone { (ref $_[0])->_clone($_[0]) }
|
||||
|
||||
1;
|
||||
|
|
|
|||
|
|
@ -100,6 +100,33 @@ polygon2perl(Polygon& poly) {
|
|||
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Polygon", GV_ADD));
|
||||
}
|
||||
|
||||
void
|
||||
perl2expolygon(SV* expoly_sv, ExPolygon& expoly)
|
||||
{
|
||||
AV* expoly_av = (AV*)SvRV(expoly_sv);
|
||||
const unsigned int num_polygons = av_len(expoly_av)+1;
|
||||
expoly.holes.resize(num_polygons-1);
|
||||
|
||||
SV** polygon_sv = av_fetch(expoly_av, 0, 0);
|
||||
perl2polygon(*polygon_sv, expoly.contour);
|
||||
for (unsigned int i = 0; i < num_polygons-1; i++) {
|
||||
polygon_sv = av_fetch(expoly_av, i+1, 0);
|
||||
perl2polygon(*polygon_sv, expoly.holes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
SV*
|
||||
expolygon2perl(ExPolygon& expoly) {
|
||||
const unsigned int num_holes = expoly.holes.size();
|
||||
AV* av = newAV();
|
||||
av_extend(av, num_holes); // -1 +1
|
||||
av_store(av, 0, polygon2perl(expoly.contour));
|
||||
for (unsigned int i = 0; i < num_holes; i++) {
|
||||
av_store(av, i+1, polygon2perl(expoly.holes[i]));
|
||||
}
|
||||
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::ExPolygon", GV_ADD));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
53
xs/src/ExPolygonCollection.hpp
Normal file
53
xs/src/ExPolygonCollection.hpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#ifndef slic3r_ExPolygonCollection_hpp_
|
||||
#define slic3r_ExPolygonCollection_hpp_
|
||||
|
||||
extern "C" {
|
||||
#include "EXTERN.h"
|
||||
#include "perl.h"
|
||||
#include "XSUB.h"
|
||||
#include "ppport.h"
|
||||
}
|
||||
|
||||
#include "ExPolygon.hpp"
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
typedef std::vector<ExPolygon> ExPolygons;
|
||||
|
||||
class ExPolygonCollection
|
||||
{
|
||||
public:
|
||||
ExPolygons expolygons;
|
||||
SV* arrayref();
|
||||
void scale(double factor);
|
||||
void translate(double x, double y);
|
||||
void rotate(double angle, Point* center);
|
||||
};
|
||||
|
||||
void
|
||||
ExPolygonCollection::scale(double factor)
|
||||
{
|
||||
for (ExPolygons::iterator it = expolygons.begin(); it != expolygons.end(); ++it) {
|
||||
(*it).scale(factor);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ExPolygonCollection::translate(double x, double y)
|
||||
{
|
||||
for (ExPolygons::iterator it = expolygons.begin(); it != expolygons.end(); ++it) {
|
||||
(*it).translate(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ExPolygonCollection::rotate(double angle, Point* center)
|
||||
{
|
||||
for (ExPolygons::iterator it = expolygons.begin(); it != expolygons.end(); ++it) {
|
||||
(*it)._rotate(angle, center);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -4,7 +4,7 @@ use strict;
|
|||
use warnings;
|
||||
|
||||
use Slic3r::XS;
|
||||
use Test::More tests => 9;
|
||||
use Test::More tests => 11;
|
||||
|
||||
use constant PI => 4 * atan2(1, 1);
|
||||
|
||||
|
|
@ -71,4 +71,15 @@ isa_ok $expolygon->[0][0], 'Slic3r::Point', 'Perl polygon points are blessed';
|
|||
], 'rotate around pure-Perl Point';
|
||||
}
|
||||
|
||||
{
|
||||
my $expolygon2 = $expolygon->clone;
|
||||
$expolygon2->scale(2);
|
||||
my $collection = Slic3r::ExPolygon::Collection->new($expolygon->arrayref, $expolygon2->arrayref);
|
||||
is_deeply [ @$collection ], [ $expolygon->arrayref, $expolygon2->arrayref ],
|
||||
'expolygon collection';
|
||||
my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
|
||||
is_deeply [ @$collection ], [ @$collection2 ],
|
||||
'expolygon collection with XS expolygons';
|
||||
}
|
||||
|
||||
__END__
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
%name{Slic3r::ExPolygon::XS} class ExPolygon {
|
||||
%name{_clone} ExPolygon(ExPolygon& self);
|
||||
~ExPolygon();
|
||||
SV* arrayref()
|
||||
%code{% RETVAL = expolygon2perl(*THIS); %};
|
||||
void scale(double factor);
|
||||
void translate(double x, double y);
|
||||
void _rotate(double angle, Point* center);
|
||||
|
|
@ -26,20 +28,6 @@ ExPolygon::new(...)
|
|||
OUTPUT:
|
||||
RETVAL
|
||||
|
||||
SV*
|
||||
ExPolygon::arrayref()
|
||||
CODE:
|
||||
AV* av = newAV();
|
||||
av_fill(av, THIS->holes.size()); // -1 +1
|
||||
av_store(av, 0, polygon2perl(THIS->contour));
|
||||
int i = 0;
|
||||
for (Polygons::iterator it = THIS->holes.begin(); it != THIS->holes.end(); ++it) {
|
||||
av_store(av, ++i, polygon2perl(*it));
|
||||
}
|
||||
RETVAL = sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::ExPolygon", GV_ADD));
|
||||
OUTPUT:
|
||||
RETVAL
|
||||
|
||||
%}
|
||||
};
|
||||
|
||||
|
|
|
|||
48
xs/xsp/ExPolygonCollection.xsp
Normal file
48
xs/xsp/ExPolygonCollection.xsp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
%module{Slic3r::XS};
|
||||
|
||||
%{
|
||||
#include <myinit.h>
|
||||
#include "ExPolygonCollection.hpp"
|
||||
%}
|
||||
|
||||
%name{Slic3r::ExPolygon::Collection} class ExPolygonCollection {
|
||||
%name{_clone} ExPolygonCollection(ExPolygonCollection& self);
|
||||
~ExPolygonCollection();
|
||||
void scale(double factor);
|
||||
void translate(double x, double y);
|
||||
void rotate(double angle, Point* center);
|
||||
%{
|
||||
|
||||
ExPolygonCollection*
|
||||
ExPolygonCollection::new(...)
|
||||
CODE:
|
||||
RETVAL = new ExPolygonCollection ();
|
||||
// ST(0) is class name, others are expolygons
|
||||
RETVAL->expolygons.resize(items-1);
|
||||
for (unsigned int i = 1; i < items; i++) {
|
||||
if (sv_isobject(ST(i)) && (SvTYPE(SvRV(ST(i))) == SVt_PVMG)) {
|
||||
// a XS ExPolygon was supplied
|
||||
RETVAL->expolygons[i-1] = *(ExPolygon *)SvIV((SV*)SvRV( ST(i) ));
|
||||
} else {
|
||||
// a Perl arrayref was supplied
|
||||
perl2expolygon(ST(i), RETVAL->expolygons[i-1]);
|
||||
}
|
||||
}
|
||||
OUTPUT:
|
||||
RETVAL
|
||||
|
||||
SV*
|
||||
ExPolygonCollection::arrayref()
|
||||
CODE:
|
||||
AV* av = newAV();
|
||||
av_fill(av, THIS->expolygons.size()-1);
|
||||
int i = 0;
|
||||
for (ExPolygons::iterator it = THIS->expolygons.begin(); it != THIS->expolygons.end(); ++it) {
|
||||
av_store(av, i++, expolygon2perl(*it));
|
||||
}
|
||||
RETVAL = newRV_noinc((SV*)av);
|
||||
OUTPUT:
|
||||
RETVAL
|
||||
|
||||
%}
|
||||
};
|
||||
|
|
@ -2,3 +2,4 @@ ZTable* O_OBJECT
|
|||
TriangleMesh* O_OBJECT
|
||||
Point* O_OBJECT
|
||||
ExPolygon* O_OBJECT
|
||||
ExPolygonCollection* O_OBJECT
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue