src.acoustools.Utilities.Points

  1from torch import Tensor
  2import torch
  3
  4from acoustools.Utilities.Setup import device, DTYPE
  5
  6def create_points(N:int=None,B:int=1,x:float|None=None,y:float|None=None,z:float|None=None, min_pos:float=-0.06, max_pos:float = 0.06) -> Tensor:
  7    '''
  8    Creates a random set of N points in B batches in shape `Bx3xN` \n
  9    :param N: Number of points per batch
 10    :param B: Number of Batches
 11    :param x: if not None all points will have this as their x position. Default: `None`
 12    :param y: if not None all points will have this as their y position. Default: `None`
 13    :param z: if not None all points will have this as their z position. Default: `None`
 14    :param min_pos: Minimum position
 15    :param max_pos: Maximum position
 16    ```
 17    from acoustools.Utilities import create_points
 18    p = create_points(N=3,B=1)
 19    ```
 20    '''
 21
 22    its = [list, tuple]
 23
 24    if N is None:
 25        if (type(x) in its and type(y) in its and type(z) in its ): 
 26            N = len(x)
 27        elif (type(x) == float and type(y) == float and type(z) == float) or (type(x) == int and type(y) == int and type(z) == int):
 28            N = 1
 29        else:
 30            raise ValueError("If N is not provided x,y and z need to be lists of points or single values")
 31
 32    points = torch.zeros((B, 3, N), device=device)
 33    
 34    if x is not None:
 35        if type(x) in its:
 36            for i in range(N):
 37                points[:,0,i] = x[i]
 38        else:
 39            points[:,0,:] = x
 40    else:
 41        points[:,0,:].uniform_(min_pos, max_pos)
 42    
 43    if y is not None:
 44        if type(y) in its:
 45            for i in range(N):
 46                points[:,1,i] = y[i]
 47        else:
 48            points[:,1,:] = y
 49    else:
 50        points[:,1,:].uniform_(min_pos, max_pos)
 51    
 52    if z is not None:
 53        if type(z) in its:
 54            for i in range(N):
 55              points[:,2,i] = z[i]
 56        else:
 57            points[:,2,:] = z
 58    else:
 59        points[:,2,:].uniform_(min_pos, max_pos)
 60
 61
 62    return points.to(DTYPE)
 63
 64
 65def random_points_ring(N=1, origin:Tensor=None, min_r:float=0.01, max_r:float=0.06):
 66
 67    theta = torch.rand((1,1,N)) * 2 * 3.1415
 68    phi = torch.rand((1,1,N)) * 2 * 3.1415
 69    
 70    sin_theta = torch.sin(theta)
 71    cos_theta = torch.cos(theta)
 72    
 73    sin_phi = torch.sin(phi)
 74    cos_phi = torch.cos(phi)
 75    
 76    min_r_x = min_r_y = min_r_z = min_r
 77
 78    max_r_x = max_r_y = max_r_z = max_r
 79
 80    rx = torch.FloatTensor(1, 1, N).uniform_(min_r_x, max_r_x).to(device).to(DTYPE)
 81    ry = torch.FloatTensor(1, 1, N).uniform_(min_r_y, max_r_y).to(device).to(DTYPE)
 82    rz = torch.FloatTensor(1, 1, N).uniform_(min_r_z, max_r_z).to(device).to(DTYPE)
 83    
 84    r = (rx**2 + ry**2 + rz**2)**0.5
 85    
 86    x = r * sin_theta * cos_phi
 87    y = r * sin_theta * sin_phi
 88    z = r * cos_theta
 89    
 90    pts = torch.cat([x,y,z], dim=1)
 91    
 92    if origin is None: origin = create_points(N,1, 0, 0, 0)
 93    
 94    pts = pts + origin
 95    
 96    return pts
 97
 98    
 99    
100        
101    
def create_points( N: int = None, B: int = 1, x: float | None = None, y: float | None = None, z: float | None = None, min_pos: float = -0.06, max_pos: float = 0.06) -> torch.Tensor:
 7def create_points(N:int=None,B:int=1,x:float|None=None,y:float|None=None,z:float|None=None, min_pos:float=-0.06, max_pos:float = 0.06) -> Tensor:
 8    '''
 9    Creates a random set of N points in B batches in shape `Bx3xN` \n
10    :param N: Number of points per batch
11    :param B: Number of Batches
12    :param x: if not None all points will have this as their x position. Default: `None`
13    :param y: if not None all points will have this as their y position. Default: `None`
14    :param z: if not None all points will have this as their z position. Default: `None`
15    :param min_pos: Minimum position
16    :param max_pos: Maximum position
17    ```
18    from acoustools.Utilities import create_points
19    p = create_points(N=3,B=1)
20    ```
21    '''
22
23    its = [list, tuple]
24
25    if N is None:
26        if (type(x) in its and type(y) in its and type(z) in its ): 
27            N = len(x)
28        elif (type(x) == float and type(y) == float and type(z) == float) or (type(x) == int and type(y) == int and type(z) == int):
29            N = 1
30        else:
31            raise ValueError("If N is not provided x,y and z need to be lists of points or single values")
32
33    points = torch.zeros((B, 3, N), device=device)
34    
35    if x is not None:
36        if type(x) in its:
37            for i in range(N):
38                points[:,0,i] = x[i]
39        else:
40            points[:,0,:] = x
41    else:
42        points[:,0,:].uniform_(min_pos, max_pos)
43    
44    if y is not None:
45        if type(y) in its:
46            for i in range(N):
47                points[:,1,i] = y[i]
48        else:
49            points[:,1,:] = y
50    else:
51        points[:,1,:].uniform_(min_pos, max_pos)
52    
53    if z is not None:
54        if type(z) in its:
55            for i in range(N):
56              points[:,2,i] = z[i]
57        else:
58            points[:,2,:] = z
59    else:
60        points[:,2,:].uniform_(min_pos, max_pos)
61
62
63    return points.to(DTYPE)

Creates a random set of N points in B batches in shape Bx3xN

Parameters
  • N: Number of points per batch
  • B: Number of Batches
  • x: if not None all points will have this as their x position. Default: None
  • y: if not None all points will have this as their y position. Default: None
  • z: if not None all points will have this as their z position. Default: None
  • min_pos: Minimum position
  • max_pos: Maximum position
from acoustools.Utilities import create_points
p = create_points(N=3,B=1)
def random_points_ring( N=1, origin: torch.Tensor = None, min_r: float = 0.01, max_r: float = 0.06):
66def random_points_ring(N=1, origin:Tensor=None, min_r:float=0.01, max_r:float=0.06):
67
68    theta = torch.rand((1,1,N)) * 2 * 3.1415
69    phi = torch.rand((1,1,N)) * 2 * 3.1415
70    
71    sin_theta = torch.sin(theta)
72    cos_theta = torch.cos(theta)
73    
74    sin_phi = torch.sin(phi)
75    cos_phi = torch.cos(phi)
76    
77    min_r_x = min_r_y = min_r_z = min_r
78
79    max_r_x = max_r_y = max_r_z = max_r
80
81    rx = torch.FloatTensor(1, 1, N).uniform_(min_r_x, max_r_x).to(device).to(DTYPE)
82    ry = torch.FloatTensor(1, 1, N).uniform_(min_r_y, max_r_y).to(device).to(DTYPE)
83    rz = torch.FloatTensor(1, 1, N).uniform_(min_r_z, max_r_z).to(device).to(DTYPE)
84    
85    r = (rx**2 + ry**2 + rz**2)**0.5
86    
87    x = r * sin_theta * cos_phi
88    y = r * sin_theta * sin_phi
89    z = r * cos_theta
90    
91    pts = torch.cat([x,y,z], dim=1)
92    
93    if origin is None: origin = create_points(N,1, 0, 0, 0)
94    
95    pts = pts + origin
96    
97    return pts