lsst.meas.astrom  15.0-1-g1eca518+1
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/geom/SkyWcs.h"
30 
31 namespace lsst { namespace meas { namespace astrom {
32 
34  // The implementation for transformPixels is identical for
35  // SipForwardTransform and SipReverseTransform. That's pretty obvious for
36  // the pixel origin and CD matrix, which are the same in both cases, but
37  // it wasn't obvious to me until I did the math that the polynomial
38  // transforms are composed with the affine transform the same way.
39  auto sInv = s.invert();
40  _pixelOrigin = s.getLinear()(_pixelOrigin - sInv.getTranslation());
41  _cdMatrix = _cdMatrix * sInv.getLinear();
42  _poly = compose(s.getLinear(), compose(getPoly(), sInv.getLinear()));
43 }
44 
46  PolynomialTransform const & poly,
47  afw::geom::Point2D const & pixelOrigin,
48  afw::geom::LinearTransform const & cdMatrix
49 ) {
50  auto forwardSipPoly = compose(
52  compose(
53  poly,
55  )
56  );
57  // Subtracting 1 here accounts for the extra terms outside the sum in the
58  // transform definition (see class docs) - note that you can fold those
59  // terms into the sum by adding 1 from the A_10 and B_01 terms.
60  forwardSipPoly._xCoeffs(1, 0) -= 1;
61  forwardSipPoly._yCoeffs(0, 1) -= 1;
62  return SipForwardTransform(pixelOrigin, cdMatrix, forwardSipPoly);
63 }
64 
66  ScaledPolynomialTransform const & scaled,
67  afw::geom::Point2D const & pixelOrigin,
68  afw::geom::LinearTransform const & cdMatrix
69 ) {
70  auto forwardSipPoly = compose(
72  compose(
73  scaled.getPoly(),
75  )
76  );
77  // Account for the terms outside the sum in the definition (see comment
78  // earlier in the file for more explanation).
79  forwardSipPoly._xCoeffs(1, 0) -= 1;
80  forwardSipPoly._yCoeffs(0, 1) -= 1;
81  return SipForwardTransform(pixelOrigin, cdMatrix, forwardSipPoly);
82 }
83 
87  return convert(scaled, pixelOrigin, cdMatrix);
88 }
89 
94  * tail;
95 }
96 
99  return getCdMatrix()(afw::geom::Extent2D(duv) + getPoly()(duv));
100 }
101 
104  result.transformPixelsInPlace(s);
105  return result;
106 }
107 
109  PolynomialTransform const & poly,
110  afw::geom::Point2D const & pixelOrigin,
111  afw::geom::LinearTransform const & cdMatrix
112 ) {
113  auto reverseSipPoly = compose(
115  compose(
116  poly,
118  )
119  );
120  // Account for the terms outside the sum in the definition (see comment
121  // earlier in the file for more explanation).
122  reverseSipPoly._xCoeffs(1, 0) -= 1;
123  reverseSipPoly._yCoeffs(0, 1) -= 1;
124  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
125 }
126 
128  ScaledPolynomialTransform const & scaled,
129  afw::geom::Point2D const & pixelOrigin,
130  afw::geom::LinearTransform const & cdMatrix
131 ) {
132  auto reverseSipPoly = compose(
134  *scaled.getOutputScalingInverse(),
135  compose(
136  scaled.getPoly(),
138  )
139  );
140  // Account for the terms outside the sum in the definition (see comment
141  // earlier in the file for more explanation).
142  reverseSipPoly._xCoeffs(1, 0) -= 1;
143  reverseSipPoly._yCoeffs(0, 1) -= 1;
144  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
145 }
146 
148  return convert(
149  scaled,
151  scaled.getInputScaling().getLinear()
152  );
153 }
154 
157  result.transformPixelsInPlace(s);
158  result._cdInverse = result._cdMatrix.invert();
159  return result;
160 }
161 
164  * (afw::geom::AffineTransform() + _poly.linearize(_cdInverse(in)))
165  * _cdInverse;
166 }
167 
169  afw::geom::Point2D UV = _cdInverse(xy);
171 }
172 
173 
175  SipForwardTransform const & sipForward,
176  SipReverseTransform const & sipReverse,
177  afw::geom::SpherePoint const & skyOrigin
178 ) {
179  if (!sipForward.getPixelOrigin().asEigen().isApprox(sipReverse.getPixelOrigin().asEigen())) {
180  std::ostringstream oss;
181  oss << "SIP forward and reverse transforms have inconsistent CRPIX: "
182  << sipForward.getPixelOrigin() << " != " << sipReverse.getPixelOrigin();
183  throw LSST_EXCEPT(
185  oss.str()
186  );
187  }
188  if (!sipForward.getCdMatrix().getMatrix().isApprox(sipReverse.getCdMatrix().getMatrix())) {
189  std::ostringstream oss;
190  oss << "SIP forward and reverse transforms have inconsistent CD matrix: "
191  << sipForward.getCdMatrix() << "\n!=\n" << sipReverse.getCdMatrix();
192  throw LSST_EXCEPT(
194  oss.str()
195  );
196  }
197  Eigen::MatrixXd sipA(sipForward.getPoly().getXCoeffs().asEigen());
198  Eigen::MatrixXd sipB(sipForward.getPoly().getYCoeffs().asEigen());
199  Eigen::MatrixXd sipAP(sipReverse.getPoly().getXCoeffs().asEigen());
200  Eigen::MatrixXd sipBP(sipReverse.getPoly().getYCoeffs().asEigen());
201 
202  return makeTanSipWcs(sipForward.getPixelOrigin(), skyOrigin, sipForward.getCdMatrix().getMatrix(), sipA,
203  sipB, sipAP, sipBP);
204 }
205 
207  afw::geom::SkyWcs const & wcs,
209 ) {
210  auto affineTransform22 = afw::geom::makeTransform(s);
211  return afw::geom::makeModifiedWcs(*affineTransform22->getInverse(), wcs, true);
212 }
213 
215  afw::geom::SkyWcs const & wcs,
216  int nQuarter,
217  afw::geom::Extent2I const & dimensions
218 ) {
219  afw::geom::Extent2D offset;
220  switch(nQuarter % 4) {
221  case 0:
222  offset = afw::geom::Extent2D(0, 0);
223  break;
224  case 1:
225  offset = afw::geom::Extent2D(dimensions.getY() - 1, 0);
226  break;
227  case 2:
228  offset = afw::geom::Extent2D(dimensions - afw::geom::Extent2I(1, 1));
229  break;
230  case 3:
231  offset = afw::geom::Extent2D(0, dimensions.getX() - 1);
232  break;
233  }
235  return transformWcsPixels(
236  wcs,
237  afw::geom::AffineTransform(rot, offset)
238  );
239 }
240 
241 }}} // 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:61
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:45
Matrix const & getMatrix() const
Extent< double, 2 > Extent2D
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:51
std::shared_ptr< SkyWcs > makeModifiedWcs(TransformPoint2ToPoint2 const &pixelTransform, SkyWcs const &wcs, bool modifyActualPixels)
afw::geom::LinearTransform const & getCdMatrix() const
Return the CD matrix of the transform.
Definition: SipTransform.h:56
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:97
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...
std::shared_ptr< afw::geom::SkyWcs > makeWcs(SipForwardTransform const &sipForward, SipReverseTransform const &sipReverse, afw::geom::SpherePoint const &skyOrigin)
Create a new TAN SIP Wcs from a pair of SIP transforms and the sky origin.
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:143
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:90
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::unique_ptr< SchemaItem< U > > result
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...
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(AffineTransform const &affine)
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:100
#define LSST_EXCEPT(type,...)
A transform that maps intermediate world coordinates to pixel coordinates according to the SIP conven...
Definition: SipTransform.h:267
static SipReverseTransform convert(PolynomialTransform const &poly, afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix)
Convert a PolynomialTransform to an equivalent SipReverseTransform.
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:33
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...
std::shared_ptr< SkyWcs > makeTanSipWcs(Point2D const &crpix, SpherePoint const &crval, Eigen::Matrix2d const &cdMatrix, Eigen::MatrixXd const &sipA, Eigen::MatrixXd const &sipB)
Extent2D const & getTranslation() const
A 2-d coordinate transform represented by a pair of standard polynomials (one for each coordinate)...