def pyoctree_volume(mesh): try: from pyoctree import pyoctree except ImportError: pytest.skip("pyoctree not available") else: vertices = np.asarray(mesh.points, dtype=np.float64, order="C") faces = np.asarray(mesh.cells_dict["triangle"], dtype=np.int32, order="C") octree = pyoctree.PyOctree(vertices, faces) return PyOctreeWrapper(vertices, faces, octree)
def _in_volume_ray(points, volume): """ Uses pyoctree's raycsasting to test if points are within a given CATMAID volume. """ tree = getattr(volume, 'pyoctree', None) if not tree: # Create octree from scratch tree = pyoctree.PyOctree( np.array(volume.vertices, dtype=float, order='C'), np.array(volume.faces, dtype=np.int32, order='C')) volume.pyoctree = tree # Get min max of volume mx = np.array(volume.vertices).max(axis=0) mn = np.array(volume.vertices).min(axis=0) # Get points outside of bounding box out = (points > mx).any(axis=1) | (points < mn).any(axis=1) isin = ~out in_points = points[~out] # Perform ray intersection on points inside bounding box rayPointList = np.array([[[p[0], p[1], mn[2]], [p[0], p[1], mx[2]]] for p in in_points], dtype=np.float32) # Unfortunately rays are bidirectional -> we have to filter intersections # to those that occur "above" the point we are querying intersections = [ len([ i for i in tree.rayIntersection(ray) if i.p[2] >= in_points[k][2] ]) for k, ray in enumerate(rayPointList) ] # Count intersections and return True for odd counts # [i % 2 != 0 for i in intersections] isin[~out] = np.remainder(list(intersections), 2) != 0 return isin
def in_volume_pyoc(points: np.ndarray, volume: Volume, n_rays: Optional[int] = 1) -> Sequence[bool]: """Use pyoctree's raycasting to test if points are within a given volume.""" if isinstance(n_rays, type(None)): n_rays = 1 if not isinstance(n_rays, (int, np.integer)): raise TypeError(f'n_rays must be integer, got "{type(n_rays)}"') if n_rays <= 0: raise ValueError('n_rays must be > 0') tree = getattr(volume, 'pyoctree', None) if not tree: # Create octree from scratch tree = pyoctree.PyOctree( np.array(volume.vertices, dtype=float, order='C'), np.array(volume.faces, dtype=np.int32, order='C')) volume.pyoctree = tree # type: ignore # Get min max of volume mx = np.array(volume.vertices).max(axis=0) mn = np.array(volume.vertices).min(axis=0) dm = mx - mn # Remove points outside of bounding box is_out = (points > mx).any(axis=1) | (points < mn).any(axis=1) # Cast rays # There is no point of vectorizing this because pyoctree's rayIntersection # does only take a single ray at a time... for i in range(n_rays): # Process only point that we think could be in in_points = points[~is_out] # If no in points left, break out if in_points.size == 0: break # Pick a random point inside the volumes bounding box as origin origin = np.random.rand(3) * dm + mn # Generate ray point list: rayPointList = np.array([[origin, p] for p in in_points], dtype=np.float32) # Get intersections and extract coordinates of intersection intersections = [ np.array([i.p for i in tree.rayIntersection(ray)]) for ray in rayPointList ] # In a few odd cases we can get the multiple intersections at the exact # same coordinate (something funny with the faces). unique_int = [ np.unique(np.round(i), axis=0) if np.any(i) else i for i in intersections ] # Unfortunately rays are bidirectional -> we have to filter intersections # to those that occur "above" the point we are querying unilat_int = [ i[i[:, 2] >= p] if np.any(i) else i for i, p in zip(unique_int, in_points[:, 2]) ] # Count intersections int_count = [i.shape[0] for i in unilat_int] # Get even (= outside volume) numbers of intersections is_even = np.remainder(int_count, 2) == 0 # Set outside points is_out[~is_out] = is_even return ~is_out
def texture_vis(model, image, data, v_t, mask): mesh = copy(_TEMPLATE_MESH) my_tex = np.zeros((1000, 1000, image.shape[2]), dtype=image.dtype) #cmesh = Mesh(_os.path.join(_os.path.dirname(__file__), # 'template-bodyparts-corrected-labeled-split5.ply')) #mesh.vc = cmesh.vc.copy() mesh.vc = [0.91, 0.67, 0.91] model.betas[:len(data['betas'])] = data['betas'] model.pose[:] = data['pose'] model.trans[:] = data['trans'] mesh.v = v_t #pdb.set_trace() mesh.v = np.array( _verts_core(model.pose, mesh.v, model.J, model.weights, model.kintree_table)) #pdb.set_trace() #new_v_t = _verts_core(model.pose, v_t, model.J, model.weights, model.kintree_table) #mesh.v = mesh.v + np.array(new_v_t) largura, altura = (image.shape[1], image.shape[0]) data['cam_c'] = np.array([largura, altura]) / 2. tex_vt, tex_faces = Template_tex() vert = mesh.v + np.array(data['trans']).reshape((1, 3)) faces = np.array(mesh.f.copy(), dtype='int32') tree = _pyoctree.PyOctree(vert, faces) cp = np.zeros((vert.shape[0], 4)) #my_map_partes = np.zeros(vert.shape[0]).astype(int) for i, f in enumerate(mesh.f): #if ((i < 300) or (i > 5046)): # continue I_VIS = True face_media = (vert[f[0]] + vert[f[1]] + vert[f[2]]) / 3.0 rayList = np.array([np.zeros(3), face_media], dtype=np.float32) intersectionFound = tree.rayIntersection(rayList) if intersectionFound: if dist(intersectionFound[0].p, face_media) > 0.0001: I_VIS = False if I_VIS: tri1 = np.zeros((3, 2), np.float32) tri2 = np.zeros((3, 2), np.float32) tri1[0, 0] = (vert[f[0]][1] * data['f']) / (vert[f[0]][2]) + data['cam_c'][1] tri1[0, 1] = (vert[f[0]][0] * data['f']) / (vert[f[0]][2]) + data['cam_c'][0] tri1[1, 0] = (vert[f[1]][1] * data['f']) / (vert[f[1]][2]) + data['cam_c'][1] tri1[1, 1] = (vert[f[1]][0] * data['f']) / (vert[f[1]][2]) + data['cam_c'][0] tri1[2, 0] = (vert[f[2]][1] * data['f']) / (vert[f[2]][2]) + data['cam_c'][1] tri1[2, 1] = (vert[f[2]][0] * data['f']) / (vert[f[2]][2]) + data['cam_c'][0] cp[f[0]][0] = 1.0 cp[f[0]][1] = tri1[0, 0] cp[f[0]][2] = tri1[0, 1] cp[f[0]][3] = vert[f[0]][2] cp[f[1]][0] = 1.0 cp[f[1]][1] = tri1[1, 0] cp[f[1]][2] = tri1[1, 1] cp[f[1]][3] = vert[f[1]][2] cp[f[2]][0] = 1.0 cp[f[2]][1] = tri1[2, 0] cp[f[2]][2] = tri1[2, 1] cp[f[2]][3] = vert[f[2]][2] tri2[0, 1] = (tex_vt[tex_faces[i][0]][0]) * 1000 tri2[0, 0] = 1000 - (tex_vt[tex_faces[i][0]][1]) * 1000 tri2[1, 1] = (tex_vt[tex_faces[i][1]][0]) * 1000 tri2[1, 0] = 1000 - (tex_vt[tex_faces[i][1]][1]) * 1000 tri2[2, 1] = (tex_vt[tex_faces[i][2]][0]) * 1000 tri2[2, 0] = 1000 - (tex_vt[tex_faces[i][2]][1]) * 1000 '''my_map_partes[f[0]] = (image[int(tri2[0,0]),int(tri2[0,1]),0])/15 if (image[int(tri2[0,0]),int(tri2[0,1]),0])/15 > 0 else my_map_partes[f[0]] my_map_partes[f[1]] = (image[int(tri2[1,0]),int(tri2[1,1]),0])/15 if (image[int(tri2[1,0]),int(tri2[1,1]),0])/15 > 0 else my_map_partes[f[1]] my_map_partes[f[2]] = (image[int(tri2[2,0]),int(tri2[2,1]),0])/15 if (image[int(tri2[2,0]),int(tri2[2,1]),0])/15 > 0 else my_map_partes[f[2]] ''' #if image[int(tri2[0,0]),int(tri2[0,1]),0] == 15: #warp_tri(tri2,tri2,image,my_tex) #tex_vt[tex_faces[i][0] - 1][1] #print tri1 warp_tri(tri1, tri2, image, my_tex) #cv2.imshow('ImageWindow', my_tex) #cv2.waitKey() #np.savetxt('test.txt', my_map_partes , delimiter='\n') return my_tex, cp
def _in_volume_ray(points, volume, multi_ray=False): """Use pyoctree's raycsasting to test if points are within volume.""" tree = getattr(volume, 'pyoctree', None) if not tree: # Create octree from scratch tree = pyoctree.PyOctree( np.array(volume.vertices, dtype=float, order='C'), np.array(volume.faces, dtype=np.int32, order='C')) volume.pyoctree = tree # Remove points outside of bounding box isin = _in_bbox(points, volume) in_points = points[isin] # mx = np.array(volume.vertices).max(axis=0) mn = np.array(volume.vertices).min(axis=0) # Perform ray intersection on points inside bounding box rayPointList = np.array([[[p[0], mn[1], mn[2]], p] for p in in_points], dtype=np.float32) # Get intersections and extract coordinates of intersection intersections = [ np.array([i.p for i in tree.rayIntersection(ray)]) for ray in rayPointList ] # In a few odd cases we can get the multiple intersections at the exact # same coordinate (something funny with the faces). unique_int = [ np.unique(np.round(i), axis=0) if np.any(i) else i for i in intersections ] # Unfortunately rays are bidirectional -> we have to filter intersections # to those that occur "above" the point we are querying unilat_int = [ i[i[:, 2] >= p] if np.any(i) else i for i, p in zip(unique_int, in_points[:, 2]) ] # Count intersections int_count = [i.shape[0] for i in unilat_int] # Get odd (= in volume) numbers of intersections is_odd = np.remainder(int_count, 2) != 0 # If we want to play it safe, run the above again with two additional rays # and find a majority decision. if multi_ray: # Run ray from left back rayPointList = np.array([[[mn[0], p[1], mn[2]], p] for p in in_points], dtype=np.float32) intersections = [ np.array([i.p for i in tree.rayIntersection(ray)]) for ray in rayPointList ] unique_int = [ np.unique(i, axis=0) if np.any(i) else i for i in intersections ] unilat_int = [ i[i[:, 0] >= p] if np.any(i) else i for i, p in zip(unique_int, in_points[:, 0]) ] int_count = [i.shape[0] for i in unilat_int] is_odd2 = np.remainder(int_count, 2) != 0 # Run ray from lower left rayPointList = np.array([[[mn[0], mn[1], p[2]], p] for p in in_points], dtype=np.float32) intersections = [ np.array([i.p for i in tree.rayIntersection(ray)]) for ray in rayPointList ] unique_int = [ np.unique(i, axis=0) if np.any(i) else i for i in intersections ] unilat_int = [ i[i[:, 1] >= p] if np.any(i) else i for i, p in zip(unique_int, in_points[:, 1]) ] int_count = [i.shape[0] for i in unilat_int] is_odd3 = np.remainder(int_count, 2) != 0 # Find majority consensus is_odd = is_odd.astype(int) + is_odd2.astype(int) + is_odd3.astype(int) is_odd = is_odd >= 2 isin[isin] = is_odd return isin
#Point coordinates is an Nx3 array representing the coordinates of the points #connectivity is an Nx3 integer array describing which points are connected mesh = trimesh.load_mesh('transectShape.ply') f = np.array(mesh.faces).astype(np.int32) v = np.array(mesh.vertices) minX = np.min(v[:, 0]) minY = np.min(v[:, 1]) v[:, 0] += -minX v[:, 1] += -minY pointCoords = v connectivity = f tree = ot.PyOctree(pointCoords, connectivity) # Choose some starting points, for example the positions of the cameras import pandas as pd pose = pd.read_csv( 'C:/Users/haavasl/PycharmProjects/Testing/venv/Code/nav_file_for_tautra_new.txt' ) X = pose['X'].values.reshape((-1, 1)) Y = pose['Y'].values.reshape((-1, 1)) Z = pose['Z'].values.reshape((-1, 1)) xyz_start = np.zeros((X.shape[0], 3), dtype=np.float32) xyz_end = np.zeros((X.shape[0], 3), dtype=np.float32)