From 8becebf8501b46afc1103b0e27e7d35f8813a6f5 Mon Sep 17 00:00:00 2001 From: Jan Wielkiewicz Date: Fri, 23 Aug 2024 20:46:34 +0200 Subject: [PATCH] intersect: Fix cpml.intersect.segment_segment bug. --- modules/intersect.lua | 50 ++++++++++++++++++++++--------------------- modules/vec3.lua | 8 +++++++ 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/modules/intersect.lua b/modules/intersect.lua index c8d7086..3d5d04c 100644 --- a/modules/intersect.lua +++ b/modules/intersect.lua @@ -305,35 +305,37 @@ end -- e is a number function intersect.segment_segment(a, b, e) local c, d = intersect.line_line(a, b, e) + local A = {vec3.component_sort(a[1], a[2])} + local B = {vec3.component_sort(b[1], b[2])} if c and (( - a[1].x <= c[1].x and - a[1].y <= c[1].y and - a[1].z <= c[1].z and - c[1].x <= a[2].x and - c[1].y <= a[2].y and - c[1].z <= a[2].z + A[1].x <= c[1].x and + A[1].y <= c[1].y and + A[1].z <= c[1].z and + c[1].x <= A[2].x and + c[1].y <= A[2].y and + c[1].z <= A[2].z ) or ( - a[1].x >= c[1].x and - a[1].y >= c[1].y and - a[1].z >= c[1].z and - c[1].x >= a[2].x and - c[1].y >= a[2].y and - c[1].z >= a[2].z + A[1].x >= c[1].x and + A[1].y >= c[1].y and + A[1].z >= c[1].z and + c[1].x >= A[2].x and + c[1].y >= A[2].y and + c[1].z >= A[2].z )) and (( - b[1].x <= c[2].x and - b[1].y <= c[2].y and - b[1].z <= c[2].z and - c[2].x <= b[2].x and - c[2].y <= b[2].y and - c[2].z <= b[2].z + B[1].x <= c[2].x and + B[1].y <= c[2].y and + B[1].z <= c[2].z and + c[2].x <= B[2].x and + c[2].y <= B[2].y and + c[2].z <= B[2].z ) or ( - b[1].x >= c[2].x and - b[1].y >= c[2].y and - b[1].z >= c[2].z and - c[2].x >= b[2].x and - c[2].y >= b[2].y and - c[2].z >= b[2].z + B[1].x >= c[2].x and + B[1].y >= c[2].y and + B[1].z >= c[2].z and + c[2].x >= B[2].x and + c[2].y >= B[2].y and + c[2].z >= B[2].z )) then return c, d end diff --git a/modules/vec3.lua b/modules/vec3.lua index b653506..d03e43f 100644 --- a/modules/vec3.lua +++ b/modules/vec3.lua @@ -309,6 +309,14 @@ function vec3.component_max(a, b) return new(math.max(a.x, b.x), math.max(a.y, b.y), math.max(a.z, b.z)) end +--- Return the component-wise minimum and maximum of two vectors. +-- @tparam vec3 a Left hand operand +-- @tparam vec3 b Right hand operand +-- @treturn vec3, vec3 sorted vectors +function vec3.component_sort(a, b) + return vec3.component_min(a, b), vec3.component_max(a, b) +end + -- Negate x axis only of vector. -- @tparam vec3 a Vector to x-flip. -- @treturn vec3 x-flipped vector