static inline cairo_bo_intersect_ordinate_t round_to_nearest (cairo_quorem64_t d, cairo_int64_t den) { cairo_bo_intersect_ordinate_t ordinate; int32_t quo = d.quo; cairo_int64_t drem_2 = _cairo_int64_mul (d.rem, _cairo_int32_to_int64 (2)); /* assert (! _cairo_int64_negative (den));*/ if (_cairo_int64_lt (drem_2, _cairo_int64_negate (den))) { quo -= 1; drem_2 = _cairo_int64_negate (drem_2); } else if (_cairo_int64_le (den, drem_2)) { quo += 1; drem_2 = _cairo_int64_negate (drem_2); } ordinate.ordinate = quo; ordinate.approx = _cairo_int64_is_zero (drem_2) ? EXACT : _cairo_int64_negative (drem_2) ? EXCESS : DEFAULT; return ordinate; }
static cairo_int64_t distance_from_face (const cairo_stroke_face_t *face, const cairo_point_t *p, cairo_bool_t negate) { int32_t dx = (p->x - face->point.x); int32_t dy = (p->y - face->point.y); cairo_int64_t d; d = _cairo_int64_sub (_cairo_int32x32_64_mul (dx, face->dev_vector.dy), _cairo_int32x32_64_mul (dy, face->dev_vector.dx)); if (negate) d = _cairo_int64_negate (d); return d; }
/* Compute the intersection of two lines as defined by two edges. The * result is provided as a coordinate pair of 128-bit integers. * * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel. */ static cairo_bool_t intersect_lines (cairo_bo_edge_t *a, cairo_bo_edge_t *b, cairo_bo_intersect_point_t *intersection) { cairo_int64_t a_det, b_det; /* XXX: We're assuming here that dx and dy will still fit in 32 * bits. That's not true in general as there could be overflow. We * should prevent that before the tessellation algorithm begins. * What we're doing to mitigate this is to perform clamping in * cairo_bo_tessellate_polygon(). */ int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x; int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y; int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x; int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y; cairo_int64_t den_det; cairo_int64_t R; cairo_quorem64_t qr; den_det = det32_64 (dx1, dy1, dx2, dy2); /* Q: Can we determine that the lines do not intersect (within range) * much more cheaply than computing the intersection point i.e. by * avoiding the division? * * X = ax + t * adx = bx + s * bdx; * Y = ay + t * ady = by + s * bdy; * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx) * => t * L = R * * Therefore we can reject any intersection (under the criteria for * valid intersection events) if: * L^R < 0 => t < 0, or * L<R => t > 1 * * (where top/bottom must at least extend to the line endpoints). * * A similar substitution can be performed for s, yielding: * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by) */ R = det32_64 (dx2, dy2, b->edge.line.p1.x - a->edge.line.p1.x, b->edge.line.p1.y - a->edge.line.p1.y); if (_cairo_int64_negative (den_det)) { if (_cairo_int64_ge (den_det, R)) return FALSE; } else { if (_cairo_int64_le (den_det, R)) return FALSE; } R = det32_64 (dy1, dx1, a->edge.line.p1.y - b->edge.line.p1.y, a->edge.line.p1.x - b->edge.line.p1.x); if (_cairo_int64_negative (den_det)) { if (_cairo_int64_ge (den_det, R)) return FALSE; } else { if (_cairo_int64_le (den_det, R)) return FALSE; } /* We now know that the two lines should intersect within range. */ a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y, a->edge.line.p2.x, a->edge.line.p2.y); b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y, b->edge.line.p2.x, b->edge.line.p2.y); /* x = det (a_det, dx1, b_det, dx2) / den_det */ qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1, b_det, dx2), den_det); if (_cairo_int64_eq (qr.rem, den_det)) return FALSE; #if 0 intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT; #else intersection->x.exactness = EXACT; if (! _cairo_int64_is_zero (qr.rem)) { if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem)) qr.rem = _cairo_int64_negate (qr.rem); qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2)); if (_cairo_int64_ge (qr.rem, den_det)) { qr.quo = _cairo_int64_add (qr.quo, _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1)); } else intersection->x.exactness = INEXACT; } #endif intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo); /* y = det (a_det, dy1, b_det, dy2) / den_det */ qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1, b_det, dy2), den_det); if (_cairo_int64_eq (qr.rem, den_det)) return FALSE; #if 0 intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT; #else intersection->y.exactness = EXACT; if (! _cairo_int64_is_zero (qr.rem)) { if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem)) qr.rem = _cairo_int64_negate (qr.rem); qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2)); if (_cairo_int64_ge (qr.rem, den_det)) { qr.quo = _cairo_int64_add (qr.quo, _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1)); } else intersection->y.exactness = INEXACT; } #endif intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo); return TRUE; }