LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
transformFactory.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * LSST Data Management System
4  * See COPYRIGHT file at the top of the source tree.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include <sstream>
25 #include <cmath>
26 
27 #include "astshim.h"
28 
29 #include "Eigen/Core"
30 
31 #include "lsst/afw/geom/Endpoint.h"
33 #include "lsst/pex/exceptions.h"
34 
35 #include "ndarray.h"
36 #include "ndarray/eigen.h"
37 
38 namespace lsst {
39 namespace afw {
40 namespace geom {
41 
42 namespace {
43 /*
44  * Print a vector to a stream.
45  *
46  * The exact details of the representation are unspecified and subject to
47  * change, but the following may be regarded as typical:
48  *
49  * [1.0, -3.560, 42.0]
50  *
51  * @tparam T the element type. Must support stream output.
52  */
53 template <typename T>
55  os << '[';
56  bool first = true;
57  for (T element : v) {
58  if (first) {
59  first = false;
60  } else {
61  os << ", ";
62  }
63  os << element;
64  }
65  os << ']';
66  return os;
67 }
68 
69 /*
70  * Convert a Matrix to the equivalent ndarray.
71  *
72  * @param matrix The matrix to convert.
73  * @returns an ndarray containing a copy of the data in `matrix`
74  *
75  * @note Returning a copy is safer that returning a view because ndarray cannot share
76  * management of the memory with Eigen. So as long as the matrix is small,
77  * making a copy is preferred even if a view would suffice.
78  */
79 template <typename Derived>
81  Eigen::MatrixBase<Derived> const &matrix) {
83  ndarray::allocate(ndarray::makeVector(matrix.rows(), matrix.cols()));
84  ndarray::asEigenMatrix(array) = matrix;
85  return array;
86 }
87 
88 /*
89  * Tests whether polynomial coefficients match the expected format.
90  *
91  * @param coeffs radial polynomial coefficients.
92  * @returns `true` if either `coeffs.size()` = 0, or `coeffs.size()` > 1,
93  * `coeffs[0]` = 0, and `coeffs[1]` &ne; 0. `false` otherwise.
94  */
95 bool areRadialCoefficients(std::vector<double> const &coeffs) noexcept {
96  if (coeffs.empty()) {
97  return true;
98  } else {
99  return coeffs.size() > 1 && coeffs[0] == 0.0 && coeffs[1] != 0.0;
100  }
101 }
102 
103 /*
104  * Make a one-dimensional polynomial distortion.
105  *
106  * The Mapping computes a scalar function
107  * @f[ f(x) = \sum_{i=1}^{N} \mathrm{coeffs[i]} \ x^i @f]
108  *
109  * @param coeffs radial polynomial coefficients. Must have `size` > 1,
110  * `coeffs[0]` = 0, and `coeffs[1]` &ne; 0.
111  * @returns the function represented by `coeffs`. The Mapping shall have an
112  * inverse, which may be approximate.
113  *
114  * @exceptsafe Provides basic exception safety.
115  *
116  * @warning Input to this function is not validated.
117  */
118 ast::PolyMap makeOneDDistortion(std::vector<double> const &coeffs) {
119  int const nCoeffs = coeffs.size() - 1; // ignore coeffs[0]
120  ndarray::Array<double, 2, 2> const polyCoeffs = ndarray::allocate(ndarray::makeVector(nCoeffs, 3));
121  for (size_t i = 1; i < coeffs.size(); ++i) {
122  polyCoeffs[i - 1][0] = coeffs[i];
123  polyCoeffs[i - 1][1] = 1;
124  polyCoeffs[i - 1][2] = i;
125  }
126 
127  return ast::PolyMap(polyCoeffs, 1, "IterInverse=1, TolInverse=1e-8, NIterInverse=30");
128 }
129 
130 } // namespace
131 
133  lsst::geom::Point2D const &inPoint) {
134  auto outPoint = original.applyForward(inPoint);
135  Eigen::Matrix2d jacobian = original.getJacobian(inPoint);
136  for (int i = 0; i < 2; ++i) {
137  if (!std::isfinite(outPoint[i])) {
138  std::ostringstream buffer;
139  buffer << "Transform ill-defined: " << inPoint << " -> " << outPoint;
141  }
142  }
143  if (!jacobian.allFinite()) {
144  std::ostringstream buffer;
145  buffer << "Transform not continuous at " << inPoint << ": J = " << jacobian;
147  }
148 
149  // y(x) = J (x - x0) + y0 = J x + (y0 - J x0)
150  auto offset = outPoint.asEigen() - jacobian * inPoint.asEigen();
151  return lsst::geom::AffineTransform(jacobian, offset);
152 }
153 
155  auto const offset = lsst::geom::Point2D(affine.getTranslation());
156  auto const jacobian = affine.getLinear().getMatrix();
157 
159  auto const map = ast::MatrixMap(toNdArray(jacobian))
160  .then(ast::ShiftMap(toEndpoint.dataFromPoint(offset)))
161  .simplified();
162  return std::make_shared<TransformPoint2ToPoint2>(*map);
163 }
164 
166  if (!areRadialCoefficients(coeffs)) {
167  std::ostringstream buffer;
168  buffer << "Invalid coefficient vector: " << coeffs;
170  }
171 
172  if (coeffs.empty()) {
173  return std::make_shared<TransformPoint2ToPoint2>(ast::UnitMap(2));
174  } else {
175  // distortion is a radial polynomial with center at focal plane center;
176  // the polynomial has an iterative inverse
177  std::vector<double> center = {0.0, 0.0};
178  ast::PolyMap const distortion = makeOneDDistortion(coeffs);
179  return std::make_shared<TransformPoint2ToPoint2>(*ast::makeRadialMapping(center, distortion));
180  }
181 }
182 
184  std::vector<double> const &inverseCoeffs) {
185  if (forwardCoeffs.empty() != inverseCoeffs.empty()) {
186  throw LSST_EXCEPT(
188  "makeRadialTransform must have either both empty or both non-empty coefficient vectors.");
189  }
190  if (forwardCoeffs.empty()) {
191  // no forward or inverse coefficients, so no distortion
192  return std::make_shared<TransformPoint2ToPoint2>(ast::UnitMap(2));
193  }
194 
195  if (!areRadialCoefficients(forwardCoeffs)) {
196  std::ostringstream buffer;
197  buffer << "Invalid forward coefficient vector: " << forwardCoeffs;
199  }
200  if (!areRadialCoefficients(inverseCoeffs)) {
201  std::ostringstream buffer;
202  buffer << "Invalid inverse coefficient vector: " << inverseCoeffs;
204  }
205  // distortion is a 1-d radial polynomial centered at focal plane center;
206  // the polynomial has coefficients specified for both the forward and inverse directions
207  std::vector<double> center = {0.0, 0.0};
208  ast::PolyMap const forward = makeOneDDistortion(forwardCoeffs);
209  auto inverse = makeOneDDistortion(inverseCoeffs).inverted();
210  auto distortion = ast::TranMap(forward, *inverse);
211  return std::make_shared<TransformPoint2ToPoint2>(*ast::makeRadialMapping(center, distortion));
212 }
213 
215  return std::make_shared<TransformPoint2ToPoint2>(ast::UnitMap(2));
216 }
217 
218 } // namespace geom
219 } // namespace afw
220 } // namespace lsst
std::shared_ptr
STL class.
ast::Mapping::simplified
std::shared_ptr< Mapping > simplified() const
Return a simplied version of the mapping (which may be a compound Mapping such as a CmpMap).
Definition: Mapping.h:248
lsst::afw::geom::Transform
Transform LSST spatial data, such as lsst::geom::Point2D and lsst::geom::SpherePoint,...
Definition: Transform.h:68
std::vector
STL class.
std::vector::size
T size(T... args)
ast::Mapping::then
SeriesMap then(Mapping const &next) const
Return a series compound mapping this(first(input)) containing shallow copies of the original.
Definition: Mapping.cc:37
lsst::afw::table._match.first
first
Definition: _match.py:74
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::meas::modelfit::Scalar
double Scalar
Typedefs to be used for probability and parameter values.
Definition: common.h:44
lsst::afw::geom::Transform::applyForward
ToPoint applyForward(FromPoint const &point) const
Transform one point in the forward direction ("from" to "to")
Definition: Transform.cc:79
lsst::afw::geom::makeRadialTransform
std::shared_ptr< TransformPoint2ToPoint2 > makeRadialTransform(std::vector< double > const &coeffs)
A purely radial polynomial distortion.
Definition: transformFactory.cc:165
lsst::afw::geom::linearizeTransform
lsst::geom::AffineTransform linearizeTransform(TransformPoint2ToPoint2 const &original, lsst::geom::Point2D const &inPoint)
Approximate a Transform by its local linearization.
Definition: transformFactory.cc:132
lsst::geom::AffineTransform::getTranslation
Extent2D const & getTranslation() const noexcept
Definition: AffineTransform.h:152
Endpoint.h
lsst::geom::Point2D
Point< double, 2 > Point2D
Definition: Point.h:324
lsst::afw::geom::Point2Endpoint
An endpoint for lsst::geom::Point2D.
Definition: Endpoint.h:261
ast::PolyMap
PolyMap is a Mapping which performs a general polynomial transformation.
Definition: PolyMap.h:49
astshim.h
ast::MatrixMap
MatrixMap is a form of Mapping which performs a general linear transformation.
Definition: MatrixMap.h:42
lsst::geom::AffineTransform
An affine coordinate transformation consisting of a linear transformation and an offset.
Definition: AffineTransform.h:75
lsst::afw::geom::operator<<
std::ostream & operator<<(std::ostream &os, GenericEndpoint const &endpoint)
Print "GenericEndpoint(_n_)" to the ostream where _n_ is the number of axes, e.g. "GenericAxes(4)".
Definition: Endpoint.cc:240
std::isfinite
T isfinite(T... args)
std::ostream
STL class.
transformFactory.h
ast::ShiftMap
ShiftMap is a linear Mapping which shifts each axis by a specified constant value.
Definition: ShiftMap.h:40
lsst::geom::AffineTransform::getLinear
LinearTransform const & getLinear() const noexcept
Definition: AffineTransform.h:155
ast::makeRadialMapping
std::shared_ptr< Mapping > makeRadialMapping(std::vector< double > const &center, Mapping const &mapping1d)
Construct a radially symmetric mapping from a 1-dimensional mapping.
Definition: functional.cc:48
lsst::geom::LinearTransform::getMatrix
Matrix const & getMatrix() const noexcept
Definition: LinearTransform.h:151
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::ostringstream
STL class.
lsst::geom
Definition: AffineTransform.h:36
lsst::afw::geom.python.transform.toEndpoint
toEndpoint
Definition: transform.py:60
os
std::ostream * os
Definition: Schema.cc:746
lsst.pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
lsst::geom::Point
A coordinate class intended to represent absolute positions.
Definition: CoordinateBase.h:39
std::vector::empty
T empty(T... args)
std::ostringstream::str
T str(T... args)
element
double element[2]
Definition: BaseTable.cc:91
lsst::afw::geom::Transform::getJacobian
Eigen::MatrixXd getJacobian(FromPoint const &x) const
The Jacobian matrix of this Transform.
Definition: Transform.cc:123
ast::TranMap
TranMap is a Mapping which combines the forward transformation of a supplied Mapping with the inverse...
Definition: TranMap.h:49
lsst::afw::geom::makeIdentityTransform
std::shared_ptr< TransformPoint2ToPoint2 > makeIdentityTransform()
Trivial Transform x → x.
Definition: transformFactory.cc:214
lsst::afw::geom::makeTransform
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(lsst::geom::AffineTransform const &affine)
Wrap an lsst::geom::AffineTransform as a Transform.
Definition: transformFactory.cc:154
exceptions.h
ast::UnitMap
A UnitMap is a unit (null) Mapping that has no effect on the coordinates supplied to it.
Definition: UnitMap.h:44