mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.95k stars 574 forks source link

Error calculating medial_axis of svg in Path2D object #1251

Closed TalalWasim closed 3 years ago

TalalWasim commented 3 years ago

Hello,

I am having trouble calculating the medial axis of an SVG image by loading it into a Path2D object. I have zipped and attached the svg file as well (because directly uploading svg is not supported by github apparently) temp_file.zip

import trimesh
with open('./temp_file.svg', 'r') as f:
    path = trimesh.path.exchange.svg_io.svg_to_path(f)
path2D = trimesh.path.Path2D(path['entities'], path['vertices'], path['metadata'], False)
path2D.medial_axis(path2D)

I get the following error:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-b716b9deb6da> in <module>
      1 path2D = trimesh.path.Path2D(path['entities'], path['vertices'], path['metadata'], False)
----> 2 path2D.medial_axis(path2D)

c:\users\wasim\appdata\local\programs\python\python37\lib\site-packages\trimesh\path\path.py in medial_axis(self, resolution, clip)
   1304 
   1305         # get a single Path2D of medial axis
-> 1306         medial = concatenate(medials)
   1307 
   1308         return medial

c:\users\wasim\appdata\local\programs\python\python37\lib\site-packages\trimesh\path\util.py in concatenate(paths)
     44     # generate the single new concatenated path
     45     # use input types so we don't have circular imports
---> 46     concat = type(path)(metadata=metadata,
     47                         entities=entities,
     48                         vertices=np.vstack(vertices))

UnboundLocalError: local variable 'path' referenced before assignment

Please guide me about what I am doing wrong.

mikedh commented 3 years ago

Hey, I think you want path2D.medial_axis(), as that call doesn't require the path to be passed back:

In [1]: import trimesh

In [2]: m = trimesh.load('temp_file.svg')

In [3]: m
Out[3]: <trimesh.Path2D(vertices.shape=(48, 2), len(entities)=16)>

In [4]: m.show()

In [5]: m.medial_axis()
Out[5]: <trimesh.Path2D(vertices.shape=(197, 2), len(entities)=5)>

In [6]: m.medial_axis?
Signature: m.medial_axis(resolution=None, clip=None)
Docstring:
Find the approximate medial axis based
on a voronoi diagram of evenly spaced points on the
boundary of the polygon.

Parameters
----------
resolution : None or float
  Distance between each sample on the polygon boundary
clip : None, or (2,) float
  Min, max number of samples

Returns
----------
medial : Path2D object
  Contains only medial axis of Path
File:      ~/trimesh/trimesh/path/path.py