]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/floatcanvas/Utilities/BBox.py
2 A Bounding Box object and assorted utilities , subclassed from a numpy array
10 A Bounding Box object:
12 Takes Data as an array. Data is any python sequence that can be turned into a
13 2x2 numpy array of floats:
18 It is a subclass of numpy.ndarray, so for the most part it can be used as
19 an array, and arrays that fit the above description can be used in its place.
21 Usually created by the factory functions:
30 def __new__(subtype
, data
):
32 Takes Data as an array. Data is any python sequence that can be turned into a
33 2x2 numpy array of floats:
38 You don't usually call this directly. BBox objects are created with the factory functions:
47 arr
= N
.array(data
, N
.float)
49 if arr
[0,0] > arr
[1,0] or arr
[0,1] > arr
[1,1]:
50 # note: zero sized BB OK.
51 raise ValueError("BBox values not aligned: \n minimum values must be less that maximum values")
52 return N
.ndarray
.__new
__(BBox
, shape
=arr
.shape
, dtype
=arr
.dtype
, buffer=arr
)
54 def Overlaps(self
, BB
):
58 Tests if the given Bounding Box overlaps with this one.
59 Returns True is the Bounding boxes overlap, False otherwise
60 If they are just touching, returns True
63 if ( (self
[1,0] >= BB
[0,0]) and (self
[0,0] <= BB
[1,0]) and
64 (self
[1,1] >= BB
[0,1]) and (self
[0,1] <= BB
[1,1]) ):
73 Tests if the given Bounding Box is entirely inside this one.
75 Returns True if it is entirely inside, or touching the
78 Returns False otherwise
80 if ( (BB
[0,0] >= self
[0,0]) and (BB
[1,0] <= self
[1,0]) and
81 (BB
[0,1] >= self
[0,1]) and (BB
[1,1] <= self
[1,1]) ):
88 Joins this bounding box with the one passed in, maybe making this one bigger
92 if BB
[0,0] < self
[0,0]: self
[0,0] = BB
[0,0]
93 if BB
[0,1] < self
[0,1]: self
[0,1] = BB
[0,1]
94 if BB
[1,0] > self
[1,0]: self
[1,0] = BB
[1,0]
95 if BB
[1,1] > self
[1,1]: self
[1,1] = BB
[1,1]
97 ### This could be used for a make BB from a bunch of BBs
99 #~ def _getboundingbox(bboxarray): # lrk: added this
100 #~ # returns the bounding box of a bunch of bounding boxes
101 #~ upperleft = N.minimum.reduce(bboxarray[:,0])
102 #~ lowerright = N.maximum.reduce(bboxarray[:,1])
103 #~ return N.array((upperleft, lowerright), N.float)
104 #~ _getboundingbox = staticmethod(_getboundingbox)
107 ## Save the ndarray __eq__ for internal use.
108 Array__eq__
= N
.ndarray
.__eq
__
109 def __eq__(self
, BB
):
111 __eq__(BB) The equality operator
113 A == B if and only if all the entries are the same
116 return N
.all(self
.Array__eq__(BB
))
121 returns a BBox object.
123 If object is a BBox, it is returned unaltered
125 If object is a numpy array, a BBox object is returned that shares a
126 view of the data with that array
130 if isinstance(data
, BBox
):
132 arr
= N
.asarray(data
, N
.float)
133 return N
.ndarray
.__new
__(BBox
, shape
=arr
.shape
, dtype
=arr
.dtype
, buffer=arr
)
135 def fromPoints(Points
):
139 reruns the bounding box of the set of points in Points. Points can
140 be any python object that can be turned into a numpy NX2 array of Floats.
142 If a single point is passed in, a zero-size Bounding Box is returned.
145 Points
= N
.asarray(Points
, N
.float).reshape(-1,2)
147 arr
= N
.vstack( (Points
.min(0), Points
.max(0)) )
148 return N
.ndarray
.__new
__(BBox
, shape
=arr
.shape
, dtype
=arr
.dtype
, buffer=arr
)
150 def fromBBArray(BBarray
):
152 Builds a BBox object from an array of Bounding Boxes.
153 The resulting Bounding Box encompases all the included BBs.
155 The BBarray is in the shape: (Nx2x2) where BBarray[n] is a 2x2 array that represents a BBox
158 #upperleft = N.minimum.reduce(BBarray[:,0])
159 #lowerright = N.maximum.reduce(BBarray[:,1])
161 # BBarray = N.asarray(BBarray, N.float).reshape(-1,2)
162 # arr = N.vstack( (BBarray.min(0), BBarray.max(0)) )
163 BBarray
= N
.asarray(BBarray
, N
.float).reshape(-1,2,2)
164 arr
= N
.vstack( (BBarray
[:,0,:].min(0), BBarray
[:,1,:].max(0)) )
166 #return asBBox( (upperleft, lowerright) ) * 2