-
Notifications
You must be signed in to change notification settings - Fork 70
Added Function of Generating Animation of DFA and NFA Reading Strings #252
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
Open
dsr20030703
wants to merge
11
commits into
caleb531:develop
Choose a base branch
from
dsr20030703:animation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
34b3b74
Added manim dependencies.
dsr20030703 bed6730
Add animation for DFA and NFA reading strings.
dsr20030703 904874b
Made classes for generating animations of automata private.
dsr20030703 6eae6a5
Revised the code according to the review.
dsr20030703 3df4fb8
Optimized checking visualization imports.
dsr20030703 aaabdb8
Added a step to build dependencies for manim.
dsr20030703 cef9444
Fixed bug that the last character will remain highlighted when the an…
dsr20030703 0502b30
Optimized animations.
dsr20030703 16d2825
Fixed type annotations.
dsr20030703 509b1f4
Revised the code according to the review.
dsr20030703 fd15fd9
Not using match clause to keep supporting Python 3.9.
dsr20030703 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,7 @@ nosetests.xml | |
|
||
# MyPy | ||
.mypy_cache | ||
|
||
# manim generated files | ||
media/ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Iterable | ||
from functools import partial | ||
from typing import TypeVar | ||
|
||
import manim | ||
import pygraphviz as pgv | ||
|
||
_POINTS_IN_INCH = 72 | ||
|
||
|
||
class Animate: | ||
"""The `Animate` class is the config class to set the behavior of animations.""" | ||
|
||
DEFAULT_COLOR = manim.WHITE | ||
HIGHLIGHT_COLOR = manim.RED | ||
M = TypeVar("M", bound=manim.Mobject) | ||
|
||
@classmethod | ||
def default_init(cls, mobject_class: type[M]) -> type[M]: | ||
return partial(mobject_class, color=cls.DEFAULT_COLOR) # type: ignore | ||
|
||
@classmethod | ||
def to_default_color(cls, mobject: manim.VMobject) -> manim.ApplyMethod: | ||
""" | ||
Parameters | ||
---------- | ||
mobject : VMobject | ||
The mobject to change to `DEFAULT_COLOR`, or cancel the highlight. | ||
|
||
Returns | ||
------- | ||
ApplyMethod | ||
The animation for the `Scene` object to `play` to change the mobject to | ||
`DEFAULT_COLOR`. | ||
""" | ||
return manim.FadeToColor(mobject, cls.DEFAULT_COLOR) | ||
|
||
@classmethod | ||
def highlight(cls, mobject: manim.VMobject) -> manim.ApplyMethod: | ||
""" | ||
Parameters | ||
---------- | ||
mobject : VMobject | ||
The mobject to set the `HIGHLIGHT_COLOR`. | ||
|
||
Returns | ||
------- | ||
ApplyMethod | ||
The animation for the `Scene` object to `play` to highlight the mobject. | ||
""" | ||
return manim.FadeToColor(mobject, cls.HIGHLIGHT_COLOR) | ||
|
||
|
||
class _ManimNode(manim.VGroup): | ||
""" | ||
The `ManimNode` class represents a `Node` object in the `AGraph` object with manim. | ||
|
||
The `ManimNode` class is a `VGroup`. For the convenience, it's also added with: | ||
|
||
Parameters | ||
---------- | ||
shape : Dot | Circle | VGroup | ||
- Dot for the nullnode (the start of the edge pointing the initial node) | ||
- Circle for non-final state | ||
- VGroup of a doublecircle for final state | ||
label: Optional[Text] | ||
The name of the state (N/A for the nullnode) | ||
""" | ||
|
||
shape: manim.Dot | manim.Circle | manim.VGroup | ||
label: manim.Text | None | ||
|
||
def __init__(self, node: pgv.Node) -> None: | ||
""" | ||
Parameters | ||
---------- | ||
node : `pygraphviz.Node` | ||
the node which to based on to construct a FA state. | ||
|
||
`node`'s label is its `attr['label']` if given, otherwise its | ||
`name` property. Its `attr` contains: | ||
- 'fontsize': %f, | ||
- 'height': %f, | ||
- 'pos': '%f,%f', | ||
- 'shape': 'point' or 'circle' or 'doublecircle', | ||
- 'width': %f (possibly equals to 'height') | ||
""" | ||
super().__init__(name=node.name) | ||
radius = float(node.attr["height"]) / 2 | ||
if node.attr["shape"] == "point": | ||
self.shape = Animate.default_init(manim.Dot)(radius=radius) | ||
self.add(self.shape) | ||
elif node.attr["shape"].endswith("circle"): | ||
circle = Animate.default_init(manim.Circle)(radius=radius) | ||
self.shape = ( | ||
manim.VGroup( | ||
circle, | ||
Animate.default_init(manim.Circle)().surround( | ||
circle, buffer_factor=0.8 | ||
), | ||
) | ||
if node.attr["shape"].startswith("double") | ||
else circle | ||
) | ||
self.add(self.shape) | ||
self.label = Animate.default_init(manim.Text)( | ||
node.name, font_size=float(node.attr["fontsize"]) | ||
) | ||
self.add(self.label) | ||
else: | ||
raise ValueError( | ||
f"Invalid node shape: {node.attr['shape']}. " | ||
"Only 'point', 'circle' and 'doublecircle' are supported." | ||
) | ||
x, y = (float(pt) / _POINTS_IN_INCH for pt in node.attr["pos"].split(",")) | ||
self.set_x(x) | ||
self.set_y(y) | ||
|
||
|
||
class _ManimEdge(manim.VGroup): | ||
""" | ||
The `ManimEdge` class represents an `Edge` object in the `AGraph` object with manim. | ||
|
||
The `ManimEdge` class is a `VGroup`. For the convenience, it's also added with: | ||
|
||
Parameters | ||
---------- | ||
edge : VGroup | ||
The curved arrow made with a series of `CubricBezier` curves objects and an | ||
`Arrow` object. | ||
label : Optional[Text] | ||
The label on the edge, which is the symbol of the transition. | ||
""" | ||
|
||
edge: manim.VGroup | ||
label: manim.Text | None | ||
|
||
def __init__(self, edge: pgv.Edge) -> None: | ||
r""" | ||
Parameters | ||
---------- | ||
edge : `pygraphviz.Edge` | ||
which to based on to construct a FA transition.<br> | ||
`edge.attr` may contain: | ||
- 'arrowsize': %f | ||
- 'fontsize': %f (not exists when 'label' not exists) | ||
- 'label': str (may not exists) | ||
- 'lp': '%f,%f' | ||
- 'pos': 'e,%f,%f(\s+%f,%f)*' | ||
""" | ||
super().__init__() | ||
self.edge = self.__parse_spline(edge.attr["pos"].replace("\\\r", "")) | ||
self.add(self.edge) | ||
if label_text := edge.attr["label"]: | ||
self.label = Animate.default_init(manim.Text)( | ||
label_text, font_size=float(edge.attr["fontsize"]) | ||
) | ||
x, y = (float(pt) / _POINTS_IN_INCH for pt in edge.attr["lp"].split(",")) | ||
self.label.set_x(x) | ||
self.label.set_y(y) | ||
self.add(self.label) | ||
|
||
@staticmethod | ||
def __parse_spline(edge_pos: str) -> manim.VGroup: | ||
""" | ||
Convert the pos attribute of the edge which is a string of spline pattern. | ||
|
||
Parameters | ||
---------- | ||
edge_pos : str | ||
a str of spline pattern: | ||
- spline = endp point (triple)+ | ||
- point = "%f,%f" | ||
- endp = "e,"point | ||
- triple = point point point | ||
""" | ||
points = edge_pos.split() | ||
control_points = tuple( | ||
(*(float(pt) / _POINTS_IN_INCH for pt in point.split(",")), 0) | ||
for point in points[1:] | ||
) | ||
result = manim.VGroup() | ||
for i in range(0, len(control_points) - 1, 3): | ||
result.add( | ||
Animate.default_init(manim.CubicBezier)(*control_points[i : i + 4]) | ||
) | ||
endp = (*(float(pt) / _POINTS_IN_INCH for pt in points[0].split(",")[1:]), 0) | ||
result.add( | ||
Animate.default_init(manim.Arrow)( | ||
start=control_points[-1], | ||
end=endp, | ||
max_tip_length_to_length_ratio=1, | ||
) | ||
) | ||
return result | ||
|
||
|
||
class _ManimInput(manim.VGroup): | ||
"""The `ManimInput` class represents the input string. The characters of the input | ||
string are separated with each character is generated to a `Text` object, so that | ||
you can get each character simply with `[]` operator.""" | ||
|
||
def __init__(self, text: str) -> None: | ||
""" | ||
Generate the input symbols and put them on the top left of the screen. | ||
|
||
Parameters | ||
---------- | ||
text : str | ||
The input string. | ||
""" | ||
super().__init__(*map(manim.Text, text)) | ||
self.arrange().align_on_border(manim.UL) | ||
|
||
def change_symbol(self, current_index: int) -> Iterable[manim.ApplyMethod]: | ||
""" | ||
Turn the prior symbol (if there is) to default color and highlight the current | ||
symbol. | ||
|
||
Parameters | ||
---------- | ||
current_index : int | ||
The index of the current symbol which will be highlighted. | ||
|
||
Returns | ||
------- | ||
Iterable[ApplyMethod] | ||
The animations for the `Scene` object to `play`. | ||
""" | ||
if current_index >= 0: | ||
yield Animate.highlight(self[current_index]) | ||
|
||
def show_result(self, accept: bool) -> manim.Write: | ||
""" | ||
Add the result ('→' with an 'accept'/'reject') next to the end of the string. | ||
|
||
Parameters | ||
---------- | ||
accept : bool | ||
The result if the input string is accepted. | ||
|
||
Returns | ||
------- | ||
Write | ||
The animation of writing the result for the `Scene` object to `play`. | ||
""" | ||
result = Animate.default_init(manim.Text)( | ||
f"→ {'accept' if accept else 'reject'}" | ||
) | ||
if len(self): | ||
result.next_to(self) | ||
else: | ||
result.align_on_border(manim.UL) | ||
return manim.Write(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.