lsst.meas.astrom  14.0-4-g4cc409d+7
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/image/TanWcs.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 
91  if (!wcs.hasDistortion()) {
92  throw LSST_EXCEPT(
94  "Constructing a SipForwardTransform from a TanWcs with no distortions is not implemented."
95  );
96  }
97  ndarray::Array<double,2,2> xCoeffs = ndarray::allocate(wcs.getSipA().rows(), wcs.getSipA().cols());
98  ndarray::Array<double,2,2> yCoeffs = ndarray::allocate(wcs.getSipB().rows(), wcs.getSipB().cols());
99  xCoeffs.asEigen() = wcs.getSipA();
100  yCoeffs.asEigen() = wcs.getSipB();
101  return SipForwardTransform(
102  wcs.getPixelOrigin(),
104  PolynomialTransform(xCoeffs, yCoeffs)
105  );
106 }
107 
111  * (afw::geom::AffineTransform() + _poly.linearize(tail(in)))
112  * tail;
113 }
114 
117  return getCDMatrix()(afw::geom::Extent2D(duv) + getPoly()(duv));
118 }
119 
121  SipForwardTransform result(*this);
122  result.transformPixelsInPlace(s);
123  return result;
124 }
125 
127  PolynomialTransform const & poly,
128  afw::geom::Point2D const & pixelOrigin,
129  afw::geom::LinearTransform const & cdMatrix
130 ) {
131  auto reverseSipPoly = compose(
133  compose(
134  poly,
136  )
137  );
138  // Account for the terms outside the sum in the definition (see comment
139  // earlier in the file for more explanation).
140  reverseSipPoly._xCoeffs(1, 0) -= 1;
141  reverseSipPoly._yCoeffs(0, 1) -= 1;
142  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
143 }
144 
146  ScaledPolynomialTransform const & scaled,
147  afw::geom::Point2D const & pixelOrigin,
148  afw::geom::LinearTransform const & cdMatrix
149 ) {
150  auto reverseSipPoly = compose(
152  *scaled.getOutputScalingInverse(),
153  compose(
154  scaled.getPoly(),
156  )
157  );
158  // Account for the terms outside the sum in the definition (see comment
159  // earlier in the file for more explanation).
160  reverseSipPoly._xCoeffs(1, 0) -= 1;
161  reverseSipPoly._yCoeffs(0, 1) -= 1;
162  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
163 }
164 
166  return convert(
167  scaled,
169  scaled.getInputScaling().getLinear()
170  );
171 }
172 
174  if (!wcs.hasDistortion()) {
175  throw LSST_EXCEPT(
177  "Constructing a SipReverseTransform from a TanWcs with no distortions is not implemented."
178  );
179  }
180  ndarray::Array<double,2,2> xCoeffs = ndarray::allocate(wcs.getSipAp().rows(), wcs.getSipAp().cols());
181  ndarray::Array<double,2,2> yCoeffs = ndarray::allocate(wcs.getSipBp().rows(), wcs.getSipBp().cols());
182  xCoeffs.asEigen() = wcs.getSipAp();
183  yCoeffs.asEigen() = wcs.getSipBp();
184  return SipReverseTransform(
185  wcs.getPixelOrigin(),
187  PolynomialTransform(xCoeffs, yCoeffs)
188  );
189 }
190 
192  SipReverseTransform result(*this);
193  result.transformPixelsInPlace(s);
194  result._cdInverse = result._cdMatrix.invert();
195  return result;
196 }
197 
200  * (afw::geom::AffineTransform() + _poly.linearize(_cdInverse(in)))
201  * _cdInverse;
202 }
203 
205  afw::geom::Point2D UV = _cdInverse(xy);
207 }
208 
209 
211  SipForwardTransform const & sipForward,
212  SipReverseTransform const & sipReverse,
213  afw::coord::Coord const & skyOrigin
214 ) {
215  if (!sipForward.getPixelOrigin().asEigen().isApprox(sipReverse.getPixelOrigin().asEigen())) {
216  std::ostringstream oss;
217  oss << "SIP forward and reverse transforms have inconsistent CRPIX: "
218  << sipForward.getPixelOrigin() << " != " << sipReverse.getPixelOrigin();
219  throw LSST_EXCEPT(
221  oss.str()
222  );
223  }
224  if (!sipForward.getCDMatrix().getMatrix().isApprox(sipReverse.getCDMatrix().getMatrix())) {
225  std::ostringstream oss;
226  oss << "SIP forward and reverse transforms have inconsistent CD matrix: "
227  << sipForward.getCDMatrix() << "\n!=\n" << sipReverse.getCDMatrix();
228  throw LSST_EXCEPT(
230  oss.str()
231  );
232  }
233  Eigen::MatrixXd sipA(sipForward.getPoly().getXCoeffs().asEigen());
234  Eigen::MatrixXd sipB(sipForward.getPoly().getYCoeffs().asEigen());
235  Eigen::MatrixXd sipAP(sipReverse.getPoly().getXCoeffs().asEigen());
236  Eigen::MatrixXd sipBP(sipReverse.getPoly().getYCoeffs().asEigen());
237  // TanWcs uses strings for coordinate systems, while Coord uses an enum.
238  // Frustratingly, there's no way to convert from the enum to the string.
239  std::string coordSys;
240  switch (skyOrigin.getCoordSystem()) {
241  case afw::coord::ICRS:
242  coordSys = "ICRS";
243  break;
244  case afw::coord::FK5:
245  coordSys = "FK5";
246  break;
247  default:
248  throw LSST_EXCEPT(
250  "Coordinate system not supported"
251  );
252  }
253  return std::make_shared<afw::image::TanWcs>(
254  skyOrigin.getPosition(afw::geom::degrees),
255  sipForward.getPixelOrigin(),
256  sipForward.getCDMatrix().getMatrix(),
257  sipA, sipB, sipAP, sipBP,
258  skyOrigin.getEpoch(),
259  coordSys
260  );
261 }
262 
264  afw::image::TanWcs const & wcs,
266 ) {
267  if (wcs.hasDistortion()) {
270  return makeWcs(fwd, rev, *wcs.getSkyOrigin());
271  } else {
272  auto sInv = s.invert();
273  auto pixelOrigin = s.getLinear()(wcs.getPixelOrigin() - sInv.getTranslation());
274  Eigen::Matrix2d cdMatrix = wcs.getCDMatrix() * sInv.getLinear().getMatrix();
275  return std::make_shared<afw::image::TanWcs>(
276  wcs.getSkyOrigin()->toIcrs().getPosition(afw::geom::degrees),
277  pixelOrigin,
278  cdMatrix,
279  wcs.getEquinox(),
280  "ICRS"
281  );
282  }
283 }
284 
286  afw::image::TanWcs const & wcs,
287  int nQuarter,
288  afw::geom::Extent2I const & dimensions
289 ) {
290  afw::geom::Extent2D offset;
291  switch(nQuarter % 4) {
292  case 0:
293  offset = afw::geom::Extent2D(0, 0);
294  break;
295  case 1:
296  offset = afw::geom::Extent2D(dimensions.getY() - 1, 0);
297  break;
298  case 2:
299  offset = afw::geom::Extent2D(dimensions - afw::geom::Extent2I(1, 1));
300  break;
301  case 3:
302  offset = afw::geom::Extent2D(0, dimensions.getX() - 1);
303  break;
304  }
306  return transformWcsPixels(
307  wcs,
308  afw::geom::AffineTransform(rot, offset)
309  );
310 }
311 
312 }}} // 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:73
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
std::shared_ptr< afw::image::TanWcs > transformWcsPixels(afw::image::TanWcs const &wcs, afw::geom::AffineTransform const &s)
Create a new TanWcs whose pixel coordinate system has been transformed via an affine transform...
Extent< double, 2 > Extent2D
Eigen::MatrixXd const & getSipBp() const
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) of the transform.
Definition: SipTransform.h:63
LinearTransform const invert() const
#define PTR(...)
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.
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...
Eigen::MatrixXd const & getSipA() const
static SipReverseTransform extract(afw::image::TanWcs const &wcs)
Extract the reverse transform from a TanWcs.
lsst::afw::geom::Point2D getPosition(lsst::afw::geom::AngleUnit unit=lsst::afw::geom::degrees) const
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
STL class.
Eigen::MatrixXd const & getSipB() const
bool hasDistortion() const override
virtual CoordSystem getCoordSystem() const
A transform that maps pixel coordinates to intermediate world coordinates according to the SIP conven...
Definition: SipTransform.h:155
std::shared_ptr< afw::image::TanWcs > rotateWcsPixelsBy90(afw::image::TanWcs const &wcs, int nQuarter, afw::geom::Extent2I const &dimensions)
Return a new TanWcs that represents a rotation of the image it corresponds to about the image&#39;s cente...
EigenVector const & asEigen() const
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
static LinearTransform makeRotation(Angle t)
afw::geom::LinearTransform const & getCDMatrix() const
Return the CD matrix of the transform.
Definition: SipTransform.h:68
T str(T... args)
static SipForwardTransform extract(afw::image::TanWcs const &wcs)
Extract the reverse transform from a TanWcs.
Definition: SipTransform.cc:90
lsst::afw::geom::Point2D getPixelOrigin() const
afw::geom::AffineTransform const & getInputScaling() const
Return the first affine transform applied to input points.
std::shared_ptr< lsst::afw::coord::Coord > getSkyOrigin() const
std::shared_ptr< afw::image::TanWcs > makeWcs(SipForwardTransform const &sipForward, SipReverseTransform const &sipReverse, afw::coord::Coord const &skyOrigin)
Create a new TAN SIP Wcs from a pair of SIP transforms and the sky origin.
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:112
#define LSST_EXCEPT(type,...)
A transform that maps intermediate world coordinates to pixel coordinates according to the SIP conven...
Definition: SipTransform.h:284
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.
double getEpoch() const
void transformPixelsInPlace(afw::geom::AffineTransform const &s)
Definition: SipTransform.cc:33
double getEquinox() const
Eigen::Matrix2d getCDMatrix() const
ndarray::Array< double const, 2, 2 > getYCoeffs() const
2-D polynomial coefficients that compute the output x coordinate.
AffineTransform const invert() const
Extent2D const & getTranslation() const
Eigen::MatrixXd const & getSipAp() const
A 2-d coordinate transform represented by a pair of standard polynomials (one for each coordinate)...