def load_bvh(geometry, bvh_name="default", auto_build_bvh=True, read_bvh_cache=True, update_bvh_cache=True, cache_dir=None, cuda_device=None): if cache_dir is None: cache = Cache() else: cache = Cache(cache_dir) mesh_hash = geometry.mesh.md5() bvh = None if read_bvh_cache and cache.exist_bvh(mesh_hash, bvh_name): logger.info('Loading BVH "%s" for geometry from cache.' % bvh_name) bvh = cache.load_bvh(mesh_hash, bvh_name) elif auto_build_bvh: logger.info('Building new BVH using recursive grid algorithm.') start = time.time() context = create_cuda_context(cuda_device) bvh = make_recursive_grid_bvh(geometry.mesh, target_degree=3) context.pop() logger.info('BVH generated in %1.1f seconds.' % (time.time() - start)) if update_bvh_cache: logger.info('Saving BVH (%s:%s) to cache.' % (mesh_hash, bvh_name)) cache.save_bvh(bvh, mesh_hash, bvh_name) return bvh
def load_bvh(geometry, bvh_name="default", auto_build_bvh=True, read_bvh_cache=False, update_bvh_cache=True, cache_dir=None, cuda_device=None): if cache_dir is None: cache = Cache() else: cache = Cache(cache_dir) mesh_hash = geometry.mesh.md5() bvh = None if read_bvh_cache and cache.exist_bvh(mesh_hash, bvh_name): logger.info('Loading BVH "%s" for geometry from cache.' % bvh_name) bvh = cache.load_bvh(mesh_hash, bvh_name) elif auto_build_bvh: logger.info('Building new BVH using recursive grid algorithm.') start = time.time() context = create_cuda_context(cuda_device) bvh = make_recursive_grid_bvh(geometry.mesh, target_degree=3) context.pop() logger.info('BVH generated in %1.1f seconds.' % (time.time() - start)) if update_bvh_cache: logger.info('Saving BVH (%s:%s) to cache.' % (mesh_hash, bvh_name)) cache.save_bvh(bvh, mesh_hash, bvh_name) return bvh
def load_bvh(geometry, bvh_name="default", auto_build_bvh=True, read_bvh_cache=True, target_degree=3, update_bvh_cache=True, cache_dir=None, bvh_method='grid', cuda_device=None, cl_device=None): if cache_dir is None: cache = Cache() else: cache = Cache(cache_dir) mesh_hash = geometry.mesh.md5() bvh = None if read_bvh_cache and cache.exist_bvh(mesh_hash, bvh_name): logger.debug('Loading BVH "%s" for geometry from cache.' % bvh_name) bvh = cache.load_bvh(mesh_hash, bvh_name) elif auto_build_bvh: logger.info('Building new BVH using recursive grid algorithm.') start = time.time() # creates quick context to make BVH if api.is_gpu_api_cuda(): context = cutools.create_cuda_context(cuda_device) if bvh_method == 'grid': bvh = make_recursive_grid_bvh(geometry.mesh, target_degree=3) elif bvh_method == 'simple': bvh = make_simple_bvh(geometry.mesh, 3) else: raise ValueError( 'Requestd BVH construction method invalid: %s' % (bvh_method)) context.pop() elif api.is_gpu_api_opencl(): context = cltools.create_cl_context(cl_device) if bvh_method == 'grid': bvh = make_recursive_grid_bvh(geometry.mesh, target_degree=target_degree) elif bvh_method == 'simple': bvh = make_simple_bvh(geometry.mesh, target_degree) else: raise ValueError( 'Requestd BVH construction method invalid: %s' % (bvh_method)) cltools.close_cl_context(context) logger.info('BVH generated in %1.1f seconds.' % (time.time() - start)) if update_bvh_cache: logger.info('Saving BVH (%s:%s) to cache.' % (mesh_hash, bvh_name)) cache.save_bvh(bvh, mesh_hash, bvh_name) return bvh
class TestCacheBVH(unittest.TestCase): def setUp(self): self.cache_dir = random_tempdir('chroma_cache_test') self.cache = Cache(self.cache_dir) self.a = Geometry() self.a.add_solid(Solid(box(1, 1, 1))) self.a.add_solid(Solid(box(1, 1, 1)), displacement=(10, 10, 10)) self.a.flatten() self.b = Geometry() self.b.add_solid(Solid(box(2, 2, 2))) self.b.add_solid(Solid(box(2, 2, 2)), displacement=(10, 10, 10)) self.b.add_solid(Solid(box(2, 2, 2)), displacement=(-10, -10, -10)) self.b.flatten() # c is not in cache self.c = Geometry() self.c.add_solid(Solid(box(2, 2, 2))) self.c.flatten() self.a_hash = self.a.mesh.md5() self.b_hash = self.b.mesh.md5() self.c_hash = self.c.mesh.md5() self.cache.save_geometry('a', self.a) self.cache.save_geometry('b', self.b) def test_list_bvh(self): self.assertEqual(len(self.cache.list_bvh(self.a_hash)), 0) self.cache.save_bvh([], self.a_hash) self.assertIn('default', self.cache.list_bvh(self.a_hash)) self.cache.save_bvh([], self.a_hash, 'foo') self.assertIn('foo', self.cache.list_bvh(self.a_hash)) self.assertEqual(len(self.cache.list_bvh(self.a_hash)), 2) def test_exist_bvh(self): self.cache.save_bvh([], self.a_hash) assert self.cache.exist_bvh(self.a_hash) self.cache.save_bvh([], self.a_hash, 'foo') assert self.cache.exist_bvh(self.a_hash, 'foo') def test_load_bvh_not_found(self): with self.assertRaises(BVHNotFoundError): self.cache.load_bvh(self.c_hash) with self.assertRaises(BVHNotFoundError): self.cache.load_bvh(self.a_hash, 'foo') def test_save_load_new_bvh(self): self.cache.save_bvh([], self.a_hash) self.cache.load_bvh(self.a_hash) self.cache.save_bvh([], self.a_hash, 'foo') self.cache.load_bvh(self.a_hash, 'foo') def test_remove_bvh(self): self.cache.remove_bvh(self.a_hash, 'does_not_exist') self.cache.save_bvh([], self.a_hash) self.cache.save_bvh([], self.a_hash, 'foo') assert self.cache.exist_bvh(self.a_hash) assert self.cache.exist_bvh(self.a_hash, 'foo') self.cache.remove_bvh(self.a_hash) assert not self.cache.exist_bvh(self.a_hash) assert self.cache.exist_bvh(self.a_hash, 'foo') self.cache.remove_bvh(self.a_hash, 'foo') assert not self.cache.exist_bvh(self.a_hash) assert not self.cache.exist_bvh(self.a_hash, 'foo') def tearDown(self): remove_path(self.cache_dir)
class TestCacheBVH(unittest.TestCase): def setUp(self): self.cache_dir = random_tempdir('chroma_cache_test') self.cache = Cache(self.cache_dir) self.a = Geometry() self.a.add_solid(Solid(box(1,1,1))) self.a.add_solid(Solid(box(1,1,1)), displacement=(10,10,10)) self.a.flatten() self.b = Geometry() self.b.add_solid(Solid(box(2,2,2))) self.b.add_solid(Solid(box(2,2,2)), displacement=(10,10,10)) self.b.add_solid(Solid(box(2,2,2)), displacement=(-10,-10,-10)) self.b.flatten() # c is not in cache self.c = Geometry() self.c.add_solid(Solid(box(2,2,2))) self.c.flatten() self.a_hash = self.a.mesh.md5() self.b_hash = self.b.mesh.md5() self.c_hash = self.c.mesh.md5() self.cache.save_geometry('a', self.a) self.cache.save_geometry('b', self.b) def test_list_bvh(self): self.assertEqual(len(self.cache.list_bvh(self.a_hash)), 0) self.cache.save_bvh([], self.a_hash) self.assertIn('default', self.cache.list_bvh(self.a_hash)) self.cache.save_bvh([], self.a_hash, 'foo') self.assertIn('foo', self.cache.list_bvh(self.a_hash)) self.assertEqual(len(self.cache.list_bvh(self.a_hash)), 2) def test_exist_bvh(self): self.cache.save_bvh([], self.a_hash) assert self.cache.exist_bvh(self.a_hash) self.cache.save_bvh([], self.a_hash, 'foo') assert self.cache.exist_bvh(self.a_hash, 'foo') def test_load_bvh_not_found(self): with self.assertRaises(BVHNotFoundError): self.cache.load_bvh(self.c_hash) with self.assertRaises(BVHNotFoundError): self.cache.load_bvh(self.a_hash, 'foo') def test_save_load_new_bvh(self): self.cache.save_bvh([], self.a_hash) self.cache.load_bvh(self.a_hash) self.cache.save_bvh([], self.a_hash, 'foo') self.cache.load_bvh(self.a_hash, 'foo') def test_remove_bvh(self): self.cache.remove_bvh(self.a_hash, 'does_not_exist') self.cache.save_bvh([], self.a_hash) self.cache.save_bvh([], self.a_hash, 'foo') assert self.cache.exist_bvh(self.a_hash) assert self.cache.exist_bvh(self.a_hash, 'foo') self.cache.remove_bvh(self.a_hash) assert not self.cache.exist_bvh(self.a_hash) assert self.cache.exist_bvh(self.a_hash, 'foo') self.cache.remove_bvh(self.a_hash, 'foo') assert not self.cache.exist_bvh(self.a_hash) assert not self.cache.exist_bvh(self.a_hash, 'foo') def tearDown(self): remove_path(self.cache_dir)