Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add origin to Transformation #4472

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Add origin to Transformation #4472

wants to merge 19 commits into from

Conversation

ffreyer
Copy link
Collaborator

@ffreyer ffreyer commented Oct 10, 2024

Description

Related: #4445, #2637

This adds an origin to Transformation so that rotate!() and scale!() can be relative to something other than Vec3(0). It can be set with translate_origin!() just like translate!().

Example:

p = rand(Point2f)
rect = Rect2f(p .- 0.1, Vec2f(0.2))

fig = Figure()
sl = Slider(fig[1, 1], range = range(0, 2pi, length=101))
ax = Axis(fig[2, 1], aspect = DataAspect())
p1 = poly!(ax, rect, color = :orange)
p2 = lines!(ax, rect, color = :red)
xlims!(ax, -1.5, 1.5)
ylims!(ax, -1.1, 1.1)

on(sl.value) do angle
    center = to_ndim(Vec3f, p, 0)

    R = Makie.rotationmatrix_z(angle)
    T = Makie.translationmatrix(-center)
    Tinv = Makie.translationmatrix(center)
    p1.transformation.model[] = Tinv * R * T # Did we break setting plot.model?
    
    translate_origin!(p2, p)
    Makie.rotate!(p2, angle)
end

fig

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • Added an entry in CHANGELOG.md (for new features and breaking changes)
  • Added or changed relevant sections in the documentation
  • Added unit tests for new algorithms, conversion methods, etc.
  • Added reference image tests for new plotting functions, recipes, visual options, etc.

@MakieBot
Copy link
Collaborator

MakieBot commented Oct 10, 2024

Compile Times benchmark

Note, that these numbers may fluctuate on the CI servers, so take them with a grain of salt. All benchmark results are based on the mean time and negative percent mean faster than the base branch. Note, that GLMakie + WGLMakie run on an emulated GPU, so the runtime benchmark is much slower. Results are from running:

using_time = @ctime using Backend
# Compile time
create_time = @ctime fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @ctime Makie.colorbuffer(display(fig))
# Runtime
create_time = @benchmark fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @benchmark Makie.colorbuffer(fig)
using create display create display
GLMakie 6.30s (6.17, 6.55) 0.11+- 167.76ms (158.57, 192.97) 8.54+- 0.62s (0.46, 3.21) 0.61+- 6.89ms (6.72, 7.24) 0.15+- 25.92ms (25.42, 26.62) 0.38+-
master 6.44s (6.24, 6.73) 0.13+- 165.96ms (159.02, 185.12) 6.00+- 477.39ms (466.39, 498.81) 9.55+- 7.03ms (6.82, 7.45) 0.15+- 25.89ms (25.49, 26.44) 0.29+-
evaluation 1.02x faster ✓, -0.14s (-1.15d, 0.00p, 0.12std) 0.99x invariant, 1.79ms (0.24d, 0.45p, 7.27std) 0.77x noisy🤷‍♀️, 0.14s (0.32d, 0.32p, 0.31std) 1.02x faster ✓, -0.13ms (-0.85d, 0.01p, 0.15std) 1.00x invariant, 0.03ms (0.08d, 0.79p, 0.34std)
CairoMakie 6.16s (5.95, 6.31) 0.10+- 170.76ms (160.74, 188.10) 7.45+- 207.84ms (199.06, 215.99) 5.59+- 6.95ms (6.63, 7.25) 0.19+- 1.10ms (1.07, 1.14) 0.02+-
master 6.15s (5.98, 6.34) 0.10+- 170.99ms (161.87, 190.02) 7.27+- 206.84ms (197.27, 216.32) 5.87+- 7.06ms (6.68, 7.74) 0.29+- 1.09ms (1.05, 1.17) 0.02+-
evaluation 1.00x invariant, 0.01s (0.13d, 0.69p, 0.10std) 1.00x invariant, -0.23ms (-0.03d, 0.92p, 7.36std) 1.00x invariant, 1.0ms (0.17d, 0.59p, 5.73std) 1.02x invariant, -0.12ms (-0.47d, 0.15p, 0.24std) 0.99x invariant, 0.01ms (0.45d, 0.17p, 0.02std)
WGLMakie 6.88s (6.60, 7.27) 0.19+- 176.39ms (157.72, 206.78) 11.77+- 5.82s (5.54, 6.30) 0.20+- 10.94ms (10.30, 11.91) 0.51+- 148.91ms (146.43, 159.51) 2.80+-
master 6.88s (6.57, 7.17) 0.18+- 174.29ms (162.51, 196.61) 9.55+- 6.11s (5.81, 6.47) 0.20+- 10.86ms (10.34, 11.84) 0.43+- 148.81ms (145.41, 153.52) 1.66+-
evaluation 1.00x invariant, -0.0s (-0.02d, 0.95p, 0.18std) 0.99x invariant, 2.1ms (0.20d, 0.54p, 10.66std) 1.05x faster ✓, -0.28s (-1.41d, 0.00p, 0.20std) 0.99x invariant, 0.08ms (0.17d, 0.59p, 0.47std) 1.00x invariant, 0.1ms (0.04d, 0.89p, 2.23std)

@ffreyer ffreyer marked this pull request as ready for review October 12, 2024 22:47
@ffreyer ffreyer mentioned this pull request Oct 14, 2024
5 tasks
@bjarthur
Copy link
Contributor

looking forward to this!

one other suggestion in addition to the typos: i find the name translate_origin! a little confusing as the plot isn't actually getting translated. how about set_origin! instead? or maybe just origin!? or, maybe rotate! and scale! could take an origin kwarg?

@jkrumbiegel
Copy link
Member

maybe rotate! and scale! could take an origin kwarg?

I would prefer that I think, or maybe around so it's rotate!(x, ..., around = p)? Or relative_to

@ffreyer
Copy link
Collaborator Author

ffreyer commented Oct 15, 2024

Having origin as an argument in rotate!() and scale!() was also my first thought but I think with how transformations work it's better to pull it out. Transformations don't consider the order you call things in. You're effectively setting or changing the state with translate!(), rotate!() etc. To me rotate!(..., origin = ...) makes it seem like you're setting the origin for this specific call, while you would actually set it for the transformation as a whole. I think a separate function for origin reflects that better.

I'd be fine with renaming the function. I also had set_origin!() originally but switched when I implemented the Accum/Absolute interface since set sounds like just overwriting (no origin = origin + arg which Accum does)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Ready to review
Development

Successfully merging this pull request may close these issues.

4 participants