| 1 |
--- lua-tikz3dtools-matrix.lua |
| 2 |
--- Matrix class for the lua-tikz3dtools package. |
| 3 |
|
| 4 |
local Vector -- set via Matrix._set_Vector() |
| 5 |
|
| 6 |
local Matrix = {} |
| 7 |
Matrix.__index = Matrix |
| 8 |
|
| 9 |
--- Create a new Matrix object (validated). |
| 10 |
--- @param matrix table The matrix data (row-major) |
| 11 |
--- @return Matrix |
| 12 |
function Matrix:new(matrix) |
| 13 |
assert(type(matrix) == "table", "A Matrix object must be a table of rows.") |
| 14 |
local l = #matrix |
| 15 |
local p = #matrix[1] |
| 16 |
for i = 1, l do |
| 17 |
assert(type(matrix[i]) == "table", "A Matrix row must be a table.") |
| 18 |
local m = #matrix[i] |
| 19 |
assert(m == p, "All Matrix rows must have the same number of columns.") |
| 20 |
for j = 1, m do |
| 21 |
local g = getmetatable(matrix[i][j]) == Vector |
| 22 |
assert( |
| 23 |
type(matrix[i][j]) == "number" or g, |
| 24 |
"A Matrix can only contain numbers or Vectors" |
| 25 |
) |
| 26 |
if g and i == 1 then |
| 27 |
for k = 2, l do |
| 28 |
assert(#matrix[k][j] == #matrix[1][j], "All Vectors in a Matrix column must have the same size.") |
| 29 |
end |
| 30 |
end |
| 31 |
end |
| 32 |
end |
| 33 |
return setmetatable(matrix, Matrix) |
| 34 |
end |
| 35 |
|
| 36 |
--- Unchecked constructor — skips validation. Use for internal calls only. |
| 37 |
--- @param matrix table |
| 38 |
--- @return Matrix |
| 39 |
function Matrix:_new(matrix) |
| 40 |
return setmetatable(matrix, Matrix) |
| 41 |
end |
| 42 |
|
| 43 |
--- Print a Matrix object |
| 44 |
--- @return string |
| 45 |
function Matrix:__tostring() |
| 46 |
local s = "Matrix{\n" |
| 47 |
local l = #self |
| 48 |
for i = 1, l do |
| 49 |
s = s .. "\t{" |
| 50 |
local m = #self[i] |
| 51 |
for j = 1, m do |
| 52 |
if type(self[i][j]) == "number" then |
| 53 |
s = s .. string.format("%.12f", self[i][j]) |
| 54 |
else |
| 55 |
s = s .. tostring(self[i][j]) |
| 56 |
end |
| 57 |
if j < m then s = s .. "," end |
| 58 |
end |
| 59 |
s = s .. "}" |
| 60 |
if i < l then s = s .. ",\n" end |
| 61 |
end |
| 62 |
s = s .. "\n}" |
| 63 |
return s |
| 64 |
end |
| 65 |
|
| 66 |
--- swap two columns of the Matrix |
| 67 |
--- @param c1 number The first column |
| 68 |
--- @param c2 number The second column |
| 69 |
function Matrix:swap_columns(c1, c2) |
| 70 |
local l = #self |
| 71 |
for i = 1, l do |
| 72 |
local t = self[i][c1] |
| 73 |
self[i][c1] = self[i][c2] |
| 74 |
self[i][c2] = t |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
--- add a scalar multiple of one column to another column |
| 79 |
--- @param src_col number The source column |
| 80 |
--- @param scalar number The scalar multiple |
| 81 |
--- @param dest_col number The destination column |
| 82 |
function Matrix:add_scalar_multiple_of_column(src_col, scalar, dest_col) |
| 83 |
local l = #self |
| 84 |
for i = 1, l do |
| 85 |
self[i][dest_col] = self[i][dest_col] + scalar * self[i][src_col] |
| 86 |
end |
| 87 |
end |
| 88 |
|
| 89 |
--- scale a column by a scalar |
| 90 |
--- @param col number The column |
| 91 |
--- @param scalar number The scalar |
| 92 |
function Matrix:scale_column(col, scalar) |
| 93 |
local l = #self |
| 94 |
for i = 1, l do |
| 95 |
self[i][col] = self[i][col] * scalar |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
--- Deep copy of Matrix object |
| 100 |
--- @return Matrix A deep copy of self |
| 101 |
function Matrix:deep_copy() |
| 102 |
local numrows = #self |
| 103 |
local numcols = #self[1] |
| 104 |
local deep_copy = {} |
| 105 |
for i = 1, numrows do |
| 106 |
deep_copy[i] = {} |
| 107 |
for j = 1, numcols do |
| 108 |
if getmetatable(self[i][j]) == Vector then |
| 109 |
deep_copy[i][j] = Vector:_new{table.unpack(self[i][j])} |
| 110 |
else |
| 111 |
deep_copy[i][j] = self[i][j] |
| 112 |
end |
| 113 |
end |
| 114 |
end |
| 115 |
return Matrix:_new(deep_copy) |
| 116 |
end |
| 117 |
|
| 118 |
--- Elementwise addition of Matrix objects |
| 119 |
--- @param other Matrix A matrix of the same shape |
| 120 |
--- @return Matrix The elementwise sum of self and other |
| 121 |
function Matrix:add(other) |
| 122 |
local numrows = #self |
| 123 |
local numcols = #self[1] |
| 124 |
|
| 125 |
assert(getmetatable(other) == Matrix, "other must be a Matrix.") |
| 126 |
assert(#other == numrows and #other[1] == numcols, |
| 127 |
"Matrices must have the same dimensions.") |
| 128 |
|
| 129 |
local result = {} |
| 130 |
for i = 1, numrows do |
| 131 |
result[i] = {} |
| 132 |
for j = 1, numcols do |
| 133 |
local lhs = self[i][j] |
| 134 |
local rhs = other[i][j] |
| 135 |
local lhs_mt = getmetatable(lhs) |
| 136 |
local rhs_mt = getmetatable(rhs) |
| 137 |
|
| 138 |
assert(lhs_mt == rhs_mt, |
| 139 |
"Matrix entries must have matching types for addition.") |
| 140 |
|
| 141 |
if lhs_mt == Vector then |
| 142 |
result[i][j] = lhs:add(rhs) |
| 143 |
else |
| 144 |
result[i][j] = lhs + rhs |
| 145 |
end |
| 146 |
end |
| 147 |
end |
| 148 |
|
| 149 |
return Matrix:_new(result) |
| 150 |
end |
| 151 |
|
| 152 |
--- Elementwise subtraction of Matrix objects |
| 153 |
--- @param other Matrix A matrix of the same shape |
| 154 |
--- @return Matrix The elementwise difference of self and other |
| 155 |
function Matrix:sub(other) |
| 156 |
local numrows = #self |
| 157 |
local numcols = #self[1] |
| 158 |
|
| 159 |
assert(getmetatable(other) == Matrix, "other must be a Matrix.") |
| 160 |
assert(#other == numrows and #other[1] == numcols, |
| 161 |
"Matrices must have the same dimensions.") |
| 162 |
|
| 163 |
local result = {} |
| 164 |
for i = 1, numrows do |
| 165 |
result[i] = {} |
| 166 |
for j = 1, numcols do |
| 167 |
local lhs = self[i][j] |
| 168 |
local rhs = other[i][j] |
| 169 |
local lhs_mt = getmetatable(lhs) |
| 170 |
local rhs_mt = getmetatable(rhs) |
| 171 |
|
| 172 |
assert(lhs_mt == rhs_mt, |
| 173 |
"Matrix entries must have matching types for subtraction.") |
| 174 |
|
| 175 |
if lhs_mt == Vector then |
| 176 |
result[i][j] = lhs:sub(rhs) |
| 177 |
else |
| 178 |
result[i][j] = lhs - rhs |
| 179 |
end |
| 180 |
end |
| 181 |
end |
| 182 |
|
| 183 |
return Matrix:_new(result) |
| 184 |
end |
| 185 |
|
| 186 |
--- Scale all entries of a Matrix by a scalar |
| 187 |
--- @param other number The scalar multiplier |
| 188 |
--- @return Matrix The scaled matrix |
| 189 |
function Matrix:scale(other) |
| 190 |
local numrows = #self |
| 191 |
local numcols = #self[1] |
| 192 |
|
| 193 |
assert(type(other) == "number", "other must be a number.") |
| 194 |
|
| 195 |
local result = {} |
| 196 |
for i = 1, numrows do |
| 197 |
result[i] = {} |
| 198 |
for j = 1, numcols do |
| 199 |
local value = self[i][j] |
| 200 |
if getmetatable(value) == Vector then |
| 201 |
result[i][j] = value:scale(other) |
| 202 |
else |
| 203 |
result[i][j] = value * other |
| 204 |
end |
| 205 |
end |
| 206 |
end |
| 207 |
|
| 208 |
return Matrix:_new(result) |
| 209 |
end |
| 210 |
|
| 211 |
--- Convert to plain Lua table (no metatables) |
| 212 |
--- @return table |
| 213 |
function Matrix:to_table() |
| 214 |
local numrows = #self |
| 215 |
local numcols = #self[1] |
| 216 |
local t = {} |
| 217 |
for i = 1, numrows do |
| 218 |
t[i] = {} |
| 219 |
for j = 1, numcols do |
| 220 |
if getmetatable(self[i][j]) == Vector then |
| 221 |
t[i][j] = self[i][j]:to_table() |
| 222 |
else |
| 223 |
t[i][j] = self[i][j] |
| 224 |
end |
| 225 |
end |
| 226 |
end |
| 227 |
return t |
| 228 |
end |
| 229 |
|
| 230 |
--- Column reduction of the Matrix to RREF form |
| 231 |
--- @return Vector|nil The solution vector (last row of RREF) |
| 232 |
function Matrix:column_reduction() |
| 233 |
local cols = self:deep_copy() |
| 234 |
local numcols = #cols |
| 235 |
if numcols == 0 then return nil end |
| 236 |
local numrows = #cols[1] |
| 237 |
local vars = numcols - 1 |
| 238 |
if vars < 1 then return nil end |
| 239 |
local eps = 1e-12 |
| 240 |
|
| 241 |
local rank = 0 |
| 242 |
local row = 1 |
| 243 |
local pivot_cols = {} |
| 244 |
|
| 245 |
for col = 1, vars do |
| 246 |
if row > numrows then break end |
| 247 |
|
| 248 |
local pivot_row = nil |
| 249 |
local maxval = eps |
| 250 |
for r = row, numrows do |
| 251 |
local value = math.abs(cols[col][r]) |
| 252 |
if value > maxval then |
| 253 |
maxval = value |
| 254 |
pivot_row = r |
| 255 |
end |
| 256 |
end |
| 257 |
|
| 258 |
if pivot_row then |
| 259 |
if pivot_row ~= row then |
| 260 |
for c = 1, numcols do |
| 261 |
cols[c][row], cols[c][pivot_row] = cols[c][pivot_row], cols[c][row] |
| 262 |
end |
| 263 |
end |
| 264 |
|
| 265 |
local pivot = cols[col][row] |
| 266 |
for c = 1, numcols do |
| 267 |
cols[c][row] = cols[c][row] / pivot |
| 268 |
end |
| 269 |
|
| 270 |
for r = 1, numrows do |
| 271 |
if r ~= row then |
| 272 |
local factor = cols[col][r] |
| 273 |
if math.abs(factor) > eps then |
| 274 |
for c = 1, numcols do |
| 275 |
cols[c][r] = cols[c][r] - factor * cols[c][row] |
| 276 |
end |
| 277 |
cols[col][r] = 0 |
| 278 |
end |
| 279 |
end |
| 280 |
end |
| 281 |
|
| 282 |
pivot_cols[#pivot_cols + 1] = col |
| 283 |
rank = rank + 1 |
| 284 |
row = row + 1 |
| 285 |
end |
| 286 |
end |
| 287 |
|
| 288 |
for r = rank + 1, numrows do |
| 289 |
local all_zero = true |
| 290 |
for c = 1, vars do |
| 291 |
if math.abs(cols[c][r]) > eps then |
| 292 |
all_zero = false |
| 293 |
break |
| 294 |
end |
| 295 |
end |
| 296 |
if all_zero and math.abs(cols[numcols][r]) > eps then |
| 297 |
return nil |
| 298 |
end |
| 299 |
end |
| 300 |
|
| 301 |
local sol = {} |
| 302 |
for i = 1, vars do |
| 303 |
sol[i] = 0 |
| 304 |
end |
| 305 |
for k, pcol in ipairs(pivot_cols) do |
| 306 |
sol[pcol] = cols[numcols][k] |
| 307 |
end |
| 308 |
|
| 309 |
for _, v in ipairs(sol) do |
| 310 |
if v ~= v then return nil end |
| 311 |
end |
| 312 |
|
| 313 |
return Vector:_new(sol) |
| 314 |
end |
| 315 |
|
| 316 |
--- Transpose of Matrix object |
| 317 |
--- @return Matrix The transposed matrix |
| 318 |
function Matrix:transpose() |
| 319 |
local numrows = #self |
| 320 |
local numcols = #self[1] |
| 321 |
local transposed = {} |
| 322 |
for j = 1, numcols do |
| 323 |
transposed[j] = {} |
| 324 |
for i = 1, numrows do |
| 325 |
transposed[j][i] = self[i][j] |
| 326 |
end |
| 327 |
end |
| 328 |
return Matrix:_new(transposed) |
| 329 |
end |
| 330 |
|
| 331 |
--- Inverse of the Matrix (row-vector convention, column GJ) |
| 332 |
--- @return Matrix|nil The inverse matrix, or nil if not invertible |
| 333 |
function Matrix:inverse() |
| 334 |
local n = #self |
| 335 |
assert(n == #self[1], "Matrix must be square.") |
| 336 |
|
| 337 |
local eps = 1e-12 |
| 338 |
|
| 339 |
local A = Matrix:_new(self:to_table()) |
| 340 |
|
| 341 |
local L_table = {} |
| 342 |
for i = 1, n do |
| 343 |
L_table[i] = {} |
| 344 |
for j = 1, n do |
| 345 |
L_table[i][j] = (i == j) and 1 or 0 |
| 346 |
end |
| 347 |
end |
| 348 |
local L = Matrix:_new(L_table) |
| 349 |
|
| 350 |
for pivot = 1, n do |
| 351 |
local pivot_col = nil |
| 352 |
local pivot_row = nil |
| 353 |
|
| 354 |
for c = pivot, n do |
| 355 |
for r = pivot, n do |
| 356 |
if math.abs(A[r][c]) > eps then |
| 357 |
pivot_col = c |
| 358 |
pivot_row = r |
| 359 |
break |
| 360 |
end |
| 361 |
end |
| 362 |
if pivot_col then break end |
| 363 |
end |
| 364 |
|
| 365 |
if not pivot_col then |
| 366 |
return nil |
| 367 |
end |
| 368 |
|
| 369 |
if pivot_col ~= pivot then |
| 370 |
A:swap_columns(pivot_col, pivot) |
| 371 |
L:swap_columns(pivot_col, pivot) |
| 372 |
end |
| 373 |
|
| 374 |
local p = A[pivot][pivot] |
| 375 |
A:scale_column(pivot, 1 / p) |
| 376 |
L:scale_column(pivot, 1 / p) |
| 377 |
|
| 378 |
for c = 1, n do |
| 379 |
if c ~= pivot then |
| 380 |
local k = -A[pivot][c] |
| 381 |
if math.abs(k) > eps then |
| 382 |
A:add_scalar_multiple_of_column(pivot, k, c) |
| 383 |
L:add_scalar_multiple_of_column(pivot, k, c) |
| 384 |
end |
| 385 |
end |
| 386 |
end |
| 387 |
end |
| 388 |
|
| 389 |
return L |
| 390 |
end |
| 391 |
|
| 392 |
--- Convert to a basis from a simplex |
| 393 |
--- @return Matrix The basis matrix |
| 394 |
function Matrix:hto_basis() |
| 395 |
local numrows = #self |
| 396 |
local numcols = #self[1] |
| 397 |
local deep_copy = self:deep_copy() |
| 398 |
for i = 1, numrows do |
| 399 |
if i ~= 1 then |
| 400 |
for j = 1, numcols - 1 do |
| 401 |
deep_copy[i][j] = deep_copy[i][j] - deep_copy[1][j] |
| 402 |
end |
| 403 |
end |
| 404 |
end |
| 405 |
return deep_copy |
| 406 |
end |
| 407 |
|
| 408 |
--- Convert to a simplex from a basis |
| 409 |
--- @return Matrix The simplex matrix |
| 410 |
function Matrix:hto_simplex() |
| 411 |
local numrows = #self |
| 412 |
local numcols = #self[1] |
| 413 |
local deep_copy = self:deep_copy() |
| 414 |
for i = 1, numrows do |
| 415 |
if i ~= 1 then |
| 416 |
for j = 1, numcols - 1 do |
| 417 |
deep_copy[i][j] = deep_copy[i][j] + deep_copy[1][j] |
| 418 |
end |
| 419 |
end |
| 420 |
end |
| 421 |
return deep_copy |
| 422 |
end |
| 423 |
|
| 424 |
--- Compute the minor of the matrix by removing row ii and column jj |
| 425 |
--- @param ii number The row to remove |
| 426 |
--- @param jj number The column to remove |
| 427 |
--- @return number|Vector The minor determinant |
| 428 |
function Matrix:minor(ii, jj) |
| 429 |
local numrows = #self |
| 430 |
local M = {} |
| 431 |
local row_idx = 0 |
| 432 |
for i = 1, numrows do |
| 433 |
if i ~= ii then |
| 434 |
row_idx = row_idx + 1 |
| 435 |
M[row_idx] = {} |
| 436 |
for j = 1, #self[i] do |
| 437 |
if j ~= jj then |
| 438 |
table.insert(M[row_idx], self[i][j]) |
| 439 |
end |
| 440 |
end |
| 441 |
end |
| 442 |
end |
| 443 |
return Matrix:_new(M):det("row", 1) |
| 444 |
end |
| 445 |
|
| 446 |
--- Compute the determinant of the matrix by cofactor expansion along a row or column |
| 447 |
--- @param switch string The row/column switch |
| 448 |
--- @param pos number The position of the row or column |
| 449 |
--- @return number|Vector The determinant |
| 450 |
function Matrix:det(switch, pos) |
| 451 |
local numrows = #self |
| 452 |
local numcols = #self[1] |
| 453 |
assert(numrows == numcols, "You tried to take the determinant of a non-square matrix.") |
| 454 |
if numrows == 2 then |
| 455 |
local A_mt = getmetatable(self[1][1]) |
| 456 |
local B_mt = getmetatable(self[2][1]) |
| 457 |
if A_mt == Vector and B_mt == Vector then |
| 458 |
-- det = A * d - B * c (column 1 has vectors) |
| 459 |
return self[1][1]:scale(self[2][2]):sub(self[2][1]:scale(self[1][2])) |
| 460 |
end |
| 461 |
A_mt = getmetatable(self[1][2]) |
| 462 |
B_mt = getmetatable(self[2][2]) |
| 463 |
if A_mt == Vector and B_mt == Vector then |
| 464 |
-- det = a * D - b * C (column 2 has vectors) |
| 465 |
return self[1][2]:scale(self[2][1]):sub(self[2][2]:scale(self[1][1])) |
| 466 |
end |
| 467 |
return self[1][1] * self[2][2] - self[1][2] * self[2][1] |
| 468 |
end |
| 469 |
if switch == "row" then |
| 470 |
local i = pos |
| 471 |
local det = 0 |
| 472 |
for j = 1, numcols do |
| 473 |
local sign = (-1)^(i + j) |
| 474 |
local minor = self:minor(i, j) |
| 475 |
local s = self[i][j] |
| 476 |
det = det + s * minor * sign |
| 477 |
end |
| 478 |
return det |
| 479 |
elseif switch == "column" then |
| 480 |
local j = pos |
| 481 |
local det |
| 482 |
if getmetatable(self[1][j]) == Vector then |
| 483 |
local t = {} |
| 484 |
for i = 1, numcols + 1 do t[i] = 0 end |
| 485 |
det = Vector:_new(t) |
| 486 |
else |
| 487 |
det = 0 |
| 488 |
end |
| 489 |
for i = 1, numrows do |
| 490 |
local sign = (-1)^(i + j) |
| 491 |
local minor = self:minor(i, j) |
| 492 |
local s = self[i][j] |
| 493 |
if getmetatable(s) == Vector then |
| 494 |
det = det:add(s:scale(minor * sign)) |
| 495 |
else |
| 496 |
det = det + s * minor * sign |
| 497 |
end |
| 498 |
end |
| 499 |
return det |
| 500 |
end |
| 501 |
end |
| 502 |
|
| 503 |
--- Homogeneous reciprocation of Matrix objects |
| 504 |
--- @return Matrix The homogeneous reciprocation of self |
| 505 |
function Matrix:reciprocate_by_homogeneous() |
| 506 |
local result = {} |
| 507 |
for i = 1, #self do |
| 508 |
result[i] = {} |
| 509 |
local w = self[i][#self[i]] |
| 510 |
for j = 1, #self[i] - 1 do |
| 511 |
result[i][j] = self[i][j] / w |
| 512 |
end |
| 513 |
result[i][#self[i]] = 1 |
| 514 |
end |
| 515 |
return Matrix:_new(result) |
| 516 |
end |
| 517 |
|
| 518 |
--- Multiply two Matrix objects |
| 519 |
--- @param other Matrix|Vector The RHS |
| 520 |
--- @return Matrix|Vector The product of self and other |
| 521 |
--- @param reciprocate boolean|nil If true, apply homogeneous reciprocation for non-4x4 results (default true for non-flag) |
| 522 |
function Matrix:multiply(other) |
| 523 |
local flag |
| 524 |
if getmetatable(other) == Vector then |
| 525 |
other = Matrix:_new{other:to_table()} |
| 526 |
flag = true |
| 527 |
end |
| 528 |
local Arows = #self |
| 529 |
local Acols = #self[1] |
| 530 |
local Bcols = #other[1] |
| 531 |
local product = {} |
| 532 |
for row = 1, Arows do |
| 533 |
product[row] = {} |
| 534 |
for col = 1, Bcols do |
| 535 |
product[row][col] = 0 |
| 536 |
for k = 1, Acols do |
| 537 |
product[row][col] = product[row][col] + self[row][k] * other[k][col] |
| 538 |
end |
| 539 |
end |
| 540 |
end |
| 541 |
if flag then |
| 542 |
return Vector:_new(product[1]) |
| 543 |
else |
| 544 |
return Matrix:_new(product) |
| 545 |
end |
| 546 |
end |
| 547 |
|
| 548 |
--- Get 3D bounding box of Matrix points |
| 549 |
--- @return table A table with 'min' and 'max' Vector objects |
| 550 |
function Matrix:get_bbox3() |
| 551 |
local min_x, min_y, min_z = math.huge, math.huge, math.huge |
| 552 |
local max_x, max_y, max_z = -math.huge, -math.huge, -math.huge |
| 553 |
for _, row in ipairs(self) do |
| 554 |
if row[1] < min_x then min_x = row[1] end |
| 555 |
if row[2] < min_y then min_y = row[2] end |
| 556 |
if row[3] < min_z then min_z = row[3] end |
| 557 |
if row[1] > max_x then max_x = row[1] end |
| 558 |
if row[2] > max_y then max_y = row[2] end |
| 559 |
if row[3] > max_z then max_z = row[3] end |
| 560 |
end |
| 561 |
return { |
| 562 |
min = Vector:_new{min_x, min_y, min_z, 1}, |
| 563 |
max = Vector:_new{max_x, max_y, max_z, 1} |
| 564 |
} |
| 565 |
end |
| 566 |
|
| 567 |
--- Get 2D bounding box of Matrix points |
| 568 |
--- @return table A table with 'min' and 'max' Vector objects |
| 569 |
function Matrix:get_bbox2() |
| 570 |
local min_x, min_y = math.huge, math.huge |
| 571 |
local max_x, max_y = -math.huge, -math.huge |
| 572 |
for _, row in ipairs(self) do |
| 573 |
if row[1] < min_x then min_x = row[1] end |
| 574 |
if row[2] < min_y then min_y = row[2] end |
| 575 |
if row[1] > max_x then max_x = row[1] end |
| 576 |
if row[2] > max_y then max_y = row[2] end |
| 577 |
end |
| 578 |
return { |
| 579 |
min = Vector:_new{min_x, min_y, 0, 1}, |
| 580 |
max = Vector:_new{max_x, max_y, 0, 1} |
| 581 |
} |
| 582 |
end |
| 583 |
|
| 584 |
--- Check if two 3D bounding boxes overlap (using pre-computed bboxes when available) |
| 585 |
--- @param other Matrix The other Matrix |
| 586 |
--- @param a_bbox table|nil Pre-computed bbox for self |
| 587 |
--- @param b_bbox table|nil Pre-computed bbox for other |
| 588 |
--- @return boolean True if they overlap, false otherwise |
| 589 |
function Matrix:bboxes_overlap3(other, a_bbox, b_bbox) |
| 590 |
a_bbox = a_bbox or self:get_bbox3() |
| 591 |
b_bbox = b_bbox or other:get_bbox3() |
| 592 |
return not ( |
| 593 |
a_bbox.max[1] < b_bbox.min[1] |
| 594 |
or a_bbox.min[1] > b_bbox.max[1] |
| 595 |
or a_bbox.max[2] < b_bbox.min[2] |
| 596 |
or a_bbox.min[2] > b_bbox.max[2] |
| 597 |
or a_bbox.max[3] < b_bbox.min[3] |
| 598 |
or a_bbox.min[3] > b_bbox.max[3] |
| 599 |
) |
| 600 |
end |
| 601 |
|
| 602 |
--- Check if two 2D bounding boxes overlap (using pre-computed bboxes when available) |
| 603 |
--- @param other Matrix The other Matrix |
| 604 |
--- @param a_bbox table|nil Pre-computed bbox for self |
| 605 |
--- @param b_bbox table|nil Pre-computed bbox for other |
| 606 |
--- @return boolean True if they overlap, false otherwise |
| 607 |
function Matrix:bboxes_overlap2(other, a_bbox, b_bbox) |
| 608 |
a_bbox = a_bbox or self:get_bbox2() |
| 609 |
b_bbox = b_bbox or other:get_bbox2() |
| 610 |
return not ( |
| 611 |
a_bbox.max[1] < b_bbox.min[1] |
| 612 |
or a_bbox.min[1] > b_bbox.max[1] |
| 613 |
or a_bbox.max[2] < b_bbox.min[2] |
| 614 |
or a_bbox.min[2] > b_bbox.max[2] |
| 615 |
) |
| 616 |
end |
| 617 |
|
| 618 |
--- Compute the homogeneous centroid of the Matrix points |
| 619 |
--- @return Vector The homogeneous centroid |
| 620 |
function Matrix:hcentroid() |
| 621 |
local ns = #self |
| 622 |
local dim = #self[1] |
| 623 |
local centroid = Vector:_new(self[1]) |
| 624 |
for i = 2, ns do |
| 625 |
centroid = centroid:hadd(Vector:_new(self[i])) |
| 626 |
end |
| 627 |
centroid = centroid:hscale(1 / ns) |
| 628 |
return centroid |
| 629 |
end |
| 630 |
|
| 631 |
--- Sort points in Matrix by angle around their homogeneous centroid |
| 632 |
--- @return table The sorted points (as plain table, not Matrix) |
| 633 |
function Matrix:hcentroid_sort() |
| 634 |
local num = #self |
| 635 |
local centroid = self:hcentroid() |
| 636 |
local I = self:deep_copy() |
| 637 |
local sum = Vector:_new{0,0,0,1} |
| 638 |
for i = 1, num do |
| 639 |
sum = sum:add(Vector:_new(I[i])) |
| 640 |
end |
| 641 |
centroid = sum:scale(1/num) |
| 642 |
local P = Vector:_new(I[1]) |
| 643 |
local u = Vector:_new(I[2]):hsub(P):hnormalize() |
| 644 |
local v = Vector:_new(I[3]):hsub(P) |
| 645 |
local normal = u:hhypercross(v):hnormalize() |
| 646 |
v = normal:hhypercross(u):hnormalize() |
| 647 |
local angles = {} |
| 648 |
for i, p in ipairs(I) do |
| 649 |
local rel = Vector:_new(p):hsub(centroid) |
| 650 |
local x = rel:hinner(u) |
| 651 |
local y = rel:hinner(v) |
| 652 |
local angle = math.atan2(y, x) |
| 653 |
angles[i] = {angle = angle, index = i} |
| 654 |
end |
| 655 |
table.sort(angles, function(a, b) return a.angle < b.angle end) |
| 656 |
local sorted = {} |
| 657 |
for i, a in ipairs(angles) do |
| 658 |
sorted[i] = I[a.index] |
| 659 |
end |
| 660 |
return sorted |
| 661 |
end |
| 662 |
|
| 663 |
--- Check if all elements in the Matrix are numeric |
| 664 |
--- @return boolean True if all elements are numeric, false otherwise |
| 665 |
function Matrix:is_numeric() |
| 666 |
for _, row in ipairs(self) do |
| 667 |
for _, val in ipairs(row) do |
| 668 |
if type(val) ~= "number" then |
| 669 |
return false |
| 670 |
end |
| 671 |
end |
| 672 |
end |
| 673 |
return true |
| 674 |
end |
| 675 |
|
| 676 |
--- 3D rotation about an arbitrary axis through the origin |
| 677 |
--- @param axis Vector The axis direction |
| 678 |
--- @param theta number The rotation angle in radians |
| 679 |
--- @return Matrix The rotation matrix |
| 680 |
function Matrix.axis_angle(axis, theta) |
| 681 |
assert(getmetatable(axis) == Vector, "axis must be a Vector.") |
| 682 |
axis = axis:hnormalize() |
| 683 |
local x = axis[1] |
| 684 |
local y = axis[2] |
| 685 |
local z = axis[3] |
| 686 |
local c = math.cos(theta) |
| 687 |
local s = math.sin(theta) |
| 688 |
local t = 1 - c |
| 689 |
return Matrix:_new{ |
| 690 |
{t * x * x + c, t * x * y + s * z, t * x * z - s * y, 0}, |
| 691 |
{t * x * y - s * z, t * y * y + c, t * y * z + s * x, 0}, |
| 692 |
{t * x * z + s * y, t * y * z - s * x, t * z * z + c, 0}, |
| 693 |
{0, 0, 0, 1} |
| 694 |
} |
| 695 |
end |
| 696 |
|
| 697 |
--- 3D shear matrix |
| 698 |
--- @param kxy number|nil x shear from y |
| 699 |
--- @param kxz number|nil x shear from z |
| 700 |
--- @param kyx number|nil y shear from x |
| 701 |
--- @param kyz number|nil y shear from z |
| 702 |
--- @param kzx number|nil z shear from x |
| 703 |
--- @param kzy number|nil z shear from y |
| 704 |
--- @return Matrix The shear matrix |
| 705 |
function Matrix.shear(kxy, kxz, kyx, kyz, kzx, kzy) |
| 706 |
kxy = kxy or 0 |
| 707 |
kxz = kxz or 0 |
| 708 |
kyx = kyx or 0 |
| 709 |
kyz = kyz or 0 |
| 710 |
kzx = kzx or 0 |
| 711 |
kzy = kzy or 0 |
| 712 |
return Matrix:_new{ |
| 713 |
{1, kyx, kzx, 0}, |
| 714 |
{kxy, 1, kzy, 0}, |
| 715 |
{kxz, kyz, 1, 0}, |
| 716 |
{0, 0, 0, 1} |
| 717 |
} |
| 718 |
end |
| 719 |
|
| 720 |
--- 3D reflection about an arbitrary axis through the origin |
| 721 |
--- @param axis Vector The axis direction |
| 722 |
--- @return Matrix The reflection matrix |
| 723 |
function Matrix.reflect_axis(axis) |
| 724 |
assert(getmetatable(axis) == Vector, "axis must be a Vector.") |
| 725 |
axis = axis:hnormalize() |
| 726 |
local x = axis[1] |
| 727 |
local y = axis[2] |
| 728 |
local z = axis[3] |
| 729 |
return Matrix:_new{ |
| 730 |
{2 * x * x - 1, 2 * x * y, 2 * x * z, 0}, |
| 731 |
{2 * x * y, 2 * y * y - 1, 2 * y * z, 0}, |
| 732 |
{2 * x * z, 2 * y * z, 2 * z * z - 1, 0}, |
| 733 |
{0, 0, 0, 1} |
| 734 |
} |
| 735 |
end |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
--- 3D translation matrix |
| 740 |
--- @param delta Vector The translation vector |
| 741 |
--- @return Matrix The translation matrix |
| 742 |
function Matrix.translate(delta) |
| 743 |
assert(getmetatable(delta) == Vector, "delta must be a Vector.") |
| 744 |
return Matrix:_new{ |
| 745 |
{1, 0, 0, 0}, |
| 746 |
{0, 1, 0, 0}, |
| 747 |
{0, 0, 1, 0}, |
| 748 |
{delta[1], delta[2], delta[3], 1} |
| 749 |
} |
| 750 |
end |
| 751 |
|
| 752 |
--- 3D scaling matrix |
| 753 |
--- @param scale Vector The scaling vector |
| 754 |
--- @return Matrix The scaling matrix |
| 755 |
function Matrix.scaling(scale) |
| 756 |
assert(getmetatable(scale) == Vector, "scale must be a Vector.") |
| 757 |
return Matrix:_new{ |
| 758 |
{scale[1], 0, 0, 0}, |
| 759 |
{0, scale[2], 0, 0}, |
| 760 |
{0, 0, scale[3], 0}, |
| 761 |
{0, 0, 0, 1} |
| 762 |
} |
| 763 |
end |
| 764 |
|
| 765 |
|
| 766 |
--- 3D ZYZ Euler rotation matrix |
| 767 |
--- @param angles Vector The Euler angles (alpha, beta, gamma) |
| 768 |
--- @return Matrix The rotation matrix |
| 769 |
function Matrix.zyzrotation(angles) |
| 770 |
assert(getmetatable(angles) == Vector, "angles must be a Vector.") |
| 771 |
return Matrix.axis_angle(Vector:_new{0, 0, 1, 1}, angles[1]) |
| 772 |
:multiply(Matrix.axis_angle(Vector:_new{0, 1, 0, 1}, angles[2])) |
| 773 |
:multiply(Matrix.axis_angle(Vector:_new{0, 0, 1, 1}, angles[3])) |
| 774 |
end |
| 775 |
|
| 776 |
--- identity matrix |
| 777 |
--- @return Matrix The identity matrix |
| 778 |
function Matrix.identity() |
| 779 |
return Matrix:_new{ |
| 780 |
{1, 0, 0, 0}, |
| 781 |
{0, 1, 0, 0}, |
| 782 |
{0, 0, 1, 0}, |
| 783 |
{0, 0, 0, 1} |
| 784 |
} |
| 785 |
end |
| 786 |
|
| 787 |
--- perspective along an arbitrary axis |
| 788 |
--- @param axis Vector The perspective axis and strength |
| 789 |
--- @return Matrix The perspective matrix |
| 790 |
function Matrix.perspective(axis) |
| 791 |
assert(getmetatable(axis) == Vector, "axis must be a Vector.") |
| 792 |
return Matrix:_new{ |
| 793 |
{1, 0, 0, axis[1]}, |
| 794 |
{0, 1, 0, axis[2]}, |
| 795 |
{0, 0, 1, axis[3]}, |
| 796 |
{0, 0, 0, 1} |
| 797 |
} |
| 798 |
end |
| 799 |
|
| 800 |
--- apply a transformation about a point |
| 801 |
--- @param point Vector The fixed point |
| 802 |
--- @param transformation Matrix The transformation to apply |
| 803 |
--- @return Matrix The composed transformation |
| 804 |
function Matrix.transform_about(point, transformation) |
| 805 |
assert(getmetatable(point) == Vector, "point must be a Vector.") |
| 806 |
return Matrix.translate(Vector:_new{-point[1], -point[2], -point[3], 1}) |
| 807 |
:multiply(transformation) |
| 808 |
:multiply(Matrix.translate(point)) |
| 809 |
end |
| 810 |
|
| 811 |
--- Set the Vector class reference (breaks circular dependency). |
| 812 |
--- @param V table The Vector class |
| 813 |
function Matrix._set_Vector(V) |
| 814 |
Vector = V |
| 815 |
end |
| 816 |
|
| 817 |
return Matrix |