How to use taichi - 10 common examples

To help you get started, we’ve selected a few taichi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yuanming-hu / taichi / tests / python / test_struct.py View on Github external
def test_linear_nested():
  x = ti.var(ti.i32)
  y = ti.var(ti.i32)

  n = 128

  @ti.layout
  def place():
    ti.root.dense(ti.i, n // 16).dense(ti.i, 16).place(x)
    ti.root.dense(ti.i, n // 16).dense(ti.i, 16).place(y)

  for i in range(n):
    x[i] = i
    y[i] = i + 123

  for i in range(n):
    assert x[i] == i
    assert y[i] == i + 123
github yuanming-hu / taichi / tests / python / test_struct.py View on Github external
def place():
    ti.root.dense(ti.ij, n // 16).dense(ti.ij, (32, 16)).place(x)
github yuanming-hu / taichi / tests / python / test_struct.py View on Github external
  @ti.layout
  def place():
    ti.root.dense(ti.ij, n // 16).dense(ti.ij, (32, 16)).place(x)
github yuanming-hu / taichi / tests / python / test_struct_for.py View on Github external
def place():
    ti.root.dense(ti.i, n // 4).dense(ti.i, 4).place(x)
    ti.root.dense(ti.i, n).place(y)
github yuanming-hu / taichi / tests / python / test_dynamic.py View on Github external
def test_dynamic_matrix():
  return
  if ti.cfg.arch == ti.cuda:
    return

  x = ti.Matrix(3, 2, dt=ti.f32)
  n = 8192

  @ti.layout
  def place():
    ti.root.dynamic(ti.i, n, chunk_size=128).place(x)

  @ti.kernel
  def func():
    ti.serialize()
    for i in range(n // 4):
      x[i * 4][1, 0] = i

  func()

  for i in range(n // 4):
    assert x[i * 4][1, 0] == i
github yuanming-hu / taichi / tests / python / test_struct.py View on Github external
@ti.all_archs
def test_linear_nested_aos():
  x = ti.var(ti.i32)
  y = ti.var(ti.i32)

  n = 128

  @ti.layout
  def place():
    ti.root.dense(ti.i, n // 16).dense(ti.i, 16).place(x, y)

  for i in range(n):
    x[i] = i
    y[i] = i + 123

  for i in range(n):
    assert x[i] == i
github yuanming-hu / taichi / examples / difftaichi / diffmpm_renderer.py View on Github external
hit_pos = eye_pos + dist * d
            closest_intersection = dist
            normal = ti.Matrix.normalized(hit_pos - x)
            c = [
                color // 256**2 / 255.0, color / 256 % 256 / 255.0,
                color % 256 / 255.0
            ]
      else:
        running = 0
        normal = [0, 0, 0]

      if closest_intersection < inf:
        running = 0
      else:
        # hits nothing. Continue ray marching
        mm = ti.Vector([0, 0, 0])
        if dis[0] <= dis[1] and dis[0] <= dis[2]:
          mm[0] = 1
        elif dis[1] <= dis[0] and dis[1] <= dis[2]:
          mm[1] = 1
        else:
          mm[2] = 1
        dis += mm * rsign * rinv
        ipos += mm * rsign

  return closest_intersection, normal, c
github yuanming-hu / taichi / examples / taichi_logo.py View on Github external
if inside(p, Vector2(0.50, 0.25), 0.25):
    if ret == -1:
      ret = 0
  if inside(p, Vector2(0.50, 0.75), 0.25):
    if ret == -1:
      ret = 1
  if p[0] < 0.5:
    if ret == -1:
      ret = 1
  else:
    if ret == -1:
      ret = 0
  return ret


x = ti.var(ti.f32)

n = 512
ti.cfg.use_llvm = True


@ti.layout
def layout():
  ti.root.dense(ti.ij, n).place(x)


@ti.kernel
def paint():
  for i in range(n * 4):
    for j in range(n * 4):
      ret = 1.0 - inside_taichi(Vector2(1.0 * i / n / 4, 1.0 * j / n / 4))
      x[i // 4, j // 4] += ret / 16
github taichi-dev / elements / engine / mpm_solver.py View on Github external
self.inv_dx = 1.0 / self.dx
        self.default_dt = 2e-2 * self.dx / size * dt_scale
        self.p_vol = self.dx**self.dim
        self.p_rho = 1000
        self.p_mass = self.p_vol * self.p_rho
        self.max_num_particles = max_num_particles
        self.gravity = ti.Vector(self.dim, dt=ti.f32, shape=())
        self.source_bound = ti.Vector(self.dim, dt=ti.f32, shape=2)
        self.source_velocity = ti.Vector(self.dim, dt=ti.f32, shape=())
        self.pid = ti.var(ti.i32)
        # position
        self.x = ti.Vector(self.dim, dt=ti.f32)
        # velocity
        self.v = ti.Vector(self.dim, dt=ti.f32)
        # affine velocity field
        self.C = ti.Matrix(self.dim, self.dim, dt=ti.f32)
        # deformation gradient
        self.F = ti.Matrix(self.dim, self.dim, dt=ti.f32)
        # material id
        self.material = ti.var(dt=ti.i32)
        self.color = ti.var(dt=ti.i32)
        # plastic deformation volume ratio
        self.Jp = ti.var(dt=ti.f32)

        if self.dim == 2:
            indices = ti.ij
        else:
            indices = ti.ijk

        offset = tuple(-self.grid_size // 2 for _ in range(self.dim))
        self.offset = offset
github yuanming-hu / taichi / examples / difftaichi / volume_renderer.py View on Github external
def kernel(angle: ti.f32, view_id: ti.i32):
    for pixel in range(res * res):
      for k in range(marching_steps):
        x = pixel // res
        y = pixel - x * res

        camera_origin = ti.Vector([
            camera_origin_radius * ti.sin(angle), 0,
            camera_origin_radius * ti.cos(angle)
        ])
        dir = ti.Vector([
            fov * (ti.cast(x, ti.f32) / (res_f32 / 2.0) - res_f32 / res_f32),
            fov * (ti.cast(y, ti.f32) / (res_f32 / 2.0) - 1.0), -1.0
        ])

        length = ti.sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2])
        dir /= length

        rotated_x = dir[0] * ti.cos(angle) + dir[2] * ti.sin(angle)
        rotated_z = -dir[0] * ti.sin(angle) + dir[2] * ti.cos(angle)
        dir[0] = rotated_x
        dir[2] = rotated_z
        point = camera_origin + (k + 1) * dx * dir

        # Convert to coordinates of the density grid box
        box_x = point[0] + 0.5
        box_y = point[1] + 0.5
        box_z = point[2] + 0.5