star-cpr

Clip 2D meshes with 2D polygons
git clone git://git.meso-star.fr/star-cpr.git
Log | Files | Refs | README | LICENSE

commit aa2327408636298b2ff7e4a61b4f8e6b159be44f
parent 58b26eeea2b9f865a10b67e9500e8c18f46be5c7
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Fri,  3 Mar 2023 16:40:21 +0100

Fix integer arithmetic for intersection computations

Diffstat:
Msrc/scpr_intersector.c | 67+++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 33 insertions(+), 34 deletions(-)

diff --git a/src/scpr_intersector.c b/src/scpr_intersector.c @@ -454,12 +454,11 @@ safe_mul int64_t b, int* ovflw) { - if(*ovflw) return 0; - if(b != 0 && abs(a) > 1 + (INT64_MAX / abs(b))) goto error; - return a * b; -error: - *ovflw = 1; - return 0; + int64_t r; + if(b == 0) return 0; + r = a * b; + if (r / b != a) *ovflw = 1; + return r; } /* Check if a triangle is CCW (>0), CW (<0), or flat (=0) */ @@ -534,38 +533,38 @@ check_two_segments_intersection } else if(ccw1 == 0 && ccw2 == 0 && ccw3 == 0 && ccw4 == 0) { /* collinear segments */ - struct scpr_callback_segment arg1, arg2; - arg1.polygon = polygon1; - arg1.component = comp_idx1; - arg1.first_vertex = r11; - arg1.last_vertex = r12; - arg2.polygon = polygon2; - arg2.component = comp_idx2; - arg2.first_vertex = r21; - arg2.last_vertex = r22; - if(callbacks->collinear_segments - && callbacks->collinear_segments(&arg1, &arg2, data)) - { - res = RES_BAD_ARG; - goto error; + if(callbacks->collinear_segments) { + struct scpr_callback_segment arg1, arg2; + arg1.polygon = polygon1; + arg1.component = comp_idx1; + arg1.first_vertex = r11; + arg1.last_vertex = r12; + arg2.polygon = polygon2; + arg2.component = comp_idx2; + arg2.first_vertex = r21; + arg2.last_vertex = r22; + if(callbacks->collinear_segments(&arg1, &arg2, data)) { + res = RES_BAD_ARG; + goto error; + } } } else if(ccw1 * ccw2 < 0 && ccw3 * ccw4 < 0) { /* Basic segment intersection */ - struct scpr_callback_segment arg1, arg2; - arg1.polygon = polygon1; - arg1.component = comp_idx1; - arg1.first_vertex = r11; - arg1.last_vertex = r12; - arg2.polygon = polygon2; - arg2.component = comp_idx2; - arg2.first_vertex = r21; - arg2.last_vertex = r22; - if(callbacks->simple_intersection - && callbacks->simple_intersection(&arg1, &arg2, data)) - { - res = RES_BAD_ARG; - goto error; + if(callbacks->simple_intersection) { + struct scpr_callback_segment arg1, arg2; + arg1.polygon = polygon1; + arg1.component = comp_idx1; + arg1.first_vertex = r11; + arg1.last_vertex = r12; + arg2.polygon = polygon2; + arg2.component = comp_idx2; + arg2.first_vertex = r21; + arg2.last_vertex = r22; + if(callbacks->simple_intersection(&arg1, &arg2, data)) { + res = RES_BAD_ARG; + goto error; + } } }