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

[misc] [refactor] 'ti.vec' and 'ti.veci' is deprecated, directly use a tuple in GUI system instead #1605

Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions docs/gui.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ Paint on a window
Draw a line of text on screen.


.. function:: ti.rgb_to_hex(rgb):

:parameter rgb: (tuple of 3 floats) The (R, G, B) float values, in range [0, 1)
archibate marked this conversation as resolved.
Show resolved Hide resolved
:return: (RGB hex or np.array of uint32) The translated hex value
archibate marked this conversation as resolved.
Show resolved Hide resolved

Convert a (R, G, B) tuple of float into a single hex value, e.g.:
archibate marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python

rgb = (0.4, 0.8, 1.0)
hex = ti.rgb_to_hex(rgb) # 0x66ccff

rgb = np.array([[0.4, 0.8, 1.0], [0.0, 0.5, 1.0]])
hex = ti.rgb_to_hex(rgb) # np.array([0x66ccff, 0x007fff])

This is helpful since the return value could be used in other paint APIs.
archibate marked this conversation as resolved.
Show resolved Hide resolved


.. _gui_event:

Event processing
Expand Down
6 changes: 3 additions & 3 deletions examples/pbf2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ def render(gui):
for j in range(dim):
pos[j] *= screen_to_world_ratio / screen_res[j]
gui.circles(pos_np, radius=particle_radius, color=particle_color)
canvas.rect(ti.vec(
0, 0), ti.vec(board_states[None][0] / boundary[0],
1.0)).radius(1.5).color(boundary_color).close().finish()
gui.rect((0, 0), (board_states[None][0] / boundary[0], 1.0),
radius=1.5,
color=boundary_color)
gui.show()


Expand Down
17 changes: 11 additions & 6 deletions python/taichi/misc/gui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numbers
import numpy as np
from taichi.core import ti_core
from .util import deprecated
from .util import deprecated, core_veci


class GUI:
Expand Down Expand Up @@ -35,14 +35,13 @@ class Event:
RELEASE = ti_core.KeyEvent.EType.Release

def __init__(self, name, res=512, background_color=0x0):
import taichi as ti
self.name = name
if isinstance(res, numbers.Number):
res = (res, res)
self.res = res
# The GUI canvas uses RGBA for storage, therefore we need NxMx4 for an image.
self.img = np.ascontiguousarray(np.zeros(self.res + (4, ), np.float32))
self.core = ti.core.GUI(name, ti.veci(*res))
self.core = ti_core.GUI(name, core_veci(*res))
self.canvas = self.core.get_canvas()
self.background_color = background_color
self.key_pressed = set()
Expand Down Expand Up @@ -258,9 +257,9 @@ def text(self, content, pos, font_size=15, color=0xFFFFFF):
import taichi as ti
# TODO: refactor Canvas::text
font_size = float(font_size)
pos = ti.vec(*pos)
r, g, b = (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff
color = ti.vec(r / 255, g / 255, b / 255, 1)
pos = ti.core_vec(*pos)
r, g, b = hex_to_rgb(color)
color = ti.core_vec(r, g, b, 1)
self.canvas.text(content, pos, font_size, color)

def show(self, file=None):
Expand Down Expand Up @@ -377,7 +376,13 @@ def rgb_to_hex(c):
return 65536 * to255(c[0]) + 256 * to255(c[1]) + to255(c[2])


def hex_to_rgb(c):
r, g, b = (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff
return r / 255, g / 255, b / 255


__all__ = [
'GUI',
'rgb_to_hex',
'hex_to_rgb',
]
16 changes: 14 additions & 2 deletions python/taichi/misc/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def config_from_dict(args):
return ti_core.config_from_dict(d)


def veci(*args):
def core_veci(*args):
from taichi.core import ti_core
if isinstance(args[0], ti_core.Vector2i):
return args[0]
Expand All @@ -34,7 +34,7 @@ def veci(*args):
assert False, type(args[0])


def vec(*args):
def core_vec(*args):
from taichi.core import ti_core
if isinstance(args[0], ti_core.Vector2f):
return args[0]
Expand Down Expand Up @@ -200,9 +200,21 @@ def print_profile_info():
taichi.ti_core.print_profile_info()


@deprecated('ti.vec(x, y)', 'a tuple (x, y) for GUI system')
def vec(*args, **kwargs):
return core_vec(*args, **kwargs)


@deprecated('ti.veci(x, y)', 'a tuple (x, y) for GUI system')
def veci(*args, **kwargs):
return core_veci(*args, **kwargs)


__all__ = [
'vec',
'veci',
'core_vec',
'core_veci',
'set_gdb_trigger',
'print_profile_info',
'set_logging_level',
Expand Down