src.acoustools.Mesh.Load

  1import vedo
  2
  3from vedo import Mesh
  4
  5from acoustools.Mesh.Transform import rotate, translate
  6from acoustools.Mesh.Filename import scatterer_file_name
  7
  8def load_scatterer(path:str, compute_areas:bool = True, compute_normals:bool=True, dx:float=0,
  9                   dy:float=0,dz:float=0, rotx:float=0, roty:float=0, rotz:float=0, root_path:str="", force:bool=False, flip_normals=False) -> Mesh:
 10    '''
 11    Loads a scatterer as a `vedo` `Mesh` and applies translations as needed
 12    :param path: The name of the scatterer to load
 13    :param compute_areas: if `True` will call `scatterer.compute_cell_size()`. Default `True`
 14    :param compute_normals: if `True` will call `scatterer.compute_normals()`. Default `True`
 15    :param dx: Translation in the x direction to apply
 16    :param dy: Translation in the y direction to apply
 17    :param dz: Translation in the z direction to apply
 18    :param rotx: Rotation around the x axis to apply
 19    :param roty: Rotation around the y axis to apply
 20    :param rotz: Rotation around the z axis to apply
 21    :param root_path: The folder containing the file, the scatterer to be loaded will be loaded from `root_path+path`
 22    :return: The `vedo` `Mesh` of the scatterer
 23    '''
 24    scatterer = vedo.load(root_path+path, force=force)
 25    
 26    if scatterer is not None:
 27        if compute_areas: scatterer.compute_cell_size()
 28        if compute_normals: 
 29            scatterer.compute_normals()
 30            if flip_normals: scatterer.flip_normals()
 31
 32        scatterer.metadata["rotX"] = 0
 33        scatterer.metadata["rotY"] = 0
 34        scatterer.metadata["rotZ"] = 0
 35
 36        # scatterer.filename = scatterer.filename.split("/")[-1]
 37        scatterer.filename = scatterer_file_name(scatterer)
 38
 39        scatterer.metadata["FILE"] = scatterer.filename.split(".")[0]
 40
 41
 42        rotate(scatterer,(1,0,0),rotx)
 43        rotate(scatterer,(0,1,0),roty)
 44        rotate(scatterer,(0,0,1),rotz)
 45
 46        translate(scatterer,dx,dy,dz)
 47    else:
 48        raise ValueError(f"File not found at {path} - please check the path")
 49
 50    return scatterer
 51
 52
 53
 54def load_multiple_scatterers(paths:list[str],  compute_areas:bool = True, compute_normals:bool=True, 
 55                             dxs:list[int]=[],dys:list[int]=[],dzs:list[int]=[], rotxs:list[int]=[], rotys:list[int]=[], rotzs:list[int]=[], root_path:str="") -> Mesh:
 56    '''
 57    Loads multiple scatterers and combines them into a single scatterer object
 58    :param path: The name of the scatterers to load
 59    :param compute_areas: if true will call `scatterer.compute_cell_size()`. Default True
 60    :param compute_normals: if true will call `scatterer.compute_normals()`. Default True
 61    :param dxs: List of translations in the x direction to apply to each scatterer
 62    :param dys: List of translations in the y direction to apply to each scatterer
 63    :param dzs: List of translations in the z direction to apply to each scatterer
 64    :param rotxs: List pf rotations around the x axis to apply to each scatterer
 65    :param rotys: List pf rotations around the y axis to apply to each scatterer
 66    :param rotzs: List pf rotations around the z axis to apply to each scatterer
 67    :param root_path: The folder containing the file, the scatterer to be loaded will be loaded from `root_path+path`
 68    :return: A merged mesh from all of the paths provided
 69    '''
 70    dxs += [0] * (len(paths) - len(dxs))
 71    dys += [0] * (len(paths) - len(dys))
 72    dzs += [0] * (len(paths) - len(dzs))
 73
 74    rotxs += [0] * (len(paths) - len(rotxs))
 75    rotys += [0] * (len(paths) - len(rotys))
 76    rotzs += [0] * (len(paths) - len(rotzs))
 77
 78    scatterers = []
 79    for i,path in enumerate(paths):
 80        scatterer = load_scatterer(path, compute_areas, compute_normals, dxs[i],dys[i],dzs[i],rotxs[i],rotys[i],rotzs[i],root_path)
 81        scatterers.append(scatterer)
 82    combined = merge_scatterers(*scatterers)
 83    return combined
 84
 85def merge_scatterers(*scatterers:Mesh, flag:bool=False) ->Mesh:
 86    '''
 87    Combines any number of scatterers into a single scatterer\n
 88    :param scatterers: any number of scatterers to combine
 89    :param flag: Value will be passed to `vedo.merge`
 90    :return: the combined scatterer
 91    '''
 92    names = []
 93    Fnames = []
 94    for scatterer in scatterers:
 95        names.append(scatterer_file_name(scatterer))
 96        Fnames.append(scatterer.metadata["FILE"][0])
 97    
 98    if flag:
 99        combined = vedo.merge(scatterers, flag=True)
100    else:
101        combined = vedo.merge(scatterers)
102    combined.filename = "".join(names)
103    combined.metadata["FILE"] = "".join(Fnames)
104    return combined
105
106
107def calculate_features(scatterer:Mesh, compute_areas:bool = True, compute_normals:bool=True):
108    '''
109    @private
110    '''
111    if compute_areas: scatterer.compute_cell_size()
112    if compute_normals: scatterer.compute_normals()
113
114    scatterer.filename = scatterer_file_name(scatterer)
115    scatterer.metadata["FILE"] = scatterer.filename.split(".")[0]
def load_scatterer( path: str, compute_areas: bool = True, compute_normals: bool = True, dx: float = 0, dy: float = 0, dz: float = 0, rotx: float = 0, roty: float = 0, rotz: float = 0, root_path: str = '', force: bool = False, flip_normals=False) -> vedo.mesh.core.Mesh:
11def load_scatterer(path:str, compute_areas:bool = True, compute_normals:bool=True, dx:float=0,
12                   dy:float=0,dz:float=0, rotx:float=0, roty:float=0, rotz:float=0, root_path:str="", force:bool=False, flip_normals=False) -> Mesh:
13    '''
14    Loads a scatterer as a `vedo` `Mesh` and applies translations as needed
15    :param path: The name of the scatterer to load
16    :param compute_areas: if `True` will call `scatterer.compute_cell_size()`. Default `True`
17    :param compute_normals: if `True` will call `scatterer.compute_normals()`. Default `True`
18    :param dx: Translation in the x direction to apply
19    :param dy: Translation in the y direction to apply
20    :param dz: Translation in the z direction to apply
21    :param rotx: Rotation around the x axis to apply
22    :param roty: Rotation around the y axis to apply
23    :param rotz: Rotation around the z axis to apply
24    :param root_path: The folder containing the file, the scatterer to be loaded will be loaded from `root_path+path`
25    :return: The `vedo` `Mesh` of the scatterer
26    '''
27    scatterer = vedo.load(root_path+path, force=force)
28    
29    if scatterer is not None:
30        if compute_areas: scatterer.compute_cell_size()
31        if compute_normals: 
32            scatterer.compute_normals()
33            if flip_normals: scatterer.flip_normals()
34
35        scatterer.metadata["rotX"] = 0
36        scatterer.metadata["rotY"] = 0
37        scatterer.metadata["rotZ"] = 0
38
39        # scatterer.filename = scatterer.filename.split("/")[-1]
40        scatterer.filename = scatterer_file_name(scatterer)
41
42        scatterer.metadata["FILE"] = scatterer.filename.split(".")[0]
43
44
45        rotate(scatterer,(1,0,0),rotx)
46        rotate(scatterer,(0,1,0),roty)
47        rotate(scatterer,(0,0,1),rotz)
48
49        translate(scatterer,dx,dy,dz)
50    else:
51        raise ValueError(f"File not found at {path} - please check the path")
52
53    return scatterer

Loads a scatterer as a vedo Mesh and applies translations as needed

Parameters
  • path: The name of the scatterer to load
  • compute_areas: if True will call scatterer.compute_cell_size(). Default True
  • compute_normals: if True will call scatterer.compute_normals(). Default True
  • dx: Translation in the x direction to apply
  • dy: Translation in the y direction to apply
  • dz: Translation in the z direction to apply
  • rotx: Rotation around the x axis to apply
  • roty: Rotation around the y axis to apply
  • rotz: Rotation around the z axis to apply
  • root_path: The folder containing the file, the scatterer to be loaded will be loaded from root_path+path
Returns

The vedo Mesh of the scatterer

def load_multiple_scatterers( paths: list[str], compute_areas: bool = True, compute_normals: bool = True, dxs: list[int] = [], dys: list[int] = [], dzs: list[int] = [], rotxs: list[int] = [], rotys: list[int] = [], rotzs: list[int] = [], root_path: str = '') -> vedo.mesh.core.Mesh:
57def load_multiple_scatterers(paths:list[str],  compute_areas:bool = True, compute_normals:bool=True, 
58                             dxs:list[int]=[],dys:list[int]=[],dzs:list[int]=[], rotxs:list[int]=[], rotys:list[int]=[], rotzs:list[int]=[], root_path:str="") -> Mesh:
59    '''
60    Loads multiple scatterers and combines them into a single scatterer object
61    :param path: The name of the scatterers to load
62    :param compute_areas: if true will call `scatterer.compute_cell_size()`. Default True
63    :param compute_normals: if true will call `scatterer.compute_normals()`. Default True
64    :param dxs: List of translations in the x direction to apply to each scatterer
65    :param dys: List of translations in the y direction to apply to each scatterer
66    :param dzs: List of translations in the z direction to apply to each scatterer
67    :param rotxs: List pf rotations around the x axis to apply to each scatterer
68    :param rotys: List pf rotations around the y axis to apply to each scatterer
69    :param rotzs: List pf rotations around the z axis to apply to each scatterer
70    :param root_path: The folder containing the file, the scatterer to be loaded will be loaded from `root_path+path`
71    :return: A merged mesh from all of the paths provided
72    '''
73    dxs += [0] * (len(paths) - len(dxs))
74    dys += [0] * (len(paths) - len(dys))
75    dzs += [0] * (len(paths) - len(dzs))
76
77    rotxs += [0] * (len(paths) - len(rotxs))
78    rotys += [0] * (len(paths) - len(rotys))
79    rotzs += [0] * (len(paths) - len(rotzs))
80
81    scatterers = []
82    for i,path in enumerate(paths):
83        scatterer = load_scatterer(path, compute_areas, compute_normals, dxs[i],dys[i],dzs[i],rotxs[i],rotys[i],rotzs[i],root_path)
84        scatterers.append(scatterer)
85    combined = merge_scatterers(*scatterers)
86    return combined

Loads multiple scatterers and combines them into a single scatterer object

Parameters
  • path: The name of the scatterers to load
  • compute_areas: if true will call scatterer.compute_cell_size(). Default True
  • compute_normals: if true will call scatterer.compute_normals(). Default True
  • dxs: List of translations in the x direction to apply to each scatterer
  • dys: List of translations in the y direction to apply to each scatterer
  • dzs: List of translations in the z direction to apply to each scatterer
  • rotxs: List pf rotations around the x axis to apply to each scatterer
  • rotys: List pf rotations around the y axis to apply to each scatterer
  • rotzs: List pf rotations around the z axis to apply to each scatterer
  • root_path: The folder containing the file, the scatterer to be loaded will be loaded from root_path+path
Returns

A merged mesh from all of the paths provided

def merge_scatterers( *scatterers: vedo.mesh.core.Mesh, flag: bool = False) -> vedo.mesh.core.Mesh:
 88def merge_scatterers(*scatterers:Mesh, flag:bool=False) ->Mesh:
 89    '''
 90    Combines any number of scatterers into a single scatterer\n
 91    :param scatterers: any number of scatterers to combine
 92    :param flag: Value will be passed to `vedo.merge`
 93    :return: the combined scatterer
 94    '''
 95    names = []
 96    Fnames = []
 97    for scatterer in scatterers:
 98        names.append(scatterer_file_name(scatterer))
 99        Fnames.append(scatterer.metadata["FILE"][0])
100    
101    if flag:
102        combined = vedo.merge(scatterers, flag=True)
103    else:
104        combined = vedo.merge(scatterers)
105    combined.filename = "".join(names)
106    combined.metadata["FILE"] = "".join(Fnames)
107    return combined

Combines any number of scatterers into a single scatterer

Parameters
  • scatterers: any number of scatterers to combine
  • flag: Value will be passed to vedo.merge
Returns

the combined scatterer