PrusaSlicer-NonPlainar/xs/src/Point.hpp

87 lines
1.8 KiB
C++
Raw Normal View History

2013-07-06 13:26:32 +00:00
#ifndef slic3r_Point_hpp_
#define slic3r_Point_hpp_
extern "C" {
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
}
2013-07-11 16:55:51 +00:00
#include <math.h>
namespace Slic3r {
2013-07-06 13:26:32 +00:00
class Point
{
public:
long x;
long y;
Point(long _x = 0, long _y = 0): x(_x), y(_y) {};
2013-07-15 14:04:49 +00:00
void scale(double factor);
void translate(double x, double y);
2013-07-11 16:55:51 +00:00
void rotate(double angle, Point* center);
2013-07-15 14:04:49 +00:00
bool coincides_with(Point* point);
2013-07-06 13:26:32 +00:00
};
2013-07-14 13:53:53 +00:00
typedef std::vector<Point> Points;
2013-07-15 14:04:49 +00:00
void
Point::scale(double factor)
{
this->x *= factor;
this->y *= factor;
}
void
Point::translate(double x, double y)
{
this->x += x;
this->y += y;
}
2013-07-11 16:55:51 +00:00
void
Point::rotate(double angle, Point* center)
{
2013-07-15 14:04:49 +00:00
double cur_x = (double)this->x;
double cur_y = (double)this->y;
this->x = (long)( (double)center->x + cos(angle) * (cur_x - (double)center->x) - sin(angle) * (cur_y - (double)center->y) );
this->y = (long)( (double)center->y + cos(angle) * (cur_y - (double)center->y) + sin(angle) * (cur_x - (double)center->x) );
}
bool
Point::coincides_with(Point* point)
{
return this->x == point->x && this->y == point->y;
2013-07-11 16:55:51 +00:00
}
2013-07-06 14:39:22 +00:00
SV*
point2perl(Point& point) {
AV* av = newAV();
av_fill(av, 1);
av_store_point_xy(av, point.x, point.y);
return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Point", GV_ADD));
2013-07-06 14:39:22 +00:00
}
void
perl2point(SV* point_sv, Point& point)
{
AV* point_av = (AV*)SvRV(point_sv);
point.x = (unsigned long)SvIV(*av_fetch(point_av, 0, 0));
point.y = (unsigned long)SvIV(*av_fetch(point_av, 1, 0));
}
2013-07-15 14:04:49 +00:00
void
perl2point_check(SV* point_sv, Point& point)
{
if (sv_isobject(point_sv) && (SvTYPE(SvRV(point_sv)) == SVt_PVMG)) {
point = *(Point*)SvIV((SV*)SvRV( point_sv ));
} else {
perl2point(point_sv, point);
}
}
}
2013-07-06 13:26:32 +00:00
#endif