From 1b3689034f20545ea77e171218a0726bbb9f1ce7 Mon Sep 17 00:00:00 2001 From: jatoben Date: Sat, 23 Nov 2024 03:53:15 -0800 Subject: [PATCH] Do not discard minimum-length paths in `FlexPath::remove_overlapping_points()` (#278) * Test minimum-length paths * Only discard point if length is strictly less than tolerance --- src/flexpath.cpp | 2 +- tests/flexpath_test.py | 9 +++++++++ tests/library_test.py | 33 +++++++++++++++++++++++++++++++-- tests/min_length_path.oas | Bin 0 -> 518 bytes 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/min_length_path.oas diff --git a/src/flexpath.cpp b/src/flexpath.cpp index 9d79ef518..4693eb619 100644 --- a/src/flexpath.cpp +++ b/src/flexpath.cpp @@ -226,7 +226,7 @@ void FlexPath::remove_overlapping_points() { const double tol_sq = spine.tolerance * spine.tolerance; Array* array = &spine.point_array; for (uint64_t i = 1; i < array->count;) { - if (((*array)[i] - (*array)[i - 1]).length_sq() <= tol_sq) { + if (((*array)[i] - (*array)[i - 1]).length_sq() < tol_sq) { array->remove(i); FlexPathElement* el = elements; for (uint64_t ne = 0; ne < num_elements; ne++, el++) diff --git a/tests/flexpath_test.py b/tests/flexpath_test.py index f598d5ab6..08686f354 100644 --- a/tests/flexpath_test.py +++ b/tests/flexpath_test.py @@ -166,3 +166,12 @@ def test_raith_data(): assert path.raith_data.periods == 5 assert path.raith_data.grating_type == 6 assert path.raith_data.dots_per_cycle == 7 + + +def test_min_length(): + lib = gdstk.Library("test") + tol = lib.precision / lib.unit + + path = gdstk.FlexPath(((0, 0), (tol, 0)), width=0.01, tolerance=tol) + assert path.to_polygons() + diff --git a/tests/library_test.py b/tests/library_test.py index 2a2ced86f..2f9ba8639 100644 --- a/tests/library_test.py +++ b/tests/library_test.py @@ -5,11 +5,14 @@ # Boost Software License - Version 1.0. See the accompanying # LICENSE file or -from datetime import datetime import hashlib import pathlib -import pytest +from datetime import datetime +from typing import Callable, Union + import numpy +import pytest + import gdstk @@ -532,3 +535,29 @@ def test_roundtrip_path_ends(tmpdir: pathlib.Path): assert ( path.ends == gds_path.ends and path.ends == oas_path.ends ), f"expected: {path.ends}, gds: {gds_path.ends}, oas: {oas_path.ends}" + + +@pytest.mark.parametrize( + "write_f, read_f", + ( + (gdstk.Library.write_oas, gdstk.read_oas), + (gdstk.Library.write_gds, gdstk.read_gds), + ), +) +def test_write_min_length_path( + write_f: Callable[[gdstk.Library, Union[str, pathlib.Path]], None], + read_f: Callable[Union[str, pathlib.Path], gdstk.Library], + tmp_path: pathlib.Path +): + source = gdstk.read_oas(pathlib.Path(__file__).parent / "min_length_path.oas") + assert source.cells[0].paths + + rw_path = tmp_path / "out" + write_f(source, rw_path) + rw = read_f(rw_path) + assert rw.cells[0].paths + + assert numpy.array_equal( + source.cells[0].paths[0].spine(), + rw.cells[0].paths[0].spine(), + ) diff --git a/tests/min_length_path.oas b/tests/min_length_path.oas new file mode 100644 index 0000000000000000000000000000000000000000..4be94d3b4a071d8b5fa598435293d20b28e03598 GIT binary patch literal 518 zcmd^*&q@P948~`+T^kGQMX^v8dXkKBN!e$-BF& zmx6C#LcT+Od>+ z=v>;~Q-qB#+r%R5k;QBcIa4^ifU9#jfQ&nvgyoVkLjUsh`FJ|L9N(T)J_5)S{iU_i O(`K6f)ZGjIk$eLn5kz(X literal 0 HcmV?d00001