Skip to content

Instantly share code, notes, and snippets.

@jarmitage
Created March 16, 2023 10:08
Show Gist options
  • Save jarmitage/be406ab7b9b949ceef2c58ef389fea06 to your computer and use it in GitHub Desktop.
Save jarmitage/be406ab7b9b949ceef2c58ef389fea06 to your computer and use it in GitHub Desktop.
Sparse particle field in Taichi using Pointers
import taichi as ti
ti.init(arch=ti.cuda) # not supported on vulkan
@ti.dataclass
class Particle:
position: ti.math.vec2
velocity: ti.math.vec2
@ti.data_oriented
class Particles:
def __init__(self, n=32):
self.n = n
self.field = Particle.field()
self.pointer = ti.root.pointer(ti.i, self.n).place(self.field)
@ti.kernel
def activate(self, i: int):
ti.activate(self.pointer, [i])
@ti.kernel
def deactivate(self, i: int):
ti.deactivate(self.pointer, [i])
particles = Particles(32)
particles.field[0] = Particle(position=[0.5,0.5],velocity=[0.5,0.5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment