Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix linestring to path #2352

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/test_exchange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from trimesh.path.exchange.misc import linestrings_to_path
from trimesh.path.exchange.load import load_path

from shapely.geometry import LineString, MultiLineString


def test_linestrings_to_path():
line = LineString([(0, 0), (1, 1), (2, 0)])

result = linestrings_to_path(line)

assert len(result["entities"]) == 1
assert len(result["vertices"]) == 3


def test_multilinestrings_to_path():
line = MultiLineString([
LineString([(0, 0), (1, 0), (2, 0)]),
LineString([(0, 1), (1, 1), (2, 1)])
])

result = linestrings_to_path(line)

assert len(result["entities"]) == 2
assert len(result["vertices"]) == 6


def test_load_path_with_multilinestrings():
line = MultiLineString([
LineString([(0, 0), (1, 0), (2, 0)]),
LineString([(0, 1), (1, 1), (2, 1)])
])

result = load_path(line)

assert len(result.entities) == 2
assert len(result.vertices) == 6
assert result.length == 4
20 changes: 12 additions & 8 deletions trimesh/path/exchange/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def dict_to_path(as_dict):

def lines_to_path(lines: ArrayLike, index: Optional[NDArray[np.int64]] = None) -> Dict:
"""
Turn line segments into a Path2D or Path3D object.
Turn line segments into argument to be used for a Path2D or Path3D.

Parameters
------------
Expand All @@ -50,7 +50,7 @@ def lines_to_path(lines: ArrayLike, index: Optional[NDArray[np.int64]] = None) -

Returns
-----------
kwargs : dict
kwargs : Dict
kwargs for Path constructor
"""
lines = np.asanyarray(lines, dtype=np.float64)
Expand Down Expand Up @@ -120,25 +120,29 @@ def polygon_to_path(polygon):
return kwargs


def linestrings_to_path(multi):
def linestrings_to_path(multi) -> Dict:
"""
Load shapely LineString objects into a trimesh.path.Path2D object
Load shapely LineString objects into arguments to create a Path2D or Path3D.

Parameters
-------------
multi : shapely.geometry.LineString or MultiLineString
Input 2D geometry
Input 2D or 3D geometry

Returns
-------------
kwargs : dict
Keyword arguments for Path2D constructor
kwargs : Dict
Keyword arguments for Path2D or Path3D constructor
"""
import shapely

# append to result as we go
entities = []
vertices = []

if not util.is_sequence(multi):
if isinstance(multi, shapely.MultiLineString):
multi = list(multi.geoms)
else:
multi = [multi]

for line in multi:
Expand Down
Loading