@@ -232,7 +232,7 @@ def resample_spline(points, smooth=.001, count=None, degree=3):
232232 return resampled
233233
234234
235- def points_to_spline_entity (points , smooth = .0005 , count = None ):
235+ def points_to_spline_entity (points , smooth = None , count = None ):
236236 """
237237 Create a spline entity from a curve in space
238238
@@ -251,7 +251,10 @@ def points_to_spline_entity(points, smooth=.0005, count=None):
251251 from scipy .interpolate import splprep
252252 if count is None :
253253 count = len (points )
254- points = np .asanyarray (points )
254+ if smooth is None :
255+ smooth = 0.002
256+
257+ points = np .asanyarray (points , dtype = np .float64 )
255258 closed = np .linalg .norm (points [0 ] - points [- 1 ]) < tol .merge
256259
257260 knots , control , degree = splprep (points .T , s = smooth )[0 ]
@@ -270,25 +273,6 @@ def points_to_spline_entity(points, smooth=.0005, count=None):
270273 return entity , control
271274
272275
273- def three_point (indices ):
274- """
275- Given a long list of ordered indices,
276- return the first, middle and last.
277-
278- Parameters
279- -----------
280- indices: (n,) array
281-
282- Returns
283- ----------
284- three: (3,) array
285- """
286- three = [indices [0 ],
287- indices [int (len (indices ) / 2 )],
288- indices [- 1 ]]
289- return np .asanyarray (three )
290-
291-
292276def simplify_basic (drawing , process = False , ** kwargs ):
293277 """
294278 Merge colinear segments and fit circles.
@@ -367,51 +351,51 @@ def simplify_basic(drawing, process=False, **kwargs):
367351 return simplified
368352
369353
370- def simplify_spline (path , smooth , path_indexes = None ):
354+ def simplify_spline (path , smooth = None , verbose = False ):
371355 """
372- Replace discrete curves with b-spline curves, and
356+ Replace discrete curves with b-spline or Arc and
373357 return the result as a new Path2D object.
374358
375359 Parameters
376360 ------------
377- path: Path2D object
378- smooth: float, amount to smooth
379- path_indexes: (n,) int, indexes of path.paths to simplify
361+ path : trimesh.path.Path2D
362+ Input geometry
363+ smooth : float
364+ Distance to smooth
380365
381366 Returns
382367 ------------
383- simplified: Path2D object, with specified entities replaced
368+ simplified : Path2D
369+ Consists of Arc and BSpline entities
384370 """
385- # if we aren't simplifying specific indexes
386- # simplify all indexes in the path object
387- if path_indexes is None :
388- path_indexes = np .arange (len (path .paths ))
389371
390- entities_keep = np .ones (len (path .entities ),
391- dtype = np .bool )
392372 new_vertices = []
393373 new_entities = []
374+ scale = path .scale
375+
376+ for discrete in path .discrete :
377+ circle = is_circle (discrete ,
378+ scale = scale ,
379+ verbose = verbose )
380+ if circle is not None :
381+ # the points are circular enough for our high standards
382+ # so replace them with a closed Arc entity
383+ new_entities .append (entities .Arc (points = np .arange (3 ) +
384+ len (new_vertices ),
385+ closed = True ))
386+ new_vertices .extend (circle )
387+ continue
394388
395- for i in path_indexes :
396389 # entities for this path
397- entity , vertices = points_to_spline_entity (path . discrete [ i ] )
390+ entity , vertices = points_to_spline_entity (discrete , smooth = smooth )
398391 # reindex returned control points
399- entity .points += len (path . vertices ) + len ( new_vertices )
392+ entity .points += len (new_vertices )
400393 # save entity and vertices
401- new_vertices .append (vertices )
394+ new_vertices .extend (vertices )
402395 new_entities .append (entity )
403- # we don't need any of the entities from the
404- # path we just consumed and replaced
405- entities_keep [path .paths [i ]] = False
406-
407- # flatten entities and vertices
408- entities = np .append (path .entities [entities_keep ],
409- new_entities )
410- vertices = np .vstack ((path .vertices ,
411- np .vstack (new_vertices )))
412396
413397 # create the Path2D object for the result
414- simplified = type (path )(entities = entities ,
415- vertices = vertices )
398+ simplified = type (path )(entities = new_entities ,
399+ vertices = new_vertices )
416400
417401 return simplified
0 commit comments