LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
rotateBBoxBy90.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division
2 #
3 # LSST Data Management System
4 # Copyright 2014 LSST Corporation.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 import numpy
24 import lsst.afw.geom as afwGeom
25 
26 __all__ = ["rotateBBoxBy90"]
27 
28 def rotateBBoxBy90(bbox, n90, dimensions):
29  """ This is a direct translation of the method by the same name in the previous camera geom
30  @param bbox -- bbox to rotate
31  @param n90 -- number of quarter rotations to perform
32  @param dimensions -- The dimensions of the parent grid. If None, rotate about LLC
33  @return rotated bounding box
34  """
35  while n90 < 0:
36  n90 += 4
37  n90 %= 4
38 
39  # sin/cos of the rotation angle
40  s = 0
41  c = 0
42  if n90 == 0:
43  s = 0
44  c = 1
45  elif n90 == 1:
46  s = 1
47  c = 0
48  elif n90 == 2:
49  s = 0
50  c = -1
51  elif n90 == 3:
52  s = -1
53  c = 0
54  else:
55  raise ValueError("n90 must be an integer")
56 
57  centerPixel = afwGeom.Point2I(int(dimensions[0]/2), int(dimensions[1]/2))
58 
59  xCorner = numpy.array([(corner.getX() - centerPixel[0]) for corner in bbox.getCorners()])
60  yCorner = numpy.array([(corner.getY() - centerPixel[1]) for corner in bbox.getCorners()])
61  x0 = int((c*xCorner - s*yCorner).min())
62  y0 = int((s*xCorner + c*yCorner).min())
63  x1 = int((c*xCorner - s*yCorner).max())
64  y1 = int((s*xCorner + c*yCorner).max())
65 
66  # Fiddle things a little if the detector has an even number of pixels so that square BBoxes
67  # will map into themselves
68 
69  if n90 == 1 :
70  if dimensions[0]%2 == 0:
71  x0 -= 1
72  x1 -= 1
73  elif n90 == 2:
74  if dimensions[0]%2 == 0:
75  x0 -= 1
76  x1 -= 1
77  if dimensions[1]%2 == 0:
78  y0 -= 1
79  y1 -= 1
80  elif n90 == 3:
81  if dimensions[1]%2 == 0:
82  y0 -= 1
83  y1 -= 1
84 
85  LLC = afwGeom.Point2I(centerPixel[0] + x0, centerPixel[1] + y0)
86  URC = afwGeom.Point2I(centerPixel[0] + x1, centerPixel[1] + y1)
87 
88  newBbox = afwGeom.Box2I(LLC, URC)
89 
90  dxy0 = centerPixel[0] - centerPixel[1]
91  if n90%2 == 1 and not dxy0 == 0:
92  newBbox.shift(afwGeom.Extent2I(-dxy0, dxy0))
93 
94  return newBbox;
An integer coordinate rectangle.
Definition: Box.h:53
double min
Definition: attributes.cc:216
double max
Definition: attributes.cc:218