Skip to content

Commit

Permalink
tranformations: Review updates
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Nov 19, 2023
1 parent 62e7692 commit 48ce40b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/draw.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "draw/grouping.typ": intersections, group, anchor, copy-anchors, place-anchors, set-ctx, get-ctx, for-each-anchor, on-layer, place-marks
#import "draw/transformations.typ": transform, rotate, translate, scale, set-origin, move-to, set-viewport
#import "draw/transformations.typ": set-transform, rotate, translate, scale, set-origin, move-to, set-viewport
#import "draw/styling.typ": set-style, fill, stroke
#import "draw/shapes.typ": circle, circle-through, arc, mark, line, grid, content, rect, bezier, bezier-through, catmull, hobby, merge-path, shadow
37 changes: 18 additions & 19 deletions src/draw/transformations.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
/// - mat (none,matrix): The 4x4 transformation matrix to set. If `none` is
/// passed, the transformation matrix is set to the identity matrix (
/// `matrix.ident()`).
#let transform(mat) = {
#let set-transform(mat) = {
let mat = if mat == none {
matrix.ident()
} else {
mat
}

assert(type(mat) == array,
message: "Transformtion matrix must be of type array. Got " + repr(mat))
assert.eq(mat.len(), 4,
message: "Transformation matrix must be of size 4x4")

(ctx => {
assert(mat == none or type(mat) == array,
message: "Transformtion matrix must be none (ident) or of type array")
ctx.transform = if mat != none {
assert.eq(mat.len(), 4,
message: "Transformation matrix must be of size 4x4")
mat
} else {
matrix.ident()
}
ctx.transform = mat
return (ctx: ctx)
},)
}
Expand All @@ -32,22 +35,18 @@

let mat = if angles.pos().len() == 1 {
matrix.transform-rotate-z(angles.pos().at(0))
} else if names.any(n => n in ("x", "y", "z")) {
assert(names.all(n => n in ("x", "y", "z")),
message: "All rotate arguments must be axis names: x, y or z")

} else if names.all(n => n in ("x", "y", "z")) {
matrix.transform-rotate-xyz(named.at("x", default: 0deg),
named.at("y", default: 0deg),
named.at("z", default: 0deg))
} else if names.any(n => n in ("yaw", "pitch", "roll")) {
assert(names.all(n => n in ("yaw", "pitch", "roll")),
message: "All rotate arguments must be: yaw, pitch or roll")

} else if names.all(n => n in ("yaw", "pitch", "roll")) {
matrix.transform-rotate-ypr(named.at("yaw", default: 0deg),
named.at("pitch", default: 0deg),
named.at("roll", default: 0deg))
} else {
panic("Invalid rotate arguments")
panic("Invalid rotate arguments." +
"Rotate expects: A single (z-axis) angle or any combination of x, y,z or any combination of yaw, pitch, roll. " +
"Got: " + repr(named))
}

(ctx => {
Expand Down
2 changes: 1 addition & 1 deletion src/matrix.typ
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
(0, 0, 0, 1))
}

/// Return 4x4 rotation matrix - yaw-pith-roll
/// Return 4x4 rotation matrix - yaw-pitch-roll
///
/// Calculates the product of the three rotation matrices
/// R = Rz(a) Ry(b) Rx(c)
Expand Down
12 changes: 6 additions & 6 deletions tests/rotation/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,43 @@
#box(stroke: 2pt + red, canvas({
import draw: *

transform(none)
set-transform(none)
rotate(z: 45deg)
draw-xyz()
}))
#box(stroke: 2pt + red, canvas({
import draw: *

transform(none)
set-transform(none)
rotate(x: 45deg)
draw-xyz()
}))
#box(stroke: 2pt + red, canvas({
import draw: *

transform(none)
set-transform(none)
rotate(y: 45deg)
draw-xyz()
}))

#box(stroke: 2pt + red, canvas({
import draw: *

transform(none)
set-transform(none)
rotate(yaw: 45deg)
draw-xyz()
}))
#box(stroke: 2pt + red, canvas({
import draw: *

transform(none)
set-transform(none)
rotate(pitch: 45deg)
draw-xyz()
}))
#box(stroke: 2pt + red, canvas({
import draw: *

transform(none)
set-transform(none)
rotate(roll: 45deg)
draw-xyz()
}))

0 comments on commit 48ce40b

Please sign in to comment.