PrusaSlicer-NonPlainar/xs/src/Polyline.hpp

104 lines
2.2 KiB
C++
Raw Normal View History

2013-07-14 14:09:54 +00:00
#ifndef slic3r_Polyline_hpp_
#define slic3r_Polyline_hpp_
extern "C" {
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
}
#include "Point.hpp"
#include <algorithm>
2013-07-15 18:31:43 +00:00
#include <string>
#include <vector>
2013-07-14 14:09:54 +00:00
namespace Slic3r {
class Polyline
{
public:
Points points;
2013-07-15 18:31:43 +00:00
void from_SV(SV* poly_sv);
void from_SV_check(SV* poly_sv);
SV* to_SV(bool pureperl = false, bool pureperl_children = false);
2013-07-14 14:09:54 +00:00
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center);
void reverse();
2013-07-15 18:31:43 +00:00
protected:
virtual char* perl_class() {
return (char*)"Slic3r::Polyline";
}
2013-07-14 14:09:54 +00:00
};
typedef std::vector<Polyline> Polylines;
void
Polyline::scale(double factor)
{
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
2013-07-15 14:04:49 +00:00
(*it).scale(factor);
2013-07-14 14:09:54 +00:00
}
}
void
Polyline::translate(double x, double y)
{
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
2013-07-15 14:04:49 +00:00
(*it).translate(x, y);
2013-07-14 14:09:54 +00:00
}
}
void
Polyline::rotate(double angle, Point* center)
{
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
(*it).rotate(angle, center);
}
}
void
Polyline::reverse()
{
std::reverse(this->points.begin(), this->points.end());
}
2013-07-14 14:09:54 +00:00
void
2013-07-15 18:31:43 +00:00
Polyline::from_SV(SV* poly_sv)
2013-07-14 14:09:54 +00:00
{
AV* poly_av = (AV*)SvRV(poly_sv);
const unsigned int num_points = av_len(poly_av)+1;
2013-07-15 18:31:43 +00:00
this->points.resize(num_points);
2013-07-14 14:09:54 +00:00
for (unsigned int i = 0; i < num_points; i++) {
SV** point_sv = av_fetch(poly_av, i, 0);
2013-07-15 18:31:43 +00:00
perl2point_check(*point_sv, this->points[i]);
2013-07-14 14:09:54 +00:00
}
}
2013-07-15 14:21:09 +00:00
void
2013-07-15 18:31:43 +00:00
Polyline::from_SV_check(SV* poly_sv)
2013-07-15 14:21:09 +00:00
{
if (sv_isobject(poly_sv) && (SvTYPE(SvRV(poly_sv)) == SVt_PVMG)) {
2013-07-15 18:31:43 +00:00
*this = *(Polyline*)SvIV((SV*)SvRV( poly_sv ));
2013-07-15 14:21:09 +00:00
} else {
2013-07-15 18:31:43 +00:00
this->from_SV(poly_sv);
2013-07-15 14:21:09 +00:00
}
}
2013-07-14 14:09:54 +00:00
SV*
2013-07-15 18:31:43 +00:00
Polyline::to_SV(bool pureperl, bool pureperl_children) {
const unsigned int num_points = this->points.size();
2013-07-14 14:09:54 +00:00
AV* av = newAV();
av_extend(av, num_points-1);
for (unsigned int i = 0; i < num_points; i++) {
2013-07-15 18:31:43 +00:00
av_store(av, i, this->points[i].to_SV(pureperl_children));
2013-07-14 14:09:54 +00:00
}
2013-07-15 18:31:43 +00:00
return sv_bless(newRV_noinc((SV*)av), gv_stashpv(this->perl_class(), GV_ADD));
2013-07-14 14:09:54 +00:00
}
}
#endif