From c79a2b40948d8cf671c50ea68e293be31d900cc6 Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Sat, 1 Aug 2020 11:35:47 +0800 Subject: [PATCH] [GUI] Support gui.arrow() and gui.arrows() --- docs/gui.rst | 6 +++--- python/taichi/misc/gui.py | 26 +++++++++++++++++++++++++- python/taichi/misc/image.py | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/docs/gui.rst b/docs/gui.rst index e2f09c4bdc521..5b12412d51d68 100644 --- a/docs/gui.rst +++ b/docs/gui.rst @@ -10,10 +10,10 @@ Create a window --------------- -.. function:: ti.GUI(title, res, bgcolor = 0x000000) +.. function:: ti.GUI(title = 'Taichi', res = (512, 512), bgcolor = 0x000000) - :parameter title: (string) the window title - :parameter res: (scalar or tuple) resolution / size of the window + :parameter title: (optional, string) the window title + :parameter res: (optional, scalar or tuple) resolution / size of the window :parameter bgcolor: (optional, RGB hex) background color of the window :return: (GUI) an object represents the window diff --git a/python/taichi/misc/gui.py b/python/taichi/misc/gui.py index 6bcbfe72f876a..52251849066e7 100644 --- a/python/taichi/misc/gui.py +++ b/python/taichi/misc/gui.py @@ -34,7 +34,7 @@ class Event: PRESS = ti_core.KeyEvent.EType.Press RELEASE = ti_core.KeyEvent.EType.Release - def __init__(self, name, res=512, background_color=0x0): + def __init__(self, name='Taichi', res=512, background_color=0x0): self.name = name if isinstance(res, numbers.Number): res = (res, res) @@ -286,6 +286,30 @@ def line(self, begin, end, radius=1, color=0xFFFFFF): self.canvas.path_single(begin[0], begin[1], end[0], end[1], color, radius) + @staticmethod + def _arrow_to_lines(orig, major, tip_scale=0.2, angle=45): + import math + angle = math.radians(180 - angle) + c, s = math.cos(angle), math.sin(angle) + minor1 = np.array([major[:, 0] * c - major[:, 1] * s, + major[:, 0] * s + major[:, 1] * c]).swapaxes(0, 1) + minor2 = np.array([major[:, 0] * c + major[:, 1] * s, + -major[:, 0] * s + major[:, 1] * c]).swapaxes(0, 1) + end = orig + major + return [(orig, end), + (end, end + minor1 * tip_scale), + (end, end + minor2 * tip_scale)] + + def arrows(self, orig, dir, radius=1, color=0xffffff, **kwargs): + for begin, end in self._arrow_to_lines(orig, dir, **kwargs): + self.lines(begin, end, radius, color) + + def arrow(self, orig, dir, radius=1, color=0xffffff, **kwargs): + orig = np.array([orig]) + dir = np.array([dir]) + for begin, end in self._arrow_to_lines(orig, dir, **kwargs): + self.line(begin[0], end[0], radius, color) + def rect(self, topleft, bottomright, radius=1, color=0xFFFFFF): a = topleft[0], topleft[1] b = bottomright[0], topleft[1] diff --git a/python/taichi/misc/image.py b/python/taichi/misc/image.py index 524c6648aaf15..b3bdbf6e03584 100644 --- a/python/taichi/misc/image.py +++ b/python/taichi/misc/image.py @@ -71,7 +71,7 @@ def imread(filename, channels=0): return img.swapaxes(0, 1)[:, ::-1, :] -def imshow(img, window_name='Taichi'): +def imshow(img, window_name='imshow'): """ Show image in a Taichi GUI. """