def load(cls, project_type, base_elem, path=None): """ Factory method to create a ProjectObject by reading it from disk and xml. Note: If <project_type> is bounding_box, no file is read and <path> is not relevant. Inputs: project_type (string) - Specifies the project type to construct (valid values are "bounding_box", "voxels", or "meshes") base_elem (ET.Element) - An Element with tag "element" and appropriate sub-elements. path (string) - path to project directory (only required if project_type is voxels or mesh). Return: New ProjectObject Exceptions: ValueError - If base_elem is not <element> or if none of its children is <id>. ValueError - If project_type is not valid. IOError - if file cannot be read. """ (id, pose, category, bounds, symmetry, score, evaluated) = \ cls._parse_xml(base_elem) # load file-based attributes and return the constructed object if project_type == "bounding_box": return ProjectObject.gen_bounding_box_object(id=id, bounds=bounds, pose=pose, category=category, symmetry=symmetry, score=score, evaluated=evaluated) elif project_type == "voxels": voxels = VoxelGrid.from_file(os.path.join(path, id + ".h5")) return ProjectObject.gen_voxels_object(id=id, bounds=bounds, voxels=voxels, pose=pose, category=category, symmetry=symmetry, score=score, evaluated=evaluated) elif project_type == "meshes": meshes = GltfModel.load_from_glb(os.path.join(path, id + ".glb")) return ProjectObject.gen_meshes_object(id=id, bounds=bounds, meshes=meshes, pose=pose, category=category, symmetry=symmetry, score=score, evaluated=evaluated) else: raise ValueError("Invalid project_type: " + project_type)
def test_file_io(self): """ Test round trip writing and reading a simple h5 file. """ temp_directory = tempfile.mkdtemp() filename = os.path.join(temp_directory, "test.h5") points = np.array([[0.1, 0.1, 0.1], [1.1, 2.1, 3.1], [1.3, 2.2, 3.4]]) voxel_size = 0.5 min_corner = Vector3f(-1, -2, -3) vg = VoxelGrid(voxel_size, min_corner=min_corner, points=points) # test writing vg.save(filename) self.assertTrue(os.path.isfile(filename)) # test reading vg2 = VoxelGrid.from_file(filename) self.assertAlmostEquals(voxel_size, vg2.voxel_size) np.testing.assert_array_almost_equal(vg.min_corner, vg2.min_corner) shutil.rmtree(temp_directory)