From cf87d395501d0998ee36853755de4455d2d502e2 Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Sat, 1 Aug 2020 10:46:43 +0800 Subject: [PATCH 1/5] [GUI] Support RG images in GUI.set_image --- docs/gui.rst | 24 +++++++++++++++--------- python/taichi/lang/meta.py | 2 ++ python/taichi/misc/gui.py | 15 +++++++++------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/docs/gui.rst b/docs/gui.rst index e2f09c4bdc521..83debefa79d6e 100644 --- a/docs/gui.rst +++ b/docs/gui.rst @@ -57,8 +57,7 @@ Paint on a window Set an image to display on the window. - The image pixels are set from the values of ``img[i, j]``, where ``i`` indicates the horizontal - coordinates (from left to right) and ``j`` the vertical coordinates (from bottom to top). + The image pixels are set from the values of ``img[i, j]``, where ``i`` indicates the horizontal coordinates (from left to right) and ``j`` the vertical coordinates (from bottom to top). If the window size is ``(x, y)``, then ``img`` must be one of: @@ -67,12 +66,18 @@ Paint on a window * ``ti.var(shape=(x, y, 3))``, where `3` is for ``(r, g, b)`` channels - * ``ti.Vector(3, shape=(x, y))`` (see :ref:`vector`) + * ``ti.var(shape=(x, y, 2))``, where `2` is for ``(r, g)`` channels + + * ``ti.Vector(3, shape=(x, y))`` ``(r, g, b)`` channels on each component (see :ref:`vector`) + + * ``ti.Vector(2, shape=(x, y))`` ``(r, g)`` channels on each component * ``np.ndarray(shape=(x, y))`` * ``np.ndarray(shape=(x, y, 3))`` + * ``np.ndarray(shape=(x, y, 2))`` + The data type of ``img`` must be one of: @@ -92,6 +97,13 @@ Paint on a window ``img`` entries will be clipped into range ``[0, 1]`` for display. +.. function:: gui.get_image() + + :return: (np.array) the current image shown on the GUI + + Get the RGBA shown image from the current GUI system which has four channels. + + .. function:: gui.circle(pos, color = 0xFFFFFF, radius = 1) :parameter gui: (GUI) the window object @@ -416,12 +428,6 @@ could make variable control more intuitive: Image I/O --------- -.. function:: gui.get_image() - - :return: a ``np.ndarray`` which is the current image shown on the GUI. - - Get the RGBA shown image from the current GUI system which has four channels. - .. function:: ti.imwrite(img, filename) :parameter img: (Matrix or Expr) the image you want to export diff --git a/python/taichi/lang/meta.py b/python/taichi/lang/meta.py index 608daa0829e63..d314161c03ccb 100644 --- a/python/taichi/lang/meta.py +++ b/python/taichi/lang/meta.py @@ -35,6 +35,8 @@ def vector_to_image(mat: ti.template(), arr: ti.ext_arr()): for I in ti.grouped(mat): for p in ti.static(range(mat.n)): arr[I, p] = cook_image_type(mat[I][p]) + if ti.static(mat.n <= 2): + arr[I, 2] = 0 @ti.kernel diff --git a/python/taichi/misc/gui.py b/python/taichi/misc/gui.py index 6bcbfe72f876a..c7fe2215b97f5 100644 --- a/python/taichi/misc/gui.py +++ b/python/taichi/misc/gui.py @@ -107,6 +107,11 @@ def cook_image(self, img): if img.shape[2] == 3: zeros = np.zeros((img.shape[0], img.shape[1], 1), np.float32) img = np.concatenate([img, zeros], axis=2) + if img.shape[2] == 2: + zeros = np.zeros((img.shape[0], img.shape[1], 2), np.float32) + img = np.concatenate([img, zeros], axis=2) + + assert img.shape[2] == 4, "Image must be grayscale, RG, RGB or RGBA" res = img.shape[:2] assert res == self.res, "Image resolution does not match GUI resolution" @@ -138,12 +143,10 @@ def set_image(self, img): self.img = self.cook_image(img.to_numpy()) else: # Type matched! We can use an optimized copy kernel. - assert img.shape \ - == self.res, "Image resolution does not match GUI resolution" - assert img.n in [ - 3, 4 - ], "Only greyscale, RGB or RGBA images are supported in GUI.set_image" - assert img.m == 1 + assert img.shape == self.res, \ + "Image resolution does not match GUI resolution" + assert img.n in [2, 3, 4] and img.m == 1, \ + "Only greyscale, RG, RGB or RGBA images are supported in GUI.set_image" from taichi.lang.meta import vector_to_image vector_to_image(img, self.img) ti.sync() From 8ed2ba86fab3a4805a8755c3364198ddeb25bc0f Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Mon, 3 Aug 2020 12:34:38 +0800 Subject: [PATCH 2/5] stable_fluid use rk3 --- examples/stable_fluid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/stable_fluid.py b/examples/stable_fluid.py index c969ead903f42..99b86557440bb 100644 --- a/examples/stable_fluid.py +++ b/examples/stable_fluid.py @@ -109,7 +109,7 @@ def backtrace_rk3(vf: ti.template(), p, dt: ti.template()): return p -backtrace = backtrace_rk1 +backtrace = backtrace_rk3 @ti.kernel From 13ebd2d217bb4914554b490713f8a95bcce5174f Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Mon, 3 Aug 2020 12:55:41 +0800 Subject: [PATCH 3/5] [skip ci] vel field in RG examples/stable_fluid.py --- examples/stable_fluid.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/stable_fluid.py b/examples/stable_fluid.py index 99b86557440bb..cc0cec03c9298 100644 --- a/examples/stable_fluid.py +++ b/examples/stable_fluid.py @@ -8,11 +8,11 @@ import numpy as np import time -res = 600 +res = 512 #600 dt = 0.03 -p_jacobi_iters = 30 +p_jacobi_iters = 100 #30 f_strength = 10000.0 -curl_strength = 0 +curl_strength = 0 #3 dye_decay = 0.99 force_radius = res / 3.0 debug = False @@ -317,7 +317,7 @@ def reset(): step(mouse_data) gui.set_image(dyes_pair.cur) - #gui.set_image(np.concatenate( - # [np.abs(velocities_pair.cur.to_numpy() * 0.01 + 0.5), - # np.zeros((res, res, 1))], axis=2)) + #gui.set_image(velocities_pair.cur.to_numpy() * 0.01 + 0.5) + #divergence(velocities_pair.cur); gui.set_image(velocity_divs.to_numpy() * 0.1 + 0.5) + #vorticity(velocities_pair.cur); gui.set_image(velocity_curls.to_numpy() * 0.03 + 0.5) gui.show() From e35bad3eb60df9617d1f79e4afbe671952895810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E4=BA=8E=E6=96=8C?= <1931127624@qq.com> Date: Mon, 3 Aug 2020 22:59:23 +0800 Subject: [PATCH 4/5] [skip ci] Update docs/gui.rst Co-authored-by: Xiao Zhai <7610945+TroyZhai@users.noreply.github.com> --- docs/gui.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/gui.rst b/docs/gui.rst index 7df3c4494fd3d..31bd3f91ef0b8 100644 --- a/docs/gui.rst +++ b/docs/gui.rst @@ -101,7 +101,7 @@ Paint on a window :return: (np.array) the current image shown on the GUI - Get the RGBA shown image from the current GUI system which has four channels. + Get the 4-channel (RGBA) image shown in the current GUI system. .. function:: gui.circle(pos, color = 0xFFFFFF, radius = 1) From c761e7a95bf84e527de5a6eaa2be5665800e62fb Mon Sep 17 00:00:00 2001 From: archibate <1931127624@qq.com> Date: Mon, 3 Aug 2020 23:09:24 +0800 Subject: [PATCH 5/5] [skip ci] apply concerns --- examples/stable_fluid.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/stable_fluid.py b/examples/stable_fluid.py index cc0cec03c9298..d31853919f0fa 100644 --- a/examples/stable_fluid.py +++ b/examples/stable_fluid.py @@ -8,11 +8,11 @@ import numpy as np import time -res = 512 #600 +res = 512 # 600 for a larger resoultion dt = 0.03 -p_jacobi_iters = 100 #30 +p_jacobi_iters = 160 # 40 for quicker but not-so-accurate result f_strength = 10000.0 -curl_strength = 0 #3 +curl_strength = 0 # 7 for unrealistic visual enhancement dye_decay = 0.99 force_radius = res / 3.0 debug = False @@ -317,7 +317,10 @@ def reset(): step(mouse_data) gui.set_image(dyes_pair.cur) + # To visualize velocity field: #gui.set_image(velocities_pair.cur.to_numpy() * 0.01 + 0.5) + # To visualize velocity divergence: #divergence(velocities_pair.cur); gui.set_image(velocity_divs.to_numpy() * 0.1 + 0.5) + # To visualize velocity vorticity: #vorticity(velocities_pair.cur); gui.set_image(velocity_curls.to_numpy() * 0.03 + 0.5) gui.show()