Skip to content

Commit

Permalink
Do not discard minimum-length paths in `FlexPath::remove_overlapping_…
Browse files Browse the repository at this point in the history
…points()` (#278)

* Test minimum-length paths

* Only discard point if length is strictly less than tolerance
  • Loading branch information
jatoben authored Nov 23, 2024
1 parent 18167ef commit 1b36890
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/flexpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void FlexPath::remove_overlapping_points() {
const double tol_sq = spine.tolerance * spine.tolerance;
Array<Vec2>* 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++)
Expand Down
9 changes: 9 additions & 0 deletions tests/flexpath_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

33 changes: 31 additions & 2 deletions tests/library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
# Boost Software License - Version 1.0. See the accompanying
# LICENSE file or <http://www.boost.org/LICENSE_1_0.txt>

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


Expand Down Expand Up @@ -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(),
)
Binary file added tests/min_length_path.oas
Binary file not shown.

0 comments on commit 1b36890

Please sign in to comment.