diff --git a/src/draw.typ b/src/draw.typ index 5194c4f61..1e46b9af9 100644 --- a/src/draw.typ +++ b/src/draw.typ @@ -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 diff --git a/src/draw/transformations.typ b/src/draw/transformations.typ index c0c42c0aa..61cf89d3b 100644 --- a/src/draw/transformations.typ +++ b/src/draw/transformations.typ @@ -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) },) } @@ -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 => { diff --git a/src/matrix.typ b/src/matrix.typ index 18d6e6e63..e36296346 100644 --- a/src/matrix.typ +++ b/src/matrix.typ @@ -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) diff --git a/tests/rotation/test.typ b/tests/rotation/test.typ index c426cb8c7..126c265d4 100644 --- a/tests/rotation/test.typ +++ b/tests/rotation/test.typ @@ -34,21 +34,21 @@ #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() })) @@ -56,21 +56,21 @@ #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() }))