-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualizer_2d.py
77 lines (65 loc) · 2.57 KB
/
visualizer_2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import taichi as ti
import utils
from fluid_simulator import *
import numpy as np
@ti.data_oriented
class Visualizer2D:
def __init__(self, grid_res, res):
self.grid_res = grid_res
self.res = res
self.tmp = ti.Vector.field(3, dtype=ti.f32, shape=(self.grid_res, self.grid_res))
self.tmp_w = ti.field(dtype=ti.f32, shape=(self.grid_res, self.grid_res))
self.color_buffer = ti.Vector.field(3, dtype=ti.f32, shape=(self.res, self.res))
@ti.func
def ij_to_xy(self, i, j):
return int((i + 0.5) / self.res * self.grid_res), \
int((j + 0.5) / self.res * self.grid_res)
@ti.kernel
def fill_power(self, sim : ti.template()):
for i, j in self.tmp:
self.tmp[i, j].fill(0.0)
self.tmp_w[i, j] = 0.0
for I in ti.grouped(ti.ndrange(sim.total_mk[None], *(sim.R, ) * sim.dim)):
p, I_x, pos = self.get_base(I)
if self.check_Tg(I_x):
T = sim.T(p, I_x)
self.tmp[I_x] += T * sim.color_p[p]
self.tmp_w[I_x] += T
V_p = (1.0 / sim.total_mk[None])
for i, j in self.color_buffer:
x, y = self.ij_to_xy(i, j)
self.color_buffer[i, j] = self.tmp[x, y] / self.tmp_w[x, y]
def visualize_factory(self, simulator):
if self.mode == 'power':
self.fill_power(simulator)
def visualize(self, simulator):
assert 0, 'Please use GUIVisualizer2D'
def end(self):
pass
@ti.data_oriented
class GUIVisualizer2D(Visualizer2D):
def __init__(self, grid_res, res, mode, title = 'demo', export=""):
super().__init__(grid_res, res)
self.mode = mode
self.window = ti.ui.Window(title, (res, res), vsync=True)
self.canvas = self.window.get_canvas()
self.frame = 0
self.export = False
if export != "":
self.export = True
self.video_manager = ti.tools.VideoManager(export)
def visualize(self, simulator):
self.canvas.set_background_color(color=(0.0, 0.0, 0.0))
if self.mode == 'p':
self.canvas.circles(simulator.p_x, 0.002, per_vertex_color=simulator.color_p)
else:
self.visualize_factory(simulator)
self.canvas.set_image(self.color_buffer)
if self.export:
img = self.window.get_image_buffer_as_numpy()
self.video_manager.write_frame(img)
self.window.show()
self.frame += 1
def end(self):
if self.export:
self.video_manager.make_video(gif=True, mp4=True)