| 1 |
--- lua-tikz3dtools-geometry.lua |
| 2 |
--- Computational geometry utilities for the lua-tikz3dtools package. |
| 3 |
--- Intersection tests, occlusion sorting, partitioning, and spatial indexing. |
| 4 |
|
| 5 |
local Vector |
| 6 |
local Matrix |
| 7 |
|
| 8 |
local Geometry = {} |
| 9 |
local MODULE_NAME = "lua-tikz3dtools" |
| 10 |
|
| 11 |
local function emit_warning(message) |
| 12 |
luatexbase.module_warning(MODULE_NAME, tostring(message)) |
| 13 |
end |
| 14 |
|
| 15 |
--- Set class references (called after all modules are loaded). |
| 16 |
function Geometry._set_classes(V, M) |
| 17 |
Vector = V |
| 18 |
Matrix = M |
| 19 |
end |
| 20 |
|
| 21 |
-- ================================================================ |
| 22 |
-- Point-level geometry (attached to Vector via methods below) |
| 23 |
-- ================================================================ |
| 24 |
|
| 25 |
--- Check if two homogeneous vectors intersect (are the same point) |
| 26 |
--- @param self Vector |
| 27 |
--- @param other Vector |
| 28 |
--- @return boolean |
| 29 |
function Geometry.hpoint_point_intersecting(self, other) |
| 30 |
return self:hdistance(other) < 1e-12 |
| 31 |
end |
| 32 |
|
| 33 |
--- Check if a homogeneous point is in the triangular prism defined by triangle T |
| 34 |
--- @param self Vector |
| 35 |
--- @param T Matrix |
| 36 |
--- @return boolean |
| 37 |
function Geometry.hpoint_in_triangular_prism(self, T) |
| 38 |
local A = Vector:_new(T[1]) |
| 39 |
local B = Vector:_new(T[2]) |
| 40 |
local C = Vector:_new(T[3]) |
| 41 |
|
| 42 |
local v0 = C:hsub(A) |
| 43 |
local v1 = B:hsub(A) |
| 44 |
local v2 = self:hsub(A) |
| 45 |
|
| 46 |
local d00 = v0:hinner(v0) |
| 47 |
local d01 = v0:hinner(v1) |
| 48 |
local d11 = v1:hinner(v1) |
| 49 |
local d20 = v2:hinner(v0) |
| 50 |
local d21 = v2:hinner(v1) |
| 51 |
local denom = d00 * d11 - d01 * d01 |
| 52 |
local v = (d11 * d20 - d01 * d21) / denom |
| 53 |
local w = (d00 * d21 - d01 * d20) / denom |
| 54 |
local u = 1 - v - w |
| 55 |
local eps = 1e-12 |
| 56 |
return u >= -3*eps and v >= -3*eps and w >= -3*eps |
| 57 |
end |
| 58 |
|
| 59 |
--- Compute barycentric coordinates of a point with respect to a triangle. |
| 60 |
--- @param self Vector |
| 61 |
--- @param T Matrix |
| 62 |
--- @return Vector|nil |
| 63 |
function Geometry.hpoint_triangle_barycentric(self, T) |
| 64 |
local A = Vector:_new(T[1]) |
| 65 |
local B = Vector:_new(T[2]) |
| 66 |
local C = Vector:_new(T[3]) |
| 67 |
|
| 68 |
local v0 = C:hsub(A) |
| 69 |
local v1 = B:hsub(A) |
| 70 |
local v2 = self:hsub(A) |
| 71 |
|
| 72 |
local d00 = v0:hinner(v0) |
| 73 |
local d01 = v0:hinner(v1) |
| 74 |
local d11 = v1:hinner(v1) |
| 75 |
local d20 = v2:hinner(v0) |
| 76 |
local d21 = v2:hinner(v1) |
| 77 |
local denom = d00 * d11 - d01 * d01 |
| 78 |
|
| 79 |
if math.abs(denom) < 1e-12 then |
| 80 |
return nil |
| 81 |
end |
| 82 |
|
| 83 |
local c_weight = (d11 * d20 - d01 * d21) / denom |
| 84 |
local b_weight = (d00 * d21 - d01 * d20) / denom |
| 85 |
local a_weight = 1 - b_weight - c_weight |
| 86 |
return Vector:_new{a_weight, b_weight, c_weight} |
| 87 |
end |
| 88 |
|
| 89 |
--- Reconstruct a point from barycentric coordinates on a triangle. |
| 90 |
--- @param T Matrix |
| 91 |
--- @param bary Vector |
| 92 |
--- @return Vector |
| 93 |
function Geometry.hpoint_from_triangle_barycentric(T, bary) |
| 94 |
local A = Vector:_new(T[1]):scale(bary[1]) |
| 95 |
local B = Vector:_new(T[2]):scale(bary[2]) |
| 96 |
local C = Vector:_new(T[3]):scale(bary[3]) |
| 97 |
return A:add(B):add(C) |
| 98 |
end |
| 99 |
|
| 100 |
--- Clip a line segment to a triangle using barycentric half-spaces. |
| 101 |
--- @param self Matrix |
| 102 |
--- @param T Matrix |
| 103 |
--- @return Matrix|nil |
| 104 |
function Geometry.hclip_line_segment_to_triangle(self, T) |
| 105 |
local P0 = Vector:_new(self[1]) |
| 106 |
local P1 = Vector:_new(self[2]) |
| 107 |
local bary0 = Geometry.hpoint_triangle_barycentric(P0, T) |
| 108 |
local bary1 = Geometry.hpoint_triangle_barycentric(P1, T) |
| 109 |
local eps = 1e-12 |
| 110 |
|
| 111 |
if bary0 == nil or bary1 == nil then |
| 112 |
return nil |
| 113 |
end |
| 114 |
|
| 115 |
local t_min = 0 |
| 116 |
local t_max = 1 |
| 117 |
|
| 118 |
for i = 1, 3 do |
| 119 |
local start_val = bary0[i] |
| 120 |
local delta = bary1[i] - bary0[i] |
| 121 |
|
| 122 |
if math.abs(delta) < eps then |
| 123 |
if start_val < -3 * eps then |
| 124 |
return nil |
| 125 |
end |
| 126 |
else |
| 127 |
local boundary_t = (-3 * eps - start_val) / delta |
| 128 |
if delta > 0 then |
| 129 |
if boundary_t > t_min then t_min = boundary_t end |
| 130 |
else |
| 131 |
if boundary_t < t_max then t_max = boundary_t end |
| 132 |
end |
| 133 |
if t_min > t_max + eps then |
| 134 |
return nil |
| 135 |
end |
| 136 |
end |
| 137 |
end |
| 138 |
|
| 139 |
if t_max < 0 or t_min > 1 then |
| 140 |
return nil |
| 141 |
end |
| 142 |
|
| 143 |
t_min = math.max(0, t_min) |
| 144 |
t_max = math.min(1, t_max) |
| 145 |
if t_max - t_min < eps then |
| 146 |
return nil |
| 147 |
end |
| 148 |
|
| 149 |
local Q0 = P0:scale(1 - t_min):add(P1:scale(t_min)) |
| 150 |
local Q1 = P0:scale(1 - t_max):add(P1:scale(t_max)) |
| 151 |
if Q0:hdistance(Q1) < eps then |
| 152 |
return nil |
| 153 |
end |
| 154 |
|
| 155 |
return Matrix:_new{Q0:to_table(), Q1:to_table()} |
| 156 |
end |
| 157 |
|
| 158 |
--- Reclip triangle-attached embedded line segments onto a child triangle. |
| 159 |
--- @param parent_triangle Matrix |
| 160 |
--- @param child_triangle Matrix |
| 161 |
--- @param embedded_segments table|nil |
| 162 |
--- @return table|nil |
| 163 |
function Geometry.reclip_embedded_segments(parent_triangle, child_triangle, embedded_segments) |
| 164 |
if embedded_segments == nil then |
| 165 |
return nil |
| 166 |
end |
| 167 |
|
| 168 |
local reclipped = {} |
| 169 |
for _, segment in ipairs(embedded_segments) do |
| 170 |
local start_point = Geometry.hpoint_from_triangle_barycentric( |
| 171 |
parent_triangle, |
| 172 |
Vector:_new(segment.start) |
| 173 |
) |
| 174 |
local stop_point = Geometry.hpoint_from_triangle_barycentric( |
| 175 |
parent_triangle, |
| 176 |
Vector:_new(segment.stop) |
| 177 |
) |
| 178 |
local clipped = Geometry.hclip_line_segment_to_triangle( |
| 179 |
Matrix:_new{start_point:to_table(), stop_point:to_table()}, |
| 180 |
child_triangle |
| 181 |
) |
| 182 |
|
| 183 |
if clipped ~= nil then |
| 184 |
local clipped_start = Vector:_new(clipped[1]) |
| 185 |
local clipped_stop = Vector:_new(clipped[2]) |
| 186 |
local start_bary = Geometry.hpoint_triangle_barycentric(Vector:_new(clipped[1]), child_triangle) |
| 187 |
local stop_bary = Geometry.hpoint_triangle_barycentric(Vector:_new(clipped[2]), child_triangle) |
| 188 |
if start_bary ~= nil and stop_bary ~= nil then |
| 189 |
table.insert(reclipped, { |
| 190 |
start = start_bary:to_table(), |
| 191 |
stop = stop_bary:to_table(), |
| 192 |
drawoptions = segment.drawoptions, |
| 193 |
arrowtail = clipped_start:hdistance(start_point) <= 1e-9 and segment.arrowtail or nil, |
| 194 |
arrowtip = clipped_stop:hdistance(stop_point) <= 1e-9 and segment.arrowtip or nil, |
| 195 |
arrowscale = segment.arrowscale |
| 196 |
}) |
| 197 |
end |
| 198 |
end |
| 199 |
end |
| 200 |
|
| 201 |
if #reclipped == 0 then |
| 202 |
return nil |
| 203 |
end |
| 204 |
return reclipped |
| 205 |
end |
| 206 |
|
| 207 |
local function copy_simplex_metadata_for_part(source, part_simplex) |
| 208 |
local meta = {} |
| 209 |
for k, v in pairs(source) do |
| 210 |
if k ~= "simplex" and k ~= "type" and k ~= "bbox2" then |
| 211 |
if k == "embedded_segments" and source.type == "triangle" then |
| 212 |
local reclipped = Geometry.reclip_embedded_segments(source.simplex, part_simplex, v) |
| 213 |
if reclipped ~= nil then |
| 214 |
meta[k] = reclipped |
| 215 |
end |
| 216 |
else |
| 217 |
meta[k] = v |
| 218 |
end |
| 219 |
end |
| 220 |
end |
| 221 |
return meta |
| 222 |
end |
| 223 |
|
| 224 |
--- Check if a homogeneous point intersects a homogeneous triangle |
| 225 |
--- @param self Vector |
| 226 |
--- @param T Matrix |
| 227 |
--- @return boolean |
| 228 |
function Geometry.hpoint_triangle_intersecting(self, T) |
| 229 |
local Tbasis = T:deep_copy():hto_basis() |
| 230 |
local S = self:horthogonal_projection_onto_plane(Tbasis) |
| 231 |
return Geometry.hpoint_in_triangular_prism(S, T) |
| 232 |
and Geometry.hpoint_point_intersecting(self, S) |
| 233 |
end |
| 234 |
|
| 235 |
--- Check occlusion sort of a homogeneous point with a homogeneous point |
| 236 |
--- @param self Vector |
| 237 |
--- @param P Vector |
| 238 |
--- @return boolean|nil |
| 239 |
function Geometry.hpoint_point_occlusion_sort(self, P) |
| 240 |
if not Geometry.hpoint_point_intersecting(self, P) then |
| 241 |
if Geometry.hpoint_point_intersecting( |
| 242 |
Vector:_new{self[1], self[2], 0, 1}, |
| 243 |
Vector:_new{P[1], P[2], 0, 1} |
| 244 |
) then |
| 245 |
return self[3] < P[3] |
| 246 |
end |
| 247 |
end |
| 248 |
return nil |
| 249 |
end |
| 250 |
|
| 251 |
--- Check collinearity of two homogeneous vectors |
| 252 |
--- @param self Vector |
| 253 |
--- @param other Vector |
| 254 |
--- @return boolean |
| 255 |
function Geometry.hcollinear(self, other) |
| 256 |
local cross = self:hhypercross(other) |
| 257 |
return cross:hnorm() < 1e-12 |
| 258 |
end |
| 259 |
|
| 260 |
--- Check opposite direction of two homogeneous vectors |
| 261 |
--- @param self Vector |
| 262 |
--- @param other Vector |
| 263 |
--- @return boolean |
| 264 |
function Geometry.hoppositely_directed(self, other) |
| 265 |
return self:hinner(other) < 0 and Geometry.hcollinear(self, other) |
| 266 |
end |
| 267 |
|
| 268 |
--- Check occlusion sort of a homogeneous point with a homogeneous line segment |
| 269 |
--- @param self Vector |
| 270 |
--- @param L Matrix |
| 271 |
--- @return boolean|nil |
| 272 |
function Geometry.hpoint_line_segment_occlusion_sort(self, L) |
| 273 |
local LA = L:hto_basis() |
| 274 |
local LO = Vector:_new(LA[1]) |
| 275 |
local OP = self:hsub(LO) |
| 276 |
local LU = Vector:_new(LA[2]) |
| 277 |
local proj = OP:hproject_onto(LU) |
| 278 |
local true_proj = LO:hadd(proj) |
| 279 |
local F = Vector:_new{self[1], self[2], 0, 1} |
| 280 |
local G = Vector:_new{true_proj[1], true_proj[2], 0, 1} |
| 281 |
if Geometry.hpoint_point_intersecting(F, G) then |
| 282 |
local temp = 1 |
| 283 |
if Geometry.hoppositely_directed(proj, LU) then |
| 284 |
temp = -1 |
| 285 |
end |
| 286 |
local test = temp * proj:hnorm() / LU:hnorm() |
| 287 |
local eps = 1e-12 |
| 288 |
if (eps < test and test < 1 - eps) then |
| 289 |
return Geometry.hpoint_point_occlusion_sort(self, true_proj) |
| 290 |
end |
| 291 |
end |
| 292 |
return nil |
| 293 |
end |
| 294 |
|
| 295 |
--- Check occlusion sort of a homogeneous point with a homogeneous triangle |
| 296 |
--- @param self Vector |
| 297 |
--- @param T Matrix |
| 298 |
--- @return boolean|nil |
| 299 |
function Geometry.hpoint_triangle_occlusion_sort(self, T) |
| 300 |
local P1 = Vector:_new{self[1], self[2], 0, 1} |
| 301 |
local T1 = Vector:_new{T[1][1], T[1][2], 0, 1} |
| 302 |
local T2 = Vector:_new{T[2][1], T[2][2], 0, 1} |
| 303 |
local T3 = Vector:_new{T[3][1], T[3][2], 0, 1} |
| 304 |
local TP = Matrix:_new{ |
| 305 |
{T1[1], T1[2], 0, 1}, |
| 306 |
{T2[1], T2[2], 0, 1}, |
| 307 |
{T3[1], T3[2], 0, 1} |
| 308 |
} |
| 309 |
local Ta = Vector:_new(T[1]) |
| 310 |
local Tb = Vector:_new(T[2]) |
| 311 |
local Tc = Vector:_new(T[3]) |
| 312 |
|
| 313 |
if Geometry.hpoint_point_intersecting(self, Ta) then return nil end |
| 314 |
if Geometry.hpoint_point_intersecting(self, Tb) then return nil end |
| 315 |
if Geometry.hpoint_point_intersecting(self, Tc) then return nil end |
| 316 |
local eps = 1e-12 |
| 317 |
if Geometry.hpoint_in_triangular_prism(P1, TP) then |
| 318 |
local vu = Tb:hsub(Ta) |
| 319 |
local vv = Tc:hsub(Ta) |
| 320 |
local sol = Matrix:_new{ |
| 321 |
{vu[1], vu[2]}, |
| 322 |
{vv[1], vv[2]}, |
| 323 |
{P1:hsub(Ta)[1], P1:hsub(Ta)[2]} |
| 324 |
}:column_reduction() |
| 325 |
local t, s |
| 326 |
if sol then |
| 327 |
t, s = sol[1], sol[2] |
| 328 |
else return nil end |
| 329 |
if ( |
| 330 |
-eps < t and t < 1 + eps |
| 331 |
and -eps < s and s < 1 + eps |
| 332 |
) then |
| 333 |
local a = Ta:hadd(vu:hscale(t)):hadd(vv:hscale(s)) |
| 334 |
return Geometry.hpoint_point_occlusion_sort(self, a) |
| 335 |
else |
| 336 |
return nil |
| 337 |
end |
| 338 |
end |
| 339 |
return nil |
| 340 |
end |
| 341 |
|
| 342 |
-- ================================================================ |
| 343 |
-- Line-segment level geometry (attached to Matrix via methods below) |
| 344 |
-- ================================================================ |
| 345 |
|
| 346 |
--- Unified line-segment / point intersection test. |
| 347 |
--- @param self Matrix 2-row line segment |
| 348 |
--- @param point Vector The point to test |
| 349 |
--- @param boundary_inclusive boolean If true, endpoints count as intersecting |
| 350 |
--- @return boolean |
| 351 |
function Geometry.hline_segment_point_intersecting(self, point, boundary_inclusive) |
| 352 |
local LA = self:hto_basis() |
| 353 |
local LO = Vector:_new(LA[1]) |
| 354 |
local LU = Vector:_new(LA[2]) |
| 355 |
local LOP = point:hsub(LO) |
| 356 |
local rhs = LOP:hproject_onto(LU) |
| 357 |
local orth = LO:hadd(rhs) |
| 358 |
|
| 359 |
if not Geometry.hpoint_point_intersecting(orth, point) then |
| 360 |
return false |
| 361 |
end |
| 362 |
|
| 363 |
local sol = Matrix:_new{ |
| 364 |
{LU[1], LU[2], LU[3]}, |
| 365 |
{0, 0, 0}, |
| 366 |
{0, 0, 0}, |
| 367 |
{rhs[1], rhs[2], rhs[3]} |
| 368 |
}:column_reduction() |
| 369 |
if sol == nil then return false end |
| 370 |
local t = sol[1] |
| 371 |
|
| 372 |
local eps = 1e-12 |
| 373 |
if boundary_inclusive then |
| 374 |
return -eps < t and t < 1 + eps |
| 375 |
else |
| 376 |
return eps < t and t < 1 - eps |
| 377 |
end |
| 378 |
end |
| 379 |
|
| 380 |
--- Partition a line segment by a point |
| 381 |
--- @param self Matrix |
| 382 |
--- @param point Vector |
| 383 |
--- @return table|nil |
| 384 |
function Geometry.hpartition_line_segment_by_point(self, point) |
| 385 |
if Geometry.hline_segment_point_intersecting(self, point, false) then |
| 386 |
return { |
| 387 |
Matrix:_new{self[1], point:to_table()}, |
| 388 |
Matrix:_new{point:to_table(), self[2]} |
| 389 |
} |
| 390 |
end |
| 391 |
return nil |
| 392 |
end |
| 393 |
|
| 394 |
--- Compute the intersection of two lines (basis form) |
| 395 |
--- @param self Matrix |
| 396 |
--- @param line Matrix |
| 397 |
--- @return Vector|nil |
| 398 |
function Geometry.hline_line_intersection(self, line) |
| 399 |
local O1 = Vector:_new(self[1]) |
| 400 |
local U1 = Vector:_new(self[2]) |
| 401 |
local O2 = Vector:_new(line[1]) |
| 402 |
local U2 = Vector:_new(line[2]) |
| 403 |
local rhs = O2:hsub(O1) |
| 404 |
local sol = Matrix:_new{ |
| 405 |
{U1[1], U1[2], U1[3]}, |
| 406 |
{-U2[1], -U2[2], -U2[3]}, |
| 407 |
{0, 0, 0}, |
| 408 |
{rhs[1], rhs[2], rhs[3]} |
| 409 |
}:column_reduction() |
| 410 |
if sol == nil then return nil end |
| 411 |
return O1:hadd(U1:hscale(sol[1])) |
| 412 |
end |
| 413 |
|
| 414 |
--- Compute the intersection of two line segments |
| 415 |
--- @param self Matrix |
| 416 |
--- @param line Matrix |
| 417 |
--- @return Vector|nil |
| 418 |
function Geometry.hline_segment_line_segment_intersection(self, line) |
| 419 |
local L1A = self:hto_basis() |
| 420 |
local L2A = line:hto_basis() |
| 421 |
local I = Geometry.hline_line_intersection(L1A, L2A) |
| 422 |
if I == nil then return nil end |
| 423 |
if Geometry.hline_segment_point_intersecting(self, I, false) |
| 424 |
and Geometry.hline_segment_point_intersecting(line, I, false) then |
| 425 |
return I |
| 426 |
end |
| 427 |
return nil |
| 428 |
end |
| 429 |
|
| 430 |
--- Compute the intersection of a line and a plane (both in basis form) |
| 431 |
--- @param self Matrix line basis |
| 432 |
--- @param plane Matrix plane basis |
| 433 |
--- @return Vector|nil |
| 434 |
function Geometry.hline_plane_intersection(self, plane) |
| 435 |
local LO = Vector:_new(self[1]) |
| 436 |
local LU = Vector:_new(self[2]) |
| 437 |
local TO = Vector:_new(plane[1]) |
| 438 |
local TU = Vector:_new(plane[2]) |
| 439 |
local TV = Vector:_new(plane[3]) |
| 440 |
local rhs = TO:hsub(LO) |
| 441 |
local sol = Matrix:_new{ |
| 442 |
{LU[1], LU[2], LU[3]}, |
| 443 |
{-TU[1], -TU[2], -TU[3]}, |
| 444 |
{-TV[1], -TV[2], -TV[3]}, |
| 445 |
{rhs[1], rhs[2], rhs[3]} |
| 446 |
}:column_reduction() |
| 447 |
if sol == nil then return nil end |
| 448 |
return LO:hadd(LU:hscale(sol[1])) |
| 449 |
end |
| 450 |
|
| 451 |
--- Compute the intersection of a line segment and a triangle |
| 452 |
--- @param self Matrix line segment (2 rows) |
| 453 |
--- @param tri Matrix triangle (3 rows) |
| 454 |
--- @return Vector|nil |
| 455 |
function Geometry.hline_segment_triangle_intersection(self, tri) |
| 456 |
local coincident_points = 0 |
| 457 |
for _, P1 in ipairs(self) do |
| 458 |
for _, P2 in ipairs(tri) do |
| 459 |
if Geometry.hpoint_point_intersecting(Vector:_new(P1), Vector:_new(P2)) then |
| 460 |
coincident_points = coincident_points + 1 |
| 461 |
end |
| 462 |
end |
| 463 |
end |
| 464 |
if coincident_points > 1 then |
| 465 |
return nil |
| 466 |
end |
| 467 |
local I = Geometry.hline_plane_intersection(self:hto_basis(), tri:hto_basis()) |
| 468 |
if I == nil then return nil end |
| 469 |
if not Geometry.hline_segment_point_intersecting(self, I, true) then |
| 470 |
return nil |
| 471 |
end |
| 472 |
if not Geometry.hpoint_in_triangular_prism(I, tri) then |
| 473 |
return nil |
| 474 |
end |
| 475 |
return I |
| 476 |
end |
| 477 |
|
| 478 |
--- Compute the intersection of two triangles |
| 479 |
--- @param self Matrix |
| 480 |
--- @param tri Matrix |
| 481 |
--- @return Matrix|nil 2-row intersection segment, or nil |
| 482 |
function Geometry.htriangle_triangle_intersections(self, tri) |
| 483 |
local edges1 = { |
| 484 |
Matrix:_new{self[1], self[2]}, |
| 485 |
Matrix:_new{self[2], self[3]}, |
| 486 |
Matrix:_new{self[3], self[1]} |
| 487 |
} |
| 488 |
local edges2 = { |
| 489 |
Matrix:_new{tri[1], tri[2]}, |
| 490 |
Matrix:_new{tri[2], tri[3]}, |
| 491 |
Matrix:_new{tri[3], tri[1]} |
| 492 |
} |
| 493 |
local points = {} |
| 494 |
local merge_eps = 1e-7 |
| 495 |
|
| 496 |
local function same_point(p1, p2) |
| 497 |
return p1:hdistance(p2) < merge_eps |
| 498 |
end |
| 499 |
|
| 500 |
local function add_unique(point) |
| 501 |
for _, p in ipairs(points) do |
| 502 |
if same_point(point, p) then |
| 503 |
return nil |
| 504 |
end |
| 505 |
end |
| 506 |
table.insert(points, point) |
| 507 |
end |
| 508 |
|
| 509 |
local function point_in_triangle_tol(point, triangle) |
| 510 |
local A = Vector:_new(triangle[1]) |
| 511 |
local B = Vector:_new(triangle[2]) |
| 512 |
local C = Vector:_new(triangle[3]) |
| 513 |
|
| 514 |
local v0 = C:hsub(A) |
| 515 |
local v1 = B:hsub(A) |
| 516 |
local v2 = point:hsub(A) |
| 517 |
|
| 518 |
local d00 = v0:hinner(v0) |
| 519 |
local d01 = v0:hinner(v1) |
| 520 |
local d11 = v1:hinner(v1) |
| 521 |
local d20 = v2:hinner(v0) |
| 522 |
local d21 = v2:hinner(v1) |
| 523 |
local denom = d00 * d11 - d01 * d01 |
| 524 |
if math.abs(denom) < merge_eps then |
| 525 |
return false |
| 526 |
end |
| 527 |
|
| 528 |
local v = (d11 * d20 - d01 * d21) / denom |
| 529 |
local w = (d00 * d21 - d01 * d20) / denom |
| 530 |
local u = 1 - v - w |
| 531 |
return u >= -3 * merge_eps and v >= -3 * merge_eps and w >= -3 * merge_eps |
| 532 |
end |
| 533 |
|
| 534 |
local function line_contains_point(edge, point) |
| 535 |
local LA = edge:hto_basis() |
| 536 |
local LO = Vector:_new(LA[1]) |
| 537 |
local LU = Vector:_new(LA[2]) |
| 538 |
local LOP = point:hsub(LO) |
| 539 |
local rhs = LOP:hproject_onto(LU) |
| 540 |
local orth = LO:hadd(rhs) |
| 541 |
|
| 542 |
if not same_point(orth, point) then |
| 543 |
return false |
| 544 |
end |
| 545 |
|
| 546 |
local sol = Matrix:_new{ |
| 547 |
{LU[1], LU[2], LU[3]}, |
| 548 |
{0, 0, 0}, |
| 549 |
{0, 0, 0}, |
| 550 |
{rhs[1], rhs[2], rhs[3]} |
| 551 |
}:column_reduction() |
| 552 |
if sol == nil then return false end |
| 553 |
|
| 554 |
local t = sol[1] |
| 555 |
return -merge_eps < t and t < 1 + merge_eps |
| 556 |
end |
| 557 |
|
| 558 |
local function edge_triangle_point(edge, triangle) |
| 559 |
local I = Geometry.hline_plane_intersection(edge:hto_basis(), triangle:hto_basis()) |
| 560 |
if I == nil then return nil end |
| 561 |
if not line_contains_point(edge, I) then return nil end |
| 562 |
if not point_in_triangle_tol(I, triangle) then return nil end |
| 563 |
return I |
| 564 |
end |
| 565 |
|
| 566 |
for _, edge in ipairs(edges1) do |
| 567 |
local I = edge_triangle_point(edge, tri) |
| 568 |
if I ~= nil then |
| 569 |
add_unique(I) |
| 570 |
end |
| 571 |
end |
| 572 |
for _, edge in ipairs(edges2) do |
| 573 |
local I = edge_triangle_point(edge, self) |
| 574 |
if I ~= nil then |
| 575 |
add_unique(I) |
| 576 |
end |
| 577 |
end |
| 578 |
|
| 579 |
if #points < 2 then |
| 580 |
return nil |
| 581 |
end |
| 582 |
|
| 583 |
if #points > 2 then |
| 584 |
local origin = points[1] |
| 585 |
local direction = nil |
| 586 |
|
| 587 |
for i = 2, #points do |
| 588 |
local diff = points[i]:hsub(origin) |
| 589 |
if diff:hnorm() > merge_eps then |
| 590 |
direction = diff |
| 591 |
break |
| 592 |
end |
| 593 |
end |
| 594 |
|
| 595 |
if direction == nil then |
| 596 |
return nil |
| 597 |
end |
| 598 |
|
| 599 |
local function are_collinear(a, b) |
| 600 |
return a:hhypercross(b):hnorm() < merge_eps |
| 601 |
end |
| 602 |
|
| 603 |
for i = 2, #points do |
| 604 |
local diff = points[i]:hsub(origin) |
| 605 |
if diff:hnorm() > merge_eps and not are_collinear(direction, diff) then |
| 606 |
return nil |
| 607 |
end |
| 608 |
end |
| 609 |
|
| 610 |
local min_point = points[1] |
| 611 |
local max_point = points[1] |
| 612 |
local min_t = 0 |
| 613 |
local max_t = 0 |
| 614 |
for i = 2, #points do |
| 615 |
local diff = points[i]:hsub(origin) |
| 616 |
local t = diff:hnorm() |
| 617 |
if diff:hinner(direction) < 0 then |
| 618 |
t = -t |
| 619 |
end |
| 620 |
if t < min_t then |
| 621 |
min_t = t |
| 622 |
min_point = points[i] |
| 623 |
end |
| 624 |
if t > max_t then |
| 625 |
max_t = t |
| 626 |
max_point = points[i] |
| 627 |
end |
| 628 |
end |
| 629 |
|
| 630 |
if same_point(min_point, max_point) then |
| 631 |
return nil |
| 632 |
end |
| 633 |
points = { min_point, max_point } |
| 634 |
end |
| 635 |
|
| 636 |
if #points ~= 2 then |
| 637 |
return nil |
| 638 |
end |
| 639 |
|
| 640 |
local ans = {} |
| 641 |
for _, p in ipairs(points) do |
| 642 |
table.insert(ans, p:to_table()) |
| 643 |
end |
| 644 |
return Matrix:_new(ans) |
| 645 |
end |
| 646 |
|
| 647 |
--- Partition a line segment by another line segment |
| 648 |
--- @param self Matrix |
| 649 |
--- @param line Matrix |
| 650 |
--- @return table|nil |
| 651 |
function Geometry.hpartition_line_segment_by_line_segment(self, line) |
| 652 |
local I = Geometry.hline_segment_line_segment_intersection(self, line) |
| 653 |
if I ~= nil then |
| 654 |
return Geometry.hpartition_line_segment_by_point(self, I) |
| 655 |
end |
| 656 |
return nil |
| 657 |
end |
| 658 |
|
| 659 |
--- Partition a line segment by a triangle |
| 660 |
--- @param self Matrix |
| 661 |
--- @param tri Matrix |
| 662 |
--- @return table|nil |
| 663 |
function Geometry.hpartition_line_segment_by_triangle(self, tri) |
| 664 |
local I = Geometry.hline_segment_triangle_intersection(self, tri) |
| 665 |
if I ~= nil then |
| 666 |
return Geometry.hpartition_line_segment_by_point(self, I) |
| 667 |
end |
| 668 |
return nil |
| 669 |
end |
| 670 |
|
| 671 |
--- Partition a triangle by another triangle |
| 672 |
--- @param self Matrix |
| 673 |
--- @param tri Matrix |
| 674 |
--- @return table|nil list of sub-triangle Matrices |
| 675 |
function Geometry.hpartition_triangle_by_triangle(self, tri) |
| 676 |
local I = Geometry.htriangle_triangle_intersections(self, tri) |
| 677 |
if I == nil then return nil end |
| 678 |
|
| 679 |
local merge_eps = 1e-7 |
| 680 |
local tri_basis = tri:hto_basis() |
| 681 |
local edges1 = { |
| 682 |
Matrix:_new{self[1], self[2]}, |
| 683 |
Matrix:_new{self[2], self[3]}, |
| 684 |
Matrix:_new{self[3], self[1]} |
| 685 |
} |
| 686 |
|
| 687 |
-- Find where each edge of self crosses the PLANE of tri |
| 688 |
local hits = {} -- each: {point=Vector, edges={[i]=true}} |
| 689 |
for i, edge in ipairs(edges1) do |
| 690 |
local int = Geometry.hline_plane_intersection(edge:hto_basis(), tri_basis) |
| 691 |
if int ~= nil and Geometry.hline_segment_point_intersecting(edges1[i], int, true) then |
| 692 |
local merged = false |
| 693 |
for _, h in ipairs(hits) do |
| 694 |
if h.point:hdistance(int) < merge_eps then |
| 695 |
h.edges[i] = true |
| 696 |
merged = true |
| 697 |
break |
| 698 |
end |
| 699 |
end |
| 700 |
if not merged then |
| 701 |
table.insert(hits, {point = int, edges = {[i] = true}}) |
| 702 |
end |
| 703 |
end |
| 704 |
end |
| 705 |
|
| 706 |
if #hits ~= 2 then |
| 707 |
return nil |
| 708 |
end |
| 709 |
|
| 710 |
local vertices = { |
| 711 |
Vector:_new(self[1]), |
| 712 |
Vector:_new(self[2]), |
| 713 |
Vector:_new(self[3]) |
| 714 |
} |
| 715 |
local A, B = hits[1].point, hits[2].point |
| 716 |
|
| 717 |
-- Vertex detection with tolerant merge |
| 718 |
-- V1 is shared by edges {1,3}, V2 by {1,2}, V3 by {2,3} |
| 719 |
local a_vertex = nil |
| 720 |
local b_vertex = nil |
| 721 |
for i = 1, 3 do |
| 722 |
if A:hdistance(vertices[i]) < merge_eps then a_vertex = i end |
| 723 |
if B:hdistance(vertices[i]) < merge_eps then b_vertex = i end |
| 724 |
end |
| 725 |
|
| 726 |
if a_vertex ~= nil and b_vertex ~= nil then |
| 727 |
return nil |
| 728 |
end |
| 729 |
|
| 730 |
if a_vertex ~= nil or b_vertex ~= nil then |
| 731 |
local vertex_index = a_vertex or b_vertex |
| 732 |
local edge_point = a_vertex and B or A |
| 733 |
local next_index = (vertex_index % 3) + 1 |
| 734 |
local prev_index = ((vertex_index + 1) % 3) + 1 |
| 735 |
return { |
| 736 |
Matrix:_new{ |
| 737 |
vertices[vertex_index]:to_table(), |
| 738 |
vertices[next_index]:to_table(), |
| 739 |
edge_point:to_table() |
| 740 |
}, |
| 741 |
Matrix:_new{ |
| 742 |
vertices[vertex_index]:to_table(), |
| 743 |
edge_point:to_table(), |
| 744 |
vertices[prev_index]:to_table() |
| 745 |
} |
| 746 |
} |
| 747 |
end |
| 748 |
|
| 749 |
-- Neither point is a vertex: find the non-intersecting edge from tracking |
| 750 |
local edge_has_point = {false, false, false} |
| 751 |
for _, h in ipairs(hits) do |
| 752 |
for i = 1, 3 do |
| 753 |
if h.edges[i] then edge_has_point[i] = true end |
| 754 |
end |
| 755 |
end |
| 756 |
|
| 757 |
local non_intersecting = nil |
| 758 |
for i = 1, 3 do |
| 759 |
if not edge_has_point[i] then |
| 760 |
non_intersecting = i |
| 761 |
break |
| 762 |
end |
| 763 |
end |
| 764 |
|
| 765 |
if non_intersecting == nil then |
| 766 |
return nil |
| 767 |
end |
| 768 |
|
| 769 |
local quad = {} |
| 770 |
local tri1 |
| 771 |
table.insert(quad, A) |
| 772 |
table.insert(quad, B) |
| 773 |
if non_intersecting == 1 then |
| 774 |
table.insert(quad, edges1[1][1]) |
| 775 |
table.insert(quad, edges1[1][2]) |
| 776 |
tri1 = Matrix:_new{A:to_table(), B:to_table(), edges1[2][2]} |
| 777 |
elseif non_intersecting == 2 then |
| 778 |
table.insert(quad, edges1[2][1]) |
| 779 |
table.insert(quad, edges1[2][2]) |
| 780 |
tri1 = Matrix:_new{A:to_table(), B:to_table(), edges1[3][2]} |
| 781 |
elseif non_intersecting == 3 then |
| 782 |
table.insert(quad, edges1[3][1]) |
| 783 |
table.insert(quad, edges1[3][2]) |
| 784 |
tri1 = Matrix:_new{A:to_table(), B:to_table(), edges1[1][2]} |
| 785 |
end |
| 786 |
quad = Matrix:_new(quad):hcentroid_sort() |
| 787 |
local Q1 = Vector:_new(quad[1]) |
| 788 |
local Q2 = Vector:_new(quad[2]) |
| 789 |
local Q3 = Vector:_new(quad[3]) |
| 790 |
local Q4 = Vector:_new(quad[4]) |
| 791 |
if Q1:hdistance(Q3) > Q2:hdistance(Q4) then |
| 792 |
return { |
| 793 |
tri1, |
| 794 |
Matrix:_new{Q2:to_table(), Q1:to_table(), Q4:to_table()}, |
| 795 |
Matrix:_new{Q2:to_table(), Q3:to_table(), Q4:to_table()} |
| 796 |
} |
| 797 |
else |
| 798 |
return { |
| 799 |
tri1, |
| 800 |
Matrix:_new{Q1:to_table(), Q2:to_table(), Q3:to_table()}, |
| 801 |
Matrix:_new{Q3:to_table(), Q4:to_table(), Q1:to_table()} |
| 802 |
} |
| 803 |
end |
| 804 |
end |
| 805 |
|
| 806 |
--- Partition a triangle by vertical edge-planes of another triangle. |
| 807 |
--- @param self Matrix |
| 808 |
--- @param tri Matrix |
| 809 |
--- @return table|nil |
| 810 |
function Geometry.hpartition_triangle_by_edge_planes(self, tri) |
| 811 |
local z_min = math.min(self[1][3], self[2][3], self[3][3]) |
| 812 |
local z_max = math.max(self[1][3], self[2][3], self[3][3]) |
| 813 |
local z_margin = math.max((z_max - z_min) * 0.1, 1e-6) |
| 814 |
z_min = z_min - z_margin |
| 815 |
z_max = z_max + z_margin |
| 816 |
|
| 817 |
local cutting_tris = {} |
| 818 |
local edges = { |
| 819 |
{tri[1], tri[2]}, |
| 820 |
{tri[2], tri[3]}, |
| 821 |
{tri[3], tri[1]} |
| 822 |
} |
| 823 |
|
| 824 |
for _, edge in ipairs(edges) do |
| 825 |
local A = edge[1] |
| 826 |
local B = edge[2] |
| 827 |
if math.abs(A[1] - B[1]) < 1e-12 and math.abs(A[2] - B[2]) < 1e-12 then |
| 828 |
goto continue_edge |
| 829 |
end |
| 830 |
local BL = {A[1], A[2], z_min, 1} |
| 831 |
local BR = {B[1], B[2], z_min, 1} |
| 832 |
local TR = {B[1], B[2], z_max, 1} |
| 833 |
local TL = {A[1], A[2], z_max, 1} |
| 834 |
table.insert(cutting_tris, Matrix:_new{BL, BR, TR}) |
| 835 |
table.insert(cutting_tris, Matrix:_new{BL, TR, TL}) |
| 836 |
::continue_edge:: |
| 837 |
end |
| 838 |
|
| 839 |
local pieces = {self:deep_copy()} |
| 840 |
for _, cutter in ipairs(cutting_tris) do |
| 841 |
local new_pieces = {} |
| 842 |
for _, piece in ipairs(pieces) do |
| 843 |
local parts = Geometry.hpartition_triangle_by_triangle(piece, cutter) |
| 844 |
if parts ~= nil then |
| 845 |
for _, part in ipairs(parts) do |
| 846 |
table.insert(new_pieces, part) |
| 847 |
end |
| 848 |
else |
| 849 |
table.insert(new_pieces, piece) |
| 850 |
end |
| 851 |
end |
| 852 |
pieces = new_pieces |
| 853 |
end |
| 854 |
|
| 855 |
if #pieces > 1 then |
| 856 |
return pieces |
| 857 |
end |
| 858 |
return nil |
| 859 |
end |
| 860 |
|
| 861 |
-- ================================================================ |
| 862 |
-- Occlusion sorting between simplices |
| 863 |
-- ================================================================ |
| 864 |
|
| 865 |
--- line-segment vs line-segment occlusion |
| 866 |
function Geometry.hline_segment_line_segment_occlusion_sort(self, L) |
| 867 |
local L1A = Vector:_new{self[1][1], self[1][2], 0, 1} |
| 868 |
local L1B = Vector:_new{self[2][1], self[2][2], 0, 1} |
| 869 |
local L2A = Vector:_new{L[1][1], L[1][2], 0, 1} |
| 870 |
local L2B = Vector:_new{L[2][1], L[2][2], 0, 1} |
| 871 |
local L1_dir = L1B:hsub(L1A) |
| 872 |
local L2_dir = L2B:hsub(L2A) |
| 873 |
|
| 874 |
local RL1A = Vector:_new(self[1]) |
| 875 |
local RL1B = Vector:_new(self[2]) |
| 876 |
local RL2A = Vector:_new(L[1]) |
| 877 |
local RL2B = Vector:_new(L[2]) |
| 878 |
|
| 879 |
local eps = 1e-12 |
| 880 |
if not Geometry.hcollinear(L1_dir, L2_dir) then |
| 881 |
local sol = Matrix:_new{ |
| 882 |
{L1_dir[1], L1_dir[2]}, |
| 883 |
{-L2_dir[1], -L2_dir[2]}, |
| 884 |
{L2A[1] - L1A[1], L2A[2] - L1A[2]} |
| 885 |
}:column_reduction() |
| 886 |
local t, s |
| 887 |
if sol ~= nil then |
| 888 |
t, s = sol[1], sol[2] |
| 889 |
else return nil end |
| 890 |
if (eps < t and t < 1 - eps and eps < s and s < 1 - eps) then |
| 891 |
local RL1I = RL1A:hadd((RL1B:hsub(RL1A)):hscale(t)) |
| 892 |
local RL2I = RL2A:hadd((RL2B:hsub(RL2A)):hscale(s)) |
| 893 |
return Geometry.hpoint_point_occlusion_sort(RL1I, RL2I) |
| 894 |
end |
| 895 |
else |
| 896 |
local M1 = Geometry.hpoint_line_segment_occlusion_sort(RL1A, L) |
| 897 |
if M1 ~= nil then return M1 end |
| 898 |
local M2 = Geometry.hpoint_line_segment_occlusion_sort(RL1B, L) |
| 899 |
if M2 ~= nil then return M2 end |
| 900 |
local M3 = Geometry.hpoint_line_segment_occlusion_sort(RL2A, self) |
| 901 |
if M3 ~= nil then return not M3 end |
| 902 |
local M4 = Geometry.hpoint_line_segment_occlusion_sort(RL2B, self) |
| 903 |
if M4 ~= nil then return not M4 end |
| 904 |
end |
| 905 |
return nil |
| 906 |
end |
| 907 |
|
| 908 |
--- line-segment vs triangle occlusion |
| 909 |
function Geometry.hline_segment_triangle_occlusion_sort(self, T) |
| 910 |
local points1 = { Vector:_new(self[1]), Vector:_new(self[2]) } |
| 911 |
for _, p1 in ipairs(points1) do |
| 912 |
local A = Geometry.hpoint_triangle_occlusion_sort(p1, T) |
| 913 |
if A ~= nil then return A end |
| 914 |
end |
| 915 |
local edges2 = { |
| 916 |
Matrix:_new{T[1], T[2]}, |
| 917 |
Matrix:_new{T[2], T[3]}, |
| 918 |
Matrix:_new{T[3], T[1]} |
| 919 |
} |
| 920 |
for _, e2 in ipairs(edges2) do |
| 921 |
local A = Geometry.hline_segment_line_segment_occlusion_sort(self, e2) |
| 922 |
if A ~= nil then return A end |
| 923 |
end |
| 924 |
return nil |
| 925 |
end |
| 926 |
|
| 927 |
--- triangle vs triangle occlusion |
| 928 |
function Geometry.htriangle_triangle_occlusion_sort(self, T) |
| 929 |
local edges1 = { |
| 930 |
Matrix:_new{self[1], self[2]}, |
| 931 |
Matrix:_new{self[2], self[3]}, |
| 932 |
Matrix:_new{self[3], self[1]} |
| 933 |
} |
| 934 |
for _, e1 in ipairs(edges1) do |
| 935 |
local A = Geometry.hline_segment_triangle_occlusion_sort(e1, T) |
| 936 |
if A ~= nil then return A end |
| 937 |
end |
| 938 |
local points1 = { Vector:_new(self[1]), Vector:_new(self[2]), Vector:_new(self[3]) } |
| 939 |
for _, p1 in ipairs(points1) do |
| 940 |
local A = Geometry.hpoint_triangle_occlusion_sort(p1, T) |
| 941 |
if A ~= nil then return A end |
| 942 |
end |
| 943 |
local points2 = { Vector:_new(T[1]), Vector:_new(T[2]), Vector:_new(T[3]) } |
| 944 |
for _, p2 in ipairs(points2) do |
| 945 |
local A = Geometry.hpoint_triangle_occlusion_sort(p2, self) |
| 946 |
if A ~= nil then return not A end |
| 947 |
end |
| 948 |
return nil |
| 949 |
end |
| 950 |
|
| 951 |
--- Top-level occlusion sort dispatcher |
| 952 |
--- @param S1 table simplex record |
| 953 |
--- @param S2 table simplex record |
| 954 |
--- @return boolean|nil |
| 955 |
function Geometry.occlusion_sort_simplices(S1, S2) |
| 956 |
if S1.type == "point" and S2.type == "point" then |
| 957 |
return Geometry.hpoint_point_occlusion_sort(S1.simplex, S2.simplex) |
| 958 |
elseif S1.type == "point" and S2.type == "line segment" then |
| 959 |
return Geometry.hpoint_line_segment_occlusion_sort(S1.simplex, S2.simplex) |
| 960 |
elseif S1.type == "line segment" and S2.type == "point" then |
| 961 |
local A = Geometry.hpoint_line_segment_occlusion_sort(S2.simplex, S1.simplex) |
| 962 |
if A ~= nil then return not A end |
| 963 |
elseif S1.type == "point" and S2.type == "triangle" then |
| 964 |
return Geometry.hpoint_triangle_occlusion_sort(S1.simplex, S2.simplex) |
| 965 |
elseif S1.type == "triangle" and S2.type == "point" then |
| 966 |
local A = Geometry.hpoint_triangle_occlusion_sort(S2.simplex, S1.simplex) |
| 967 |
if A ~= nil then return not A end |
| 968 |
elseif S1.type == "line segment" and S2.type == "line segment" then |
| 969 |
return Geometry.hline_segment_line_segment_occlusion_sort(S1.simplex, S2.simplex) |
| 970 |
elseif S1.type == "line segment" and S2.type == "triangle" then |
| 971 |
local A = Geometry.hline_segment_triangle_occlusion_sort(S1.simplex, S2.simplex) |
| 972 |
if A == nil then return nil else return A end |
| 973 |
elseif S1.type == "triangle" and S2.type == "line segment" then |
| 974 |
local A = Geometry.hline_segment_triangle_occlusion_sort(S2.simplex, S1.simplex) |
| 975 |
if A == nil then return nil else return not A end |
| 976 |
elseif S1.type == "triangle" and S2.type == "triangle" then |
| 977 |
return Geometry.htriangle_triangle_occlusion_sort(S1.simplex, S2.simplex) |
| 978 |
end |
| 979 |
end |
| 980 |
|
| 981 |
-- ================================================================ |
| 982 |
-- 2D grid spatial index for bbox overlap queries |
| 983 |
-- ================================================================ |
| 984 |
|
| 985 |
--- Build a uniform 2D grid index from a list of simplex records. |
| 986 |
--- Each record must have a .bbox2 field (pre-computed). |
| 987 |
--- @param simplices table list of simplex records |
| 988 |
--- @param cell_size number grid cell width/height |
| 989 |
--- @return table grid { cells = {}, cell_size = N, ... } |
| 990 |
function Geometry.build_grid(simplices, cell_size) |
| 991 |
local cells = {} |
| 992 |
local function key(cx, cy) |
| 993 |
return cx .. "," .. cy |
| 994 |
end |
| 995 |
for i, s in ipairs(simplices) do |
| 996 |
local bb = s.bbox2 |
| 997 |
if bb then |
| 998 |
local cx0 = math.floor(bb.min[1] / cell_size) |
| 999 |
local cy0 = math.floor(bb.min[2] / cell_size) |
| 1000 |
local cx1 = math.floor(bb.max[1] / cell_size) |
| 1001 |
local cy1 = math.floor(bb.max[2] / cell_size) |
| 1002 |
for cx = cx0, cx1 do |
| 1003 |
for cy = cy0, cy1 do |
| 1004 |
local k = key(cx, cy) |
| 1005 |
if not cells[k] then cells[k] = {} end |
| 1006 |
table.insert(cells[k], i) |
| 1007 |
end |
| 1008 |
end |
| 1009 |
end |
| 1010 |
end |
| 1011 |
return { cells = cells, cell_size = cell_size, key = key } |
| 1012 |
end |
| 1013 |
|
| 1014 |
--- Iterate over candidate pairs that share at least one grid cell. |
| 1015 |
--- Returns a set of {i,j} pairs (i < j) as keys in a table. |
| 1016 |
--- @param grid table |
| 1017 |
--- @return table set of "i,j" keys |
| 1018 |
function Geometry.grid_candidate_pairs(grid) |
| 1019 |
local pairs_seen = {} |
| 1020 |
for _, bucket in pairs(grid.cells) do |
| 1021 |
for a = 1, #bucket do |
| 1022 |
for b = a + 1, #bucket do |
| 1023 |
local i, j = bucket[a], bucket[b] |
| 1024 |
if i > j then i, j = j, i end |
| 1025 |
local k = i .. "," .. j |
| 1026 |
pairs_seen[k] = { i, j } |
| 1027 |
end |
| 1028 |
end |
| 1029 |
end |
| 1030 |
return pairs_seen |
| 1031 |
end |
| 1032 |
|
| 1033 |
--- Return candidate indices whose grid cells overlap a 2D bbox. |
| 1034 |
--- @param grid table |
| 1035 |
--- @param bbox table |
| 1036 |
--- @return table |
| 1037 |
function Geometry.grid_candidate_indices(grid, bbox) |
| 1038 |
local seen = {} |
| 1039 |
local indices = {} |
| 1040 |
local cx0 = math.floor(bbox.min[1] / grid.cell_size) |
| 1041 |
local cy0 = math.floor(bbox.min[2] / grid.cell_size) |
| 1042 |
local cx1 = math.floor(bbox.max[1] / grid.cell_size) |
| 1043 |
local cy1 = math.floor(bbox.max[2] / grid.cell_size) |
| 1044 |
|
| 1045 |
for cx = cx0, cx1 do |
| 1046 |
for cy = cy0, cy1 do |
| 1047 |
local bucket = grid.cells[grid.key(cx, cy)] |
| 1048 |
if bucket then |
| 1049 |
for _, idx in ipairs(bucket) do |
| 1050 |
if not seen[idx] then |
| 1051 |
seen[idx] = true |
| 1052 |
indices[#indices + 1] = idx |
| 1053 |
end |
| 1054 |
end |
| 1055 |
end |
| 1056 |
end |
| 1057 |
end |
| 1058 |
|
| 1059 |
table.sort(indices) |
| 1060 |
return indices |
| 1061 |
end |
| 1062 |
|
| 1063 |
-- ================================================================ |
| 1064 |
-- SCC-based occlusion sort with spatial indexing |
| 1065 |
-- ================================================================ |
| 1066 |
|
| 1067 |
--- Strongly connected components sort for simplices based on occlusion. |
| 1068 |
--- Uses a 2D grid to reduce pair comparisons, Tarjan's SCC to detect cycles, |
| 1069 |
--- and edge-plane partitioning to break them. |
| 1070 |
--- @param simplices table list of simplex records (each must have .bbox2) |
| 1071 |
--- @param depth number|nil |
| 1072 |
--- @return table sorted list |
| 1073 |
--- @return table diagnostics |
| 1074 |
function Geometry.scc(simplices, depth) |
| 1075 |
depth = depth or 0 |
| 1076 |
local max_depth = 16 |
| 1077 |
local n = #simplices |
| 1078 |
|
| 1079 |
local diagnostics = { |
| 1080 |
depth = depth, |
| 1081 |
had_cycle = false, |
| 1082 |
unresolved_cycles = false, |
| 1083 |
unresolved_component_count = 0, |
| 1084 |
max_depth_reached = false, |
| 1085 |
} |
| 1086 |
|
| 1087 |
-- Estimate cell size from average bbox diagonal |
| 1088 |
local total_diag = 0 |
| 1089 |
local diag_count = 0 |
| 1090 |
for _, s in ipairs(simplices) do |
| 1091 |
if s.bbox2 then |
| 1092 |
local dx = s.bbox2.max[1] - s.bbox2.min[1] |
| 1093 |
local dy = s.bbox2.max[2] - s.bbox2.min[2] |
| 1094 |
total_diag = total_diag + math.sqrt(dx*dx + dy*dy) |
| 1095 |
diag_count = diag_count + 1 |
| 1096 |
end |
| 1097 |
end |
| 1098 |
local cell_size = (diag_count > 0) and (total_diag / diag_count * 2) or 1.0 |
| 1099 |
if cell_size < 1e-6 then cell_size = 1.0 end |
| 1100 |
|
| 1101 |
local grid = Geometry.build_grid(simplices, cell_size) |
| 1102 |
local candidate_pairs = Geometry.grid_candidate_pairs(grid) |
| 1103 |
|
| 1104 |
local adj = {} |
| 1105 |
for i = 1, n do adj[i] = {} end |
| 1106 |
|
| 1107 |
for _, pair in pairs(candidate_pairs) do |
| 1108 |
local i, j = pair[1], pair[2] |
| 1109 |
local si, sj = simplices[i], simplices[j] |
| 1110 |
if si.type ~= "label" and sj.type ~= "label" |
| 1111 |
and si.type ~= "point" and sj.type ~= "point" |
| 1112 |
then |
| 1113 |
-- Use cached bboxes for the overlap check |
| 1114 |
if si.simplex:bboxes_overlap2(sj.simplex, si.bbox2, sj.bbox2) then |
| 1115 |
local cmp = Geometry.occlusion_sort_simplices(si, sj) |
| 1116 |
if cmp == true then |
| 1117 |
table.insert(adj[i], j) |
| 1118 |
elseif cmp == false then |
| 1119 |
table.insert(adj[j], i) |
| 1120 |
end |
| 1121 |
end |
| 1122 |
end |
| 1123 |
end |
| 1124 |
|
| 1125 |
-- Tarjan's SCC |
| 1126 |
local index_counter = 0 |
| 1127 |
local stack = {} |
| 1128 |
local on_stack = {} |
| 1129 |
local indices = {} |
| 1130 |
local lowlinks = {} |
| 1131 |
local components = {} |
| 1132 |
|
| 1133 |
local function strongconnect(v) |
| 1134 |
indices[v] = index_counter |
| 1135 |
lowlinks[v] = index_counter |
| 1136 |
index_counter = index_counter + 1 |
| 1137 |
table.insert(stack, v) |
| 1138 |
on_stack[v] = true |
| 1139 |
|
| 1140 |
for _, w in ipairs(adj[v]) do |
| 1141 |
if indices[w] == nil then |
| 1142 |
strongconnect(w) |
| 1143 |
lowlinks[v] = math.min(lowlinks[v], lowlinks[w]) |
| 1144 |
elseif on_stack[w] then |
| 1145 |
lowlinks[v] = math.min(lowlinks[v], indices[w]) |
| 1146 |
end |
| 1147 |
end |
| 1148 |
|
| 1149 |
if lowlinks[v] == indices[v] then |
| 1150 |
local component = {} |
| 1151 |
while true do |
| 1152 |
local w = table.remove(stack) |
| 1153 |
on_stack[w] = false |
| 1154 |
table.insert(component, w) |
| 1155 |
if w == v then break end |
| 1156 |
end |
| 1157 |
table.insert(components, component) |
| 1158 |
end |
| 1159 |
end |
| 1160 |
|
| 1161 |
for i = 1, n do |
| 1162 |
if indices[i] == nil then strongconnect(i) end |
| 1163 |
end |
| 1164 |
|
| 1165 |
-- Cycle resolution |
| 1166 |
local has_cycle = false |
| 1167 |
local cycle_component_count = 0 |
| 1168 |
for _, comp in ipairs(components) do |
| 1169 |
if #comp > 1 then |
| 1170 |
has_cycle = true |
| 1171 |
cycle_component_count = cycle_component_count + 1 |
| 1172 |
end |
| 1173 |
end |
| 1174 |
|
| 1175 |
diagnostics.had_cycle = has_cycle |
| 1176 |
|
| 1177 |
if has_cycle and depth < max_depth then |
| 1178 |
local to_remove = {} |
| 1179 |
local to_add = {} |
| 1180 |
|
| 1181 |
for _, comp in ipairs(components) do |
| 1182 |
if #comp > 1 then |
| 1183 |
local split_done = false |
| 1184 |
for ci = 1, #comp do |
| 1185 |
if split_done then break end |
| 1186 |
local idx = comp[ci] |
| 1187 |
if simplices[idx].type == "triangle" and not to_remove[idx] then |
| 1188 |
for cj = 1, #comp do |
| 1189 |
if cj ~= ci then |
| 1190 |
local other_idx = comp[cj] |
| 1191 |
if simplices[other_idx].type == "triangle" then |
| 1192 |
local parts = Geometry.hpartition_triangle_by_edge_planes( |
| 1193 |
simplices[idx].simplex, |
| 1194 |
simplices[other_idx].simplex) |
| 1195 |
if parts ~= nil then |
| 1196 |
to_remove[idx] = true |
| 1197 |
for _, part in ipairs(parts) do |
| 1198 |
local ns = { |
| 1199 |
simplex = part, |
| 1200 |
type = "triangle", |
| 1201 |
bbox2 = part:get_bbox2() |
| 1202 |
} |
| 1203 |
local meta = copy_simplex_metadata_for_part(simplices[idx], part) |
| 1204 |
for k, v in pairs(meta) do ns[k] = v end |
| 1205 |
table.insert(to_add, ns) |
| 1206 |
end |
| 1207 |
split_done = true |
| 1208 |
break |
| 1209 |
end |
| 1210 |
end |
| 1211 |
end |
| 1212 |
end |
| 1213 |
end |
| 1214 |
end |
| 1215 |
end |
| 1216 |
end |
| 1217 |
|
| 1218 |
if next(to_remove) then |
| 1219 |
local new_simplices = {} |
| 1220 |
for i, s in ipairs(simplices) do |
| 1221 |
if not to_remove[i] then |
| 1222 |
table.insert(new_simplices, s) |
| 1223 |
end |
| 1224 |
end |
| 1225 |
for _, s in ipairs(to_add) do |
| 1226 |
table.insert(new_simplices, s) |
| 1227 |
end |
| 1228 |
local sorted, recursive_diagnostics = Geometry.scc(new_simplices, depth + 1) |
| 1229 |
recursive_diagnostics = recursive_diagnostics or diagnostics |
| 1230 |
recursive_diagnostics.had_cycle = recursive_diagnostics.had_cycle or has_cycle |
| 1231 |
return sorted, recursive_diagnostics |
| 1232 |
end |
| 1233 |
end |
| 1234 |
|
| 1235 |
if has_cycle then |
| 1236 |
diagnostics.unresolved_cycles = true |
| 1237 |
diagnostics.unresolved_component_count = cycle_component_count |
| 1238 |
diagnostics.max_depth_reached = depth >= max_depth |
| 1239 |
|
| 1240 |
local warning = ( |
| 1241 |
"Unresolved occlusion cycle(s) remain after SCC sorting (%d component(s) at depth %d); rendering order may be unstable." |
| 1242 |
):format(cycle_component_count, depth) |
| 1243 |
|
| 1244 |
emit_warning(warning) |
| 1245 |
end |
| 1246 |
|
| 1247 |
-- Topological sort (DFS) |
| 1248 |
local visited = {} |
| 1249 |
local sorted = {} |
| 1250 |
|
| 1251 |
local function visit(u) |
| 1252 |
if visited[u] then return end |
| 1253 |
visited[u] = true |
| 1254 |
for _, v in ipairs(adj[u]) do |
| 1255 |
visit(v) |
| 1256 |
end |
| 1257 |
table.insert(sorted, 1, simplices[u]) |
| 1258 |
end |
| 1259 |
|
| 1260 |
for i = 1, n do visit(i) end |
| 1261 |
|
| 1262 |
return sorted, diagnostics |
| 1263 |
end |
| 1264 |
|
| 1265 |
-- ================================================================ |
| 1266 |
-- Partition simplices by parents (with screen-space overlap check) |
| 1267 |
-- ================================================================ |
| 1268 |
|
| 1269 |
--- Partition simplices by their parents recursively, retaining all terminal pieces. |
| 1270 |
--- Only tests pairs that overlap in screen-space (bbox2). |
| 1271 |
--- Skips self-partitioning. |
| 1272 |
--- @param simplices table |
| 1273 |
--- @param parents table |
| 1274 |
--- @return table |
| 1275 |
function Geometry.partition_simplices_by_parents(simplices, parents) |
| 1276 |
local result = {} |
| 1277 |
|
| 1278 |
-- Pre-compute parent bbox caches and build a 2D grid index so each piece |
| 1279 |
-- only checks nearby parents before the more expensive 3D overlap test. |
| 1280 |
local parent_bbox3 = {} |
| 1281 |
local parent_indices = {} |
| 1282 |
local total_diag = 0 |
| 1283 |
local diag_count = 0 |
| 1284 |
for pi, parent in ipairs(parents) do |
| 1285 |
if parent.type ~= "point" and parent.type ~= "label" then |
| 1286 |
parent_bbox3[pi] = parent.simplex:get_bbox3() |
| 1287 |
parent.bbox2 = parent.bbox2 or parent.simplex:get_bbox2() |
| 1288 |
parent_indices[#parent_indices + 1] = pi |
| 1289 |
|
| 1290 |
local dx = parent.bbox2.max[1] - parent.bbox2.min[1] |
| 1291 |
local dy = parent.bbox2.max[2] - parent.bbox2.min[2] |
| 1292 |
total_diag = total_diag + math.sqrt(dx * dx + dy * dy) |
| 1293 |
diag_count = diag_count + 1 |
| 1294 |
end |
| 1295 |
end |
| 1296 |
|
| 1297 |
local cell_size = (diag_count > 0) and (total_diag / diag_count * 2) or 1.0 |
| 1298 |
if cell_size < 1e-6 then cell_size = 1.0 end |
| 1299 |
local parent_grid = Geometry.build_grid(parents, cell_size) |
| 1300 |
|
| 1301 |
local max_depth = 50 |
| 1302 |
|
| 1303 |
local function partition_recursive(piece, depth, skip_parent) |
| 1304 |
depth = depth or 0 |
| 1305 |
if depth >= max_depth |
| 1306 |
or piece.type == "point" |
| 1307 |
or piece.type == "label" then |
| 1308 |
table.insert(result, piece) |
| 1309 |
return |
| 1310 |
end |
| 1311 |
|
| 1312 |
local meta = {} |
| 1313 |
for k, v in pairs(piece) do |
| 1314 |
if k ~= "simplex" and k ~= "type" and k ~= "bbox2" then |
| 1315 |
meta[k] = v |
| 1316 |
end |
| 1317 |
end |
| 1318 |
|
| 1319 |
local split_occurred = false |
| 1320 |
piece.bbox2 = piece.bbox2 or piece.simplex:get_bbox2() |
| 1321 |
local piece_bbox2 = piece.bbox2 |
| 1322 |
local piece_bbox3 = piece.simplex:get_bbox3() |
| 1323 |
local candidate_indices = parent_indices |
| 1324 |
|
| 1325 |
if piece_bbox2 then |
| 1326 |
candidate_indices = Geometry.grid_candidate_indices(parent_grid, piece_bbox2) |
| 1327 |
end |
| 1328 |
|
| 1329 |
for _, pi in ipairs(candidate_indices) do |
| 1330 |
local parent = parents[pi] |
| 1331 |
if pi ~= skip_parent and piece ~= parent |
| 1332 |
and parent.type ~= "point" and parent.type ~= "label" |
| 1333 |
and piece.type ~= "point" and piece.type ~= "label" |
| 1334 |
then |
| 1335 |
local pb = parent_bbox3[pi] |
| 1336 |
local parent_bbox2 = parent.bbox2 |
| 1337 |
if parent_bbox2 |
| 1338 |
and piece.simplex:bboxes_overlap2(parent.simplex, piece_bbox2, parent_bbox2) |
| 1339 |
and pb |
| 1340 |
and piece.simplex:bboxes_overlap3(parent.simplex, piece_bbox3, pb) |
| 1341 |
then |
| 1342 |
local parts = nil |
| 1343 |
local piece_type = piece.type |
| 1344 |
local parent_type = parent.type |
| 1345 |
|
| 1346 |
if parent_type == "point" and piece_type == "line segment" then |
| 1347 |
parts = Geometry.hpartition_line_segment_by_point(piece.simplex, parent.simplex) |
| 1348 |
elseif parent_type == "line segment" and piece_type == "line segment" then |
| 1349 |
parts = Geometry.hpartition_line_segment_by_line_segment(piece.simplex, parent.simplex) |
| 1350 |
elseif parent_type == "triangle" and piece_type == "line segment" then |
| 1351 |
parts = Geometry.hpartition_line_segment_by_triangle(piece.simplex, parent.simplex) |
| 1352 |
elseif parent_type == "triangle" and piece_type == "triangle" then |
| 1353 |
parts = Geometry.hpartition_triangle_by_triangle(piece.simplex, parent.simplex) |
| 1354 |
end |
| 1355 |
|
| 1356 |
if parts ~= nil then |
| 1357 |
for _, part in ipairs(parts) do |
| 1358 |
local new_piece = { |
| 1359 |
simplex = part, |
| 1360 |
type = piece_type, |
| 1361 |
bbox2 = part:get_bbox2(), |
| 1362 |
} |
| 1363 |
local part_meta = meta |
| 1364 |
if piece_type == "triangle" then |
| 1365 |
part_meta = copy_simplex_metadata_for_part(piece, part) |
| 1366 |
end |
| 1367 |
for k, v in pairs(part_meta) do new_piece[k] = v end |
| 1368 |
partition_recursive(new_piece, depth + 1, pi) |
| 1369 |
end |
| 1370 |
split_occurred = true |
| 1371 |
break |
| 1372 |
end |
| 1373 |
end |
| 1374 |
end |
| 1375 |
end |
| 1376 |
|
| 1377 |
if not split_occurred then |
| 1378 |
table.insert(result, piece) |
| 1379 |
end |
| 1380 |
end |
| 1381 |
|
| 1382 |
for _, simplex in ipairs(simplices) do |
| 1383 |
partition_recursive(simplex) |
| 1384 |
end |
| 1385 |
|
| 1386 |
return result |
| 1387 |
end |
| 1388 |
|
| 1389 |
return Geometry |