Coverage for tests/test_box.py: 9%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#
2# Developed for the LSST Data Management System.
3# This product includes software developed by the LSST Project
4# (https://www.lsst.org).
5# See the COPYRIGHT file at the top-level directory of this distribution
6# for details of code ownership.
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <https://www.gnu.org/licenses/>.
20#
22import unittest
24import numpy as np
26import lsst.utils.tests
27import lsst.pex.exceptions
28import lsst.geom as geom
31class Box2ITestCase(lsst.utils.tests.TestCase):
32 def setUp(self):
33 np.random.seed(1)
35 def testEmpty(self):
36 box = geom.Box2I()
37 self.assertTrue(box.isEmpty())
38 self.assertEqual(box.getWidth(), 0)
39 self.assertEqual(box.getHeight(), 0)
40 for x in (-1, 0, 1):
41 for y in (-1, 0, 1):
42 point = geom.Point2I(x, y)
43 self.assertFalse(box.contains(point))
44 box.include(point)
45 self.assertTrue(box.contains(point))
46 box = geom.Box2I()
47 box.grow(3)
48 self.assertTrue(box.isEmpty())
50 def testConstruction(self):
51 for n in range(10):
52 xmin, xmax, ymin, ymax = [
53 int(i) for i in np.random.randint(low=-5, high=5, size=4)]
54 if xmin > xmax:
55 xmin, xmax = xmax, xmin
56 if ymin > ymax:
57 ymin, ymax = ymax, ymin
58 pmin = geom.Point2I(xmin, ymin)
59 pmax = geom.Point2I(xmax, ymax)
60 # min/max constructor
61 box = geom.Box2I(pmin, pmax)
62 self.assertEqual(box.getMin(), pmin)
63 self.assertEqual(box.getMax(), pmax)
64 box = geom.Box2I(pmax, pmin)
65 self.assertEqual(box.getMin(), pmin)
66 self.assertEqual(box.getMax(), pmax)
67 box = geom.Box2I(pmin, pmax, False)
68 self.assertEqual(box.getMin(), pmin)
69 self.assertEqual(box.getMax(), pmax)
70 box = geom.Box2I(pmax, pmin, False)
71 self.assertTrue(box.isEmpty() or pmax == pmin)
72 self.assertEqual(box, geom.Box2I(box))
73 # min/dim constructor
74 dim = geom.Extent2I(1) + pmax - pmin
75 if any(dim.eq(0)):
76 box = geom.Box2I(pmin, dim)
77 self.assertTrue(box.isEmpty())
78 box = geom.Box2I(pmin, dim, False)
79 self.assertTrue(box.isEmpty())
80 else:
81 box = geom.Box2I(pmin, dim)
82 self.assertEqual(box.getMin(), pmin)
83 self.assertEqual(box.getDimensions(), dim)
84 box = geom.Box2I(pmin, dim, False)
85 self.assertEqual(box.getMin(), pmin)
86 self.assertEqual(box.getDimensions(), dim)
87 dim = -dim
88 box = geom.Box2I(pmin, dim)
89 self.assertEqual(box.getMin(), pmin + dim + geom.Extent2I(1))
90 self.assertEqual(box.getDimensions(),
91 geom.Extent2I(abs(dim.getX()), abs(dim.getY())))
93 def testOverflowDetection(self):
94 try:
95 box = geom.Box2I(geom.Point2I(2147483645, 149),
96 geom.Extent2I(8, 8))
97 except lsst.pex.exceptions.OverflowError:
98 pass
99 else:
100 # On some platforms, sizeof(int) may be > 4, so this test doesn't overflow.
101 # In that case, we just verify that there was in fact no overflow.
102 # It's hard to construct a more platform-independent test because Python doesn't
103 # provide an easy way to get sizeof(int); note that sys.maxint is
104 # usually sizeof(long).
105 self.assertLess(box.getWidth(), 0)
107 def testRepr(self):
108 box = geom.Box2I()
109 repr_str = box.__repr__()
110 self.assertTrue(repr_str.startswith('Box2I'))
111 box = geom.Box2D()
112 repr_str = box.__repr__()
113 self.assertTrue(repr_str.startswith('Box2D'))
115 def testPointExtent(self):
116 box = geom.Box2I()
117 self.assertIs(box.Point, geom.Point2I)
118 self.assertIs(box.Extent, geom.Extent2I)
119 box = geom.Box2D()
120 self.assertIs(box.Point, geom.Point2D)
121 self.assertIs(box.Extent, geom.Extent2D)
123 def testSwap(self):
124 x00, y00, x01, y01 = (0, 1, 2, 3)
125 x10, y10, x11, y11 = (4, 5, 6, 7)
127 box0 = geom.Box2I(geom.PointI(x00, y00), geom.PointI(x01, y01))
128 box1 = geom.Box2I(geom.PointI(x10, y10), geom.PointI(x11, y11))
129 box0.swap(box1)
131 self.assertEqual(box0.getMinX(), x10)
132 self.assertEqual(box0.getMinY(), y10)
133 self.assertEqual(box0.getMaxX(), x11)
134 self.assertEqual(box0.getMaxY(), y11)
136 self.assertEqual(box1.getMinX(), x00)
137 self.assertEqual(box1.getMinY(), y00)
138 self.assertEqual(box1.getMaxX(), x01)
139 self.assertEqual(box1.getMaxY(), y01)
141 def testConversion(self):
142 for n in range(10):
143 xmin, xmax, ymin, ymax = np.random.uniform(low=-10, high=10, size=4)
144 if xmin > xmax:
145 xmin, xmax = xmax, xmin
146 if ymin > ymax:
147 ymin, ymax = ymax, ymin
148 fpMin = geom.Point2D(xmin, ymin)
149 fpMax = geom.Point2D(xmax, ymax)
150 if any((fpMax-fpMin).lt(3)):
151 continue # avoid empty boxes
152 with self.subTest(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax):
153 fpBox = geom.Box2D(fpMin, fpMax)
154 intBoxBig = geom.Box2I(fpBox, geom.Box2I.EXPAND)
155 fpBoxBig = geom.Box2D(intBoxBig)
156 intBoxSmall = geom.Box2I(fpBox, geom.Box2I.SHRINK)
157 fpBoxSmall = geom.Box2D(intBoxSmall)
158 self.assertTrue(fpBoxBig.contains(fpBox))
159 self.assertTrue(fpBox.contains(fpBoxSmall))
160 self.assertTrue(intBoxBig.contains(intBoxSmall))
161 self.assertTrue(geom.Box2D(intBoxBig))
162 self.assertEqual(geom.Box2I(fpBoxBig, geom.Box2I.SHRINK), intBoxBig)
163 self.assertEqual(geom.Box2I(fpBoxSmall, geom.Box2I.SHRINK), intBoxSmall)
164 self.assertTrue(geom.Box2I(geom.Box2D()).isEmpty())
165 self.assertRaises(lsst.pex.exceptions.InvalidParameterError, geom.Box2I,
166 geom.Box2D(geom.Point2D(), geom.Point2D(float("inf"), float("inf"))))
168 def testAccessors(self):
169 xmin, xmax, ymin, ymax = [
170 int(i) for i in np.random.randint(low=-5, high=5, size=4)]
171 if xmin > xmax:
172 xmin, xmax = xmax, xmin
173 if ymin > ymax:
174 ymin, ymax = ymax, ymin
175 pmin = geom.Point2I(xmin, ymin)
176 pmax = geom.Point2I(xmax, ymax)
177 box = geom.Box2I(pmin, pmax, True)
178 self.assertEqual(pmin, box.getMin())
179 self.assertEqual(pmax, box.getMax())
180 self.assertEqual(box.getMinX(), xmin)
181 self.assertEqual(box.getMinY(), ymin)
182 self.assertEqual(box.getMaxX(), xmax)
183 self.assertEqual(box.getMaxY(), ymax)
184 self.assertEqual(box.getBegin(), pmin)
185 self.assertEqual(box.getEnd(), (pmax + geom.Extent2I(1)))
186 self.assertEqual(box.getBeginX(), xmin)
187 self.assertEqual(box.getBeginY(), ymin)
188 self.assertEqual(box.getEndX(), xmax + 1)
189 self.assertEqual(box.getEndY(), ymax + 1)
190 self.assertEqual(box.getDimensions(), (pmax - pmin + geom.Extent2I(1)))
191 self.assertEqual(box.getWidth(), (xmax - xmin + 1))
192 self.assertEqual(box.getHeight(), (ymax - ymin + 1))
193 self.assertAlmostEqual(box.getArea(), box.getWidth() * box.getHeight(),
194 places=14)
195 self.assertEqual(box.getCenterX(), 0.5*(pmax.getX() + pmin.getX()))
196 self.assertEqual(box.getCenterY(), 0.5*(pmax.getY() + pmin.getY()))
197 self.assertEqual(box.getCenter().getX(), box.getCenterX())
198 self.assertEqual(box.getCenter().getY(), box.getCenterY())
199 corners = box.getCorners()
200 self.assertEqual(corners[0], box.getMin())
201 self.assertEqual(corners[1].getX(), box.getMaxX())
202 self.assertEqual(corners[1].getY(), box.getMinY())
203 self.assertEqual(corners[2], box.getMax())
204 self.assertEqual(corners[3].getX(), box.getMinX())
205 self.assertEqual(corners[3].getY(), box.getMaxY())
207 def testRelations(self):
208 box = geom.Box2I(geom.Point2I(-2, -3), geom.Point2I(2, 1), True)
209 self.assertNotEqual(box, (3, 4, 5)) # should not throw
210 inPoints = [
211 geom.Point2I(-2, -3),
212 geom.Point2I(2, -3),
213 geom.Point2I(0, 0),
214 geom.Point2I(2, 1),
215 geom.Point2I(-2, 1),
216 ]
217 outPoints = [
218 geom.Point2I(-2, -4),
219 geom.Point2I(-3, -3),
220 geom.Point2I(2, -4),
221 geom.Point2I(3, -3),
222 geom.Point2I(3, 1),
223 geom.Point2I(2, 2),
224 geom.Point2I(-3, 1),
225 geom.Point2I(-2, 2),
226 ]
227 for point in inPoints:
228 with self.subTest(point=point):
229 self.assertTrue(box.contains(point))
230 for point in outPoints:
231 with self.subTest(point=point):
232 self.assertFalse(box.contains(point))
233 inX, inY = zip(*inPoints)
234 outX, outY = zip(*outPoints)
235 self.assertTrue(all(box.contains(np.array(inX), np.array(inY))))
236 self.assertFalse(any(box.contains(np.array(outX), np.array(outY))))
237 self.assertTrue(box.contains(geom.Box2I(
238 geom.Point2I(-1, -2), geom.Point2I(1, 0))))
239 self.assertTrue(box.contains(box))
240 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-2, -3),
241 geom.Point2I(2, 2))))
242 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-2, -3),
243 geom.Point2I(3, 1))))
244 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-3, -3),
245 geom.Point2I(2, 1))))
246 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-3, -4),
247 geom.Point2I(2, 1))))
248 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-2, -3),
249 geom.Point2I(2, 2))))
250 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-2, -3),
251 geom.Point2I(3, 1))))
252 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-3, -3),
253 geom.Point2I(2, 1))))
254 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-3, -4),
255 geom.Point2I(2, 1))))
256 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-1, -2),
257 geom.Point2I(1, 0))))
258 self.assertTrue(box.overlaps(box))
259 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(-5, -3),
260 geom.Point2I(-3, 1))))
261 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(-2, -6),
262 geom.Point2I(2, -4))))
263 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(3, -3),
264 geom.Point2I(4, 1))))
265 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(-2, 2),
266 geom.Point2I(2, 2))))
268 def testMutators(self):
269 box = geom.Box2I(geom.Point2I(-2, -3), geom.Point2I(2, 1), True)
270 box.grow(1)
271 self.assertEqual(box, geom.Box2I(
272 geom.Point2I(-3, -4), geom.Point2I(3, 2), True))
273 box.grow(geom.Extent2I(2, 3))
274 self.assertEqual(box, geom.Box2I(
275 geom.Point2I(-5, -7), geom.Point2I(5, 5), True))
276 box.shift(geom.Extent2I(3, 2))
277 self.assertEqual(box, geom.Box2I(
278 geom.Point2I(-2, -5), geom.Point2I(8, 7), True))
279 box.include(geom.Point2I(-4, 2))
280 self.assertEqual(box, geom.Box2I(
281 geom.Point2I(-4, -5), geom.Point2I(8, 7), True))
282 box.include(geom.Point2I(0, -6))
283 self.assertEqual(box, geom.Box2I(
284 geom.Point2I(-4, -6), geom.Point2I(8, 7), True))
285 box.include(geom.Box2I(geom.Point2I(0, 0), geom.Point2I(10, 11), True))
286 self.assertEqual(box, geom.Box2I(
287 geom.Point2I(-4, -6), geom.Point2I(10, 11), True))
288 box.clip(geom.Box2I(geom.Point2I(0, 0), geom.Point2I(11, 12), True))
289 self.assertEqual(box, geom.Box2I(
290 geom.Point2I(0, 0), geom.Point2I(10, 11), True))
291 box.clip(geom.Box2I(geom.Point2I(-1, -2), geom.Point2I(5, 4), True))
292 self.assertEqual(box, geom.Box2I(
293 geom.Point2I(0, 0), geom.Point2I(5, 4), True))
296class Box2DTestCase(lsst.utils.tests.TestCase):
298 def setUp(self):
299 np.random.seed(1)
301 def testEmpty(self):
302 box = geom.Box2D()
303 self.assertTrue(box.isEmpty())
304 self.assertEqual(box.getWidth(), 0.0)
305 self.assertEqual(box.getHeight(), 0.0)
306 for x in (-1, 0, 1):
307 for y in (-1, 0, 1):
308 point = geom.Point2D(x, y)
309 self.assertFalse(box.contains(point))
310 box.include(point)
311 self.assertTrue(box.contains(point))
312 box = geom.Box2D()
313 box.grow(3)
314 self.assertTrue(box.isEmpty())
316 def testConstruction(self):
317 for n in range(10):
318 xmin, xmax, ymin, ymax = np.random.uniform(low=-5, high=5, size=4)
319 if xmin > xmax:
320 xmin, xmax = xmax, xmin
321 if ymin > ymax:
322 ymin, ymax = ymax, ymin
323 pmin = geom.Point2D(xmin, ymin)
324 pmax = geom.Point2D(xmax, ymax)
325 # min/max constructor
326 box = geom.Box2D(pmin, pmax)
327 self.assertEqual(box.getMin(), pmin)
328 self.assertEqual(box.getMax(), pmax)
329 box = geom.Box2D(pmax, pmin)
330 self.assertEqual(box.getMin(), pmin)
331 self.assertEqual(box.getMax(), pmax)
332 box = geom.Box2D(pmin, pmax, False)
333 self.assertEqual(box.getMin(), pmin)
334 self.assertEqual(box.getMax(), pmax)
335 box = geom.Box2D(pmax, pmin, False)
336 self.assertTrue(box.isEmpty())
337 self.assertEqual(box, geom.Box2D(box))
338 # min/dim constructor
339 dim = pmax - pmin
340 if any(dim.eq(0)):
341 box = geom.Box2D(pmin, dim)
342 self.assertTrue(box.isEmpty())
343 box = geom.Box2D(pmin, dim, False)
344 self.assertTrue(box.isEmpty())
345 else:
346 box = geom.Box2D(pmin, dim)
347 self.assertEqual(box.getMin(), pmin)
348 self.assertEqual(box.getDimensions(), dim)
349 box = geom.Box2D(pmin, dim, False)
350 self.assertEqual(box.getMin(), pmin)
351 self.assertEqual(box.getDimensions(), dim)
352 dim = -dim
353 box = geom.Box2D(pmin, dim)
354 self.assertEqual(box.getMin(), pmin + dim)
355 self.assertFloatsAlmostEqual(box.getDimensions(),
356 geom.Extent2D(abs(dim.getX()), abs(dim.getY())))
358 def testSwap(self):
359 x00, y00, x01, y01 = (0., 1., 2., 3.)
360 x10, y10, x11, y11 = (4., 5., 6., 7.)
362 box0 = geom.Box2D(geom.PointD(x00, y00), geom.PointD(x01, y01))
363 box1 = geom.Box2D(geom.PointD(x10, y10), geom.PointD(x11, y11))
364 box0.swap(box1)
366 self.assertEqual(box0.getMinX(), x10)
367 self.assertEqual(box0.getMinY(), y10)
368 self.assertEqual(box0.getMaxX(), x11)
369 self.assertEqual(box0.getMaxY(), y11)
371 self.assertEqual(box1.getMinX(), x00)
372 self.assertEqual(box1.getMinY(), y00)
373 self.assertEqual(box1.getMaxX(), x01)
374 self.assertEqual(box1.getMaxY(), y01)
376 def testAccessors(self):
377 xmin, xmax, ymin, ymax = np.random.uniform(low=-5, high=5, size=4)
378 if xmin > xmax:
379 xmin, xmax = xmax, xmin
380 if ymin > ymax:
381 ymin, ymax = ymax, ymin
382 pmin = geom.Point2D(xmin, ymin)
383 pmax = geom.Point2D(xmax, ymax)
384 box = geom.Box2D(pmin, pmax, True)
385 self.assertEqual(pmin, box.getMin())
386 self.assertEqual(pmax, box.getMax())
387 self.assertEqual(box.getMinX(), xmin)
388 self.assertEqual(box.getMinY(), ymin)
389 self.assertEqual(box.getMaxX(), xmax)
390 self.assertEqual(box.getMaxY(), ymax)
391 self.assertEqual(box.getDimensions(), (pmax - pmin))
392 self.assertEqual(box.getWidth(), (xmax - xmin))
393 self.assertEqual(box.getHeight(), (ymax - ymin))
394 self.assertEqual(box.getArea(), box.getWidth() * box.getHeight())
395 self.assertEqual(box.getCenterX(), 0.5*(pmax.getX() + pmin.getX()))
396 self.assertEqual(box.getCenterY(), 0.5*(pmax.getY() + pmin.getY()))
397 self.assertEqual(box.getCenter().getX(), box.getCenterX())
398 self.assertEqual(box.getCenter().getY(), box.getCenterY())
399 corners = box.getCorners()
400 self.assertEqual(corners[0], box.getMin())
401 self.assertEqual(corners[1].getX(), box.getMaxX())
402 self.assertEqual(corners[1].getY(), box.getMinY())
403 self.assertEqual(corners[2], box.getMax())
404 self.assertEqual(corners[3].getX(), box.getMinX())
405 self.assertEqual(corners[3].getY(), box.getMaxY())
407 def testRelations(self):
408 box = geom.Box2D(geom.Point2D(-2, -3), geom.Point2D(2, 1), True)
409 inPoints = [
410 geom.Point2D(0, 0),
411 geom.Point2D(-2, -3),
412 ]
413 outPoints = [
414 geom.Point2D(2, -3),
415 geom.Point2D(2, 1),
416 geom.Point2D(-2, 1),
417 ]
418 for point in inPoints:
419 with self.subTest(point=point):
420 self.assertTrue(box.contains(point))
421 for point in outPoints:
422 with self.subTest(point=point):
423 self.assertFalse(box.contains(point))
424 inX, inY = zip(*inPoints)
425 outX, outY = zip(*outPoints)
426 self.assertTrue(box.contains(geom.Box2D(
427 geom.Point2D(-1, -2), geom.Point2D(1, 0))))
428 self.assertTrue(box.contains(box))
429 self.assertFalse(box.contains(geom.Box2D(
430 geom.Point2D(-2, -3), geom.Point2D(2, 2))))
431 self.assertFalse(box.contains(geom.Box2D(
432 geom.Point2D(-2, -3), geom.Point2D(3, 1))))
433 self.assertFalse(box.contains(geom.Box2D(
434 geom.Point2D(-3, -3), geom.Point2D(2, 1))))
435 self.assertFalse(box.contains(geom.Box2D(
436 geom.Point2D(-3, -4), geom.Point2D(2, 1))))
437 self.assertTrue(box.overlaps(geom.Box2D(
438 geom.Point2D(-2, -3), geom.Point2D(2, 2))))
439 self.assertTrue(box.overlaps(geom.Box2D(
440 geom.Point2D(-2, -3), geom.Point2D(3, 1))))
441 self.assertTrue(box.overlaps(geom.Box2D(
442 geom.Point2D(-3, -3), geom.Point2D(2, 1))))
443 self.assertTrue(box.overlaps(geom.Box2D(
444 geom.Point2D(-3, -4), geom.Point2D(2, 1))))
445 self.assertTrue(box.overlaps(geom.Box2D(
446 geom.Point2D(-1, -2), geom.Point2D(1, 0))))
447 self.assertTrue(box.overlaps(box))
448 self.assertFalse(box.overlaps(geom.Box2D(
449 geom.Point2D(-5, -3), geom.Point2D(-3, 1))))
450 self.assertFalse(box.overlaps(geom.Box2D(
451 geom.Point2D(-2, -6), geom.Point2D(2, -4))))
452 self.assertFalse(box.overlaps(geom.Box2D(
453 geom.Point2D(3, -3), geom.Point2D(4, 1))))
454 self.assertFalse(box.overlaps(geom.Box2D(
455 geom.Point2D(-2, 2), geom.Point2D(2, 2))))
456 self.assertFalse(box.overlaps(geom.Box2D(
457 geom.Point2D(-2, -5), geom.Point2D(2, -3))))
458 self.assertFalse(box.overlaps(geom.Box2D(
459 geom.Point2D(-4, -3), geom.Point2D(-2, 1))))
460 self.assertFalse(box.contains(geom.Box2D(
461 geom.Point2D(-2, 1), geom.Point2D(2, 3))))
462 self.assertFalse(box.contains(geom.Box2D(
463 geom.Point2D(2, -3), geom.Point2D(4, 1))))
465 def testMutators(self):
466 box = geom.Box2D(geom.Point2D(-2, -3), geom.Point2D(2, 1), True)
467 box.grow(1)
468 self.assertEqual(box, geom.Box2D(
469 geom.Point2D(-3, -4), geom.Point2D(3, 2), True))
470 box.grow(geom.Extent2D(2, 3))
471 self.assertEqual(box, geom.Box2D(
472 geom.Point2D(-5, -7), geom.Point2D(5, 5), True))
473 box.shift(geom.Extent2D(3, 2))
474 self.assertEqual(box, geom.Box2D(
475 geom.Point2D(-2, -5), geom.Point2D(8, 7), True))
476 box.include(geom.Point2D(-4, 2))
477 self.assertEqual(box, geom.Box2D(
478 geom.Point2D(-4, -5), geom.Point2D(8, 7), True))
479 self.assertTrue(box.contains(geom.Point2D(-4, 2)))
480 box.include(geom.Point2D(0, -6))
481 self.assertEqual(box, geom.Box2D(
482 geom.Point2D(-4, -6), geom.Point2D(8, 7), True))
483 box.include(geom.Box2D(geom.Point2D(0, 0), geom.Point2D(10, 11), True))
484 self.assertEqual(box, geom.Box2D(
485 geom.Point2D(-4, -6), geom.Point2D(10, 11), True))
486 box.clip(geom.Box2D(geom.Point2D(0, 0), geom.Point2D(11, 12), True))
487 self.assertEqual(box, geom.Box2D(
488 geom.Point2D(0, 0), geom.Point2D(10, 11), True))
489 box.clip(geom.Box2D(geom.Point2D(-1, -2), geom.Point2D(5, 4), True))
490 self.assertEqual(box, geom.Box2D(
491 geom.Point2D(0, 0), geom.Point2D(5, 4), True))
493 def testFlipI(self):
494 parentExtent = geom.Extent2I(15, 20)
495 x00, y00, x11, y11 = (8, 11, 13, 16)
496 lrx00, lry00, lrx11, lry11 = (1, 11, 6, 16)
497 tbx00, tby00, tbx11, tby11 = (8, 3, 13, 8)
499 box0 = geom.Box2I(geom.Point2I(x00, y00),
500 geom.Point2I(x11, y11))
501 box1 = geom.Box2I(geom.Point2I(x00, y00),
502 geom.Point2I(x11, y11))
503 box0.flipLR(parentExtent[0])
504 box1.flipTB(parentExtent[1])
506 # test flip RL
507 self.assertEqual(box0.getMinX(), lrx00)
508 self.assertEqual(box0.getMinY(), lry00)
509 self.assertEqual(box0.getMaxX(), lrx11)
510 self.assertEqual(box0.getMaxY(), lry11)
512 # test flip TB
513 self.assertEqual(box1.getMinX(), tbx00)
514 self.assertEqual(box1.getMinY(), tby00)
515 self.assertEqual(box1.getMaxX(), tbx11)
516 self.assertEqual(box1.getMaxY(), tby11)
518 def testFlipD(self):
519 parentExtent = geom.Extent2D(15.1, 20.6)
520 x00, y00, x11, y11 = (8.3, 11.4, 13.2, 16.9)
521 lrx00, lry00, lrx11, lry11 = (1.9, 11.4, 6.8, 16.9)
522 tbx00, tby00, tbx11, tby11 = (8.3, 3.7, 13.2, 9.2)
524 box0 = geom.Box2D(geom.Point2D(x00, y00),
525 geom.Point2D(x11, y11))
526 box1 = geom.Box2D(geom.Point2D(x00, y00),
527 geom.Point2D(x11, y11))
528 box0.flipLR(parentExtent[0])
529 box1.flipTB(parentExtent[1])
531 # test flip RL
532 self.assertAlmostEqual(box0.getMinX(), lrx00, places=6)
533 self.assertAlmostEqual(box0.getMinY(), lry00, places=6)
534 self.assertAlmostEqual(box0.getMaxX(), lrx11, places=6)
535 self.assertAlmostEqual(box0.getMaxY(), lry11, places=6)
537 # test flip TB
538 self.assertAlmostEqual(box1.getMinX(), tbx00, places=6)
539 self.assertAlmostEqual(box1.getMinY(), tby00, places=6)
540 self.assertAlmostEqual(box1.getMaxX(), tbx11, places=6)
541 self.assertAlmostEqual(box1.getMaxY(), tby11, places=6)
544class SharedBoxTestCase(lsst.utils.tests.TestCase):
545 """Tests of Box2I and Box2D where the code for both classes is the same,
546 and only the test fixtures need be different.
547 """
548 def setUp(self):
549 np.random.seed(1)
551 def testMakeCenteredBox(self):
552 dimensionsI = [geom.Extent2I(100, 50), geom.Extent2I(15, 15),
553 geom.Extent2I(0, 10), geom.Extent2I(25, 30),
554 geom.Extent2I(15, -5)]
555 dimensionsD = [geom.Extent2D(d) for d in dimensionsI] \
556 + [geom.Extent2D(1.5, 2.1), geom.Extent2D(4, 3.7),
557 geom.Extent2D(-0.1, -0.1), geom.Extent2D(5.5, 5.5),
558 geom.Extent2D(-np.nan, 5.5), geom.Extent2D(4, np.inf)]
559 locations = [geom.Point2D(0, 0), geom.Point2D(0.2, 0.7),
560 geom.Point2D(1, 1.5),
561 geom.Point2D(-0.5 + 1e-4, -0.5 + 1e-4),
562 geom.Point2D(-0.5 - 1e-4, -0.5 - 1e-4),
563 geom.Point2D(-np.nan, 0), geom.Point2D(1.0, np.inf),
564 ]
566 for center in locations:
567 for size in dimensionsI:
568 self._checkBoxConstruction(geom.Box2I, size, center, np.sqrt(0.5))
569 for size in dimensionsD:
570 self._checkBoxConstruction(geom.Box2D, size, center, 1e-10)
572 def _checkBoxConstruction(self, boxClass, size, center, precision):
573 """Test attempts to create a centered box of a particular type
574 and parameters.
576 Parameters
577 ----------
578 boxClass : `type`
579 One of `lsst.geom.Box2I` or `lsst.geom.Box2D`.
580 size : ``boxClass.Extent``
581 The desired dimensions of the box.
582 center : `lsst.geom.Point2D`
583 The desired center of the box.
584 precision : `float`
585 The maximum distance by which the box can be offset from ``center``.
586 """
587 msg = 'Box size = %s, location = %s' % (size, center)
589 if all(np.isfinite(center)):
590 box = boxClass.makeCenteredBox(center, size)
591 if all(size.gt(0)):
592 self.assertIsNotNone(box, msg=msg)
593 self.assertPairsAlmostEqual(box.getCenter(), center, maxDiff=precision, msg=msg)
594 self.assertPairsAlmostEqual(box.getDimensions(), size, msg=msg)
595 else:
596 self.assertTrue(box.isEmpty(), msg=msg)
597 elif boxClass == geom.Box2I:
598 with self.assertRaises(lsst.pex.exceptions.InvalidParameterError, msg=msg):
599 boxClass.makeCenteredBox(center, size)
602class MemoryTester(lsst.utils.tests.MemoryTestCase):
603 pass
606def setup_module(module):
607 lsst.utils.tests.init()
610if __name__ == "__main__": 610 ↛ 611line 610 didn't jump to line 611, because the condition on line 610 was never true
611 lsst.utils.tests.init()
612 unittest.main()