@@ -394,10 +394,8 @@ def adaptive_mesh_estimate(
394394
395395 mn = self .srcPts .min (axis = 1 )
396396 mx = self .srcPts .max (axis = 1 )
397- xt , yt = np .meshgrid (
398- np .linspace (mn [0 ], mx [0 ], starting_grid ),
399- np .linspace (mn [1 ], mx [1 ], starting_grid ))
400- new_src = np .vstack ((xt .flatten (), yt .flatten ())).transpose ()
397+ new_src = self .src_array (
398+ mn [0 ], mn [1 ], mx [0 ], mx [1 ], starting_grid , starting_grid )
401399 old_src = self .srcPts .transpose ()
402400 old_dst = self .tform (old_src )
403401
@@ -411,3 +409,82 @@ def adaptive_mesh_estimate(
411409 max_iter = max_iter ,
412410 nworst = nworst ,
413411 niter = 0 )
412+
413+ @staticmethod
414+ def src_array (xmin , ymin , xmax , ymax , nx , ny ):
415+ """create N x 2 array of regularly spaced points
416+
417+ Parameters
418+ ----------
419+ xmin : float
420+ minimum of x grid
421+ ymin : float
422+ minimum of y grid
423+ xmax : float
424+ maximum of x grid
425+ ymax : float
426+ maximum of y grid
427+ nx : int
428+ number of points in x axis
429+ ny : int
430+ number of points in y axis
431+
432+ Returns
433+ -------
434+ src : :class:`numpy.ndarray`
435+ (nx * ny) x 2 array of coordinated.
436+
437+ """
438+ src = np .mgrid [xmin :xmax :nx * 1j , ymin :ymax :ny * 1j ].reshape (2 , - 1 ).T
439+ return src
440+
441+ def scale_coordinates (
442+ self ,
443+ factor ,
444+ ngrid = 20 ,
445+ preserve_srcPts = False ):
446+ """estimates a new ThinPlateSplineTransform from the current one
447+ in a scaled transform space.
448+
449+ Parameters
450+ ----------
451+ factor : float
452+ the factor by which to scale the space
453+ ngrid : int
454+ number of points per axis for the estimation grid
455+ preserve_srcPts : bool
456+ one might want to keep the original scaled srcPts
457+ for example, if pts were made specially for a mask
458+ or a crack or fold
459+
460+ Returns
461+ -------
462+ new_tform : :class:`renderapi.transform.ThinPlateSplineTransform`
463+ the new transform in the scaled space
464+
465+ """
466+
467+ new_tform = ThinPlateSplineTransform ()
468+ computeAffine = True
469+ if self .aMtx is None :
470+ computeAffine = False
471+
472+ mn = self .srcPts .min (axis = 1 )
473+ mx = self .srcPts .max (axis = 1 )
474+ src = self .src_array (mn [0 ], mn [1 ], mx [0 ], mx [1 ], ngrid , ngrid )
475+
476+ if preserve_srcPts :
477+ # do not repeat close points
478+ dist = scipy .spatial .distance .cdist (
479+ src ,
480+ self .srcPts .transpose (),
481+ metric = 'euclidean' )
482+ ind = np .invert (np .any (dist < 1e-3 , axis = 0 ))
483+ src = np .vstack ((src , self .srcPts .transpose ()[ind ]))
484+
485+ new_tform .estimate (
486+ src * factor ,
487+ self .tform (src ) * factor ,
488+ computeAffine = computeAffine )
489+
490+ return new_tform
0 commit comments