def is_shadowed(self, light_position, hit_point): vec = subtract(light_position, hit_point) distance = magnitude(vec) direction = normalize(vec) ray = Ray(hit_point, direction) intersections = self.intersect(ray) hit_intersection = shadow_hit(intersections) return hit_intersection is not None and hit_intersection.t < distance
def normal_to_world(self, normal): normal = multiply_tuple(transpose(self.__inverse_transform), normal) normal[3] = 0 normal = normalize(normal) if self.parent: normal = self.parent.normal_to_world(normal) return normal
def main(): p = dict(position=point(0, 1, 0), velocity=normalize(vector(1, 1, 0))) e = dict(gravity=vector(0, -0.1, 0), wind=vector(-0.01, 0, 0)) while p['position'][1] > 0.0: print( f"position {p['position'][0]}, {p['position'][1]}, {p['position'][2]}" ) p = tick(e, p)
def ray_for_pixel(self, pixel_x, pixel_y): xoffset = (pixel_x + 0.5) * self.pixel_size yoffset = (pixel_y + 0.5) * self.pixel_size world_x = self.half_width - xoffset world_y = self.half_height - yoffset pixel = multiply_tuple(self.__inverse_transform, point(world_x, world_y, -1)) origin = multiply_tuple(self.__inverse_transform, point(0, 0, 0)) direction = normalize(subtract(pixel, origin)) return Ray(origin, direction)
def main(): p = dict(position=point(0, 1, 0), velocity=multiply(normalize(vector(1, 1.8, 0)), 11.25)) e = dict(gravity=vector(0, -0.1, 0), wind=vector(-0.01, 0, 0)) c = Canvas(900, 550) while p['position'][1] > 0.0: print( f"position {p['position'][0]}, {p['position'][1]}, {p['position'][2]}" ) c.set_pixel(round(p['position'][0]), c.height - round(p['position'][1]), color(0.0, 1.0, 0.0)) p = tick(e, p) with open('cannon.ppm', 'w') as out_file: out_file.write(c.to_ppm())
def __init__(self, p1, p2, p3, n1=None, n2=None, n3=None): super().__init__() self.p1 = p1 self.p2 = p2 self.p3 = p3 self.n1 = n1 self.n2 = n2 self.n3 = n3 self.smoothed = False # pre-compute edge vectors self.e1 = subtract(p2, p1) self.e2 = subtract(p3, p1) # pre-compute normal self.normal = normalize(cross(self.e2, self.e1))
def _evaluateHiddenState(self,obs,use_ext=True): nobs = len(obs) nmix = self._nstates ln2pi = np.log(2.0 * np.pi) z = np.tile(np.log(self.pi) + 0.5 * self._elnt - 0.5 * ln2pi ,(nobs,1)) if use_ext and ext_imported : pass else : for k in range(nmix): # very slow! need Fortran or C codes dobs = obs - self.m[k] z[:,k] -= 0.5 * (1.0/self.beta[k] + self.nu[k]*self.s[k]*(dobs**2)) z = z - z.max(1)[np.newaxis].T z = np.exp(z) z = normalize(z,1) return z
def _evaluateHiddenState(self, obs, use_ext=True): nobs = len(obs) nmix = self._nstates ln2pi = np.log(2.0 * np.pi) z = np.tile( np.log(self.pi) + 0.5 * self._elnt - 0.5 * ln2pi, (nobs, 1)) if use_ext and ext_imported: pass else: for k in xrange(nmix): # very slow! need Fortran or C codes dobs = obs - self.m[k] z[:, k] -= 0.5 * (1.0 / self.beta[k] + self.nu[k] * self.s[k] * (dobs**2)) z = z - z.max(1)[np.newaxis].T z = np.exp(z) z = normalize(z, 1) return z
def main(): canvas_pixels = 500 canvas = Canvas(canvas_pixels, canvas_pixels) shape = Sphere() # assign material shape.material = Material() shape.material.color = color(1, 0.2, 1) light_position = point(-10, 10, -10) light_color = color(1, 1, 1) light = PointLight(light_position, light_color) ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7.0 pixel_size = wall_size / canvas_pixels half = wall_size / 2 for y in range(canvas_pixels): world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x pos = point(world_x, world_y, wall_z) r = Ray(ray_origin, normalize(subtract(pos, ray_origin))) xs = shape.intersect(r) shape_hit = hit(xs) if shape_hit is not None: hit_point = r.position_at(shape_hit.t) normal = shape_hit.object.normal_at(hit_point) eye = negate(r.direction) px_color = lighting(shape_hit.object.material, shape_hit.object, light, hit_point, eye, normal) canvas.set_pixel(x, y, px_color) with open('render_phong_sphere.ppm', 'w') as out_file: out_file.write(canvas.to_ppm())
def main(): canvas_pixels = 400 canvas = Canvas(canvas_pixels, canvas_pixels) red = color(1, 0, 0) shape = Sphere() # shrink it along the y axis #shape.set_transform(scaling(1, 0.5, 1)) # shrink it along the x axis #shape.set_transform(scaling(0.5, 1, 1)) # shrink it, and rotate it! # shape.set_transform(multiply_matrix(rotation_z(pi / 4), scaling(0.5, 1, # 1))) # shrink it, and skew it! # shape.set_transform( # multiply_matrix(shearing(1, 0, 0, 0, 0, 0), scaling(0.5, 1, 1))) ray_origin = point(0, 0, -5) wall_z = 10 wall_size = 7.0 pixel_size = wall_size / canvas_pixels half = wall_size / 2 for y in range(canvas_pixels): world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x pos = point(world_x, world_y, wall_z) r = Ray(ray_origin, normalize(subtract(pos, ray_origin))) xs = shape.intersect(r) if hit(xs) is not None: canvas.set_pixel(x, y, red) with open('render_sphere.ppm', 'w') as out_file: out_file.write(canvas.to_ppm())
def step_assert_n_equals_normalized_n(context): assert_tuple(context.n, normalize(context.n))
def step_create_eye_vector_v1_from_eye_to_p(context): context.v1 = normalize(subtract(context.eye, context.p))
datasets = DataLoader(dataset, batch_size=32, shuffle=True) for idx, (data, label) in enumerate(datasets): pass # test modeler # get data and configures net = RNN_Net(5, 12, 5) opt = optim.Adam(net.parameters(), lr=1e-3) lr_decay = optim.lr_scheduler.ReduceLROnPlateau(opt) seq = get_mat_data(f'Data/linear_signals.mat', f'linear_signals') # train_test_split1 测试 train_sub, test_sub = train_test_split1(seq) print(train_sub, test_sub, train_sub.shape, test_sub.shape) # 归一化测试 train_sub, test_sub = normalize(train_sub, test_sub) print(train_sub, test_sub, train_sub.shape, test_sub.shape) # series2xy 测试 X_train, y_train = series2xy(train_sub) print(X_train, y_train) # train_test_split2 测试 X_train, X_valid, y_train, y_valid = train_test_split2(X_train, y_train, split=0.7) print(X_train, X_valid, y_train, y_valid) print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape) train_loader, valid_loader, test_loader = make_loader(seq, tt_split=0.7,