Skip to content

Simplify

simplify

Simplifying functions.

dsimplify

dsimplify(
    points: list[DPoint], tolerance: float
) -> list[kdb.DPoint]

Simplify a list of um points to a certain tolerance (in um).

Uses Ramer-Douglas-Peucker algorithm

Parameters:

Name Type Description Default
points list[DPoint]

list of klayout.db.DPoint to simplify

required
tolerance float

if two points are > tolerance (in um) apart, delete most suitable points.

required
Source code in kfactory/utils/simplify.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def dsimplify(points: list[kdb.DPoint], tolerance: float) -> list[kdb.DPoint]:
    """Simplify a list of um points to a certain tolerance (in um).

    Uses [Ramer-Douglas-Peucker algorithm](https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm)

    Args:
        points: list of `klayout.db.DPoint` to simplify
        tolerance: if two points are > tolerance (in um) apart,
            delete most suitable points.
    """
    if len(points) < MIN_POINTS_FOR_SIMPLIFY:
        return points

    e = kdb.DEdge(points[0], points[-1])
    dists = [e.distance_abs(p) for p in points]
    ind_dist = int(np.argmax(dists))
    maxd = dists[ind_dist]

    return (
        [points[0], points[-1]]
        if maxd <= tolerance
        else (
            dsimplify(points[: ind_dist + 1], tolerance)
            + dsimplify(points[ind_dist:], tolerance)[1:]
        )
    )

simplify

simplify(
    points: list[Point], tolerance: float
) -> list[kdb.Point]

Simplify a list of klayout.db.Point to a certain tolerance (in dbu).

Uses Ramer-Douglas-Peucker algorithm

Parameters:

Name Type Description Default
points list[Point]

list of points to simplify

required
tolerance float

if two points are > tolerance (in dbu) apart, delete most suitable points.

required
Source code in kfactory/utils/simplify.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def simplify(points: list[kdb.Point], tolerance: float) -> list[kdb.Point]:
    """Simplify a list of `klayout.db.Point` to a certain tolerance (in dbu).

    Uses [Ramer-Douglas-Peucker algorithm](https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm)

    Args:
        points: list of points to simplify
        tolerance: if two points are > tolerance (in dbu) apart,
            delete most suitable points.
    """
    if len(points) < MIN_POINTS_FOR_SIMPLIFY:
        return points

    e = kdb.Edge(points[0], points[-1])
    dists = [e.distance_abs(p) for p in points]
    ind_dist = int(np.argmax(dists))
    maxd = dists[ind_dist]

    return (
        [points[0], points[-1]]
        if maxd <= tolerance
        else (
            simplify(points[: ind_dist + 1], tolerance)
            + simplify(points[ind_dist:], tolerance)[1:]
        )
    )