Skip to content

Commit

Permalink
intersect: Fix cpml.intersect.segment_segment bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
kromka-chleba committed Aug 23, 2024
1 parent eb209f6 commit 8becebf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
50 changes: 26 additions & 24 deletions modules/intersect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions modules/vec3.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8becebf

Please sign in to comment.