def spawn(self, **kwargs): # position = kwargs.pop('position') vel = kwargs.get('velocity', PVector(0, 0, 0)) kwargs['velocity'] = PVector( *[sum([s_i, v_i]) for s_i, v_i in zip(self.spawn_velocity, vel)]) if len(self) > self.capacity: self.pop(0) self.append(Particle(**kwargs))
def step(self, field=None): if field: self.velocity = PVector( *[sum([v_i, f_i]) for (v_i, f_i) in zip(self.velocity, field)]) if self.velocity: self.position = PVector(*[ sum([p_i, v_i]) for (p_i, v_i) in zip(self.position, self.velocity) ])
def draw(): """ Required for pyprocessing.run() """ global screen_size global camera_pos global spawner global frame_count global swarm global back_col global img_dir pyp.camera(camera_pos.x, camera_pos.y, camera_pos.z, 0, 0, 0, 1, 0, 0) s = max(screen_size) pyp.pointLight(255, 255, 255, 10 * s, 0, 0) pyp.pointLight(255, 255, 255, 0, 10 * s, 0) pyp.pointLight(255, 255, 255, 0, 0, 10 * s) pyp.background(back_col) #calculate spawner angle animation_angle = 2.0 * pi * (frame_count % frame_cycles) / frame_cycles spawner.angle = 65 * animation_angle # print 'spawner', pformat({ # 'angle':spawner.angle, # 'position':spawner.spawn_position # }) seed(animation_angle) swarm.spawn(orientation=PVector(s / 50, 0, 0), position=spawner.spawn_position, velocity=PVector((sin(animation_angle)) * s / 100, (cos(animation_angle)) * s / 100, (1 - random() * 2) * s / 100), active=True) for particle in swarm: # print 'particle', pformat({ # 'position': particle.position, # 'velocity': particle.velocity, # 'orientation': particle.orientation # }) particle.draw() particle.step() frame_count += 1 if frame_count in range(360, 720): img = pyp.get() # print type(img) pyp.save(os.path.join(img_dir, 'image_%s.jpg' % (frame_count)))
def transform(cls, transformation, vector): assert isinstance(transformation, np.ndarray) assert isinstance(vector, PVector) # if transformation is 3x3 then vector should be 3 assert all( [len(vector) == dimension for dimension in transformation.shape]) response = np.matmul(transformation, vector) return PVector(*response)
def __init__(self, cx, cy, radius): """ Initialize instance of ArcBall with no constraint on axis of rotation """ self.center_x = cx self.center_y = cy self.radius = radius self.v_down = PVector() self.v_drag = PVector() self.q_now = Quaternion() self.q_down = Quaternion() self.q_drag = Quaternion() self.axis_set = [ PVector(1.0, 0.0, 0.0), PVector(0.0, 1.0, 0.0), PVector(0.0, 0.0, 1.0) ] self.axis = -1
def spawn_position(self): circle = PVector(self.size * sin(self.angle), self.size * cos(self.angle), 0) (r, theta, phi) = self.orientation_spherical if theta: circle = Transformation.rotate_y(-theta, circle) if phi: circle = Transformation.rotate_z(phi, circle) if self.position: circle = Transformation.transpose(self.position, circle) return circle
def setup(): """ Required for pyprocessing.run() """ global screen_size # size of the raster canvas global camera_pos # PVector of camera's position global spawner # an object used for spawning particles global frame_count # number of framse currently global swarm # Collection of background partciles global img_dir # dir where frames are stored run_stamp = time.strftime("%Y-%m-%d_%H-%M-%S") img_dir = './images/%s' % run_stamp if not os.path.exists(img_dir): os.makedirs(img_dir) frame_count = 0 s = max(screen_size) camera_pos = PVector(0, 0, s) spawner = Spawner(position=PVector(0, 0, s), orientation=PVector(0, 0, s / 10)) swarm = Swarm(spawn_velocity=PVector(0, 0, -s / 40), capacity=background_particle_count)
def __mouse2sphere(self, x, y): """ private map mouse to ArcBall (sphere) """ v = PVector() v.x = (x - self.center_x) / self.radius v.y = (y - self.center_y) / self.radius mag = v.x * v.x + v.y * v.y if (mag > 1.0): v.normalize() else: v.z = sqrt(1.0 - mag) if (self.axis != -1): v = self.__constrain(v, self.axis_set[self.axis]) return v
def __init__(self, **kwargs): self.position = kwargs.get('position', PVector(0, 0, 0)) self.orientation = kwargs.get('orientation', PVector(1, 0, 0))
def cartesian_to_spherical(cls, vector): length = vector.mag() azimuth = np.arctan2(vector.x, vector.y) polar = cls.angle_between(PVector(0, 0, 1), vector) return (length, polar, azimuth)
def transpose(cls, offset, vector): return PVector(*[sum(elems) for elems in zip(offset, vector)])
def __init__(self, **kwargs): self.capacity = kwargs.pop('capacity', 100) self.spawn_velocity = kwargs.pop('spawn_velocity', PVector(-1, 0, 0)) Drawable.__init__(self, **kwargs) list.__init__(self, **kwargs)
def draw_poly(self): pyp.sphere(self.size) pyp.line(self.position, PVector(0, 0, 0))
def __init__(self, **kwargs): self.velocity = kwargs.get('velocity', PVector(0, 0, 0)) self.mass = kwargs.get('mass', 1)