def far_point_subsample(mesh, n, seed=None, center_scale=False): # seed should be a dict with key mesh name and value list of seed vertices N x 3 (where N is number of points) # return val is mesh object that I wrote v = mesh.vertices if seed is None: seed_t = np.empty([0,0]) else: seed_t = seed[mesh.name] if n > v.shape[0] or n < seed_t.shape[0]: raise ValueError('n larger than number of vertices or smaller than number of seed_t points') if isinstance(seed_t, ndarray) and seed_t.size and v.shape[1] == 3 and v.ndim == 2: if seed_t.ndim == 1 and seed_t.shape[0] == 3: seed_t = np.array([seed_t]) # are s in v (or close enough?) if all([any(all(isclose(x, v), 1)) for x in seed_t]): # get ind for seed_t points subidx = [where(all(isclose(x, v), axis=1))[0][0] for x in seed_t] else: raise ValueError('seed improperly formed, expecting n x 3 array') else: random.seed() subidx = [random.randint(0, v.shape[0]-1)] for i in range(len(subidx),n): subidx.append(argmax(amin(cdist(v[subidx], v), axis=0))) # list of integers that subsampled return MeshFactory.mesh_from_data(v[subidx], center_scale=center_scale, name=mesh.name)
def exportAlignedMeshes(Auto3dgmData, exportFolder, phase=2): from auto3dgm_nazar.mesh.meshexport import MeshExport from auto3dgm_nazar.mesh.meshfactory import MeshFactory if phase == 1: label = "Phase 1" elif phase == 2: label = "Phase 2" else: raise ValueError( 'Unaccepted phase number passed to Auto3dgmLogic.exportAlignedMeshes' ) m = Auto3dgmData.datasetCollection.datasets[0] r = Auto3dgmData.datasetCollection.analysis_sets[ label].globalized_alignment['r'] for idx, mesh in enumerate(m): new_vertices = np.transpose(r[idx] @ np.transpose(mesh.vertices)) new_faces = mesh.faces.astype('int64') new_mesh = MeshFactory.mesh_from_data(new_vertices, faces=new_faces, name=mesh.name, center_scale=True, deep=True) MeshExport.writeToFile(exportFolder, new_mesh, format='ply')
def ds_from_filelist(filelist): meshes = [] mesheslist = [] for file in filelist: meshes.append(MeshFactory.mesh_from_file(file)) mesheslist.append(meshes) return (DatasetCollection(datasets=mesheslist))
def ds_from_dir(dir_path, center_scale=False): c = [join(dir_path, x) for x in listdir(dir_path)] files = [ f for f in c if isfile(f) and splitext(f)[1] in DatasetFactory.__ftypes ] if not files: msg = 'No appropriate files were found in ' + dir_path raise OSError(msg) meshes = [] mesheslist = [] for file in files: meshes.append(MeshFactory.mesh_from_file(file, center_scale)) mesheslist.append(meshes) return (DatasetCollection(datasets=mesheslist))
def ds_from_meshdata(vertices, faces, deep=True): for i in range(0, len(vertices)): meshes.append( MeshFactory.mesh_from_data(vertices[i], faces[i], deep)) mesheslist.append(meshes) return (DatasetCollection(datasets=mesheslist))
array([[ 1.38777878e-16, 1.00000000e+00, 2.49800181e-16], [ -1.11022302e-16, -3.60822483e-16, 1.00000000e+00], [ 1.00000000e+00, -1.38777878e-17, 1.66533454e-16]])) ''' print(AA[1]) ''' Out[40]: array([[ 4.49640325e-15, 1.00000000e+00, -3.87190280e-15], [ -3.94129174e-15, 3.94129174e-15, 1.00000000e+00], [ 1.00000000e+00, -4.38538095e-15, 4.21884749e-15]]) ''' # So the alignment works as expected. However, when we integrate the mesh class: mesh1 = MeshFactory.mesh_from_data(A) mesh2 = MeshFactory.mesh_from_data(B) BB = Correspondence.best_pairwise_PCA_alignment(mesh1, mesh2, 0) print(BB) ''' Out[45]: (array([3, 1, 2, 0]), array([[ 0.18660992, -0.95001991, -0.25027765], [ 0.02395067, -0.25027765, 0.96787781], [ 0.9821421 , 0.18660992, 0.02395067]])) ''' AA = Correspondence.locgpd(mesh1, mesh2, 0, 0, 5, 0) print(AA[1])
#Test script for the mesh factory from auto3dgm_nazar.mesh.meshfactory import MeshFactory #Test case 0: Giving out a non-sense file #Conditions: filestring refers to a non existing file #Expect: When I try to create a mesh, I get an error filestring = '/home/safari/Desktop/tutkimus/Slicer/HackathonJAN/testdata/20_Test_Teeth_PLY/Non-existing file.ply' MeshFactory.mesh_from_file(filestring) ''' In [27]: filestring='/home/safari/Desktop/tutkimus/Slicer/HackathonJAN/testdata/20_Test_Teeth_PLY/Non-existing file.ply' In [28]: MeshFactory.mesh_from_file(filestring) --------------------------------------------------------------------------- OSError Traceback (most recent call last) <ipython-input-28-dce76620cc80> in <module>() ----> 1 MeshFactory.mesh_from_file(filestring) /home/safari/Desktop/tutkimus/Slicer/HackathonJAN/gitstuff/auto3dgm/auto3dgm/mesh/meshfactory.py in mesh_from_file(file_path) 52 msg = 'File {} not present or not allowed filetype: {}'.format( 53 file_path, ', '.join(allowed_filetypes)) ---> 54 raise OSError(msg) 55 56 @staticmethod OSError: File /home/safari/Desktop/tutkimus/Slicer/HackathonJAN/testdata/20_Test_Teeth_PLY/Non-existing file.ply not present or not allowed filetype: .ply, .obj, .stl ''' # Result: Success! #Test case 1: Giving out an invalid #Conditions: filestring refers to a file type not supported #Expect: When I try to create a mesh, I get an error
def print_polyfaces(polydata): cell_n = polydata.GetNumberOfCells() point_n = polydata.GetCell(1).GetNumberOfPoints() faces_new = zeros((cell_n, point_n), dtype=int) for i in range(cell_n): print('cell/polygon ' + str(i)) for j in range(point_n): print(polydata.GetCell(i).GetPointId(j)) vertices = array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [2, 2, 1]], dtype=int) faces = array([[0, 1, 2], [1, 2, 3], [1, 3, 4], [2, 3, 0]], dtype=int) mesh = MeshFactory.mesh_from_data(vertices, faces, False) print(mesh) print(mesh.vertices) print(mesh.faces) """ Print output should look like this: <auto3dgm.mesh.mesh.Mesh object at 0x1048962b0> [[0. 0. 0.] [1. 0. 0.] [1. 1. 0.] [0. 1. 0.] [2. 2. 1.]] [[0 1 2] [1 2 3]
from auto3dgm_nazar.mesh.meshfactory import MeshFactory from auto3dgm_nazar.mesh.subsample import Subsample from numpy import array vertices = array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [2, 2, 1]]) seed = vertices[[1, 4]] mesh = MeshFactory.mesh_from_data(vertices) submesh_noseed = Subsample.far_point_subsample(mesh, 4) print(submesh_noseed) print(submesh_noseed.vertices) print(submesh_noseed.faces) submesh_seed = Subsample.far_point_subsample(mesh, 4, seed) print(submesh_seed) print(submesh_seed.vertices) print(submesh_seed.faces) """ Print results for first set will be random, but second set will be stable Print results should look something like this: <auto3dgm.mesh.mesh.Mesh object at 0x117cd5518> [[0 0 0] [2 2 1] [1 1 0] [1 0 0]] [] <auto3dgm.mesh.mesh.Mesh object at 0x10f5e31d0> [[1 0 0]