lsst.meas.astrom  13.0-16-g30f6da5+11
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
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 
33 void SipTransformBase::transformPixelsInPlace(afw::geom::AffineTransform const & s) {
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(
51  afw::geom::AffineTransform(cdMatrix.invert()),
52  compose(
53  poly,
54  afw::geom::AffineTransform(afw::geom::Extent2D(pixelOrigin))
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(
71  afw::geom::AffineTransform(cdMatrix.invert())*scaled.getOutputScalingInverse(),
72  compose(
73  scaled.getPoly(),
74  scaled.getInputScaling()*afw::geom::AffineTransform(afw::geom::Extent2D(pixelOrigin))
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 
85  afw::geom::Point2D pixelOrigin(-scaled.getOutputScalingInverse().getTranslation());
86  afw::geom::LinearTransform cdMatrix(scaled.getInputScaling().getLinear().invert());
87  return convert(scaled, pixelOrigin, cdMatrix);
88 }
89 
90 SipForwardTransform SipForwardTransform::extract(afw::image::TanWcs const & wcs) {
91  if (!wcs.hasDistortion()) {
92  throw LSST_EXCEPT(
93  pex::exceptions::LogicError,
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(),
103  afw::geom::LinearTransform(wcs.getCDMatrix()),
104  PolynomialTransform(xCoeffs, yCoeffs)
105  );
106 }
107 
108 afw::geom::AffineTransform SipForwardTransform::linearize(afw::geom::Point2D const & in) const {
109  afw::geom::AffineTransform tail(-afw::geom::Extent2D(getPixelOrigin()));
110  return afw::geom::AffineTransform(_cdMatrix)
111  * (afw::geom::AffineTransform() + _poly.linearize(tail(in)))
112  * tail;
113 }
114 
115 afw::geom::Point2D SipForwardTransform::operator()(afw::geom::Point2D const & uv) const {
116  afw::geom::Point2D duv(uv - afw::geom::Extent2D(getPixelOrigin()));
117  return getCDMatrix()(afw::geom::Extent2D(duv) + getPoly()(duv));
118 }
119 
120 SipForwardTransform SipForwardTransform::transformPixels(afw::geom::AffineTransform const & s) const {
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(
132  afw::geom::AffineTransform(-afw::geom::Extent2D(pixelOrigin)),
133  compose(
134  poly,
135  afw::geom::AffineTransform(cdMatrix)
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(
151  afw::geom::AffineTransform(-afw::geom::Extent2D(pixelOrigin))
152  *scaled.getOutputScalingInverse(),
153  compose(
154  scaled.getPoly(),
155  scaled.getInputScaling()*afw::geom::AffineTransform(cdMatrix)
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,
168  afw::geom::Point2D(scaled.getOutputScalingInverse().getTranslation()),
169  scaled.getInputScaling().getLinear()
170  );
171 }
172 
173 SipReverseTransform SipReverseTransform::extract(afw::image::TanWcs const & wcs) {
174  if (!wcs.hasDistortion()) {
175  throw LSST_EXCEPT(
176  pex::exceptions::LogicError,
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(),
186  afw::geom::LinearTransform(wcs.getCDMatrix()),
187  PolynomialTransform(xCoeffs, yCoeffs)
188  );
189 }
190 
191 SipReverseTransform SipReverseTransform::transformPixels(afw::geom::AffineTransform const & s) const {
192  SipReverseTransform result(*this);
193  result.transformPixelsInPlace(s);
194  result._cdInverse = result._cdMatrix.invert();
195  return result;
196 }
197 
198 afw::geom::AffineTransform SipReverseTransform::linearize(afw::geom::Point2D const & in) const {
199  return afw::geom::AffineTransform(afw::geom::Extent2D(getPixelOrigin()))
200  * (afw::geom::AffineTransform() + _poly.linearize(_cdInverse(in)))
201  * _cdInverse;
202 }
203 
204 afw::geom::Point2D SipReverseTransform::operator()(afw::geom::Point2D const & xy) const {
205  afw::geom::Point2D UV = _cdInverse(xy);
206  return afw::geom::Extent2D(UV) + afw::geom::Extent2D(getPixelOrigin()) + getPoly()(UV);
207 }
208 
209 
210 PTR(afw::image::TanWcs) makeWcs(
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(
220  pex::exceptions::InvalidParameterError,
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(
229  pex::exceptions::InvalidParameterError,
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(
249  pex::exceptions::InvalidParameterError,
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 
263 std::shared_ptr<afw::image::TanWcs> transformWcsPixels(
264  afw::image::TanWcs const & wcs,
265  afw::geom::AffineTransform const & s
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 
285 std::shared_ptr<afw::image::TanWcs> rotateWcsPixelsBy90(
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  }
305  auto rot = afw::geom::LinearTransform::makeRotation(nQuarter*90.0*afw::geom::degrees);
306  return transformWcsPixels(
307  wcs,
308  afw::geom::AffineTransform(rot, offset)
309  );
310 }
311 
312 }}} // namespace lsst::meas::astrom
SipForwardTransform(afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix, PolynomialTransform const &forwardSipPoly)
Construct a SipForwardTransform from its components.
Definition: SipTransform.h:205
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
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...
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
PolynomialTransform compose(afw::geom::AffineTransform const &t1, PolynomialTransform const &t2)
Return a PolynomialTransform that is equivalent to the composition t1(t2())
afw::geom::Point2D operator()(afw::geom::Point2D const &xy) const
Apply the transform to a point.
SipReverseTransform(afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix, PolynomialTransform const &reverseSipPoly)
Construct a SipReverseTransform from its components.
Definition: SipTransform.h:334
static SipReverseTransform extract(afw::image::TanWcs const &wcs)
Extract the reverse transform from a TanWcs.
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 const & getOutputScalingInverse() const
Return the affine transform applied to points after the polynomial transform.
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...
afw::geom::Point2D const & getPixelOrigin() const
Return the pixel origin (CRPIX) of the transform.
Definition: SipTransform.h:63
static SipForwardTransform extract(afw::image::TanWcs const &wcs)
Extract the reverse transform from a TanWcs.
Definition: SipTransform.cc:90
PolynomialTransform const & getPoly() const
Return the polynomial component of the transform (A,B) or (AP,BP).
Definition: SipTransform.h:73
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
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::Point2D operator()(afw::geom::Point2D const &uv) const
Apply the transform to a point.
afw::geom::LinearTransform _cdMatrix
Definition: SipTransform.h:112
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.
void transformPixelsInPlace(afw::geom::AffineTransform const &s)
Definition: SipTransform.cc:33
PolynomialTransform const & getPoly() const
Return the polynomial transform applied after the input scaling.
afw::geom::LinearTransform const & getCDMatrix() const
Return the CD matrix of the transform.
Definition: SipTransform.h:68
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::AffineTransform const & getInputScaling() const
Return the first affine transform applied to input points.
A 2-d coordinate transform represented by a pair of standard polynomials (one for each coordinate)...
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.