Skip to content

Commit

Permalink
matrix: Unroll matrix-vector product
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Oct 30, 2023
1 parent ae720c5 commit b737b98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/matrix.typ
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@
)
}

// Multiply 4x4 matrix with vector of size 3 or 4.
// The value of vec_4 defaults to w (1).
//
// The resulting vector is of dimension 3
#let mul4x4-vec3(mat, vec, w: 1) = {
assert(vec.len() <= 4)
let out = (0, 0, 0)
for m in range(0, 3) {
let v = (mat.at(m).at(0) * vec.at(0, default: 0)
+ mat.at(m).at(1) * vec.at(1, default: 0)
+ mat.at(m).at(2) * vec.at(2, default: 0)
+ mat.at(m).at(3) * vec.at(3, default: w))
out.at(m) = v
}
return out
}

// Multiply matrix with vector
#let mul-vec(mat, vec) = {
if mat.len() != vector.dim(vec) {
Expand Down
6 changes: 2 additions & 4 deletions src/util.typ
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
/// - ..vecs (vectors): Vectors to get transformed. Only the positional part of the sink is used. A dictionary of vectors can be passed and all will be transformed.
/// -> vectors If multiple vectors are given they are returned as an array, if only one vector is given only one will be returned, if a dictionary is given they will be returned in the dictionary with the same keys.
#let apply-transform(transform, ..vecs) = {
let t = vec => matrix.mul-vec(
transform,
vector.as-vec(vec, init: (0, 0, 0, 1))
).slice(0, 3)
let t = vec => matrix.mul4x4-vec3(
transform, vec)
if type(vecs.pos().first()) == dictionary {
vecs = vecs.pos().first()
for (k, vec) in vecs {
Expand Down

0 comments on commit b737b98

Please sign in to comment.