LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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  PolynomialTransform const & poly,
35  afw::geom::Point2D const & pixelOrigin,
36  afw::geom::LinearTransform const & cdMatrix
37 ) {
38  auto forwardSipPoly = compose(
40  compose(
41  poly,
43  )
44  );
45  // Subtracting 1 here accounts for the extra terms outside the sum in the
46  // transform definition (see class docs) - note that you can fold those
47  // terms into the sum by adding 1 from the A_10 and B_01 terms.
48  forwardSipPoly._xCoeffs(1, 0) -= 1;
49  forwardSipPoly._yCoeffs(0, 1) -= 1;
50  return SipForwardTransform(pixelOrigin, cdMatrix, forwardSipPoly);
51 }
52 
54  ScaledPolynomialTransform const & scaled,
55  afw::geom::Point2D const & pixelOrigin,
56  afw::geom::LinearTransform const & cdMatrix
57 ) {
58  auto forwardSipPoly = compose(
60  compose(
61  scaled.getPoly(),
63  )
64  );
65  // Account for the terms outside the sum in the definition (see comment
66  // earlier in the file for more explanation).
67  forwardSipPoly._xCoeffs(1, 0) -= 1;
68  forwardSipPoly._yCoeffs(0, 1) -= 1;
69  return SipForwardTransform(pixelOrigin, cdMatrix, forwardSipPoly);
70 }
71 
75  return convert(scaled, pixelOrigin, cdMatrix);
76 }
77 
82  * tail;
83 }
84 
87  return getCDMatrix()(afw::geom::Extent2D(duv) + getPoly()(duv));
88 }
89 
91  PolynomialTransform const & poly,
92  afw::geom::Point2D const & pixelOrigin,
93  afw::geom::LinearTransform const & cdMatrix
94 ) {
95  auto reverseSipPoly = compose(
97  compose(
98  poly,
100  )
101  );
102  // Account for the terms outside the sum in the definition (see comment
103  // earlier in the file for more explanation).
104  reverseSipPoly._xCoeffs(1, 0) -= 1;
105  reverseSipPoly._yCoeffs(0, 1) -= 1;
106  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
107 }
108 
110  ScaledPolynomialTransform const & scaled,
111  afw::geom::Point2D const & pixelOrigin,
112  afw::geom::LinearTransform const & cdMatrix
113 ) {
114  auto reverseSipPoly = compose(
116  *scaled.getOutputScalingInverse(),
117  compose(
118  scaled.getPoly(),
120  )
121  );
122  // Account for the terms outside the sum in the definition (see comment
123  // earlier in the file for more explanation).
124  reverseSipPoly._xCoeffs(1, 0) -= 1;
125  reverseSipPoly._yCoeffs(0, 1) -= 1;
126  return SipReverseTransform(pixelOrigin, cdMatrix, reverseSipPoly);
127 }
128 
130  return convert(
131  scaled,
133  scaled.getInputScaling().getLinear()
134  );
135 }
136 
140  * _cdInverse;
141 }
142 
146 }
147 
148 
150  SipForwardTransform const & sipForward,
151  SipReverseTransform const & sipReverse,
152  afw::coord::Coord const & skyOrigin
153 ) {
154  if (!sipForward.getPixelOrigin().asEigen().isApprox(sipReverse.getPixelOrigin().asEigen())) {
155  std::ostringstream oss;
156  oss << "SIP forward and reverse transforms have inconsistent CRPIX: "
157  << sipForward.getPixelOrigin() << " != " << sipReverse.getPixelOrigin();
158  throw LSST_EXCEPT(
159  pex::exceptions::InvalidParameterError,
160  oss.str()
161  );
162  }
163  if (!sipForward.getCDMatrix().getMatrix().isApprox(sipReverse.getCDMatrix().getMatrix())) {
164  std::ostringstream oss;
165  oss << "SIP forward and reverse transforms have inconsistent CD matrix: "
166  << sipForward.getCDMatrix() << "\n!=\n" << sipReverse.getCDMatrix();
167  throw LSST_EXCEPT(
168  pex::exceptions::InvalidParameterError,
169  oss.str()
170  );
171  }
172  Eigen::MatrixXd sipA(sipForward.getPoly().getXCoeffs().asEigen());
173  Eigen::MatrixXd sipB(sipForward.getPoly().getYCoeffs().asEigen());
174  Eigen::MatrixXd sipAP(sipReverse.getPoly().getXCoeffs().asEigen());
175  Eigen::MatrixXd sipBP(sipReverse.getPoly().getYCoeffs().asEigen());
176  // TanWcs uses strings for coordinate systems, while Coord uses an enum.
177  // Frustratingly, there's no way to convert from the enum to the string.
178  std::string coordSys;
179  switch (skyOrigin.getCoordSystem()) {
180  case afw::coord::ICRS:
181  coordSys = "ICRS";
182  break;
183  case afw::coord::FK5:
184  coordSys = "FK5";
185  break;
186  default:
187  throw LSST_EXCEPT(
188  pex::exceptions::InvalidParameterError,
189  "Coordinate system not supported"
190  );
191  }
192  return std::make_shared<afw::image::TanWcs>(
193  skyOrigin.getPosition(afw::geom::degrees),
194  sipForward.getPixelOrigin(),
195  sipForward.getCDMatrix().getMatrix(),
196  sipA, sipB, sipAP, sipBP,
197  skyOrigin.getEpoch(),
198  coordSys
199  );
200 }
201 
202 }}} // namespace lsst::meas::astrom
static SipReverseTransform convert(PolynomialTransform const &poly, afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix)
Convert a PolynomialTransform to an equivalent SipReverseTransform.
Definition: SipTransform.cc:90
afw::geom::Point2D operator()(afw::geom::Point2D const &xy) const
Apply the transform to a point.
lsst::afw::coord::Coord Coord
Definition: misc.h:35
afw::geom::AffineTransform const & getInputScaling() const
Return the first affine transform applied to input points.
PolynomialTransform const & getPoly() const
Return the polynomial transform applied after the input scaling.
Extent< double, 2 > Extent2D
Definition: Extent.h:361
PolynomialTransform compose(afw::geom::AffineTransform const &t1, PolynomialTransform const &t2)
Return a PolynomialTransform that is equivalent to the composition t1(t2())
afw::geom::LinearTransform const & getCDMatrix() const
Return the CD matrix of the transform.
Definition: SipTransform.h:65
afw::geom::AffineTransform const & getOutputScalingInverse() const
Return the affine transform applied to points after the polynomial transform.
afw::geom::LinearTransform _cdInverse
Definition: SipTransform.h:347
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
LinearTransform const & getLinear() const
SipReverseTransform(afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix, PolynomialTransform const &reverseSipPoly)
Construct a SipReverseTransform from its components.
Definition: SipTransform.h:313
A 2D linear coordinate transformation.
A transform that maps pixel coordinates to intermediate world coordinates according to the SIP conven...
Definition: SipTransform.h:150
AngleUnit const degrees
Definition: Angle.h:91
Implementation of the WCS standard for the special case of the Gnomonic (tangent plane) projection...
Definition: TanWcs.h:68
An affine coordinate transformation consisting of a linear transformation and an offset.
SipForwardTransform(afw::geom::Point2D const &pixelOrigin, afw::geom::LinearTransform const &cdMatrix, PolynomialTransform const &forwardSipPoly)
Construct a SipForwardTransform from its components.
Definition: SipTransform.h:195
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.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
A transform that maps intermediate world coordinates to pixel coordinates according to the SIP conven...
Definition: SipTransform.h:268
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
Definition: SipTransform.cc:78
afw::geom::AffineTransform linearize(afw::geom::Point2D const &in) const
Return an approximate affine transform at the given point.
LinearTransform const invert() const
#define PTR(...)
Definition: base.h:41
afw::geom::Point2D operator()(afw::geom::Point2D const &uv) const
Apply the transform to a point.
Definition: SipTransform.cc:85
Extent2D const & getTranslation() const
afw::geom::Point2D const & getPixelOrigin() const
Return the pixel origin (CRPIX) of the transform.
Definition: SipTransform.h:60
afw::geom::LinearTransform _cdMatrix
Definition: SipTransform.h:107
PolynomialTransform const & getPoly() const
Return the polynomial component of the transform (A,B) or (AP,BP).
Definition: SipTransform.h:70
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:33
A 2-d coordinate transform represented by a pair of standard polynomials (one for each coordinate)...
Functions to handle coordinates.