lsst.meas.astrom  14.0-7-g0d69b06+3
SipTransform.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2016 LSST/AURA
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #include <sstream>
26 
27 #include "lsst/afw/coord/Coord.h"
28 #include "lsst/afw/geom/SkyWcs.h"
31 
32 namespace lsst { namespace meas { namespace astrom {
33 
35  // The implementation for transformPixels is identical for
36  // SipForwardTransform and SipReverseTransform. That's pretty obvious for
37  // the pixel origin and CD matrix, which are the same in both cases, but
38  // it wasn't obvious to me until I did the math that the polynomial
39  // transforms are composed with the affine transform the same way.
40  auto sInv = s.invert();
41  _pixelOrigin = s.getLinear()(_pixelOrigin - sInv.getTranslation());
42  _cdMatrix = _cdMatrix * sInv.getLinear();
43  _poly = compose(s.getLinear(), compose(getPoly(), sInv.getLinear()));
44 }
45 
47  PolynomialTransform const & poly,
48  afw::geom::Point2D const & pixelOrigin,
49  afw::geom::LinearTransform const & cdMatrix
50 ) {
51  auto forwardSipPoly = compose(
53  compose(
54  poly,
56  )
57  );
58  // Subtracting 1 here accounts for the extra terms outside the sum in the
59  // transform definition (see class docs) - note that you can fold those
60  // terms into the sum by adding 1 from the A_10 and B_01 terms.
61  forwardSipPoly._xCoeffs(1, 0) -= 1;
62  forwardSipPoly._yCoeffs(0, 1) -= 1;
63  return SipForwardTransform(pixelOrigin, cdMatrix, forwardSipPoly);
64 }
65 
67  ScaledPolynomialTransform const & scaled,
68  afw::geom::Point2D const & pixelOrigin,
69  afw::geom::LinearTransform const & cdMatrix
70 ) {
71  auto forwardSipPoly = compose(
73  compose(
74  scaled.getPoly(),
76  )
77  );
78  // Account for the terms outside the sum in the definition (see comment
79  // earlier in the file for more explanation).
80  forwardSipPoly._xCoeffs(1, 0) -= 1;
81  forwardSipPoly._yCoeffs(0, 1) -= 1;
82  return SipForwardTransform(pixelOrigin, cdMatrix, forwardSipPoly);
83 }
84 
88  return convert(scaled, pixelOrigin, cdMatrix);
89 }
90 
95  * tail;
96 }
97 
100  return getCdMatrix()(afw::geom::Extent2D(duv) + getPoly()(duv));
101 }
102 
104  SipForwardTransform result(*this);
105  result.transformPixelsInPlace(s);
106  return result;
107 }
108 
110  PolynomialTransform const & poly,
111  afw::geom::Point2D const & pixelOrigin,
112  afw::geom::LinearTransform const & cdMatrix
113 ) {
114  auto reverseSipPoly = compose(
116  compose(
117  poly,
119  )
120  );
121  // Account for the terms outside the sum in the definition (see comment
122  // earlier in the file for more explanation).
123  reverseSipPoly._xCoeffs(1, 0) -= 1;
124  reverseSipPoly._yCoeffs(0, 1) -= 1;
125  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
126 }
127 
129  ScaledPolynomialTransform const & scaled,
130  afw::geom::Point2D const & pixelOrigin,
131  afw::geom::LinearTransform const & cdMatrix
132 ) {
133  auto reverseSipPoly = compose(
135  *scaled.getOutputScalingInverse(),
136  compose(
137  scaled.getPoly(),
139  )
140  );
141  // Account for the terms outside the sum in the definition (see comment
142  // earlier in the file for more explanation).
143  reverseSipPoly._xCoeffs(1, 0) -= 1;
144  reverseSipPoly._yCoeffs(0, 1) -= 1;
145  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
146 }
147 
149  return convert(
150  scaled,
152  scaled.getInputScaling().getLinear()
153  );
154 }
155 
157  SipReverseTransform result(*this);
158  result.transformPixelsInPlace(s);
159  result._cdInverse = result._cdMatrix.invert();
160  return result;
161 }
162 
165  * (afw::geom::AffineTransform() + _poly.linearize(_cdInverse(in)))
166  * _cdInverse;
167 }
168 
170  afw::geom::Point2D UV = _cdInverse(xy);
172 }
173 
174 
176  SipForwardTransform const & sipForward,
177  SipReverseTransform const & sipReverse,
178  afw::coord::IcrsCoord const & skyOrigin
179 ) {
180  if (!sipForward.getPixelOrigin().asEigen().isApprox(sipReverse.getPixelOrigin().asEigen())) {
181  std::ostringstream oss;
182  oss << "SIP forward and reverse transforms have inconsistent CRPIX: "
183  << sipForward.getPixelOrigin() << " != " << sipReverse.getPixelOrigin();
184  throw LSST_EXCEPT(
186  oss.str()
187  );
188  }
189  if (!sipForward.getCdMatrix().getMatrix().isApprox(sipReverse.getCdMatrix().getMatrix())) {
190  std::ostringstream oss;
191  oss << "SIP forward and reverse transforms have inconsistent CD matrix: "
192  << sipForward.getCdMatrix() << "\n!=\n" << sipReverse.getCdMatrix();
193  throw LSST_EXCEPT(
195  oss.str()
196  );
197  }
198  Eigen::MatrixXd sipA(sipForward.getPoly().getXCoeffs().asEigen());
199  Eigen::MatrixXd sipB(sipForward.getPoly().getYCoeffs().asEigen());
200  Eigen::MatrixXd sipAP(sipReverse.getPoly().getXCoeffs().asEigen());
201  Eigen::MatrixXd sipBP(sipReverse.getPoly().getYCoeffs().asEigen());
202 
203  return makeTanSipWcs(sipForward.getPixelOrigin(), skyOrigin, sipForward.getCdMatrix().getMatrix(), sipA,
204  sipB, sipAP, sipBP);
205 }
206 
208  afw::geom::SkyWcs const & wcs,
210 ) {
211  auto affineTransform22 = afw::geom::makeTransform(s);
212  return afw::geom::makeModifiedWcs(*affineTransform22->getInverse(), wcs, true);
213 }
214 
216  afw::geom::SkyWcs const & wcs,
217  int nQuarter,
218  afw::geom::Extent2I const & dimensions
219 ) {
220  afw::geom::Extent2D offset;
221  switch(nQuarter % 4) {
222  case 0:
223  offset = afw::geom::Extent2D(0, 0);
224  break;
225  case 1:
226  offset = afw::geom::Extent2D(dimensions.getY() - 1, 0);
227  break;
228  case 2:
229  offset = afw::geom::Extent2D(dimensions - afw::geom::Extent2I(1, 1));
230  break;
231  case 3:
232  offset = afw::geom::Extent2D(0, dimensions.getX() - 1);
233  break;
234  }
236  return transformWcsPixels(
237  wcs,
238  afw::geom::AffineTransform(rot, offset)
239  );
240 }
241 
242 }}} // namespace lsst::meas::astrom
SipReverseTransform transformPixels(afw::geom::AffineTransform const &s) const
Return a new reverse SIP transform that includes a transformation of the pixel coordinate system by t...
afw::geom::Point2D operator()(afw::geom::Point2D const &xy) const
Apply the transform to a point.
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
PolynomialTransform const & getPoly() const
Return the polynomial component of the transform (A,B) or (AP,BP).
Definition: SipTransform.h:68
ndarray::Array< double const, 2, 2 > getXCoeffs() const
2-D polynomial coefficients that compute the output x coordinate.
static SipForwardTransform convert(PolynomialTransform const &poly, afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix)
Convert a PolynomialTransform to an equivalent SipForwardTransform.
Definition: SipTransform.cc:46
std::shared_ptr< afw::geom::SkyWcs > makeWcs(SipForwardTransform const &sipForward, SipReverseTransform const &sipReverse, afw::coord::IcrsCoord const &skyOrigin)
Create a new TAN SIP Wcs from a pair of SIP transforms and the sky origin.
Matrix const & getMatrix() const
Extent< double, 2 > Extent2D
std::shared_ptr< SkyWcs > makeTanSipWcs(Point2D const &crpix, coord::IcrsCoord const &crval, Eigen::Matrix2d const &cdMatrix, Eigen::MatrixXd const &sipA, Eigen::MatrixXd const &sipB)
LinearTransform const & getLinear() const
PolynomialTransform compose(afw::geom::AffineTransform const &t1, PolynomialTransform const &t2)
Return a PolynomialTransform that is equivalent to the composition t1(t2())
AngleUnit constexpr degrees
afw::geom::Point2D const & getPixelOrigin() const
Return the pixel origin (CRPIX, but zero-indexed) of the transform.
Definition: SipTransform.h:58
afw::geom::LinearTransform const & getCdMatrix() const
Return the CD matrix of the transform.
Definition: SipTransform.h:63
LinearTransform const invert() const
PolynomialTransform const & getPoly() const
Return the polynomial transform applied after the input scaling.
afw::geom::Point2D operator()(afw::geom::Point2D const &uv) const
Apply the transform to a point.
Definition: SipTransform.cc:98
SipForwardTransform transformPixels(afw::geom::AffineTransform const &s) const
Return a new forward SIP transform that includes a transformation of the pixel coordinate system by t...
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
A transform that maps pixel coordinates to intermediate world coordinates according to the SIP conven...
Definition: SipTransform.h:150
EigenVector const & asEigen() const
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
Definition: SipTransform.cc:91
static LinearTransform makeRotation(Angle t)
T str(T... args)
afw::geom::AffineTransform const & getInputScaling() const
Return the first affine transform applied to input points.
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(AffineTransform const &affine)
std::shared_ptr< afw::geom::SkyWcs > rotateWcsPixelsBy90(afw::geom::SkyWcs const &wcs, int nQuarter, afw::geom::Extent2I const &dimensions)
Return a new SkyWcs that represents a rotation of the image it corresponds to about the image&#39;s cente...
A 2-d coordinate transform represented by a lazy composition of an AffineTransform, a PolynomialTransform, and another AffineTransform.
afw::geom::LinearTransform _cdMatrix
Definition: SipTransform.h:107
#define LSST_EXCEPT(type,...)
A transform that maps intermediate world coordinates to pixel coordinates according to the SIP conven...
Definition: SipTransform.h:274
static SipReverseTransform convert(PolynomialTransform const &poly, afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix)
Convert a PolynomialTransform to an equivalent SipReverseTransform.
std::shared_ptr< SkyWcs > makeModifiedWcs(TransformPoint2ToPoint2 const &pixelTransform, SkyWcs const &wcs, bool modifyActualPixels)
afw::geom::AffineTransform const & getOutputScalingInverse() const
Return the affine transform applied to points after the polynomial transform.
void transformPixelsInPlace(afw::geom::AffineTransform const &s)
Definition: SipTransform.cc:34
ndarray::Array< double const, 2, 2 > getYCoeffs() const
2-D polynomial coefficients that compute the output x coordinate.
AffineTransform const invert() const
std::shared_ptr< afw::geom::SkyWcs > transformWcsPixels(afw::geom::SkyWcs const &wcs, afw::geom::AffineTransform const &s)
Create a new SkyWcs whose pixel coordinate system has been transformed via an affine transform...
Extent2D const & getTranslation() const
A 2-d coordinate transform represented by a pair of standard polynomials (one for each coordinate)...