From 8db2d96c75006155af969633ea493950a4011756 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Wed, 19 May 2021 12:01:30 +0200 Subject: [PATCH] Point::ccw() optimization: Calculate with int64, not doubles. --- src/libslic3r/Point.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/Point.cpp b/src/libslic3r/Point.cpp index b16abe7be..7f351d259 100644 --- a/src/libslic3r/Point.cpp +++ b/src/libslic3r/Point.cpp @@ -117,7 +117,9 @@ bool Point::nearest_point(const Points &points, Point* point) const */ double Point::ccw(const Point &p1, const Point &p2) const { - return (double)(p2(0) - p1(0))*(double)((*this)(1) - p1(1)) - (double)(p2(1) - p1(1))*(double)((*this)(0) - p1(0)); + static_assert(sizeof(coord_t) == 4, "Point::ccw() requires a 32 bit coord_t"); + return cross2((p2 - p1).cast(), (*this - p1).cast()); +// return cross2((p2 - p1).cast(), (*this - p1).cast()); } double Point::ccw(const Line &line) const @@ -129,9 +131,9 @@ double Point::ccw(const Line &line) const // i.e. this assumes a CCW rotation from p1 to p2 around this double Point::ccw_angle(const Point &p1, const Point &p2) const { - double angle = atan2(p1(0) - (*this)(0), p1(1) - (*this)(1)) - - atan2(p2(0) - (*this)(0), p2(1) - (*this)(1)); - + //FIXME this calculates an atan2 twice! Project one vector into the other! + double angle = atan2(p1.x() - (*this).x(), p1.y() - (*this).y()) + - atan2(p2.x() - (*this).x(), p2.y() - (*this).y()); // we only want to return only positive angles return angle <= 0 ? angle + 2*PI : angle; } @@ -201,12 +203,12 @@ int orient(const Vec2crd &p1, const Vec2crd &p2, const Vec2crd &p3) { Slic3r::Vector v1(p2 - p1); Slic3r::Vector v2(p3 - p1); - return Int128::sign_determinant_2x2_filtered(v1(0), v1(1), v2(0), v2(1)); + return Int128::sign_determinant_2x2_filtered(v1.x(), v1.y(), v2.x(), v2.y()); } int cross(const Vec2crd &v1, const Vec2crd &v2) { - return Int128::sign_determinant_2x2_filtered(v1(0), v1(1), v2(0), v2(1)); + return Int128::sign_determinant_2x2_filtered(v1.x(), v1.y(), v2.x(), v2.y()); } }