@@ -33,61 +33,48 @@ def __init__(self, points):
3333 self ._points = N .asarray (points , N .float_ )
3434 self .track = False
3535
36- # JDH: if you define a del method, the garbage collector won't
37- # destory cyclic references, so make sure you either manage these
38- # yourself or remove the __del__ after testing
39- def __del__ (self ):
40- if self .track :
41- print "Bbox::__del__"
42-
4336 #@staticmethod
4437 def unit ():
45- return Bbox ([[ 0 , 0 ], [ 1 , 1 ]] )
38+ return Bbox . from_lbrt ( 0. , 0. , 1. , 1. )
4639 unit = staticmethod (unit )
4740
4841 #@staticmethod
4942 def from_lbwh (left , bottom , width , height ):
50- return Bbox ([[ left , bottom ], [ left + width , bottom + height ]] )
43+ return Bbox . from_lbrt ( left , bottom , left + width , bottom + height )
5144 from_lbwh = staticmethod (from_lbwh )
5245
5346 #@staticmethod
54- def from_lbrt (left , bottom , right , top ):
55- return Bbox ([[left , bottom ], [right , top ]])
47+ def from_lbrt (* args ):
48+ points = N .array (args , dtype = N .float_ ).reshape (2 , 2 )
49+ return Bbox (points )
5650 from_lbrt = staticmethod (from_lbrt )
5751
58-
52+ def __cmp__ (self , other ):
53+ # MGDTODO: Totally suboptimal
54+ if isinstance (other , Bbox ):
55+ if (self ._points == other ._points ).all ():
56+ return 0
57+ return - 1
58+
5959 # JDH: the update method will update the box limits from the
6060 # existing limits and the new data; it appears here you are just
6161 # using the new data. We use an "ignore" flag to specify whether
6262 # you want to include the existing data or not in the update
6363 def update_from_data (self , x , y ):
6464 self ._points = N .array ([[x .min (), y .min ()], [x .max (), y .max ()]], N .float_ )
6565 self .invalidate ()
66- if self .track :
67- print "Bbox::update_from_data" , self ._points
6866
6967 def copy (self ):
70- if self .track :
71- print "Bbox::copy"
7268 return Bbox (self ._points .copy ())
7369
7470 def __repr__ (self ):
7571 return 'Bbox(%s)' % repr (self ._points )
7672 __str__ = __repr__
7773
78- def __cmp__ (self , other ):
79- # MGDTODO: Totally suboptimal
80- if isinstance (other , Bbox ):
81- return (self ._points == other ._points ).all ()
82- return - 1
83-
8474 # MGDTODO: Probably a more efficient ways to do this...
8575 def _get_xmin (self ):
86- if self .track :
87- print "Bbox::_get_xmin"
8876 return self ._points [0 , 0 ]
8977 def _set_xmin (self , val ):
90- print "Bbox::_set_xmin"
9178 self ._points [0 , 0 ] = val
9279 self .invalidate ()
9380 xmin = property (_get_xmin , _set_xmin )
@@ -150,10 +137,10 @@ def _get_height(self):
150137 height = property (_get_height )
151138
152139 def transformed (self , transform ):
153- return Bbox (self . transform (self ._points ))
140+ return Bbox (transform (self ._points ))
154141
155142 def inverse_transformed (self , transform ):
156- return Bbox (self . transform .inverted ()(self ._points ))
143+ return Bbox (transform .inverted ()(self ._points ))
157144
158145 def get_bounds (self ):
159146 return (self .xmin , self .ymin ,
@@ -249,6 +236,14 @@ def __repr__(self):
249236 return "Affine2D(%s)" % repr (self ._mtx )
250237 __str__ = __repr__
251238
239+ def __cmp__ (self , other ):
240+ # MGDTODO: We need to decide if we want deferred transforms
241+ # to be equal to this one
242+ if isinstance (other , Affine2D ):
243+ if (self .get_matrix () == other .get_matrix ()).all ():
244+ return 0
245+ return - 1
246+
252247 def _do_invalidation (self ):
253248 result = self ._inverted is None
254249 self ._inverted = None
@@ -380,10 +375,9 @@ def _make__mtx(self):
380375 if self ._mtx is None :
381376 x_mtx = self ._x .get_matrix ()
382377 y_mtx = self ._y .get_matrix ()
378+ # This works because we already know the transforms are
379+ # separable
383380 self ._mtx = N .vstack ([x_mtx [0 ], y_mtx [1 ], [0.0 , 0.0 , 1.0 ]])
384- # self._mtx = self.matrix_from_values(
385- # x_mtx[0,0], 0.0, 0.0, y_mtx[1,1], x_mtx[0,2], y_mtx[1,2])
386- print "Blended" , x_mtx , y_mtx , self ._mtx
387381
388382 def is_separable (self ):
389383 return True
@@ -429,8 +423,8 @@ def _do_invalidation(self):
429423 def _make__mtx (self ):
430424 if self ._mtx is None :
431425 self ._mtx = self ._concat (
432- self ._b .get_matrix (),
433- self ._a .get_matrix ())
426+ self ._a .get_matrix (),
427+ self ._b .get_matrix ())
434428
435429 def get_matrix (self ):
436430 self ._make__mtx ()
@@ -547,12 +541,70 @@ def interval_contains_open(interval, val):
547541 return interval [0 ] < val and interval [1 ] > val
548542
549543if __name__ == '__main__' :
544+ bbox = Bbox .from_lbrt (10. , 15. , 20. , 25. )
545+ assert bbox .xmin == 10
546+ assert bbox .ymin == 15
547+ assert bbox .xmax == 20
548+ assert bbox .ymax == 25
549+
550+ assert N .all (bbox .min == [10 , 15 ])
551+ assert N .all (bbox .max == [20 , 25 ])
552+ assert N .all (bbox .intervalx == (10 , 20 ))
553+ assert N .all (bbox .intervaly == (15 , 25 ))
554+
555+ assert bbox .width == 10
556+ assert bbox .height == 10
557+
558+ assert bbox .get_bounds () == (10 , 15 , 10 , 10 )
559+
560+ bbox .intervalx = (11 , 21 )
561+ bbox .intervaly = (16 , 26 )
562+
563+ assert bbox .get_bounds () == (11 , 16 , 10 , 10 )
564+
565+ bbox .xmin = 12
566+ bbox .ymin = 17
567+ bbox .xmax = 22
568+ bbox .ymax = 27
569+
570+ assert bbox .get_bounds () == (12 , 17 , 10 , 10 )
571+
572+ bbox = Bbox .from_lbwh (10 , 11 , 12 , 13 )
573+ assert bbox .get_bounds () == (10 , 11 , 12 , 13 )
574+
575+ bbox_copy = bbox .copy ()
576+ assert bbox == bbox_copy
577+ bbox_copy .max = (14 , 15 )
578+ assert bbox .get_bounds () == (10 , 11 , 12 , 13 )
579+ assert bbox_copy .get_bounds () == (10 , 11 , 4 , 4 )
580+
550581 bbox1 = Bbox ([[10. , 15. ], [20. , 25. ]])
551582 bbox2 = Bbox ([[30. , 35. ], [40. , 45. ]])
552583 trans = BboxTransform (bbox1 , bbox2 )
553- print trans (bbox1 ._points )
554-
555- bbox2 .intervalx = 50 , 55
556- print trans (bbox1 ._points )
584+ bbox3 = bbox1 .transformed (trans )
585+ assert bbox3 == bbox2
586+
587+ translation = Affine2D ().translate (10 , 20 )
588+ assert translation .to_values () == (1 , 0 , 0 , 1 , 10 , 20 )
589+ scale = Affine2D ().scale (10 , 20 )
590+ assert scale .to_values () == (10 , 0 , 0 , 20 , 0 , 0 )
591+ rotation = Affine2D ().rotate_deg (30 )
592+ print rotation .to_values () == (0.86602540378443871 , 0.49999999999999994 , - 0.49999999999999994 , 0.86602540378443871 , 0.0 , 0.0 )
593+
594+ points = N .array ([[1 ,2 ],[3 ,4 ],[5 ,6 ],[7 ,8 ]], N .float_ )
595+ translated_points = translation (points )
596+ assert (translated_points == [[11. , 22. ], [13. , 24. ], [15. , 26. ], [17. , 28. ]]).all ()
597+ scaled_points = scale (points )
598+ print scaled_points
599+ rotated_points = rotation (points )
600+ print rotated_points
601+
602+ tpoints1 = rotation (translation (scale (points )))
603+ trans_sum = rotation + translation + scale
604+ tpoints2 = trans_sum (points )
605+ print tpoints1 , tpoints2
606+ print tpoints1 == tpoints2
607+ # Need to do some sort of fuzzy comparison here?
608+ # assert (tpoints1 == tpoints2).all()
557609
558610__all__ = ['Transform' , 'Affine2D' ]
0 commit comments