Point::ccw() optimization: Calculate with int64, not doubles.

This commit is contained in:
Vojtech Bubnik 2021-05-19 12:01:30 +02:00
parent 6b54f34ce1
commit 8db2d96c75

View File

@ -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<int64_t>(), (*this - p1).cast<int64_t>());
// return cross2((p2 - p1).cast<double>(), (*this - p1).cast<double>());
}
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());
}
}