src.acoustools.Mesh.Transform

  1import vedo
  2import numpy as np
  3
  4from vedo import Mesh
  5
  6from acoustools.Mesh.Filename import scatterer_file_name
  7from acoustools.Mesh.Features import get_centre_of_mass_as_points
  8
  9
 10def scale_to_diameter(scatterer:Mesh , diameter: float, reset:bool=True, origin:bool=True) -> None:
 11    '''
 12    Scale a mesh to a given diameter in the x-axis and recomputes normals and areas \n
 13    Modifies scatterer in place so does not return anything.\n
 14
 15    :param scatterer: The scatterer to scale
 16    :param diameter: The diameter target
 17    '''
 18    x1,x2,y1,y2,z1,z2 = scatterer.bounds()
 19    diameter_sphere = x2 - x1
 20    scatterer.scale(diameter/diameter_sphere,reset=reset, origin=origin)
 21    scatterer.compute_cell_size()
 22    scatterer.compute_normals()
 23    scatterer.filename = scatterer_file_name(scatterer)
 24    
 25
 26
 27def translate(scatterer:Mesh, dx:float=0,dy:float=0,dz:float=0) -> None:
 28    '''
 29    Translates a scatterer by (dx,dy,dz) \n
 30    Modifies inplace so does not return a value \n
 31    :param scatterer: The scatterer to use
 32    :param dx: Translation in the x direction
 33    :param dy: Translation in the y direction
 34    :param dz: Translation in the z direction
 35    '''
 36    scatterer.shift(np.array([dx,dy,dz]))
 37    scatterer.filename = scatterer_file_name(scatterer)
 38
 39def rotate(scatterer:Mesh, axis:tuple[int], rot:float, centre:tuple[int]=(0, 0, 0), rotate_around_COM:bool=False):
 40    '''
 41    Rotates a scatterer in axis by rot\n
 42    Modifies inplace so does not return a value\n
 43    :param scatterer: The scatterer to use
 44    :param axis: The axis to rotate in
 45    :param rot: Angle to rotate in degrees
 46    :param centre: point to rotate around
 47    :param rotate_around_COM: If True will set `centre` to `scatterer`s centre of mass
 48    '''
 49    if rotate_around_COM:
 50        centre = vedo.vector(get_centre_of_mass_as_points(scatterer).cpu().detach().squeeze())
 51
 52    if axis[0]:
 53        scatterer.metadata["rotX"] = scatterer.metadata["rotX"] + rot
 54    if axis[1]:
 55        scatterer.metadata["rotY"] = scatterer.metadata["rotY"] + rot
 56    if axis[2]:
 57        scatterer.metadata["rotZ"] = scatterer.metadata["rotZ"] + rot
 58    scatterer.rotate(rot, axis,point=centre)
 59    scatterer.filename = scatterer_file_name(scatterer)
 60
 61 
 62def downsample(scatterer:Mesh, factor:int=2, n:int|None=None, method:str='quadric', boundaries:bool=False, compute_areas:bool=True, compute_normals:bool=True) -> Mesh:
 63    '''
 64    Downsamples a mesh to have `factor` less elements\n
 65    :param scatterer: The scatterer to use
 66    :param factor: The factor to downsample by
 67    :param n: The desired number of final points, passed to `Vedo.Mesh.decimate`
 68    :param method:, `boundaries` - passed to `vedo.decimate`
 69    :param compute_areas: if true will call `scatterer.compute_cell_size()`. Default `True`
 70    :param compute_normals: if true will call `scatterer.compute_normals()`. Default `True`
 71    :return: downsampled mesh
 72    '''
 73    scatterer_small =  scatterer.decimate(1/factor, n, method, boundaries)
 74    
 75    scatterer_small.metadata["rotX"] = scatterer.metadata["rotX"]
 76    scatterer_small.metadata["rotY"] = scatterer.metadata["rotY"]
 77    scatterer_small.metadata["rotZ"] = scatterer.metadata["rotZ"]
 78
 79    if compute_areas: scatterer_small.compute_cell_size()
 80    if compute_normals: 
 81        scatterer_small.compute_normals()
 82
 83    scatterer_small.filename = scatterer_file_name(scatterer_small)  + "-scale-" + str(factor)
 84
 85
 86    return scatterer_small
 87
 88
 89def centre_scatterer(scatterer:Mesh) -> list[int]:
 90    '''
 91    Translate scatterer so the centre of mass is at (0,0,0)\n
 92    Modifies Mesh in place \n
 93    :param scatterer: Scatterer to centre
 94    :return: Returns the amount needed to move in each direction
 95    '''
 96    com = get_centre_of_mass_as_points(scatterer).cpu()
 97    correction = [-1*com[:,0].item(), -1*com[:,1].item(), -1*com[:,2].item()]
 98    translate(scatterer, dx = correction[0], dy = correction[1], dz=  correction[2])
 99
100    return correction
def scale_to_diameter( scatterer: vedo.mesh.core.Mesh, diameter: float, reset: bool = True, origin: bool = True) -> None:
12def scale_to_diameter(scatterer:Mesh , diameter: float, reset:bool=True, origin:bool=True) -> None:
13    '''
14    Scale a mesh to a given diameter in the x-axis and recomputes normals and areas \n
15    Modifies scatterer in place so does not return anything.\n
16
17    :param scatterer: The scatterer to scale
18    :param diameter: The diameter target
19    '''
20    x1,x2,y1,y2,z1,z2 = scatterer.bounds()
21    diameter_sphere = x2 - x1
22    scatterer.scale(diameter/diameter_sphere,reset=reset, origin=origin)
23    scatterer.compute_cell_size()
24    scatterer.compute_normals()
25    scatterer.filename = scatterer_file_name(scatterer)

Scale a mesh to a given diameter in the x-axis and recomputes normals and areas

Modifies scatterer in place so does not return anything.

Parameters
  • scatterer: The scatterer to scale
  • diameter: The diameter target
def translate( scatterer: vedo.mesh.core.Mesh, dx: float = 0, dy: float = 0, dz: float = 0) -> None:
29def translate(scatterer:Mesh, dx:float=0,dy:float=0,dz:float=0) -> None:
30    '''
31    Translates a scatterer by (dx,dy,dz) \n
32    Modifies inplace so does not return a value \n
33    :param scatterer: The scatterer to use
34    :param dx: Translation in the x direction
35    :param dy: Translation in the y direction
36    :param dz: Translation in the z direction
37    '''
38    scatterer.shift(np.array([dx,dy,dz]))
39    scatterer.filename = scatterer_file_name(scatterer)

Translates a scatterer by (dx,dy,dz)

Modifies inplace so does not return a value

Parameters
  • scatterer: The scatterer to use
  • dx: Translation in the x direction
  • dy: Translation in the y direction
  • dz: Translation in the z direction
def rotate( scatterer: vedo.mesh.core.Mesh, axis: tuple[int], rot: float, centre: tuple[int] = (0, 0, 0), rotate_around_COM: bool = False):
41def rotate(scatterer:Mesh, axis:tuple[int], rot:float, centre:tuple[int]=(0, 0, 0), rotate_around_COM:bool=False):
42    '''
43    Rotates a scatterer in axis by rot\n
44    Modifies inplace so does not return a value\n
45    :param scatterer: The scatterer to use
46    :param axis: The axis to rotate in
47    :param rot: Angle to rotate in degrees
48    :param centre: point to rotate around
49    :param rotate_around_COM: If True will set `centre` to `scatterer`s centre of mass
50    '''
51    if rotate_around_COM:
52        centre = vedo.vector(get_centre_of_mass_as_points(scatterer).cpu().detach().squeeze())
53
54    if axis[0]:
55        scatterer.metadata["rotX"] = scatterer.metadata["rotX"] + rot
56    if axis[1]:
57        scatterer.metadata["rotY"] = scatterer.metadata["rotY"] + rot
58    if axis[2]:
59        scatterer.metadata["rotZ"] = scatterer.metadata["rotZ"] + rot
60    scatterer.rotate(rot, axis,point=centre)
61    scatterer.filename = scatterer_file_name(scatterer)

Rotates a scatterer in axis by rot

Modifies inplace so does not return a value

Parameters
  • scatterer: The scatterer to use
  • axis: The axis to rotate in
  • rot: Angle to rotate in degrees
  • centre: point to rotate around
  • rotate_around_COM: If True will set centre to scatterers centre of mass
def downsample( scatterer: vedo.mesh.core.Mesh, factor: int = 2, n: int | None = None, method: str = 'quadric', boundaries: bool = False, compute_areas: bool = True, compute_normals: bool = True) -> vedo.mesh.core.Mesh:
64def downsample(scatterer:Mesh, factor:int=2, n:int|None=None, method:str='quadric', boundaries:bool=False, compute_areas:bool=True, compute_normals:bool=True) -> Mesh:
65    '''
66    Downsamples a mesh to have `factor` less elements\n
67    :param scatterer: The scatterer to use
68    :param factor: The factor to downsample by
69    :param n: The desired number of final points, passed to `Vedo.Mesh.decimate`
70    :param method:, `boundaries` - passed to `vedo.decimate`
71    :param compute_areas: if true will call `scatterer.compute_cell_size()`. Default `True`
72    :param compute_normals: if true will call `scatterer.compute_normals()`. Default `True`
73    :return: downsampled mesh
74    '''
75    scatterer_small =  scatterer.decimate(1/factor, n, method, boundaries)
76    
77    scatterer_small.metadata["rotX"] = scatterer.metadata["rotX"]
78    scatterer_small.metadata["rotY"] = scatterer.metadata["rotY"]
79    scatterer_small.metadata["rotZ"] = scatterer.metadata["rotZ"]
80
81    if compute_areas: scatterer_small.compute_cell_size()
82    if compute_normals: 
83        scatterer_small.compute_normals()
84
85    scatterer_small.filename = scatterer_file_name(scatterer_small)  + "-scale-" + str(factor)
86
87
88    return scatterer_small

Downsamples a mesh to have factor less elements

Parameters
  • scatterer: The scatterer to use
  • factor: The factor to downsample by
  • n: The desired number of final points, passed to Vedo.Mesh.decimate
  • method: , boundaries - passed to vedo.decimate
  • compute_areas: if true will call scatterer.compute_cell_size(). Default True
  • compute_normals: if true will call scatterer.compute_normals(). Default True
Returns

downsampled mesh

def centre_scatterer(scatterer: vedo.mesh.core.Mesh) -> list[int]:
 91def centre_scatterer(scatterer:Mesh) -> list[int]:
 92    '''
 93    Translate scatterer so the centre of mass is at (0,0,0)\n
 94    Modifies Mesh in place \n
 95    :param scatterer: Scatterer to centre
 96    :return: Returns the amount needed to move in each direction
 97    '''
 98    com = get_centre_of_mass_as_points(scatterer).cpu()
 99    correction = [-1*com[:,0].item(), -1*com[:,1].item(), -1*com[:,2].item()]
100    translate(scatterer, dx = correction[0], dy = correction[1], dz=  correction[2])
101
102    return correction

Translate scatterer so the centre of mass is at (0,0,0)

Modifies Mesh in place

Parameters
  • scatterer: Scatterer to centre
Returns

Returns the amount needed to move in each direction