def args(self): self._tiles = tiles = create_tiles(self._width, self._height, self._nthreads) n = self.nsamples() subx, suby = self._subpixel_location() args = [IntArg('width', self._width), IntArg('height', self._height), FloatArg('pixelsize', self._pixelsize), IntArg('nsamples', n), IntArg('npass', self._pass), IntArg('subx', subx), IntArg('suby', suby)] targs = [StructArg('tile', tile) for tile in tiles] targ = ArgList('tile', targs) args.append(targ) curx_args = [IntArg('curx', 0) for tile in tiles] curx_arg = ArgList('curx', curx_args) args.append(curx_arg) cury_args = [IntArg('cury', tile.y) for tile in tiles] cury_arg = ArgList('cury', cury_args) args.append(cury_arg) endx_args = [IntArg('endx', tile.width) for tile in tiles] endx_arg = ArgList('endx', endx_args) args.append(endx_arg) endy_args = [IntArg('endy', tile.y + tile.height) for tile in tiles] endy_arg = ArgList('endy', endy_args) args.append(endy_arg) return args
def test_isect_b_sph(self): sph_shader = Sphere.isect_b_shader('isect_b_sphere') sph_shader.compile() runtimes = [Runtime()] sph_shader.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_b_sphere(ray, sphere, min_dist) """ direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) r_arg = StructArg('ray', ray) sph_arg = StructArg('sphere', sphere) p1 = IntArg('p1', 6) args = [r_arg, sph_arg, p1] shader = Shader(code=code, args=args) shader.compile([sph_shader.shader]) shader.prepare(runtimes) shader.execute() result = shader.get_value('p1') self.assertEqual(result, 1)
def test_mesh_b(mesh, nrays=1): dep_shader = type(mesh).isect_b_shader('ray_flat_mesh_b_isect') dep_shader.compile() runtimes = [Runtime()] dep_shader.prepare(runtimes) code = """ min_dist = 99999.0 ret = ray_flat_mesh_b_isect(ray, mesh, min_dist) """ origin = calculate_origin(mesh) rpoint = random_in_bbox(mesh._grid.bbox) direction = rpoint - origin direction.normalize() ray = Ray(origin, direction) r_arg = StructArg('ray', ray) mesh_arg = StructArg('mesh', mesh) ret = IntArg('ret', 6) args = [r_arg, mesh_arg, ret] shader = Shader(code=code, args=args) shader.compile([dep_shader.shader]) shader.prepare(runtimes) hp = mesh.isect_b(ray) shader.execute() print("Bool isect", hp, shader.get_value('ret'))
def _func_args(self, spectrum): func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)), IntArg('mat_idx', 0) ] return func_args
def test_isect_b_rect(self): rect_shader = Rectangle.isect_b_shader('isect_b_rectangle') rect_shader.compile() runtimes = [Runtime()] rect_shader.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_b_rectangle(ray, rectangle, min_dist) """ origin = Vector3(3.0, 2.5, 0.0) direction = Vector3(0.0, 0.1, 0.88) direction.normalize() ray = Ray(origin, direction) point = Vector3(0.0, 0.0, 55.92) e1 = Vector3(55.28, 0.0, 0.0) e2 = Vector3(0.0, 54.88, 0.0) normal = Vector3(0.0, 0.0, -1.0) rectangle = Rectangle(point, e1, e2, normal) r_arg = StructArg('ray', ray) sph_arg = StructArg('rectangle', rectangle) p1 = IntArg('p1', 6) args = [r_arg, sph_arg, p1] shader = Shader(code=code, args=args) shader.compile([rect_shader.shader]) shader.prepare(runtimes) shader.execute() result = shader.get_value('p1') self.assertEqual(result, 1)
def argument_factory(name, cls): if cls == int: return IntArg(name, int()) elif cls == float: return FloatArg(name, float()) elif cls == Vector3: return Vec3Arg(name, Vector3.zero()) else: raise ValueError("Argument factory. Unsuported arugment type", name, cls)
def test_linear(self): sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) mgr = ShapeManager() mgr.add('sph1', sphere) sphere2 = Sphere(Vector3(0.0, 2.0, 0.0), 3.0, 0) mgr.add('sph2', sphere2) isector = LinearIsect(mgr) runtimes = [Runtime()] direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) isector.compile() isector.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_scene(ray, hitpoint, min_dist) """ direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) r_arg = StructArg('ray', ray) harg = StructArg('hitpoint', hitpoint) p1 = IntArg('p1', 6) args = [r_arg, harg, p1] shader = Shader(code=code, args=args) shader.compile([isector.shader]) shader.prepare(runtimes) hp2 = isector.isect(ray) shader.execute() hitpoint = shader.get_value('hitpoint') self.assertAlmostEqual(hp2.t, hitpoint.t, places=5) self.assertEqual(hp2.mat_idx, hitpoint.mat_idx) n1 = hp2.normal n2 = hitpoint.normal self.assertAlmostEqual(n1.x, n2.x) self.assertAlmostEqual(n1.y, n2.y, places=6) self.assertAlmostEqual(n1.z, n2.z) self.assertAlmostEqual(hitpoint.hit.x, hp2.hit.x, places=6) self.assertAlmostEqual(hitpoint.hit.y, hp2.hit.y, places=6) self.assertAlmostEqual(hitpoint.hit.z, hp2.hit.z, places=6) result = shader.get_value('p1') self.assertEqual(result, 1)
def test_isect_rect(self): rect_shader = Rectangle.isect_shader('isect_rectangle') rect_shader.compile() runtimes = [Runtime()] rect_shader.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_rectangle(ray, rectangle, hitpoint, min_dist) """ origin = Vector3(3.0, 2.5, 0.0) direction = Vector3(0.0, 0.1, 0.88) direction.normalize() ray = Ray(origin, direction) point = Vector3(0.0, 0.0, 55.92) e1 = Vector3(55.28, 0.0, 0.0) e2 = Vector3(0.0, 54.88, 0.0) normal = Vector3(0.0, 0.0, -1.0) rectangle = Rectangle(point, e1, e2, normal) hitpoint = HitPoint.factory() hitpoint.mat_idx = 5 r_arg = StructArg('ray', ray) sph_arg = StructArg('rectangle', rectangle) harg = StructArg('hitpoint', hitpoint) p1 = IntArg('p1', 6) args = [r_arg, sph_arg, harg, p1] shader = Shader(code=code, args=args) shader.compile([rect_shader.shader]) shader.prepare(runtimes) shader.execute() hp2 = rectangle.isect(ray) hitpoint = shader.get_value('hitpoint') self.assertAlmostEqual(hp2.t, hitpoint.t, places=5) self.assertEqual(hp2.mat_idx, hitpoint.mat_idx) n1 = hp2.normal n2 = hitpoint.normal self.assertAlmostEqual(n1.x, n2.x) self.assertAlmostEqual(n1.y, n2.y) self.assertAlmostEqual(n1.z, n2.z) self.assertAlmostEqual(hitpoint.hit.x, hp2.hit.x, places=5) self.assertAlmostEqual(hitpoint.hit.y, hp2.hit.y, places=5) self.assertAlmostEqual(hitpoint.hit.z, hp2.hit.z, places=5) result = shader.get_value('p1') self.assertEqual(result, 1)
def prepare_standalone(self): runtimes = [Runtime()] self.compile() self.prepare(runtimes) code = """ ret = generate_sample(sample) """ args = [StructArg('sample', Sample.factory()), IntArg('ret', 11)] self._standalone = Shader(code=code, args=args) self._standalone.compile([self.shader]) self._standalone.prepare(runtimes)
def test_mesh(mesh, nrays=1): dep_shader = type(mesh).isect_shader('ray_flat_mesh_isect') dep_shader.compile() runtimes = [Runtime()] dep_shader.prepare(runtimes) code = """ min_dist = 99999.0 ret = ray_flat_mesh_isect(ray, mesh, hitpoint, min_dist) """ ray = generate_ray(mesh) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) r_arg = StructArg('ray', ray) mesh_arg = StructArg('mesh', mesh) harg = StructArg('hitpoint', hitpoint) ret = IntArg('ret', 6) args = [r_arg, mesh_arg, harg, ret] shader = Shader(code=code, args=args) shader.compile([dep_shader.shader]) shader.prepare(runtimes) for i in range(nrays): ray = generate_ray(mesh) # origin = Vector3(-0.21099534597992897,-0.02090535280108452,-0.09716709856688976) # direction = Vector3(0.7856996643888073,0.4629769683728137,0.4102783983292736) # ray = Ray(origin, direction) shader.set_value('ray', ray) hp2 = mesh.isect(ray) hp, index = isect_ray_mesh(ray, mesh) shader.execute() # print(hp, shader.get_value('ret')) if hp: ret = shader.get_value('ret') hp_new = shader.get_value('hitpoint') if round(hp.t - hp_new.t, 5) != 0: print(hp.t, hp_new.t, ret, index, hp2.t) print(ray.origin) print(ray.direction) p0, p1, p2 = mesh.get_points(index) print(p0) print(p1) print(p2) print('------------------------------------------')
def test_isect_flat_triangle1(self): runtimes = [Runtime()] tri_shader = FlatTriangle.isect_shader('isect_flat_triangle') tri_shader.compile() tri_shader.prepare(runtimes) p0 = Vector3(-0.0831229984, 0.0591476, -0.03213749) p1 = Vector3(-0.082775, 0.059025, -0.031787) p2 = Vector3(-0.0831229, 0.0591773, -0.031787) origin = Vector3(-0.21276909825205803, -0.021492251798510553, -0.09822520208358765) direction = Vector3(0.7788769269741005, 0.4843782624535974, 0.3984073687694737) ray = Ray(origin, direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) t = FlatTriangle(p0, p1, p2) code = """ min_dist = 150.0 ret = isect_flat_triangle(ray, triangle, hitpoint, min_dist) """ r_arg = StructArg('ray', ray) tri_arg = StructArg('triangle', t) harg = StructArg('hitpoint', hitpoint) ret = IntArg('ret', 6) args = [r_arg, tri_arg, harg, ret] shader = Shader(code=code, args=args) shader.compile([tri_shader.shader]) shader.prepare(runtimes) shader.execute() min_dist = 150.0 hit = t.isect(ray, min_dist) hp = shader.get_value('hitpoint') self.assertAlmostEqual(hit.t, hp.t, places=6) self._almost_equal_vec3(hit.hit, hp.hit, places=6) self._almost_equal_vec3(hit.normal, hp.normal, places=6) self.assertEqual(hit.mat_idx, hp.mat_idx) self.assertAlmostEqual(hit.u, hp.u) self.assertAlmostEqual(hit.v, hp.v)
def test_isect_flat_triangle(self): runtimes = [Runtime()] tri_shader = FlatTriangle.isect_shader('isect_flat_triangle') tri_shader.compile() tri_shader.prepare(runtimes) p0 = Vector3(2.2, 4.4, 6.6) p1 = Vector3(1.1, 1.1, 1.1) p2 = Vector3(5.1, -1.1, 5.1) origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(3, 3.0, 3.01) direction.normalize() ray = Ray(origin, direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) t = FlatTriangle(p0, p1, p2) code = """ min_dist = 150.0 ret = isect_flat_triangle(ray, triangle, hitpoint, min_dist) """ r_arg = StructArg('ray', ray) tri_arg = StructArg('triangle', t) harg = StructArg('hitpoint', hitpoint) ret = IntArg('ret', 6) args = [r_arg, tri_arg, harg, ret] shader = Shader(code=code, args=args) shader.compile([tri_shader.shader]) shader.prepare(runtimes) shader.execute() min_dist = 150.0 hit = t.isect(ray, min_dist) hp = shader.get_value('hitpoint') self.assertAlmostEqual(hit.t, hp.t, places=6) self._almost_equal_vec3(hit.hit, hp.hit, places=6) self._almost_equal_vec3(hit.normal, hp.normal, places=6) self.assertEqual(hit.mat_idx, hp.mat_idx) self.assertAlmostEqual(hit.u, hp.u) self.assertAlmostEqual(hit.v, hp.v)
def test_isect_sph(self): sph_shader = Sphere.isect_shader('isect_sphere') sph_shader.compile() runtimes = [Runtime()] sph_shader.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_sphere(ray, sphere, hitpoint, min_dist) """ direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) r_arg = StructArg('ray', ray) sph_arg = StructArg('sphere', sphere) harg = StructArg('hitpoint', hitpoint) p1 = IntArg('p1', 6) args = [r_arg, sph_arg, harg, p1] shader = Shader(code=code, args=args) shader.compile([sph_shader.shader]) shader.prepare(runtimes) shader.execute() hp2 = sphere.isect(ray) hitpoint = shader.get_value('hitpoint') self.assertAlmostEqual(hp2.t, hitpoint.t) self.assertEqual(hp2.mat_idx, hitpoint.mat_idx) n1 = hp2.normal n2 = hitpoint.normal self.assertAlmostEqual(n1.x, n2.x) self.assertAlmostEqual(n1.y, n2.y) self.assertAlmostEqual(n1.z, n2.z) self.assertAlmostEqual(hitpoint.hit.x, hp2.hit.x) self.assertAlmostEqual(hitpoint.hit.y, hp2.hit.y) self.assertAlmostEqual(hitpoint.hit.z, hp2.hit.z) result = shader.get_value('p1') self.assertEqual(result, 1)
def test_isect_b_flat_triangle(self): runtimes = [Runtime()] tri_shader = FlatTriangle.isect_b_shader('isect_b_flat_triangle') tri_shader.compile() tri_shader.prepare(runtimes) p0 = Vector3(2.2, 4.4, 6.6) p1 = Vector3(1.1, 1.1, 1.1) p2 = Vector3(5.1, -1.1, 5.1) origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(3, 3.0, 3.01) direction.normalize() ray = Ray(origin, direction) t = FlatTriangle(p0, p1, p2) code = """ min_dist = 150.0 ret = isect_b_flat_triangle(ray, triangle, min_dist) """ r_arg = StructArg('ray', ray) tri_arg = StructArg('triangle', t) ret = IntArg('ret', 6) args = [r_arg, tri_arg, ret] shader = Shader(code=code, args=args) shader.compile([tri_shader.shader]) shader.prepare(runtimes) shader.execute() min_dist = 150.0 hit = t.isect_b(ray, min_dist) hit2 = shader.get_value('ret') if hit2 == 0: hit2 = False elif hit2 == 1: hit2 = True else: raise ValueError("Unexpected value for isect flat triangle ", hit2) self.assertEqual(hit, hit2)
def _get_visibility_code(self, shape_type): num_objects = 'num_vobjects_%s' % id(shape_type) obj_array_name = 'obj_varray_%s' % id(shape_type) isect_object = 'ray_object_isect_b_%s' % id(shape_type) code = """ i = 0 while i < %s: shape_object = %s[i] ret = %s(ray, shape_object, distance) if ret == 1: return 0 i = i + 1 """ % (num_objects, obj_array_name, isect_object) darr = self.shp_mgr._shape_arrays[shape_type] nobj = IntArg(num_objects, len(darr)) arr_arg = ArrayArg(obj_array_name, darr) return code, [nobj, arr_arg]
def test_light_manager(self): sam_mgr = SampledManager() register_rgb_shadepoint() runtimes = [Runtime(), Runtime()] lgt = GeneralLight() lgt.load('point', sam_mgr, spectral=False) lgt2 = GeneralLight() lgt2.load('point', sam_mgr, spectral=False) lgt2.set_value('intensity', RGBSpectrum(0.3, 0.3, 0.3)) lgt2.set_value('position', Vector3(2.0, 2.0, 2.0)) mgr = LightManager() mgr.add('light1', lgt) mgr.add('light2', lgt2) mgr.compile_shaders(sam_mgr, spectral=False) mgr.prepare_shaders(runtimes) code = """ hp = HitPoint() hp.hit = (4.0, 5, 6) sp = ShadePoint() light_radiance(hp, sp, 1) wi = sp.wi n = number_of_lights() """ wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) nlights = IntArg('n', 999) shader = Shader(code=code, args=[wi, nlights]) shader.compile(shaders=[mgr.rad_shader, mgr.nlights_shader]) shader.prepare(runtimes) shader.execute() wi = Vector3(2.0, 2.0, 2.0) - Vector3(4.0, 5.0, 6.0) wi.normalize() wi_s = shader.get_value('wi') self.assertAlmostEqual(wi.x, wi_s.x) self.assertAlmostEqual(wi.y, wi_s.y) self.assertAlmostEqual(wi.z, wi_s.z) self.assertEqual(2, shader.get_value('n'))
def test_linear_visiblity(self): sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) mgr = ShapeManager() mgr.add('sph1', sphere) sphere2 = Sphere(Vector3(0.0, 2.0, 0.0), 3.0, 0) mgr.add('sph2', sphere2) isector = LinearIsect(mgr) runtimes = [Runtime()] direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) isector.compile() isector.prepare(runtimes) code = """ p1 = (9, 8, 7) p2 = (-2, -5, -3) ret = visibility(p1, p2) """ ret = IntArg('ret', 6) args = [ret] shader = Shader(code=code, args=args) shader.compile([isector.visible_shader]) shader.prepare(runtimes) p1 = Vector3(9.0, 8.0, 7.0) p2 = Vector3(-2.0, -5.0, -3.0) ret = isector.visibility(p1, p2) shader.execute() ret_s = shader.get_value('ret') if ret is True and ret_s == 0: raise ValueError("Linear visiblity is calculated wrong", ret, ret_s) if ret is False and ret_s == 1: raise ValueError("Linear visiblity is calculated wrong", ret, ret_s)
def _get_shape_code(self, shape_type): num_objects = 'num_objects_%s' % id(shape_type) obj_array_name = 'obj_array_%s' % id(shape_type) isect_object = 'ray_object_isect_%s' % id(shape_type) code = """ i = 0 while i < %s: shape_object = %s[i] ret = %s(ray, shape_object, hitpoint, min_dist) if ret == 1: hit_happend = 1 if hitpoint.t < min_dist: min_dist = hitpoint.t i = i + 1 """ % (num_objects, obj_array_name, isect_object) darr = self.shp_mgr._shape_arrays[shape_type] nobj = IntArg(num_objects, len(darr)) arr_arg = ArrayArg(obj_array_name, darr) return code, [nobj, arr_arg]
def _lgt_emission(self, color_mgr): code = """ if light_id < 0: shadepoint.light_intensity = Spectrum(0.0) shadepoint.light_pdf = 1.0 else: ptr_func = lgt_ptrs[light_id] __light_emission(hitpoint, shadepoint, ptr_func) """ lgt_ptrs = ArrayArg('lgt_ptrs', PtrsArray()) al = ArgList('lgt_ptrs', [lgt_ptrs]) spec = color_mgr.zero() func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spec)), IntArg('light_id', -1) ] args = [al] self.emission_shader = Shader(code=code, args=args, name='light_emission', func_args=func_args, is_func=True)