| 1 |
--- lua-tikz3dtools-vector.lua |
| 2 |
--- Vector class for the lua-tikz3dtools package. |
| 3 |
|
| 4 |
local Vector = {} |
| 5 |
Vector.__index = Vector |
| 6 |
|
| 7 |
--- Create a new 1xn Vector object (validated). |
| 8 |
--- @param vec table<number> The 1xn vector data |
| 9 |
--- @return Vector A 1xn vector |
| 10 |
function Vector:new(vec) |
| 11 |
local A = type(vec) == "table" |
| 12 |
local n = #vec |
| 13 |
A = A and n > 0 |
| 14 |
if A == true then |
| 15 |
for c = 1, n do |
| 16 |
A = A and type(vec[c]) == "number" |
| 17 |
end |
| 18 |
end |
| 19 |
assert(A, "A Vector object must be a 1xn table of numbers, where n>0.") |
| 20 |
return setmetatable(vec, Vector) |
| 21 |
end |
| 22 |
|
| 23 |
--- Unchecked constructor — skips validation. Use for internal calls only. |
| 24 |
--- @param vec table<number> |
| 25 |
--- @return Vector |
| 26 |
function Vector:_new(vec) |
| 27 |
return setmetatable(vec, Vector) |
| 28 |
end |
| 29 |
|
| 30 |
--- Pretty print for Vector object |
| 31 |
--- @return string |
| 32 |
function Vector:__tostring() |
| 33 |
return ("Vector{%.12f" .. string.rep(",%.12f", #self - 1) .. "}"):format(table.unpack(self)) |
| 34 |
end |
| 35 |
|
| 36 |
--- Deep copy of Vector object |
| 37 |
--- @return Vector A deep copy of self |
| 38 |
function Vector:deep_copy() |
| 39 |
local n = #self |
| 40 |
local copy = {} |
| 41 |
for i = 1, n do |
| 42 |
copy[i] = self[i] |
| 43 |
end |
| 44 |
return Vector:_new(copy) |
| 45 |
end |
| 46 |
|
| 47 |
--- Convert Vector to table |
| 48 |
--- @return table<number> A table representation of self |
| 49 |
function Vector:to_table() |
| 50 |
local n = #self |
| 51 |
local t = {} |
| 52 |
for i = 1, n do |
| 53 |
t[i] = self[i] |
| 54 |
end |
| 55 |
return t |
| 56 |
end |
| 57 |
|
| 58 |
--- Homogeneous addition of Vector objects |
| 59 |
--- @param other Vector A vector of same size |
| 60 |
--- @return Vector The sum of self and other |
| 61 |
function Vector:hadd(other) |
| 62 |
local lo = #other |
| 63 |
local V = {} |
| 64 |
for i = 1, lo - 1 do |
| 65 |
V[i] = self[i] + other[i] |
| 66 |
end |
| 67 |
V[lo] = self[lo] |
| 68 |
return Vector:_new(V) |
| 69 |
end |
| 70 |
|
| 71 |
--- Addition of Vector objects |
| 72 |
--- @param other Vector A vector of same size |
| 73 |
--- @return Vector The sum of self and other |
| 74 |
function Vector:add(other) |
| 75 |
local lo = #other |
| 76 |
local V = {} |
| 77 |
for i = 1, lo do |
| 78 |
V[i] = self[i] + other[i] |
| 79 |
end |
| 80 |
return Vector:_new(V) |
| 81 |
end |
| 82 |
|
| 83 |
--- Homogeneous subtraction of Vector objects |
| 84 |
--- @param other Vector A vector of same size |
| 85 |
--- @return Vector The difference of self and other |
| 86 |
function Vector:hsub(other) |
| 87 |
local lo = #other |
| 88 |
local V = {} |
| 89 |
for i = 1, lo - 1 do |
| 90 |
V[i] = self[i] - other[i] |
| 91 |
end |
| 92 |
V[lo] = self[lo] |
| 93 |
return Vector:_new(V) |
| 94 |
end |
| 95 |
|
| 96 |
--- Subtraction of Vector objects |
| 97 |
--- @param other Vector A vector of same size |
| 98 |
--- @return Vector The difference of self and other |
| 99 |
function Vector:sub(other) |
| 100 |
local lo = #other |
| 101 |
local V = {} |
| 102 |
for i = 1, lo do |
| 103 |
V[i] = self[i] - other[i] |
| 104 |
end |
| 105 |
return Vector:_new(V) |
| 106 |
end |
| 107 |
|
| 108 |
--- Homogeneous scaling of Vector objects |
| 109 |
--- @param scalar number The scalar |
| 110 |
--- @return Vector The scale of self by scalar |
| 111 |
function Vector:hscale(scalar) |
| 112 |
local ls = #self |
| 113 |
local V = {} |
| 114 |
for i = 1, ls - 1 do |
| 115 |
V[i] = self[i] * scalar |
| 116 |
end |
| 117 |
V[ls] = self[ls] |
| 118 |
return Vector:_new(V) |
| 119 |
end |
| 120 |
|
| 121 |
--- Scaling of Vector objects |
| 122 |
--- @param scalar number The scalar |
| 123 |
--- @return Vector The scale of self by scalar |
| 124 |
function Vector:scale(scalar) |
| 125 |
local ls = #self |
| 126 |
local V = {} |
| 127 |
for i = 1, ls do |
| 128 |
V[i] = self[i] * scalar |
| 129 |
end |
| 130 |
return Vector:_new(V) |
| 131 |
end |
| 132 |
|
| 133 |
--- Homogeneous inner product of Vector objects |
| 134 |
--- @param other Vector The RHS |
| 135 |
--- @return number The homogeneous inner product of self with other |
| 136 |
function Vector:hinner(other) |
| 137 |
local ls = #self |
| 138 |
local result = 0 |
| 139 |
for i = 1, ls - 1 do |
| 140 |
result = result + self[i] * other[i] |
| 141 |
end |
| 142 |
return result |
| 143 |
end |
| 144 |
|
| 145 |
--- Inner product of Vector objects |
| 146 |
--- @param other Vector The RHS |
| 147 |
--- @return number The inner product of self with other |
| 148 |
function Vector:inner(other) |
| 149 |
local ls = #self |
| 150 |
local result = 0 |
| 151 |
for i = 1, ls do |
| 152 |
result = result + self[i] * other[i] |
| 153 |
end |
| 154 |
return result |
| 155 |
end |
| 156 |
|
| 157 |
--- Homogeneous hypercross product of Vector objects |
| 158 |
--- @param ... Vector Additional vectors |
| 159 |
--- @return Vector The homogeneous hypercross product of self with the additional vectors |
| 160 |
function Vector:hhypercross(...) |
| 161 |
-- Forward-declared; Matrix is set via Vector._set_Matrix() |
| 162 |
local Matrix = Vector._Matrix |
| 163 |
local args = { ... } |
| 164 |
local n = #self - 1 |
| 165 |
|
| 166 |
local M = {} |
| 167 |
for i = 1, n do |
| 168 |
local e = {} |
| 169 |
for j = 1, n do |
| 170 |
e[j] = (i == j) and 1 or 0 |
| 171 |
end |
| 172 |
e[n + 1] = 0 |
| 173 |
|
| 174 |
M[i] = {} |
| 175 |
M[i][1] = Vector:_new(e) |
| 176 |
M[i][2] = self[i] |
| 177 |
|
| 178 |
for j = 1, n - 2 do |
| 179 |
M[i][j + 2] = args[j][i] |
| 180 |
end |
| 181 |
end |
| 182 |
local result = Matrix:_new(M):det("column", 1) |
| 183 |
result[n + 1] = self[n + 1] |
| 184 |
return Vector:_new(result) |
| 185 |
end |
| 186 |
|
| 187 |
--- Homogeneous normalization of Vector objects |
| 188 |
--- @return Vector The homogeneous normalization of self |
| 189 |
function Vector:hnormalize() |
| 190 |
local ls = #self |
| 191 |
local len = 0 |
| 192 |
for i = 1, ls - 1 do |
| 193 |
len = len + self[i] * self[i] |
| 194 |
end |
| 195 |
len = math.sqrt(len) |
| 196 |
local V = {} |
| 197 |
for i = 1, ls - 1 do |
| 198 |
V[i] = self[i] / len |
| 199 |
end |
| 200 |
V[ls] = self[ls] |
| 201 |
return Vector:_new(V) |
| 202 |
end |
| 203 |
|
| 204 |
--- Normalization of Vector objects |
| 205 |
--- @return Vector The normalization of self |
| 206 |
function Vector:normalize() |
| 207 |
local ls = #self |
| 208 |
local len = 0 |
| 209 |
for i = 1, ls do |
| 210 |
len = len + self[i] * self[i] |
| 211 |
end |
| 212 |
len = math.sqrt(len) |
| 213 |
local V = {} |
| 214 |
for i = 1, ls do |
| 215 |
V[i] = self[i] / len |
| 216 |
end |
| 217 |
return Vector:_new(V) |
| 218 |
end |
| 219 |
|
| 220 |
--- Homogeneous distance between Vector objects |
| 221 |
--- @param other Vector A vector of same size |
| 222 |
--- @return number The homogeneous distance between self and other |
| 223 |
function Vector:hdistance(other) |
| 224 |
local ls = #self |
| 225 |
local result = 0 |
| 226 |
for i = 1, ls - 1 do |
| 227 |
result = result + (self[i] - other[i])^2 |
| 228 |
end |
| 229 |
return math.sqrt(result) |
| 230 |
end |
| 231 |
|
| 232 |
--- Distance between Vector objects |
| 233 |
--- @param other Vector A vector of same size |
| 234 |
--- @return number The distance between self and other |
| 235 |
function Vector:distance(other) |
| 236 |
local ls = #self |
| 237 |
local result = 0 |
| 238 |
for i = 1, ls do |
| 239 |
result = result + (self[i] - other[i])^2 |
| 240 |
end |
| 241 |
return math.sqrt(result) |
| 242 |
end |
| 243 |
|
| 244 |
--- Homogeneous projection of Vector objects |
| 245 |
--- @param other Vector A vector of same size |
| 246 |
--- @return Vector The homogeneous projection of self onto other |
| 247 |
function Vector:hproject_onto(other) |
| 248 |
local denom = other:hinner(other) |
| 249 |
local scalar = self:hinner(other) / denom |
| 250 |
return other:hscale(scalar) |
| 251 |
end |
| 252 |
|
| 253 |
--- Projection of Vector objects |
| 254 |
--- @param other Vector A vector of same size |
| 255 |
--- @return Vector The projection of self onto other |
| 256 |
function Vector:project_onto(other) |
| 257 |
local denom = other:inner(other) |
| 258 |
local scalar = self:inner(other) / denom |
| 259 |
return other:scale(scalar) |
| 260 |
end |
| 261 |
|
| 262 |
--- Homogeneous orthogonal projection of Vector objects onto a plane |
| 263 |
--- @param plane_basis table A 3-row basis (origin, u-dir, v-dir) in homogeneous coordinates |
| 264 |
--- @return Vector The homogeneous orthogonal projection of self onto the plane |
| 265 |
function Vector:horthogonal_projection_onto_plane(plane_basis) |
| 266 |
local Matrix = Vector._Matrix |
| 267 |
local TO = Vector:_new(plane_basis[1]) |
| 268 |
local TU = Vector:_new(plane_basis[2]) |
| 269 |
local TV = Vector:_new(plane_basis[3]) |
| 270 |
local TW = TU:hhypercross(TV) |
| 271 |
local TOS = self:hsub(TO) |
| 272 |
if TW:hinner(TOS) < 0 then |
| 273 |
TW = TW:hscale(-1) |
| 274 |
end |
| 275 |
local proj = TOS:hproject_onto(TW) |
| 276 |
return self:hsub(proj) |
| 277 |
end |
| 278 |
|
| 279 |
--- Homogeneous norm of Vector objects |
| 280 |
--- @return number The homogeneous norm of self |
| 281 |
function Vector:hnorm() |
| 282 |
local sum = 0 |
| 283 |
for i = 1, #self - 1 do |
| 284 |
sum = sum + self[i] * self[i] |
| 285 |
end |
| 286 |
return math.sqrt(sum) |
| 287 |
end |
| 288 |
|
| 289 |
--- Norm of Vector objects |
| 290 |
--- @return number The norm of self |
| 291 |
function Vector:norm() |
| 292 |
local sum = 0 |
| 293 |
for i = 1, #self do |
| 294 |
sum = sum + self[i] * self[i] |
| 295 |
end |
| 296 |
return math.sqrt(sum) |
| 297 |
end |
| 298 |
|
| 299 |
--- Homogeneous reciprocation of Vector objects |
| 300 |
--- @return Vector The homogeneous reciprocation of self |
| 301 |
function Vector:reciprocate_by_homogeneous() |
| 302 |
local V = {} |
| 303 |
local w = self[#self] |
| 304 |
for i = 1, #self - 1 do |
| 305 |
V[i] = self[i] / w |
| 306 |
end |
| 307 |
V[#self] = 1 |
| 308 |
return Vector:_new(V) |
| 309 |
end |
| 310 |
|
| 311 |
--- Find an arbitrary vector orthogonal to self |
| 312 |
--- @return Vector An arbitrary vector orthogonal to self |
| 313 |
function Vector:orthogonal_vector() |
| 314 |
local v |
| 315 |
if ( |
| 316 |
math.abs(self[2]) > 1e-3 |
| 317 |
and math.abs(self[1]) < 1e-3 |
| 318 |
and math.abs(self[3]) < 1e-3 |
| 319 |
) then |
| 320 |
v = self:hhypercross(Vector:_new{0,1,0,1}) |
| 321 |
else |
| 322 |
v = self:hhypercross(Vector:_new{1,0,0,1}) |
| 323 |
end |
| 324 |
return v |
| 325 |
end |
| 326 |
|
| 327 |
--- Multiply Vector by Matrix (applies projective divide) |
| 328 |
--- @param matrix table The matrix |
| 329 |
--- @return Vector The product of self and matrix, divided by homogeneous w |
| 330 |
function Vector:multiply(matrix) |
| 331 |
local Matrix = Vector._Matrix |
| 332 |
local M = Matrix:_new{{table.unpack(self)}}:multiply(matrix) |
| 333 |
return Vector:_new(M[1]):reciprocate_by_homogeneous() |
| 334 |
end |
| 335 |
|
| 336 |
--- Create a homogeneous sphere point from spherical coordinates |
| 337 |
--- @param v Vector the theta, phi, and radius |
| 338 |
--- @return Vector |
| 339 |
function Vector.sphere(v) |
| 340 |
local theta, phi, radius = v[1], v[2], v[3] |
| 341 |
local s = math.sin(phi) |
| 342 |
local x = radius * s * math.cos(theta) |
| 343 |
local y = radius * s * math.sin(theta) |
| 344 |
local z = radius * math.cos(phi) |
| 345 |
return Vector:_new{x, y, z, 1} |
| 346 |
end |
| 347 |
|
| 348 |
--- The stereographic projection |
| 349 |
--- @param v Vector the 3D vector |
| 350 |
--- @return Vector the stereographic projection |
| 351 |
function Vector.stereographic_projection(v) |
| 352 |
local x, y, z = v[1], v[2], v[3] |
| 353 |
return Vector:_new{x/(1-z), y/(1-z), 0, 1} |
| 354 |
end |
| 355 |
|
| 356 |
--- The inverse stereographic projection |
| 357 |
--- @param x number the x point |
| 358 |
--- @param y number the y point |
| 359 |
--- @return Vector the inverse stereographic projection |
| 360 |
function Vector.inverse_stereographic_projection(v) |
| 361 |
local x, y = v[1], v[2] |
| 362 |
return Vector:_new{2*x/(1+x^2+y^2), 2*y/(1+x^2+y^2), (-1+x^2+y^2)/(1+x^2+y^2), 1} |
| 363 |
end |
| 364 |
|
| 365 |
--- Set the Matrix class reference (breaks circular dependency). |
| 366 |
--- @param M table The Matrix class |
| 367 |
function Vector._set_Matrix(M) |
| 368 |
Vector._Matrix = M |
| 369 |
end |
| 370 |
|
| 371 |
return Vector |