]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/floatcanvas/Utilities/BBox.py
Remove const bool
[wxWidgets.git] / wxPython / wx / lib / floatcanvas / Utilities / BBox.py
1 """
2 A Bounding Box object and assorted utilities , subclassed from a numpy array
3
4 """
5
6 import numpy as N
7
8 class BBox(N.ndarray):
9 """
10 A Bounding Box object:
11
12 Takes Data as an array. Data is any python sequence that can be turned into a
13 2x2 numpy array of floats:
14
15 [[MinX, MinY ],
16 [MaxX, MaxY ]]
17
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.
20
21 Usually created by the factory functions:
22
23 asBBox
24
25 and
26
27 fromPoints
28
29 """
30 def __new__(subtype, data):
31 """
32 Takes Data as an array. Data is any python sequence that can be turned into a
33 2x2 numpy array of floats:
34
35 [[MinX, MinY ],
36 [MaxX, MaxY ]]
37
38 You don't usually call this directly. BBox objects are created with the factory functions:
39
40 asBBox
41
42 and
43
44 fromPoints
45
46 """
47 arr = N.array(data, N.float)
48 arr.shape = (2,2)
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)
53
54 def Overlaps(self, BB):
55 """
56 Overlap(BB):
57
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
61 """
62
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]) ):
65 return True
66 else:
67 return False
68
69 def Inside(self, BB):
70 """
71 Inside(BB):
72
73 Tests if the given Bounding Box is entirely inside this one.
74
75 Returns True if it is entirely inside, or touching the
76 border.
77
78 Returns False otherwise
79 """
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]) ):
82 return True
83 else:
84 return False
85
86 def Merge(self, BB):
87 """
88 Joins this bounding box with the one passed in, maybe making this one bigger
89
90 """
91
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]
96
97 ### This could be used for a make BB from a bunch of BBs
98
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)
105
106
107 ## Save the ndarray __eq__ for internal use.
108 Array__eq__ = N.ndarray.__eq__
109 def __eq__(self, BB):
110 """
111 __eq__(BB) The equality operator
112
113 A == B if and only if all the entries are the same
114
115 """
116 return N.all(self.Array__eq__(BB))
117
118
119 def asBBox(data):
120 """
121 returns a BBox object.
122
123 If object is a BBox, it is returned unaltered
124
125 If object is a numpy array, a BBox object is returned that shares a
126 view of the data with that array
127
128 """
129
130 if isinstance(data, BBox):
131 return data
132 arr = N.asarray(data, N.float)
133 return N.ndarray.__new__(BBox, shape=arr.shape, dtype=arr.dtype, buffer=arr)
134
135 def fromPoints(Points):
136 """
137 fromPoints (Points).
138
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.
141
142 If a single point is passed in, a zero-size Bounding Box is returned.
143
144 """
145 Points = N.asarray(Points, N.float).reshape(-1,2)
146
147 arr = N.vstack( (Points.min(0), Points.max(0)) )
148 return N.ndarray.__new__(BBox, shape=arr.shape, dtype=arr.dtype, buffer=arr)
149
150 def fromBBArray(BBarray):
151 """
152 Builds a BBox object from an array of Bounding Boxes.
153 The resulting Bounding Box encompases all the included BBs.
154
155 The BBarray is in the shape: (Nx2x2) where BBarray[n] is a 2x2 array that represents a BBox
156 """
157
158 #upperleft = N.minimum.reduce(BBarray[:,0])
159 #lowerright = N.maximum.reduce(BBarray[:,1])
160
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)) )
165 return asBBox(arr)
166 #return asBBox( (upperleft, lowerright) ) * 2
167
168
169
170