def test_mesh_isect(mesh, nrays=1): mgr = ShapeManager() print("Populate shape manager with %i triangles" % mesh.ntriangles()) start = time.clock() populate_mgr_with_triangles(mgr, mesh) print("Population of shape manager took %f seconds" % (time.clock() - start,)) runtimes = [Runtime()] mesh.isect_asm(runtimes, "ray_mesh_isect") assembler = create_assembler() mc = assembler.assemble(asm_code("ray_mesh_isect", mesh)) runtime = runtimes[0] ds = runtime.load("test", mc) mesh.populate_ds(ds, mesh, "mesh1") isector = LinearIsect(mgr) bbox = mesh.bbox() print("Intersection tests begin") for i in range(nrays): ray = generate_ray(bbox) ray.populate_ds(ds, ray, "ray1") hp1 = mesh.isect(ray) hp2 = isector.isect(ray) runtime.run("test") if hp1 is False and hp2 is False and ds['ret'] == 0: continue if hp1 is False and hp2 is not False: print(ray) raise ValueError("Intersection error!") if hp1 is False and ds['ret'] == 1: print(ray) raise ValueError("Intersection error!") compare_floats(hp1.t, hp2.t) compare_floats(hp1.t, ds["hp1.t"]) normal1 = (hp1.normal.x, hp1.normal.y, hp1.normal.z) normal2 = (hp2.normal.x, hp2.normal.y, hp2.normal.z) normal3 = ds["hp1.normal"][0:3] compare_tuple(normal1, normal2) compare_tuple(normal1, normal3) hit1 = (hp1.hit.x, hp1.hit.y, hp1.hit.z) hit2 = (hp2.hit.x, hp2.hit.y, hp2.hit.z) hit3 = ds["hp1.hit"][0:3] compare_tuple(hit1, hit2) compare_tuple(hit1, hit3) if mesh.has_uv(): compare_floats(hp1.u, hp2.u) compare_floats(hp1.v, hp2.v) compare_floats(hp1.u, ds["hp1.u"]) compare_floats(hp1.v, ds["hp1.v"])
v0 = Vector3(2.6, 4.4, 6.6) v1 = Vector3(1.2, 1.1, 1.1) v2 = Vector3(5.1, -1.1, 5.1) t2 = Triangle(v0, v1, v2) mgr = ShapeManager() mgr.add('t1', t) mgr.add('t2', t2) origin = Vector3(0.2, 0.4, 0.6) direction = Vector3(1.2, 1.1, 1.12) direction.normalize() ray = Ray(origin, direction) linear = LinearIsect(mgr) hit = linear.isect(ray) runtime = Runtime() linear.isect_asm([runtime], 'ray_scene_intersection') code = "#DATA \n" code += Ray.asm_struct() + HitPoint.asm_struct() + """ Ray ray1 Hitpoint hp1 uint32 ret #CODE macro mov eax, ray1 macro mov ebx, hp1
counter += 1 def populate_mgr(mgr, fname): meshes = load_meshes(fname) for m in meshes: populate(mgr, m.tb, m.vb) #fname = "F://ray_tracing_scenes/cube/cube.obj" fname = "F://ray_tracing_scenes/dragon/dragon.obj" #fname = "F://ray_tracing_scenes/head/head.obj" mgr = ShapeManager() populate_mgr(mgr, fname) linear = LinearIsect(mgr) runtime = Runtime() linear.isect_asm([runtime], 'ray_scene_intersection') code = "#DATA \n" code += Ray.asm_struct() + HitPoint.asm_struct() + """ Ray ray1 Hitpoint hp1 uint32 ret #CODE macro mov eax, ray1 macro mov ebx, hp1 call ray_scene_intersection mov dword [ret], eax #END
uv0=uv0, uv1=uv1, uv2=uv2) mgr.add(name+str(counter), t) counter += 1 def populate_mgr(mgr, fname): meshes = load_meshes(fname) for m in meshes: populate(mgr, m.tb, m.vb) #fname = "F://ray_tracing_scenes/cube/cube.obj" fname = "F://ray_tracing_scenes/dragon/dragon.obj" #fname = "F://ray_tracing_scenes/head/head.obj" mgr = ShapeManager() populate_mgr(mgr, fname) linear = LinearIsect(mgr) runtime = Runtime() linear.isect_asm([runtime], 'ray_scene_intersection') code = "#DATA \n" code += Ray.asm_struct() + HitPoint.asm_struct() + """ Ray ray1 Hitpoint hp1 uint32 ret #CODE macro mov eax, ray1 macro mov ebx, hp1 call ray_scene_intersection mov dword [ret], eax #END