def parse_from_description(description_list): triangle = Triangle() for description in description_list: if description[0] == "a": triangle.a = vector.Vector3( device_control.to_gpu_if_possible( parse_utils.parse_tensor_from_string_toks( description[1:])).view(1, -1)) elif description[0] == "b": triangle.b = vector.Vector3( device_control.to_gpu_if_possible( parse_utils.parse_tensor_from_string_toks( description[1:])).view(1, -1)) elif description[0] == "c": triangle.c = vector.Vector3( device_control.to_gpu_if_possible( parse_utils.parse_tensor_from_string_toks( description[1:])).view(1, -1)) else: triangle.load_default_property(description) triangle.plane = plane.Plane(triangle.a, triangle.b, triangle.c) return triangle
def _extract_shape_statistics(shapes): coeffs = [] for shape in shapes: shape_coeff = [ shape.ambient_coeff, shape.diffuse_coeff, shape.specular_coeff, shape.specular_exponent, shape.refractive_index, shape.ref_coeff ] # 0 1 2 3 4 5 coeffs.append(shape_coeff) coeff_tensor = device_control.to_gpu_if_possible( torch.FloatTensor(coeffs)) return coeff_tensor
def _get_initial_rays(self, image_dimension): indices = np.indices((image_dimension, image_dimension)).reshape( (2, -1)).transpose() indices_torch = device_control.to_gpu_if_possible( torch.FloatTensor(indices)) multiplier = 1.4 normalized_i = (-indices_torch[:, 0] / image_dimension) + 0.5 normalized_j = (indices_torch[:, 1] / image_dimension) - 0.5 normalized_i *= multiplier normalized_j *= multiplier ray_directions = self.camera.get_right_vector().scale_by( normalized_j) + self.camera.get_up_vector().scale_by( normalized_i) + self.camera.get_forward_vector() return ray.Ray(self.camera.position, ray_directions)
def parse_dsl_file(file_path): def parse_shape(description): object_type = description[0][0] description = description[1:] if object_type == "CHECKERBOARD": return checkerboard.CheckerBoard.parse_from_description( description) if object_type == "SPHERE": return sphere.Sphere.parse_from_description(description) if object_type == "TRIANGLE": return triangle.Triangle.parse_from_description(description) with open(file_path, 'r') as f_in: shapes = [] lights = [] for line in f_in: line = line.strip() if len(line) == 0: continue line_toks = line.split() if line_toks[0] == "recDepth": recursive_depth = int(line_toks[1]) elif line_toks[0] == "pixels": image_dimension = int(line_toks[1]) elif line_toks[0] == "light": light_position = torch.FloatTensor( [[float(elm) for elm in line_toks[1:]]]) light_position = vector.Vector3( device_control.to_gpu_if_possible(light_position)) lights.append(light_position) elif line_toks[0] == "objStart": current_shape_desciprtion = [[line_toks[1]]] elif line_toks[0] == "objEnd": parsed_shape = parse_shape(current_shape_desciprtion) if parsed_shape is not None: shapes.append(parsed_shape) else: current_shape_desciprtion.append(line_toks) return shapes, lights, recursive_depth, image_dimension
from geometric_shapes import checkerboard from geometric_primitives import vector from control import device_control import torch from ray_tracing import trace_rays from main_package import camera, display_utils c1 = device_control.to_gpu_if_possible(torch.FloatTensor([1.0, 1.0, 1.0])) c2 = device_control.to_gpu_if_possible(torch.FloatTensor([0.0, 0.0, 0.0])) ckbd = checkerboard.CheckerBoard(5, c1, c2, .4, .1, .4, .1, 3.) shapes = [ckbd] l1 = vector.Vector3( device_control.to_gpu_if_possible(torch.FloatTensor([70.0, 70.0, 70.0]))) l2 = vector.Vector3( device_control.to_gpu_if_possible(torch.FloatTensor([-70.0, 1.0, -70.0]))) lights = [l1, l2] camera = camera.Camera() rt = trace_rays.RayTracer(shapes, lights, camera, 1) traced = rt.trace_rays(300) display_utils.display_numpy_image(traced)
def parse_tensor_from_string_toks(toks): toks = [float(elm) for elm in toks] toks = torch.FloatTensor(toks) return device_control.to_gpu_if_possible(toks)