Skip to content

Commit 5758cc8

Browse files
committed
Fixes a bug in grid where the cartesian coordinate array is not set properly when creating the object with the number of desired point as the single parameter (issue #380).
1 parent f6d11c9 commit 5758cc8

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

CHANGELOG.rst

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Bugfix
1818
directivity to ``Room.add_microphone_array``, the directivity was dropped
1919
from the object.
2020

21+
- Fixes issue #380: Caused by the attribute ``cartesian`` of ``GridSphere`` not
22+
being set properly when the grid is only initialized with a number of points.
23+
2124
`0.8.2`_ - 2024-11-06
2225
---------------------
2326

pyroomacoustics/doa/grid.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,11 @@ def __init__(
209209
# If no list was provided, samples points on the sphere
210210
# as uniformly as possible
211211

212-
self.x, self.y, self.z = fibonacci_spherical_sampling(n_points)
212+
self.x[:], self.y[:], self.z[:] = fibonacci_spherical_sampling(n_points)
213213

214214
# Create convenient arrays
215215
# to access both in cartesian and spherical coordinates
216-
self.azimuth[:] = np.arctan2(self.y, self.x)
217-
self.colatitude[:] = np.arctan2(np.sqrt(self.x**2 + self.y**2), self.z)
216+
self.azimuth[:], self.colatitude[:], _ = cart2spher(self.cartesian)
218217

219218
self._neighbors = None
220219
if precompute_neighbors:

pyroomacoustics/doa/tests/test_grid.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33
from scipy.spatial import SphericalVoronoi
44

5-
from pyroomacoustics.doa import fibonacci_spherical_sampling
5+
import pyroomacoustics as pra
66

77

88
@pytest.mark.parametrize("n", [20, 100, 200, 500, 1000, 2000, 5000, 10000])
@@ -18,7 +18,7 @@ def test_voronoi_area(n, tol):
1818
We observed empirically that the relative max error is
1919
around 6% so we set that as the threshold for the test
2020
"""
21-
points = fibonacci_spherical_sampling(n_points=n)
21+
points = pra.doa.fibonacci_spherical_sampling(n_points=n)
2222
sphere_area = 4.0 * np.pi
2323
area_one_pt = sphere_area / n
2424

@@ -34,6 +34,43 @@ def test_voronoi_area(n, tol):
3434
assert max_err < tol
3535

3636

37+
def _check_grid_consistency(grid):
38+
39+
cart = pra.doa.spher2cart(grid.azimuth, grid.colatitude)
40+
assert np.allclose(grid.cartesian, np.array([grid.x, grid.y, grid.z]))
41+
assert np.allclose(grid.cartesian, cart)
42+
43+
az, co, _ = pra.doa.cart2spher(grid.cartesian)
44+
assert np.allclose(grid.spherical, np.array([grid.azimuth, grid.colatitude]))
45+
assert np.allclose(grid.spherical, np.array([az, co]))
46+
47+
48+
@pytest.mark.parametrize("n_points", [20, 100, 200, 500, 1000])
49+
def test_grid_sphere_from_spherical(n_points):
50+
51+
x, y, z = pra.doa.fibonacci_spherical_sampling(n_points)
52+
az, co, _ = pra.doa.cart2spher(np.array([x, y, z]))
53+
54+
grid = pra.doa.GridSphere(spherical_points=np.array([az, co]))
55+
_check_grid_consistency(grid)
56+
57+
58+
@pytest.mark.parametrize("n_points", [20, 100, 200, 500, 1000])
59+
def test_grid_sphere_from_cartesian(n_points):
60+
61+
x, y, z = pra.doa.fibonacci_spherical_sampling(n_points)
62+
63+
grid = pra.doa.GridSphere(cartesian_points=np.array([x, y, z]))
64+
_check_grid_consistency(grid)
65+
66+
67+
@pytest.mark.parametrize("n_points", [20, 100, 200, 500, 1000])
68+
def test_grid_sphere_from_fibonacci(n_points):
69+
70+
grid = pra.doa.GridSphere(n_points=n_points)
71+
_check_grid_consistency(grid)
72+
73+
3774
if __name__ == "__main__":
3875
test_voronoi_area(20, 0.01)
3976
test_voronoi_area(100, 0.01)

0 commit comments

Comments
 (0)