Skip to content

Commit

Permalink
Fix 3D mark drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Nov 10, 2023
1 parent 99b3a07 commit bb026a8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/drawable.typ
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@
})
let norm-dir = (-dir.at(1), dir.at(0), dir.at(2))

// Marks in 3D are rotated on the z axis depending on the style
if dir.at(2) != 0 {
norm-dir = vector.cross(dir, style.z-up)
}

// Generic positions
//
// t t - tip
Expand Down
28 changes: 14 additions & 14 deletions src/matrix.typ
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@

// Return 4x4 rotate z matrix
#let transform-rotate-z(angle) = {
// let (cos, sin) = (calc.cos, calc.sin)
((cos(angle), -sin(angle), 0, 0),
(sin(angle), cos(angle), 0, 0),
(0, 0, 1, 0),
Expand All @@ -83,26 +82,27 @@

// Return 4x4 rotate xz matrix
#let transform-rotate-xz(x, z) = {
// let (pi, cos, sin) = (calc.pi, calc.cos, calc.sin)
((cos(z), sin(z), 0, 0),
(-cos(x)*sin(z), cos(x)*cos(z), -sin(x), 0),
(sin(x)*sin(z), -sin(x)*cos(z), cos(x), 1),
(0, 0, 0, 1))
}

// Return 4x4 rotate xyz matrix

/// Return 4x4 rotation matrix
///
/// Calculates the product of the three rotation matrices
/// R = Rz(z) Ry(y) Rx(x)
///
/// - x (angle): Rotation about x
/// - y (angle): Rotation about y
/// - z (angle): Rotation about z
/// -> matrix
#let transform-rotate-xyz(x, y, z) = {
// let (pi, cos, sin) = (calc.pi, calc.cos, calc.sin)
((cos(x)*cos(y)*cos(z)-sin(x)*sin(z),
-cos(x)*cos(y)*sin(z)-sin(x)*cos(z),
cos(x)*sin(y), 0),
(sin(x)*cos(y)*cos(z)+cos(x)*sin(z),
-sin(x)*cos(y)*sin(z)+cos(x)*cos(z),
sin(x)*sin(y), 0),
(-sin(y)*cos(z),
sin(y)*sin(z),
cos(y), 0),
(0, 0, 0, 1))
((cos(y)*cos(z), sin(x)*sin(y)*cos(z) - cos(x)*sin(z), cos(x)*sin(y)*cos(z) + sin(x)*sin(z), 0),
(cos(y)*sin(z), sin(x)*sin(y)*sin(z) + cos(x)*cos(z), cos(x)*sin(y)*sin(z) - sin(x)*cos(z), 0),
(-sin(y), sin(x)*cos(y), cos(x)*cos(y), 0),
(0,0,0,1))
}

// Multiply matrix with matrix
Expand Down
5 changes: 5 additions & 0 deletions src/styles.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
// - inset Sets the inner length of triangular shaped marks
// - scale A factor that is applied to all of the three attributes above
// - sep Is the distance between multiple marks along their path
//
// If a mark is pointing to positive or negative z, the mark will be drawn
// with width on the axis perpendicular to its direction and the styles `z-up`
// vector.
#let _default-mark = (
scale: 1, // Scaling factor
length: .2, // Length
width: 0.15, // Width
inset: .05, // Arrow mark base inset
sep: .1, // Extra distance between marks
z-up: (0,1,0),// Z-Axis upwards vector
start: none, // Mark start symbol(s)
end: none, // Mark end symbol(s)
stroke: auto,
Expand Down
6 changes: 3 additions & 3 deletions src/vector.typ
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
/// Calculate cross product of two vectors of dim 3
#let cross(v1, v2) = {
assert(dim(v1) == 3 and dim(v2) == 3)
let x = v1.at(1) * v2.at(2) - v1.at(2) * v1.at(1)
let y = v1.at(2) * v2.at(0) - v1.at(0) * v1.at(2)
let z = v1.at(0) * v2.at(1) - v1.at(1) * v1.at(0)
let x = v1.at(1) * v2.at(2) - v1.at(2) * v2.at(1)
let y = v1.at(2) * v2.at(0) - v1.at(0) * v2.at(2)
let z = v1.at(0) * v2.at(1) - v1.at(1) * v2.at(0)
return (x, y, z)
}

Expand Down
4 changes: 2 additions & 2 deletions tests/mark/multiple/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
bezier((-1,-.5), (1,1), (0,-.5), (0,1),
mark: (start: l, end: l, fill: red, stroke: blue, flex: false))
bezier((-1,-1.5), (1,0), (0,-1.5), (0,0),
mark: (start: l, end: l, fill: red, stroke: blue, flex: false, scale: .05))
mark: (start: l, end: l, fill: red, stroke: blue, flex: false, scale: .5))
}))

#box(stroke: 2pt + red, canvas({
Expand All @@ -28,7 +28,7 @@
bezier((-1,-.5), (1,1), (0,-.5), (0,1),
mark: (start: l, end: l, fill: red, stroke: blue, flex: true))
bezier((-1,-1.5), (1,0), (0,-1.5), (0,0),
mark: (start: l, end: l, fill: red, stroke: blue, flex: true, scale: .05))
mark: (start: l, end: l, fill: red, stroke: blue, flex: true, scale: .5))
}))

#box(stroke: 2pt + red, canvas({
Expand Down
Binary file added tests/mark/z-axis/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/mark/z-axis/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#set page(width: auto, height: auto)
#import "/src/lib.typ": *

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

line((-1,0), (1,0), mark: (start: ">", end: ">"))
line((0,-1), (0,1), mark: (start: ">", end: ">"))
line((0,0,-1), (0,0,1), mark: (start: ">", end: ">",
scale: 1))
}))

0 comments on commit bb026a8

Please sign in to comment.