Skip to content

Commit 492e422

Browse files
Revert "simplify smoothing (#212)" (#215)
This reverts commit 0c9d5d0.
1 parent 5ea5932 commit 492e422

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

src/iris/nodes/geometry_refinement/smoothing.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,32 @@ def _cut_into_arcs(self, polygon: np.ndarray, center_xy: Tuple[float, float]) ->
9191
Returns:
9292
Tuple[List[np.ndarray], int]: Tuple with: (list of list of vertices, number of gaps detected in a contour).
9393
"""
94-
# sort points by phi angle
95-
_, phi = math.cartesian2polar(polygon[:, 0], polygon[:, 1], *center_xy)
96-
idx_order = np.argsort(phi)
97-
phi = phi[idx_order]
98-
polygon = polygon[idx_order]
99-
100-
# find gaps
101-
dphi = phi - np.roll(phi, 1)
102-
dphi[0] += 2 * np.pi
103-
gap_idx = np.where(dphi > np.radians(self.params.gap_threshold))[0]
104-
105-
# cut into arcs - each gap index marks the start of an arc
106-
if gap_idx.size == 0:
107-
return [polygon], 0
108-
arcs = [np.vstack([polygon[gap_idx[-1] :], polygon[: gap_idx[0]]])]
109-
arcs.extend([polygon[gap_idx[i] : gap_idx[i + 1]] for i in range(0, len(gap_idx) - 1)])
110-
return arcs, gap_idx.size
94+
rho, phi = math.cartesian2polar(polygon[:, 0], polygon[:, 1], *center_xy)
95+
phi, rho = self._sort_two_arrays(phi, rho)
96+
97+
differences = np.abs(phi - np.roll(phi, -1))
98+
# True distance between first and last point
99+
differences[-1] = 2 * np.pi - differences[-1]
100+
101+
gap_indices = np.argwhere(differences > np.radians(self.params.gap_threshold)).flatten()
102+
103+
if gap_indices.size < 2:
104+
return [polygon], gap_indices.size
105+
106+
gap_indices += 1
107+
phi, rho = np.split(phi, gap_indices), np.split(rho, gap_indices)
108+
109+
arcs = [
110+
np.column_stack(math.polar2cartesian(rho_coords, phi_coords, *center_xy))
111+
for rho_coords, phi_coords in zip(rho, phi)
112+
]
113+
114+
# Connect arc which lies between 0 and 2π.
115+
if len(arcs) == gap_indices.size + 1:
116+
arcs[0] = np.vstack([arcs[0], arcs[-1]])
117+
arcs = arcs[:-1]
118+
119+
return arcs, gap_indices.size
111120

112121
def _smooth_arc(self, vertices: np.ndarray, center_xy: Tuple[float, float]) -> np.ndarray:
113122
"""Smooth a single contour arc.

tests/unit_tests/nodes/geometry_refinement/test_smoothing.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,25 @@ def algorithm() -> Smoothing:
1616
@pytest.mark.parametrize(
1717
"arc,expected_num_gaps",
1818
[
19-
# on arc - no gaps
2019
(generate_arc(10, 0.0, 0.0, 0.0, 2 * np.pi), 0),
21-
# one arc - one gap
2220
(generate_arc(10, 0.0, 0.0, 0.0, np.pi), 1),
23-
# one arc - one gap not at 0/2pi
2421
(generate_arc(10, 0.0, 0.0, np.pi, 2.5 * np.pi), 1),
25-
# two arcs - two gaps
2622
(np.vstack([generate_arc(10, 0.0, 0.0, 0.0, np.pi / 4), generate_arc(10, 0.0, 0.0, np.pi, 4 / 3 * np.pi)]), 2),
27-
# one arc in two parts - one gap
2823
(
2924
np.vstack(
3025
[generate_arc(10, 0.0, 0.0, 0.0, np.pi / 4), generate_arc(10, 0.0, 0.0, 4 / 3 * np.pi, 2 * np.pi)]
3126
),
3227
1,
3328
),
34-
# one arc shuffled - one gap
35-
(np.random.permutation(generate_arc(10, 0.0, 0.0, 0.0, np.pi)), 1),
3629
],
3730
)
3831
def test_cut_into_arcs(algorithm: Smoothing, arc: np.ndarray, expected_num_gaps: int) -> None:
3932
center_x, center_y = 0.0, 0.0
4033

41-
result_arcs, result_num_gaps = algorithm._cut_into_arcs(arc, (center_x, center_y))
34+
_, result_num_gaps = algorithm._cut_into_arcs(arc, (center_x, center_y))
4235

43-
# Ensure number of gaps is as expected.
4436
assert result_num_gaps == expected_num_gaps
4537

46-
# Ensure that both arrays have the exact same elements.
47-
arc_sorted = arc[np.lexsort(arc.T)]
48-
result_stacked = np.vstack(result_arcs)
49-
result_sorted = result_stacked[np.lexsort(result_stacked.T)]
50-
np.testing.assert_equal(arc_sorted, result_sorted)
51-
5238

5339
@pytest.mark.parametrize(
5440
"phis, rhos, expected_result",

0 commit comments

Comments
 (0)