taichi.ui.gui
#
- class taichi.ui.gui.GUI(name='Taichi', res=512, background_color=0, show_gui=True, fullscreen=False, fast_gui=False)#
Taichi Graphical User Interface class.
- Parameters:
name (str, optional) – The name of the GUI to be constructed. Default is ‘Taichi’.
res (Union[int, List[int]], optional) – The resolution of created GUI. Default is 512*512. If res is scalar, then width will be equal to height.
background_color (int, optional) – The background color of created GUI. Default is 0x000000.
show_gui (bool, optional) – Specify whether to render the GUI. Default is True.
fullscreen (bool, optional) – Specify whether to render the GUI in fullscreen mode. Default is False.
fast_gui (bool, optional) – Specify whether to use fast gui mode of Taichi. Default is False.
- Returns:
The created taichi GUI object.
- Return type:
GUI
- class Event#
Class for holding a gui event.
An event is represented by:
type (PRESS, MOTION, RELEASE)
modifier (modifier keys like ctrl, shift, etc)
pos (mouse position)
key (event key)
delta (for holding mouse wheel)
- class EventFilter(*e_filter)#
A set to store detected user events.
- match(self, e)#
Check if a specified event e is among the detected events.
- class WidgetValue(gui, wid)#
Class for maintaining id of gui widgets.
- ALT = Alt#
- BACKSPACE = BackSpace#
- CAPSLOCK = Caps_Lock#
- CTRL = Control#
- DOWN = Down#
- ESCAPE = Escape#
- EXIT = WMClose#
- LEFT = Left#
- LMB = LMB#
- MMB = MMB#
- MOTION#
- MOVE = Motion#
- PRESS#
- RELEASE#
- RETURN = Return#
- RIGHT = Right#
- RMB = RMB#
- SHIFT = Shift#
- SPACE =#
- TAB = Tab#
- UP = Up#
- WHEEL = Wheel#
- arrow(self, orig, direction, radius=1, color=16777215, **kwargs)#
Draws a single arrow on canvas.
- Parameters:
orig (List[Number]) – The position where arrow starts. Shape must be 2.
direction (List[Number]) – The direction where arrow points to. Shape must be 2.
radius (Number, optional) – The width of arrow. Default is 1.
color (int, optional) – The color of arrow. Default is 0xFFFFFF.
- arrow_field(self, direction, radius=1, color=16777215, bound=0.5, **kwargs)#
Draw a field of arrows on canvas.
- Parameters:
direction (np.array) – The pattern and direction of the field of arrows.
color (Union[int, np.array], optional) – The color or colors of arrows. Default is 0xFFFFFF.
bound (Number, optional) – The boundary of the field. Default is 0.5.
- arrows(self, orig, direction, radius=1, color=16777215, **kwargs)#
Draw a list arrows on canvas.
- Parameters:
orig (numpy.array) – The positions where arrows start.
direction (numpy.array) – The directions where arrows point to.
radius (Union[Number, np.array], optional) – The width of arrows. Default is 1.
color (Union[int, np.array], optional) – The color or colors of arrows. Default is 0xffffff.
- button(self, text, event_name=None)#
Create a button object on canvas to be manipulated with.
- Parameters:
text (str) – The title of button.
event_name (str, optional) – The event name associated with button. Default is WidgetButton_{text}
- Returns:
The event name associated with created button.
- circle(self, pos, color=16777215, radius=1)#
Draws a circle on canvas.
- Parameters:
pos (Union[List[int], numpy.array]) – The position of the circle.
color (int, Optional) – The color of the circle. Default is 0xFFFFFF.
radius (Number, Optional) – The radius of the circle in pixel. Default is 1.
- circles(self, pos, radius=1, color=16777215, palette=None, palette_indices=None)#
Draws a list of circles on canvas.
- Parameters:
pos (numpy.array) – The positions of the circles.
radius (Union[Number, numpy.array], optional) – The radius of the circles in pixel. Can be either a number, which will be applied to all circles, or a 1D NumPy array of the same length as pos. The default is 1.
color (int, optional) – The color of the circles. Default is 0xFFFFFF.
palette (list[int], optional) – The List of colors from which to choose to draw. Default is None.
palette_indices (Union[list[int], ti.field, numpy.array], optional) – The List of indices that choose color from palette for each circle. Shape must match pos. Default is None.
- clear(self, color=None)#
Clears the canvas with the color provided.
- Parameters:
color (int, optional) – Specify the color to clear the canvas. Default is the background color of GUI.
- close(self)#
Close this GUI.
Example:
>>> while gui.running:
>>> if gui.get_event(gui.PRESS, ti.GUI.ESCAPE):
>>> gui.close()
>>> gui.show()
- contour(self, scalar_field, normalize=False)#
Plot a contour view of a scalar field.
The input scalar_field will be converted to a Numpy array first, and then plotted by the Matplotlib colormap ‘Plasma’. Notice this method will automatically perform a bilinear interpolation on the field if the size of the field does not match with the GUI window size.
- Parameters:
scalar_field (ti.field) – The scalar field being plotted.
normalize (bool, Optional) – Display the normalized scalar field if set to True.
False. (Default is) –
- cook_image(self, img)#
Converts an img to range [0, 1] for display.
The input image is stored in a numpy.ndarray, if it’s dtype is int it will be rescaled and mapped into range [0, 1]. If the dtype is float it will be directly casted to 32-bit float type.
- static get_bool_environ(key, default)#
Get an environment variable and cast it to bool.
- Parameters:
key (str) – The environment variable key.
default (bool) – The default value.
- Returns:
The environment variable value cast to bool. If the value is not found, directly return argument ‘default’.
- get_cursor_pos(self)#
Returns the current position of mouse as a pair of floats in the range [0, 1] x [0, 1].
The origin of the coordinates system is located at the lower left corner, with +x direction points to the right, and +y direcntion points upward.
- Returns:
The current position of mouse.
- get_event(self, *e_filter)#
Checks if the specified event is triggered.
- Parameters:
*e_filter (ti.GUI.EVENT) – The specific event to be checked.
- Returns:
whether or not the specified event is triggered.
- Return type:
bool
- get_events(self, *e_filter)#
Gets a list of events that are triggered.
- Parameters:
*e_filter (List[ti.GUI.EVENT]) – The type of events to be filtered.
- Returns:
A list of events that are triggered.
- Return type:
EVENT
- get_image(self)#
Return the window content as an numpy.ndarray.
- Returns:
The image data in numpy contiguous array type.
- Return type:
numpy.array
- get_key_event(self)#
Gets keyboard triggered event.
- Returns:
The keyboard triggered event.
- Return type:
EVENT
- has_key_event(self)#
Check if there is any key event registered.
- Returns:
whether or not there is any key event registered.
- Return type:
bool
- is_pressed(self, *keys)#
Checks if any key among a set of specified keys is pressed.
- Parameters:
*keys (Union[str, List[str]]) – The keys to be listened to.
- Returns:
whether or not any key among the specified keys is pressed.
- Return type:
bool
- label(self, text)#
Creates a label object on canvas.
- Parameters:
text (str) – The title of label.
- Returns:
The created label object.
- Return type:
WidgetValue
- line(self, begin, end, radius=1, color=16777215)#
Draws a single line on canvas.
- Parameters:
begin (List[Number]) – The position of one end of line. Shape must be 2.
end (List[Number]) – The position of the other end of line. Shape must be 2.
radius (Number, optional) – The width of line. Default is 1.
color (int, optional) – The color of line. Default is 0xFFFFFF.
- lines(self, begin, end, radius=1, color=16777215)#
Draw a list of lines on canvas.
- Parameters:
begin (numpy.array) – The positions of one end of lines.
end (numpy.array) – The positions of the other end of lines.
radius (Union[Number, numpy.array], optional) – The width of lines. Can be either a single width or a list of width whose shape matches the shape of begin & end. Default is 1.
color (Union[int, numpy.array], optional) – The color or colors of lines. Can be either a single color or a list of colors whose shape matches the shape of begin & end. Default is 0xFFFFFF.
- point_field(self, radius, color=16777215, bound=0.5)#
Draws a field of points on canvas.
- Parameters:
radius (np.array) – The pattern and radius of the field of points.
color (Union[int, np.array], optional) – The color or colors of points. Default is 0xFFFFFF.
bound (Number, optional) – The boundary of the field. Default is 0.5.
- rect(self, topleft, bottomright, radius=1, color=16777215)#
Draws a single rectangle on canvas.
- Parameters:
topleft (List[Number]) – The position of the topleft corner of rectangle. Shape must be 2.
bottomright (List[Number]) – The position of the bottomright corner of rectangle. Shape must be 2.
radius (Number, optional) – The width of rectangle’s sides. Default is 1.
color (int, optional) – The color of rectangle. Default is 0xFFFFFF.
- set_image(self, img)#
Sets 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).
- If the window size is (x, y), then img must be one of:
ti.field(shape=(x, y)), a gray-scale image
ti.field(shape=(x, y, 3)), where 3 is for (r, g, b) channels
ti.field(shape=(x, y, 2)), where 2 is for (r, g) channels
ti.Vector.field(3, shape=(x, y)) (r, g, b) channels on each component
ti.Vector.field(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:
uint8, range [0, 255]
uint16, range [0, 65535]
uint32, range [0, 4294967295]
float32, range [0, 1]
float64, range [0, 1]
- Parameters:
img (Union[
taichi.field
, numpy.array]) – The color array representing the image to be drawn. Support greyscale, RG, RGB, and RGBA color representations. Its shape must match GUI resolution.
- show(self, file=None)#
Shows the frame content in the gui window, or save the content to an image file.
- Parameters:
file (str, optional) – output filename. The default is None, and the frame content is displayed in the gui window. If it’s a valid image filename the frame will be saved as the specified image.
- slider(self, text, minimum, maximum, step=1)#
Creates a slider object on canvas to be manipulated with.
- Parameters:
text (str) – The title of slider.
minimum (int, float) – The minimum value of slider.
maximum (int, float) – The maximum value of slider.
step (int, float) – The changing step of slider. Optional and default to 1.
- Returns:
The created slider object.
- Return type:
WidgetValue
- text(self, content, pos, font_size=15, color=16777215)#
Draws texts on canvas.
- Parameters:
content (str) – The text to be drawn on canvas.
pos (List[Number]) – The position where the text is to be put.
font_size (Number, optional) – The font size of the text.
color (int, optional) – The color of the text. Default is 0xFFFFFF.
- triangle(self, a, b, c, color=16777215)#
Draws a single triangle on canvas.
- Parameters:
a (List[Number]) – The position of the first point of triangle. Shape must be 2.
b (List[Number]) – The position of the second point of triangle. Shape must be 2.
c (List[Number]) – The position of the third point of triangle. Shape must be 2.
color (int, optional) – The color of the triangle. Default is 0xFFFFFF.
- triangles(self, a, b, c, color=16777215)#
Draws a list of triangles on canvas.
- Parameters:
a (numpy.array) – The positions of the first points of triangles.
b (numpy.array) – The positions of the second points of triangles.
c (numpy.array) – The positions of the third points of triangles.
color (Union[int, numpy.array], optional) – The color or colors of triangles. Can be either a single color or a list of colors whose shape matches the shape of a & b & c. Default is 0xFFFFFF.
- vector_field(self, vector_field, arrow_spacing=5, color=16777215)#
Display a vector field on canvas.
- Parameters:
vector_field (ti.Vector.field) – The vector field being displayed.
arrow_spacing (int, optional) – The spacing between vectors.
color (Union[int, np.array], optional) – The color of vectors.
- taichi.ui.gui.hex_to_rgb(color)#
Converts hex color format to rgb color format.
- Parameters:
color (int) – The hex representation of color.
- Returns:
The rgb representation of color.
- taichi.ui.gui.rgb_to_hex(c)#
Converts rgb color format to hex color format.
- Parameters:
c (List[int]) – The rgb representation of color.
- Returns:
The hex representation of color.