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

[GUI] Support gui.arrow() and gui.arrows() #1625

Merged
merged 1 commit into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions docs/gui.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 25 additions & 1 deletion python/taichi/misc/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

Is this equivalent to transpose?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

swapaxes? Yes!

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]
Expand Down
2 changes: 1 addition & 1 deletion python/taichi/misc/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down