Coverage for tests/test_box.py: 7%
419 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-03 02:35 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-03 02:35 -0700
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 test_Box2I_repr(self):
108 from lsst.geom import Box2I, Point2I, Extent2I
109 self.assertEqual(eval(repr(Box2I())), Box2I())
110 self.assertEqual(eval(repr(Box2I(Point2I(1, 2), Extent2I(3, 4)))),
111 Box2I(Point2I(1, 2), Extent2I(3, 4)))
113 def test_Box2D_repr(self):
114 from lsst.geom import Box2D, Point2D, Extent2D
115 print(repr(Box2D()))
116 self.assertEqual(eval(repr(Box2D())), Box2D())
117 self.assertEqual(eval(repr(Box2D(Point2D(1.0, 2.0), Extent2D(3.0, 4.0)))),
118 Box2D(Point2D(1.0, 2.0), Extent2D(3.0, 4.0)))
120 def testPointExtent(self):
121 box = geom.Box2I()
122 self.assertIs(box.Point, geom.Point2I)
123 self.assertIs(box.Extent, geom.Extent2I)
124 box = geom.Box2D()
125 self.assertIs(box.Point, geom.Point2D)
126 self.assertIs(box.Extent, geom.Extent2D)
128 def testSwap(self):
129 x00, y00, x01, y01 = (0, 1, 2, 3)
130 x10, y10, x11, y11 = (4, 5, 6, 7)
132 box0 = geom.Box2I(geom.PointI(x00, y00), geom.PointI(x01, y01))
133 box1 = geom.Box2I(geom.PointI(x10, y10), geom.PointI(x11, y11))
134 box0.swap(box1)
136 self.assertEqual(box0.getMinX(), x10)
137 self.assertEqual(box0.getMinY(), y10)
138 self.assertEqual(box0.getMaxX(), x11)
139 self.assertEqual(box0.getMaxY(), y11)
141 self.assertEqual(box1.getMinX(), x00)
142 self.assertEqual(box1.getMinY(), y00)
143 self.assertEqual(box1.getMaxX(), x01)
144 self.assertEqual(box1.getMaxY(), y01)
146 def testConversion(self):
147 for n in range(10):
148 xmin, xmax, ymin, ymax = np.random.uniform(low=-10, high=10, size=4)
149 if xmin > xmax:
150 xmin, xmax = xmax, xmin
151 if ymin > ymax:
152 ymin, ymax = ymax, ymin
153 fpMin = geom.Point2D(xmin, ymin)
154 fpMax = geom.Point2D(xmax, ymax)
155 if any((fpMax-fpMin).lt(3)):
156 continue # avoid empty boxes
157 with self.subTest(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax):
158 fpBox = geom.Box2D(fpMin, fpMax)
159 intBoxBig = geom.Box2I(fpBox, geom.Box2I.EXPAND)
160 fpBoxBig = geom.Box2D(intBoxBig)
161 intBoxSmall = geom.Box2I(fpBox, geom.Box2I.SHRINK)
162 fpBoxSmall = geom.Box2D(intBoxSmall)
163 self.assertTrue(fpBoxBig.contains(fpBox))
164 self.assertTrue(fpBox.contains(fpBoxSmall))
165 self.assertTrue(intBoxBig.contains(intBoxSmall))
166 self.assertTrue(geom.Box2D(intBoxBig))
167 self.assertEqual(geom.Box2I(fpBoxBig, geom.Box2I.SHRINK), intBoxBig)
168 self.assertEqual(geom.Box2I(fpBoxSmall, geom.Box2I.SHRINK), intBoxSmall)
169 self.assertTrue(geom.Box2I(geom.Box2D()).isEmpty())
170 self.assertRaises(lsst.pex.exceptions.InvalidParameterError, geom.Box2I,
171 geom.Box2D(geom.Point2D(), geom.Point2D(float("inf"), float("inf"))))
173 def testAccessors(self):
174 xmin, xmax, ymin, ymax = [
175 int(i) for i in np.random.randint(low=-5, high=5, size=4)]
176 if xmin > xmax:
177 xmin, xmax = xmax, xmin
178 if ymin > ymax:
179 ymin, ymax = ymax, ymin
180 pmin = geom.Point2I(xmin, ymin)
181 pmax = geom.Point2I(xmax, ymax)
182 box = geom.Box2I(pmin, pmax, True)
183 self.assertEqual(pmin, box.getMin())
184 self.assertEqual(pmax, box.getMax())
185 self.assertEqual(box.getMinX(), xmin)
186 self.assertEqual(box.getMinY(), ymin)
187 self.assertEqual(box.getMaxX(), xmax)
188 self.assertEqual(box.getMaxY(), ymax)
189 self.assertEqual(box.getBegin(), pmin)
190 self.assertEqual(box.getEnd(), (pmax + geom.Extent2I(1)))
191 self.assertEqual(box.getBeginX(), xmin)
192 self.assertEqual(box.getBeginY(), ymin)
193 self.assertEqual(box.getEndX(), xmax + 1)
194 self.assertEqual(box.getEndY(), ymax + 1)
195 self.assertEqual(box.getDimensions(), (pmax - pmin + geom.Extent2I(1)))
196 self.assertEqual(box.getWidth(), (xmax - xmin + 1))
197 self.assertEqual(box.getHeight(), (ymax - ymin + 1))
198 self.assertAlmostEqual(box.getArea(), box.getWidth() * box.getHeight(),
199 places=14)
200 self.assertEqual(box.getCenterX(), 0.5*(pmax.getX() + pmin.getX()))
201 self.assertEqual(box.getCenterY(), 0.5*(pmax.getY() + pmin.getY()))
202 self.assertEqual(box.getCenter().getX(), box.getCenterX())
203 self.assertEqual(box.getCenter().getY(), box.getCenterY())
204 corners = box.getCorners()
205 self.assertEqual(corners[0], box.getMin())
206 self.assertEqual(corners[1].getX(), box.getMaxX())
207 self.assertEqual(corners[1].getY(), box.getMinY())
208 self.assertEqual(corners[2], box.getMax())
209 self.assertEqual(corners[3].getX(), box.getMinX())
210 self.assertEqual(corners[3].getY(), box.getMaxY())
212 def testRelations(self):
213 box = geom.Box2I(geom.Point2I(-2, -3), geom.Point2I(2, 1), True)
214 self.assertNotEqual(box, (3, 4, 5)) # should not throw
215 inPoints = [
216 geom.Point2I(-2, -3),
217 geom.Point2I(2, -3),
218 geom.Point2I(0, 0),
219 geom.Point2I(2, 1),
220 geom.Point2I(-2, 1),
221 ]
222 outPoints = [
223 geom.Point2I(-2, -4),
224 geom.Point2I(-3, -3),
225 geom.Point2I(2, -4),
226 geom.Point2I(3, -3),
227 geom.Point2I(3, 1),
228 geom.Point2I(2, 2),
229 geom.Point2I(-3, 1),
230 geom.Point2I(-2, 2),
231 ]
232 for point in inPoints:
233 with self.subTest(point=point):
234 self.assertTrue(box.contains(point))
235 for point in outPoints:
236 with self.subTest(point=point):
237 self.assertFalse(box.contains(point))
238 inX, inY = zip(*inPoints)
239 outX, outY = zip(*outPoints)
240 self.assertTrue(all(box.contains(np.array(inX), np.array(inY))))
241 self.assertFalse(any(box.contains(np.array(outX), np.array(outY))))
242 self.assertTrue(box.contains(geom.Box2I(
243 geom.Point2I(-1, -2), geom.Point2I(1, 0))))
244 self.assertTrue(box.contains(box))
245 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-2, -3),
246 geom.Point2I(2, 2))))
247 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-2, -3),
248 geom.Point2I(3, 1))))
249 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-3, -3),
250 geom.Point2I(2, 1))))
251 self.assertFalse(box.contains(geom.Box2I(geom.Point2I(-3, -4),
252 geom.Point2I(2, 1))))
253 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-2, -3),
254 geom.Point2I(2, 2))))
255 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-2, -3),
256 geom.Point2I(3, 1))))
257 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-3, -3),
258 geom.Point2I(2, 1))))
259 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-3, -4),
260 geom.Point2I(2, 1))))
261 self.assertTrue(box.overlaps(geom.Box2I(geom.Point2I(-1, -2),
262 geom.Point2I(1, 0))))
263 self.assertTrue(box.overlaps(box))
264 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(-5, -3),
265 geom.Point2I(-3, 1))))
266 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(-2, -6),
267 geom.Point2I(2, -4))))
268 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(3, -3),
269 geom.Point2I(4, 1))))
270 self.assertFalse(box.overlaps(geom.Box2I(geom.Point2I(-2, 2),
271 geom.Point2I(2, 2))))
273 def testMutators(self):
274 box = geom.Box2I(geom.Point2I(-2, -3), geom.Point2I(2, 1), True)
275 box.grow(1)
276 self.assertEqual(box, geom.Box2I(
277 geom.Point2I(-3, -4), geom.Point2I(3, 2), True))
278 box.grow(geom.Extent2I(2, 3))
279 self.assertEqual(box, geom.Box2I(
280 geom.Point2I(-5, -7), geom.Point2I(5, 5), True))
281 box.shift(geom.Extent2I(3, 2))
282 self.assertEqual(box, geom.Box2I(
283 geom.Point2I(-2, -5), geom.Point2I(8, 7), True))
284 box.include(geom.Point2I(-4, 2))
285 self.assertEqual(box, geom.Box2I(
286 geom.Point2I(-4, -5), geom.Point2I(8, 7), True))
287 box.include(geom.Point2I(0, -6))
288 self.assertEqual(box, geom.Box2I(
289 geom.Point2I(-4, -6), geom.Point2I(8, 7), True))
290 box.include(geom.Box2I(geom.Point2I(0, 0), geom.Point2I(10, 11), True))
291 self.assertEqual(box, geom.Box2I(
292 geom.Point2I(-4, -6), geom.Point2I(10, 11), True))
293 box.clip(geom.Box2I(geom.Point2I(0, 0), geom.Point2I(11, 12), True))
294 self.assertEqual(box, geom.Box2I(
295 geom.Point2I(0, 0), geom.Point2I(10, 11), True))
296 box.clip(geom.Box2I(geom.Point2I(-1, -2), geom.Point2I(5, 4), True))
297 self.assertEqual(box, geom.Box2I(
298 geom.Point2I(0, 0), geom.Point2I(5, 4), True))
301class Box2DTestCase(lsst.utils.tests.TestCase):
303 def setUp(self):
304 np.random.seed(1)
306 def testEmpty(self):
307 box = geom.Box2D()
308 self.assertTrue(box.isEmpty())
309 self.assertEqual(box.getWidth(), 0.0)
310 self.assertEqual(box.getHeight(), 0.0)
311 for x in (-1, 0, 1):
312 for y in (-1, 0, 1):
313 point = geom.Point2D(x, y)
314 self.assertFalse(box.contains(point))
315 box.include(point)
316 self.assertTrue(box.contains(point))
317 box = geom.Box2D()
318 box.grow(3)
319 self.assertTrue(box.isEmpty())
321 def testConstruction(self):
322 for n in range(10):
323 xmin, xmax, ymin, ymax = np.random.uniform(low=-5, high=5, size=4)
324 if xmin > xmax:
325 xmin, xmax = xmax, xmin
326 if ymin > ymax:
327 ymin, ymax = ymax, ymin
328 pmin = geom.Point2D(xmin, ymin)
329 pmax = geom.Point2D(xmax, ymax)
330 # min/max constructor
331 box = geom.Box2D(pmin, pmax)
332 self.assertEqual(box.getMin(), pmin)
333 self.assertEqual(box.getMax(), pmax)
334 box = geom.Box2D(pmax, pmin)
335 self.assertEqual(box.getMin(), pmin)
336 self.assertEqual(box.getMax(), pmax)
337 box = geom.Box2D(pmin, pmax, False)
338 self.assertEqual(box.getMin(), pmin)
339 self.assertEqual(box.getMax(), pmax)
340 box = geom.Box2D(pmax, pmin, False)
341 self.assertTrue(box.isEmpty())
342 self.assertEqual(box, geom.Box2D(box))
343 # min/dim constructor
344 dim = pmax - pmin
345 if any(dim.eq(0)):
346 box = geom.Box2D(pmin, dim)
347 self.assertTrue(box.isEmpty())
348 box = geom.Box2D(pmin, dim, False)
349 self.assertTrue(box.isEmpty())
350 else:
351 box = geom.Box2D(pmin, dim)
352 self.assertEqual(box.getMin(), pmin)
353 self.assertEqual(box.getDimensions(), dim)
354 box = geom.Box2D(pmin, dim, False)
355 self.assertEqual(box.getMin(), pmin)
356 self.assertEqual(box.getDimensions(), dim)
357 dim = -dim
358 box = geom.Box2D(pmin, dim)
359 self.assertEqual(box.getMin(), pmin + dim)
360 self.assertFloatsAlmostEqual(box.getDimensions(),
361 geom.Extent2D(abs(dim.getX()), abs(dim.getY())))
363 def testSwap(self):
364 x00, y00, x01, y01 = (0., 1., 2., 3.)
365 x10, y10, x11, y11 = (4., 5., 6., 7.)
367 box0 = geom.Box2D(geom.PointD(x00, y00), geom.PointD(x01, y01))
368 box1 = geom.Box2D(geom.PointD(x10, y10), geom.PointD(x11, y11))
369 box0.swap(box1)
371 self.assertEqual(box0.getMinX(), x10)
372 self.assertEqual(box0.getMinY(), y10)
373 self.assertEqual(box0.getMaxX(), x11)
374 self.assertEqual(box0.getMaxY(), y11)
376 self.assertEqual(box1.getMinX(), x00)
377 self.assertEqual(box1.getMinY(), y00)
378 self.assertEqual(box1.getMaxX(), x01)
379 self.assertEqual(box1.getMaxY(), y01)
381 def testAccessors(self):
382 xmin, xmax, ymin, ymax = np.random.uniform(low=-5, high=5, size=4)
383 if xmin > xmax:
384 xmin, xmax = xmax, xmin
385 if ymin > ymax:
386 ymin, ymax = ymax, ymin
387 pmin = geom.Point2D(xmin, ymin)
388 pmax = geom.Point2D(xmax, ymax)
389 box = geom.Box2D(pmin, pmax, True)
390 self.assertEqual(pmin, box.getMin())
391 self.assertEqual(pmax, box.getMax())
392 self.assertEqual(box.getMinX(), xmin)
393 self.assertEqual(box.getMinY(), ymin)
394 self.assertEqual(box.getMaxX(), xmax)
395 self.assertEqual(box.getMaxY(), ymax)
396 self.assertEqual(box.getDimensions(), (pmax - pmin))
397 self.assertEqual(box.getWidth(), (xmax - xmin))
398 self.assertEqual(box.getHeight(), (ymax - ymin))
399 self.assertEqual(box.getArea(), box.getWidth() * box.getHeight())
400 self.assertEqual(box.getCenterX(), 0.5*(pmax.getX() + pmin.getX()))
401 self.assertEqual(box.getCenterY(), 0.5*(pmax.getY() + pmin.getY()))
402 self.assertEqual(box.getCenter().getX(), box.getCenterX())
403 self.assertEqual(box.getCenter().getY(), box.getCenterY())
404 corners = box.getCorners()
405 self.assertEqual(corners[0], box.getMin())
406 self.assertEqual(corners[1].getX(), box.getMaxX())
407 self.assertEqual(corners[1].getY(), box.getMinY())
408 self.assertEqual(corners[2], box.getMax())
409 self.assertEqual(corners[3].getX(), box.getMinX())
410 self.assertEqual(corners[3].getY(), box.getMaxY())
412 def testRelations(self):
413 box = geom.Box2D(geom.Point2D(-2, -3), geom.Point2D(2, 1), True)
414 inPoints = [
415 geom.Point2D(0, 0),
416 geom.Point2D(-2, -3),
417 ]
418 outPoints = [
419 geom.Point2D(2, -3),
420 geom.Point2D(2, 1),
421 geom.Point2D(-2, 1),
422 ]
423 for point in inPoints:
424 with self.subTest(point=point):
425 self.assertTrue(box.contains(point))
426 for point in outPoints:
427 with self.subTest(point=point):
428 self.assertFalse(box.contains(point))
429 inX, inY = zip(*inPoints)
430 outX, outY = zip(*outPoints)
431 self.assertTrue(box.contains(geom.Box2D(
432 geom.Point2D(-1, -2), geom.Point2D(1, 0))))
433 self.assertTrue(box.contains(box))
434 self.assertFalse(box.contains(geom.Box2D(
435 geom.Point2D(-2, -3), geom.Point2D(2, 2))))
436 self.assertFalse(box.contains(geom.Box2D(
437 geom.Point2D(-2, -3), geom.Point2D(3, 1))))
438 self.assertFalse(box.contains(geom.Box2D(
439 geom.Point2D(-3, -3), geom.Point2D(2, 1))))
440 self.assertFalse(box.contains(geom.Box2D(
441 geom.Point2D(-3, -4), geom.Point2D(2, 1))))
442 self.assertTrue(box.overlaps(geom.Box2D(
443 geom.Point2D(-2, -3), geom.Point2D(2, 2))))
444 self.assertTrue(box.overlaps(geom.Box2D(
445 geom.Point2D(-2, -3), geom.Point2D(3, 1))))
446 self.assertTrue(box.overlaps(geom.Box2D(
447 geom.Point2D(-3, -3), geom.Point2D(2, 1))))
448 self.assertTrue(box.overlaps(geom.Box2D(
449 geom.Point2D(-3, -4), geom.Point2D(2, 1))))
450 self.assertTrue(box.overlaps(geom.Box2D(
451 geom.Point2D(-1, -2), geom.Point2D(1, 0))))
452 self.assertTrue(box.overlaps(box))
453 self.assertFalse(box.overlaps(geom.Box2D(
454 geom.Point2D(-5, -3), geom.Point2D(-3, 1))))
455 self.assertFalse(box.overlaps(geom.Box2D(
456 geom.Point2D(-2, -6), geom.Point2D(2, -4))))
457 self.assertFalse(box.overlaps(geom.Box2D(
458 geom.Point2D(3, -3), geom.Point2D(4, 1))))
459 self.assertFalse(box.overlaps(geom.Box2D(
460 geom.Point2D(-2, 2), geom.Point2D(2, 2))))
461 self.assertFalse(box.overlaps(geom.Box2D(
462 geom.Point2D(-2, -5), geom.Point2D(2, -3))))
463 self.assertFalse(box.overlaps(geom.Box2D(
464 geom.Point2D(-4, -3), geom.Point2D(-2, 1))))
465 self.assertFalse(box.contains(geom.Box2D(
466 geom.Point2D(-2, 1), geom.Point2D(2, 3))))
467 self.assertFalse(box.contains(geom.Box2D(
468 geom.Point2D(2, -3), geom.Point2D(4, 1))))
470 def testMutators(self):
471 box = geom.Box2D(geom.Point2D(-2, -3), geom.Point2D(2, 1), True)
472 box.grow(1)
473 self.assertEqual(box, geom.Box2D(
474 geom.Point2D(-3, -4), geom.Point2D(3, 2), True))
475 box.grow(geom.Extent2D(2, 3))
476 self.assertEqual(box, geom.Box2D(
477 geom.Point2D(-5, -7), geom.Point2D(5, 5), True))
478 box.shift(geom.Extent2D(3, 2))
479 self.assertEqual(box, geom.Box2D(
480 geom.Point2D(-2, -5), geom.Point2D(8, 7), True))
481 box.include(geom.Point2D(-4, 2))
482 self.assertEqual(box, geom.Box2D(
483 geom.Point2D(-4, -5), geom.Point2D(8, 7), True))
484 self.assertTrue(box.contains(geom.Point2D(-4, 2)))
485 box.include(geom.Point2D(0, -6))
486 self.assertEqual(box, geom.Box2D(
487 geom.Point2D(-4, -6), geom.Point2D(8, 7), True))
488 box.include(geom.Box2D(geom.Point2D(0, 0), geom.Point2D(10, 11), True))
489 self.assertEqual(box, geom.Box2D(
490 geom.Point2D(-4, -6), geom.Point2D(10, 11), True))
491 box.clip(geom.Box2D(geom.Point2D(0, 0), geom.Point2D(11, 12), True))
492 self.assertEqual(box, geom.Box2D(
493 geom.Point2D(0, 0), geom.Point2D(10, 11), True))
494 box.clip(geom.Box2D(geom.Point2D(-1, -2), geom.Point2D(5, 4), True))
495 self.assertEqual(box, geom.Box2D(
496 geom.Point2D(0, 0), geom.Point2D(5, 4), True))
498 def testFlipI(self):
499 parentExtent = geom.Extent2I(15, 20)
500 x00, y00, x11, y11 = (8, 11, 13, 16)
501 lrx00, lry00, lrx11, lry11 = (1, 11, 6, 16)
502 tbx00, tby00, tbx11, tby11 = (8, 3, 13, 8)
504 box0 = geom.Box2I(geom.Point2I(x00, y00),
505 geom.Point2I(x11, y11))
506 box1 = geom.Box2I(geom.Point2I(x00, y00),
507 geom.Point2I(x11, y11))
508 box0.flipLR(parentExtent[0])
509 box1.flipTB(parentExtent[1])
511 # test flip RL
512 self.assertEqual(box0.getMinX(), lrx00)
513 self.assertEqual(box0.getMinY(), lry00)
514 self.assertEqual(box0.getMaxX(), lrx11)
515 self.assertEqual(box0.getMaxY(), lry11)
517 # test flip TB
518 self.assertEqual(box1.getMinX(), tbx00)
519 self.assertEqual(box1.getMinY(), tby00)
520 self.assertEqual(box1.getMaxX(), tbx11)
521 self.assertEqual(box1.getMaxY(), tby11)
523 def testFlipD(self):
524 parentExtent = geom.Extent2D(15.1, 20.6)
525 x00, y00, x11, y11 = (8.3, 11.4, 13.2, 16.9)
526 lrx00, lry00, lrx11, lry11 = (1.9, 11.4, 6.8, 16.9)
527 tbx00, tby00, tbx11, tby11 = (8.3, 3.7, 13.2, 9.2)
529 box0 = geom.Box2D(geom.Point2D(x00, y00),
530 geom.Point2D(x11, y11))
531 box1 = geom.Box2D(geom.Point2D(x00, y00),
532 geom.Point2D(x11, y11))
533 box0.flipLR(parentExtent[0])
534 box1.flipTB(parentExtent[1])
536 # test flip RL
537 self.assertAlmostEqual(box0.getMinX(), lrx00, places=6)
538 self.assertAlmostEqual(box0.getMinY(), lry00, places=6)
539 self.assertAlmostEqual(box0.getMaxX(), lrx11, places=6)
540 self.assertAlmostEqual(box0.getMaxY(), lry11, places=6)
542 # test flip TB
543 self.assertAlmostEqual(box1.getMinX(), tbx00, places=6)
544 self.assertAlmostEqual(box1.getMinY(), tby00, places=6)
545 self.assertAlmostEqual(box1.getMaxX(), tbx11, places=6)
546 self.assertAlmostEqual(box1.getMaxY(), tby11, places=6)
549class SharedBoxTestCase(lsst.utils.tests.TestCase):
550 """Tests of Box2I and Box2D where the code for both classes is the same,
551 and only the test fixtures need be different.
552 """
553 def setUp(self):
554 np.random.seed(1)
556 def testMakeCenteredBox(self):
557 dimensionsI = [geom.Extent2I(100, 50), geom.Extent2I(15, 15),
558 geom.Extent2I(0, 10), geom.Extent2I(25, 30),
559 geom.Extent2I(15, -5)]
560 dimensionsD = [geom.Extent2D(d) for d in dimensionsI] \
561 + [geom.Extent2D(1.5, 2.1), geom.Extent2D(4, 3.7),
562 geom.Extent2D(-0.1, -0.1), geom.Extent2D(5.5, 5.5),
563 geom.Extent2D(-np.nan, 5.5), geom.Extent2D(4, np.inf)]
564 locations = [geom.Point2D(0, 0), geom.Point2D(0.2, 0.7),
565 geom.Point2D(1, 1.5),
566 geom.Point2D(-0.5 + 1e-4, -0.5 + 1e-4),
567 geom.Point2D(-0.5 - 1e-4, -0.5 - 1e-4),
568 geom.Point2D(-np.nan, 0), geom.Point2D(1.0, np.inf),
569 ]
571 for center in locations:
572 for size in dimensionsI:
573 self._checkBoxConstruction(geom.Box2I, size, center, np.sqrt(0.5))
574 for size in dimensionsD:
575 self._checkBoxConstruction(geom.Box2D, size, center, 1e-10)
577 def _checkBoxConstruction(self, boxClass, size, center, precision):
578 """Test attempts to create a centered box of a particular type
579 and parameters.
581 Parameters
582 ----------
583 boxClass : `type`
584 One of `lsst.geom.Box2I` or `lsst.geom.Box2D`.
585 size : ``boxClass.Extent``
586 The desired dimensions of the box.
587 center : `lsst.geom.Point2D`
588 The desired center of the box.
589 precision : `float`
590 The maximum distance by which the box can be offset from ``center``.
591 """
592 msg = 'Box size = %s, location = %s' % (size, center)
594 if all(np.isfinite(center)):
595 box = boxClass.makeCenteredBox(center, size)
596 if all(size.gt(0)):
597 self.assertIsNotNone(box, msg=msg)
598 self.assertPairsAlmostEqual(box.getCenter(), center, maxDiff=precision, msg=msg)
599 self.assertPairsAlmostEqual(box.getDimensions(), size, msg=msg)
600 else:
601 self.assertTrue(box.isEmpty(), msg=msg)
602 elif boxClass == geom.Box2I:
603 with self.assertRaises(lsst.pex.exceptions.InvalidParameterError, msg=msg):
604 boxClass.makeCenteredBox(center, size)
607class MemoryTester(lsst.utils.tests.MemoryTestCase):
608 pass
611def setup_module(module):
612 lsst.utils.tests.init()
615if __name__ == "__main__": 615 ↛ 616line 615 didn't jump to line 616, because the condition on line 615 was never true
616 lsst.utils.tests.init()
617 unittest.main()