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

[GSoC'21] Different morphing modes #374

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Javis.jl - Changelog

## Unreleased
- Add a manim-like :long morhping mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be updated here as well


## v0.5.3 (26th of July 2021)
- Allow all kinds of iterable ways in the `act!` function such that `act!(::Matrix, ::Action)` also works
- Updated `anim_translate`
Expand Down
7 changes: 6 additions & 1 deletion src/Shape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function print_basic(s::Shape)
end


function prepare_to_interpolate(from_shape, to_shape)
function prepare_to_interpolate(from_shape, to_shape, style)
Sov-trotter marked this conversation as resolved.
Show resolved Hide resolved
# match number of points for outer polygon
from_outer, to_outer = match_num_points(from_shape.points, to_shape.points)

Expand All @@ -239,6 +239,11 @@ function prepare_to_interpolate(from_shape, to_shape)
rotate_i, _ = compute_shortest_morphing_dist(from_outer, to_outer)
new_from_outer = circshift(from_outer, -rotate_i + 1)

if style == :long
new_from_outer =
circshift(reverse!(new_from_outer), floor(-length(new_from_outer) / 2.6)) # found this to work best
end

new_from_holes = Vector{Vector{Point}}()

# rotate holes
Expand Down
31 changes: 22 additions & 9 deletions src/morphs.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include("Shape.jl")

"""
morph_to(to_func::Function; object=:stroke)
morph_to(to_func::Function; style=:short, object=:stroke)

A closure for the [`_morph_to`](@ref) function.
This makes it easier to write the function inside an `Object`.
Expand All @@ -11,6 +11,7 @@ It especially does not work with functions which produce more than one polygon
or which produce filled polygons.
Blending between fills of polygons is definitely coming at a later stage.

To get a more visually appealing morphing one can try setting `style = :long`.
**Important:** The functions itself should not draw the polygon
i.e. use `circle(Point(100,100), 50)` instead of `circle(Point(100,100), 50, :stroke)`

Expand All @@ -19,6 +20,7 @@ i.e. use `circle(Point(100,100), 50)` instead of `circle(Point(100,100), 50, :st
which will be displayed at the end of the Object

# Keywords
- `style::Symbol` Sets the style of morphing
- `do_action::Symbol` defines whether the object has a fill or just a stroke. Defaults to `:stroke`.

# Example
Expand All @@ -37,13 +39,20 @@ circle_obj = Object(11:20, acirc)
act!(circle_obj, Action(:same, morph_to(astar)))
```
"""
function morph_to(to_func::Function; do_action = :stroke)
return (video, object, action, frame) ->
_morph_to(video, object, action, frame, to_func; do_action = do_action)
function morph_to(to_func::Function; style = :short, do_action = :stroke)
return (video, object, action, frame) -> _morph_to(
video,
object,
action,
frame,
to_func;
style = style,
do_action = do_action,
)
end

"""
_morph_to(video::Video, object::Object, action::Action, frame, to_func::Function; do_action=:stroke)
_morph_to(video::Video, object::Object, action::Action, frame, to_func::Function; style=:short, do_action=:stroke)

Internal version of [`morph_to`](@ref) but described there.
"""
Expand All @@ -53,6 +62,7 @@ function _morph_to(
action::Action,
frame,
to_func::Function;
style = :short,
do_action = :stroke,
)
if frame == last(get_frames(action))
Expand All @@ -78,6 +88,7 @@ function _morph_to(
frame,
from_polys,
to_polys;
style = style,
do_action = do_action,
)
end
Expand All @@ -86,7 +97,7 @@ end
"""
morph_between(video::Video, action::Action, frame,
from_polys::Vector{Vector{Point}}, to_polys::Vector{Vector{Point}};
do_action=:stroke)
style=:short, do_action=:stroke)

Internal version of [`morph_to`](@ref) after the from poly is defined.
"""
Expand All @@ -96,14 +107,15 @@ function morph_between(
frame,
from_polys::Vector{Vector{Point}},
to_polys::Vector{Vector{Point}};
style = :short,
do_action = :stroke,
)
cs = get_current_setting()
cs.show_object = false

# computation of the polygons and the best way to morph in the first frame
if frame == first(get_frames(action))
save_morph_polygons!(action, from_polys, to_polys)
save_morph_polygons!(action, from_polys, to_polys, style)
end

# obtain the computed polygons. These polygons have the same number of points.
Expand Down Expand Up @@ -227,7 +239,7 @@ end

"""
save_morph_polygons!(action::Action, from_func::Vector{Vector{Point}},
to_func::Vector{Vector{Point}})
to_func::Vector{Vector{Point}}, style::Symbol)

Calls the functions to polygons and calls [`match_num_points`](@ref)
such that both polygons have the same number of points.
Expand All @@ -240,6 +252,7 @@ function save_morph_polygons!(
action::Action,
from_polys::Vector{Vector{Point}},
to_polys::Vector{Vector{Point}},
style::Symbol,
)
# delete polygons with less than 2 points
for i in length(from_polys):-1:1
Expand Down Expand Up @@ -291,7 +304,7 @@ function save_morph_polygons!(
)
end
else
from_shape, to_shape = prepare_to_interpolate(from_shape, to_shape)
from_shape, to_shape = prepare_to_interpolate(from_shape, to_shape, style)

push!(action.defs[:from_shape], from_shape)
push!(action.defs[:to_shape], to_shape)
Expand Down
28 changes: 28 additions & 0 deletions test/morphing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ end
end
end


@testset "Morphing mutates the object" begin
function ground(args...)
background("black")
Expand Down Expand Up @@ -215,3 +216,30 @@ end
rm("images/$(lpad(i, 10, "0")).png")
end
end

@testset "Morphing modes" begin
function ground(args...)
background("black")
sethue("white")
end

astar(args...; do_action = :stroke) = star(O, 50, 5, 0.5, 0, do_action)
abox(args...; do_action = :stroke) = rect(-150, -150, 100, 100, do_action)
acirc(args...; do_action = :stroke) = circle(Point(0, 0), 50, do_action)

video = Video(500, 500)
back = Background(1:50, ground)
star_obj = Object(1:50, astar)
box_obj = Object(1:50, abox)
act!(star_obj, Action(5:20, morph_to(acirc, style = :long)))
act!(box_obj, Action(30:50, morph_to(astar, style = :long)))
render(video; tempdirectory = "images", pathname = "")

for i in [5, 10, 15, 19, 20, 21, 30, 35, 40, 45, 49, 50]
@test_reference "refs/morph_modes$i.png" load("images/$(lpad(i, 10, "0")).png")
end

for i in 1:50
rm("images/$(lpad(i, 10, "0")).png")
end
end
Binary file added test/refs/morph_modes10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes30.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes35.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes40.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes45.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes49.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/refs/morph_modes50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.