How to use the taichi.Vector 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 / 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 / difftaichi / diffmpm3d.py View on Github external
def g2p(f: ti.i32):
  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, real)
    w = [
        0.5 * ti.sqr(1.5 - fx), 0.75 - ti.sqr(fx - 1.0), 0.5 * ti.sqr(fx - 0.5)
    ]
    new_v = ti.Vector(zero_vec())
    new_C = ti.Matrix(zero_matrix())

    for i in ti.static(range(3)):
      for j in ti.static(range(3)):
        for k in ti.static(range(3)):
          dpos = ti.cast(ti.Vector([i, j, k]), real) - fx
          g_v = grid_v_out[base(0) + i, base(1) + j, base(2) + k]
          weight = w[i](0) * w[j](1) * w[k](2)
          new_v += weight * g_v
          new_C += 4 * weight * ti.outer_product(g_v, dpos) * inv_dx

    v[f + 1, p] = new_v
    x[f + 1, p] = x[f, p] + dt * v[f + 1, p]
    C[f + 1, p] = new_C
github yuanming-hu / taichi / examples / difftaichi / electric.py View on Github external
def advance(t: ti.i32):
  for _ in range(1):  # parallelize this loop
    gravitational_force = ti.Vector([0.0, 0.0])
    for i in ti.static(range(n_gravitation)):  # instead of this one
      r = x[t - 1] - ti.Vector(gravitation_position[i])
      len_r = ti.max(r.norm(), 1e-1)
      gravitational_force += K * gravitation[t, i] / (len_r * len_r * len_r) * r
    v[t] = v[t - 1] * math.exp(-dt * damping) + dt * gravitational_force
    x[t] = x[t - 1] + dt * v[t]
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 / diffmpm3d.py View on Github external
gravity = 10
target = [0.8, 0.2, 0.2]
use_apic = False

scalar = lambda: ti.var(dt=real)
vec = lambda: ti.Vector(dim, dt=real)
mat = lambda: ti.Matrix(dim, dim, dt=real)

actuator_id = ti.global_var(ti.i32)
particle_type = ti.global_var(ti.i32)
x, v = vec(), vec()
grid_v_in, grid_m_in = vec(), scalar()
grid_v_out = vec()
C, F = mat(), mat()

screen = ti.Vector(3, dt=real)

loss = scalar()

n_sin_waves = 4
weights = scalar()
bias = scalar()
x_avg = vec()

actuation = scalar()
actuation_omega = 40
act_strength = 5

# ti.cfg.arch = ti.x86_64
# ti.cfg.use_llvm = True
ti.cfg.arch = ti.cuda
github yuanming-hu / taichi_mpm / scripts / mls-cpic / water_wheel.py View on Github external
type='rigid',
      scale=(s, s, s),
      density=rho,
      friction=0.2,
      scripted_position=tc.constant_function13(tc.Vector(0.5, 0.5, 0.5)),
      rotation_axis=(0, 0, 1),
      codimensional=True,
      angular_damping=damping,
      mesh_fn='$mpm/ww_gears.obj')

  object3 = mpm.add_particles(
      type='rigid',
      scale=(s, s, s),
      density=rho,
      friction=0.2,
      scripted_position=tc.constant_function13(tc.Vector(0.5, 0.5, 0.5)),
      rotation_axis=(0, 0, 1),
      codimensional=True,
      angular_damping=damping,
      mesh_fn='$mpm/ww_sides.obj')

  object4 = mpm.add_particles(
      type='rigid',
      scale=(s, s, s),
      density=rho,
      friction=0.2,
      scripted_position=tc.constant_function13(tc.Vector(0.5, 0.5, 0.5)),
      rotation_axis=(0, 0, 1),
      codimensional=True,
      angular_damping=damping,
      mesh_fn='$mpm/ww_spoke.obj')
github taichi-dev / elements / engine / voxelizer.py View on Github external
for i in range(num_triangles):
            jitter_scale = ti.cast(0, self.precision)
            if ti.static(self.precision is ti.f32):
                jitter_scale = 1e-4
            else:
                jitter_scale = 1e-8
            # We jitter the vertices to prevent voxel samples from lying precicely at triangle edges
            jitter = ti.Vector([
                -0.057616723909439505, -0.25608986292614977,
                0.06716309129743714
            ]) * jitter_scale
            a = ti.Vector([triangles[i, 0], triangles[i, 1], triangles[i, 2]
                           ]) + jitter
            b = ti.Vector([triangles[i, 3], triangles[i, 4], triangles[i, 5]
                           ]) + jitter
            c = ti.Vector([triangles[i, 6], triangles[i, 7], triangles[i, 8]
                           ]) + jitter

            bound_min = ti.Vector.zero(self.precision, 3)
            bound_max = ti.Vector.zero(self.precision, 3)
            for k in ti.static(range(3)):
                bound_min[k] = min(a[k], b[k], c[k])
                bound_max[k] = max(a[k], b[k], c[k])

            p_min = int(ti.floor(bound_min[0] * self.inv_dx))
            p_max = int(ti.floor(bound_max[0] * self.inv_dx)) + 1

            p_min = max(self.padding, p_min)
            p_max = min(self.res[0] - self.padding, p_max)

            q_min = int(ti.floor(bound_min[1] * self.inv_dx))
            q_max = int(ti.floor(bound_max[1] * self.inv_dx)) + 1
github yuanming-hu / taichi / examples / difftaichi / diffmpm_benchmark.py View on Github external
def p2g(f: ti.i32):
  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]
    F[f + 1, p] = new_F
    J = ti.determinant(new_F)
    r, s = ti.polar_decompose(new_F)
    cauchy = 2 * mu * (new_F - r) @ ti.transposed(new_F) + \
             ti.Matrix.diag(2, la * (J - 1) * J)
    stress = -(dt * p_vol * 4 * inv_dx * inv_dx) * cauchy
    affine = stress + p_mass * C[f, p]
    for i in ti.static(range(3)):
      for j in ti.static(range(3)):
        offset = ti.Vector([i, j])
        dpos = (ti.cast(ti.Vector([i, j]), real) - fx) * dx
        weight = w[i](0) * w[j](1)
        grid_v_in[base + offset].atomic_add(
            weight * (p_mass * v[f, p] + affine @ dpos))
        grid_m_in[base + offset].atomic_add(weight * p_mass)
github yuanming-hu / taichi / examples / difftaichi / mass_spring_velocity.py View on Github external
def clear_states():
  for t in range(0, max_steps):
    for i in range(0, n_objects):
      x.grad[t, i] = ti.Vector([0.0, 0.0])
      v.grad[t, i] = ti.Vector([0.0, 0.0])
      v_inc[t, i] = ti.Vector([0.0, 0.0])
      v_inc.grad[t, i] = ti.Vector([0.0, 0.0])
github yuanming-hu / taichi / examples / difftaichi / diffmpm_simple.py View on Github external
def g2p(f: ti.i32):
  for p in range(n_particles):
    base = ti.cast(x[f, p] * inv_dx - 0.5, ti.i32)
    fx = x[f, p] * inv_dx - ti.cast(base, real)
    w = [
        0.5 * ti.sqr(1.5 - fx), 0.75 - ti.sqr(fx - 1.0), 0.5 * ti.sqr(fx - 0.5)
    ]
    new_v = ti.Vector([0.0, 0.0])
    new_C = ti.Matrix([[0.0, 0.0], [0.0, 0.0]])

    for i in ti.static(range(3)):
      for j in ti.static(range(3)):
        dpos = ti.cast(ti.Vector([i, j]), real) - fx
        g_v = grid_v_out[f, base(0) + i, base(1) + j]
        weight = w[i](0) * w[j](1)
        new_v += weight * g_v
        new_C += 4 * weight * ti.outer_product(g_v, dpos) * inv_dx

    v[f + 1, p] = new_v
    x[f + 1, p] = x[f, p] + dt * v[f + 1, p]
    C[f + 1, p] = new_C