How to use the taichi.max function in taichi

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_ad_basics.py View on Github external
  grad_test(lambda x: ti.max(x, 1), lambda x: np.maximum(x, 1))
  grad_test(lambda x: ti.max(0, x), lambda x: np.maximum(0, x))
github yuanming-hu / taichi / examples / difftaichi / diffmpm_renderer.py View on Github external
def dda_particle(eye_pos, d_, t):
  grid_res = particle_grid_res

  bbox_min = bbox[0]
  bbox_max = bbox[1]

  hit_pos = ti.Vector([0.0, 0.0, 0.0])
  normal = ti.Vector([0.0, 0.0, 0.0])
  c = ti.Vector([0.0, 0.0, 0.0])
  d = d_
  for i in ti.static(range(3)):
    if ti.abs(d[i]) < 1e-6:
      d[i] = 1e-6

  inter, near, far = ray_aabb_intersection(bbox_min, bbox_max, eye_pos, d)
  near = ti.max(0, near)

  closest_intersection = inf

  if inter:
    pos = eye_pos + d * (near + eps)

    rinv = 1.0 / d
    rsign = ti.Vector([0, 0, 0])
    for i in ti.static(range(3)):
      if d[i] > 0:
        rsign[i] = 1
      else:
        rsign[i] = -1

    o = grid_res * pos
    ipos = ti.Matrix.floor(o).cast(ti.i32)
github yuanming-hu / taichi / examples / difftaichi / diffmpm.py View on Github external
for p in range(0, n_particles):
    base = ti.cast(x[f, p] * inv_dx - 0.5, ti.i32)
    fx = x[f, p] * inv_dx - ti.cast(base, ti.i32)
    w = [0.5 * ti.sqr(1.5 - fx), 0.75 - ti.sqr(fx - 1), 0.5 * ti.sqr(fx - 0.5)]
    new_F = (ti.Matrix.diag(dim=2, val=1) + dt * C[f, p]) @ F[f, p]
    J = ti.determinant(new_F)
    if particle_type[p] == 0:  # fluid
      sqrtJ = ti.sqrt(J)
      new_F = ti.Matrix([[sqrtJ, 0], [0, sqrtJ]])

    F[f + 1, p] = new_F
    r, s = ti.polar_decompose(new_F)

    act_id = actuator_id[p]

    act = actuation[f, ti.max(0, act_id)] * act_strength
    if act_id == -1:
      act = 0.0
    # ti.print(act)

    A = ti.Matrix([[0.0, 0.0], [0.0, 1.0]]) * act
    cauchy = ti.Matrix([[0.0, 0.0], [0.0, 0.0]])
    mass = 0.0
    if particle_type[p] == 0:
      mass = 4
      cauchy = ti.Matrix([[1.0, 0.0], [0.0, 0.1]]) * (J - 1) * E
    else:
      mass = 1
      cauchy = 2 * mu * (new_F - r) @ ti.transposed(new_F) + \
               ti.Matrix.diag(2, la * (J - 1) * J)
    cauchy += new_F @ A @ ti.transposed(new_F)
    stress = -(dt * p_vol * 4 * inv_dx * inv_dx) * cauchy
github yuanming-hu / taichi / examples / renderer.py View on Github external
def sdf(o_):
  if ti.static(supporter == 0):
    o = o_ - ti.Vector([0.5, 0.002, 0.5])
    p = o
    h = 0.02
    ra = 0.29
    rb = 0.005
    d = (ti.Vector([p[0], p[2]]).norm() - 2.0 * ra + rb, ti.abs(p[1]) - h)
    dist = ti.min(ti.max(d[0], d[1]), 0.0) + ti.Vector(
        [ti.max(d[0], 0.0), ti.max(d[1], 0)]).norm() - rb
    return dist
  elif ti.static(supporter == 1):
    o = o_ - ti.Vector([0.5, 0.002, 0.5])
    dist = (o.abs() - ti.Vector([0.5, 0.02, 0.5])).max()
  else:
    dist = o_[1] - 0.027

  return dist
github yuanming-hu / taichi / examples / difftaichi / rigid_body.py View on Github external
def apply_impulse(t, i, impulse, location, toi_input):
  # ti.print(toi)
  delta_v = impulse * inverse_mass[i]
  delta_omega = cross(location - x[t, i], impulse) * inverse_inertia[i]

  toi = ti.min(ti.max(0.0, toi_input), dt)

  ti.atomic_add(x_inc[t + 1, i], toi * (-delta_v))
  ti.atomic_add(rotation_inc[t + 1, i], toi * (-delta_omega))

  ti.atomic_add(v_inc[t + 1, i], delta_v)
  ti.atomic_add(omega_inc[t + 1, i], delta_omega)
github yuanming-hu / taichi / examples / difftaichi / volume_renderer.py View on Github external
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

        # Density grid location
        index_x = ti.cast(ti.floor(box_x * density_res), ti.i32)
        index_y = ti.cast(ti.floor(box_y * density_res), ti.i32)
        index_z = ti.cast(ti.floor(box_z * density_res), ti.i32)
        index_x = ti.max(0, ti.min(index_x, density_res - 1))
        index_y = ti.max(0, ti.min(index_y, density_res - 1))
        index_z = ti.max(0, ti.min(index_z, density_res - 1))

        flag = 0
        if in_box(point[0], point[1], point[2]):
          flag = 1

        contribution = density[index_z, index_y, index_x] * flag

        ti.atomic_add(field[view_id, y, x], contribution)
github yuanming-hu / taichi / examples / difftaichi / liquid_render.py View on Github external
def render_photon_map(t: ti.i32, offset_x: ti.f32, offset_y: ti.f32):
  for i in range(n_grid):  # Parallelized over GPU threads
    for j in range(n_grid):
      grad = height_gradient[i, j] * (1 - offset_x) * (1 - offset_y) + \
             height_gradient[i + 1, j] * offset_x * (1 - offset_y) + \
             height_gradient[i, j + 1] * (1 - offset_x) * offset_y + \
             height_gradient[i + 1, j + 1] * offset_x * offset_y

      scale = 5.0
      sample_x = i - grad[0] * scale + offset_x
      sample_y = j - grad[1] * scale + offset_y
      sample_x = ti.min(n_grid - 1, ti.max(0, sample_x))
      sample_y = ti.min(n_grid - 1, ti.max(0, sample_y))
      sample_xi = ti.cast(ti.floor(sample_x), ti.i32)
      sample_yi = ti.cast(ti.floor(sample_y), ti.i32)

      frac_x = sample_x - sample_xi
      frac_y = sample_y - sample_yi

      x = sample_xi
      y = sample_yi

      ti.atomic_add(rendered[x, y], (1 - frac_x) * (1 - frac_y))
      ti.atomic_add(rendered[x, y + 1], (1 - frac_x) * frac_y)
      ti.atomic_add(rendered[x + 1, y], frac_x * (1 - frac_y))
      ti.atomic_add(rendered[x + 1, y + 1], frac_x * frac_y)
github yuanming-hu / taichi / examples / difftaichi / diffmpm.py View on Github external
fx = x[f, p] * inv_dx - ti.cast(base, ti.i32)
    w = [0.5 * ti.sqr(1.5 - fx), 0.75 - ti.sqr(fx - 1),
         0.5 * ti.sqr(fx - 0.5)]
    new_F = (ti.Matrix.diag(dim=2, val=1) + dt * C[f, p]) @ F[f, p]
    J = ti.determinant(new_F)
    if particle_type[p] == 0: # fluid
      sqrtJ = ti.sqrt(J)
      new_F = ti.Matrix([[sqrtJ, 0], [0, sqrtJ]])
      
    
    F[f + 1, p] = new_F
    r, s = ti.polar_decompose(new_F)
    
    act_id = actuator_id[p]
    
    act = actuation[f, ti.max(0, act_id)] * act_strength
    if act_id == -1:
      act = 0.0
    # ti.print(act)
      
    A = ti.Matrix([[0.0, 0.0], [0.0, 1.0]]) * act
    cauchy = ti.Matrix([[0.0, 0.0], [0.0, 0.0]])
    mass = 0.0
    if particle_type[p] == 0:
      mass = 4
      cauchy = ti.Matrix([[1.0, 0.0], [0.0, 0.1]]) * (J - 1) * E
    else:
      mass = 1
      cauchy = 2 * mu * (new_F - r) @ ti.transposed(new_F) + \
               ti.Matrix.diag(2, la * (J - 1) * J)
    cauchy += new_F @ A @ ti.transposed(new_F)
    stress = -(dt * p_vol * 4 * inv_dx * inv_dx) * cauchy