lsst.jointcal  16.0-23-gcb65559+1
SimpleAstrometryMapping.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * This file is part of jointcal.
4  *
5  * Developed for the LSST Data Management System.
6  * This product includes software developed by the LSST Project
7  * (https://www.lsst.org).
8  * See the COPYRIGHT file at the top-level directory of this distribution
9  * for details of code ownership.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef LSST_JOINTCAL_SIMPLE_ASTROMETRY_MAPPING_H
26 #define LSST_JOINTCAL_SIMPLE_ASTROMETRY_MAPPING_H
27 
28 #include <memory> // for unique_ptr
29 
31 #include "lsst/jointcal/Gtransfo.h"
32 #include "lsst/jointcal/CcdImage.h"
33 
35 
39 namespace lsst {
40 namespace jointcal {
41 
43 public:
45  : toBeFit(toBeFit), transfo(gtransfo.clone()), errorProp(transfo), lin(new GtransfoLin) {
46  // in this order:
47  // take a copy of the input transfo,
48  // assign the transformation used to propagate errors to the transfo itself
49  // reserve some memory space to compute the derivatives (efficiency).
50  }
51 
57 
58  virtual void freezeErrorTransform() {
59  // from there on, updating the transfo does not change the errors.
60  errorProp = transfo->clone();
61  }
62 
63  // interface Mapping functions:
64 
66  unsigned getNpar() const {
67  if (toBeFit)
68  return transfo->getNpar();
69  else
70  return 0;
71  }
72 
75  if (indices.size() < getNpar()) indices.resize(getNpar());
76  for (unsigned k = 0; k < getNpar(); ++k) indices[k] = index + k;
77  }
78 
80  void transformPosAndErrors(FatPoint const &where, FatPoint &outPoint) const {
81  transfo->transformPosAndErrors(where, outPoint);
82  FatPoint tmp;
83  errorProp->transformPosAndErrors(where, tmp);
84  outPoint.vx = tmp.vx;
85  outPoint.vy = tmp.vy;
86  outPoint.vxy = tmp.vxy;
87  }
88 
90  void positionDerivative(Point const &where, Eigen::Matrix2d &derivative, double epsilon) const {
91  errorProp->computeDerivative(where, *lin, epsilon);
92  derivative(0, 0) = lin->coeff(1, 0, 0);
93  //
94  /* This does not work : it was proved by rotating the frame
95  see the compilation switch ROTATE_T2 in constrainedAstrometryModel.cc
96  derivative(1,0) = lin->coeff(1,0,1);
97  derivative(0,1) = lin->coeff(0,1,0);
98  */
99  derivative(1, 0) = lin->coeff(0, 1, 0);
100  derivative(0, 1) = lin->coeff(1, 0, 1);
101  derivative(1, 1) = lin->coeff(0, 1, 1);
102  }
103 
105  void offsetParams(Eigen::VectorXd const &delta) {
106  if (toBeFit) transfo->offsetParams(delta);
107  }
108 
110  unsigned getIndex() const { return index; }
111 
113  void setIndex(unsigned i) { index = i; }
114 
115  virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint,
116  Eigen::MatrixX2d &H) const {
117  transformPosAndErrors(where, outPoint);
118  transfo->paramDerivatives(where, &H(0, 0), &H(0, 1));
119  }
120 
122  virtual Gtransfo const &getTransfo() const { return *transfo; }
123 
125  bool getToBeFit() const { return toBeFit; }
127  void setToBeFit(bool value) { toBeFit = value; }
128 
129 protected:
130  // Whether this Mapping is fit as part of a Model.
131  bool toBeFit;
132  unsigned index;
133  /* inheritance may also work. Perhaps with some trouble because
134  some routines in Mapping and Gtransfo have the same name */
136 
138  /* to avoid allocation at every call of positionDerivative.
139  use a pointer for constness */
141 };
142 
145 public:
147 
148  // ! contructor.
151  SimplePolyMapping(GtransfoLin const &CenterAndScale, GtransfoPoly const &gtransfo)
152  : SimpleGtransfoMapping(gtransfo), _centerAndScale(CenterAndScale) {
153  // We assume that the initialization was done properly, for example that
154  // gtransfo = pix2TP*CenterAndScale.inverted(), so we do not touch transfo.
155  /* store the (spatial) derivative of _centerAndScale. For the extra
156  diagonal terms, just copied the ones in positionDerivatives */
157  preDer(0, 0) = _centerAndScale.coeff(1, 0, 0);
158  preDer(1, 0) = _centerAndScale.coeff(0, 1, 0);
159  preDer(0, 1) = _centerAndScale.coeff(1, 0, 1);
160  preDer(1, 1) = _centerAndScale.coeff(0, 1, 1);
161 
162  // check of matrix indexing (once for all)
163  MatrixX2d H(3, 2);
164  assert((&H(1, 0) - &H(0, 0)) == 1);
165  }
166 
168  SimplePolyMapping(SimplePolyMapping const &) = delete;
170  SimplePolyMapping &operator=(SimplePolyMapping const &) = delete;
172 
173  /* The SimpleGtransfoMapping version does not account for the
174  _centerAndScale transfo */
175 
176  void positionDerivative(Point const &where, Eigen::Matrix2d &derivative, double epsilon) const {
177  Point tmp = _centerAndScale.apply(where);
178  errorProp->computeDerivative(tmp, *lin, epsilon);
179  derivative(0, 0) = lin->coeff(1, 0, 0);
180  //
181  /* This does not work : it was proved by rotating the frame
182  see the compilation switch ROTATE_T2 in constrainedAstrometryModel.cc
183  derivative(1,0) = lin->coeff(1,0,1);
184  derivative(0,1) = lin->coeff(0,1,0);
185  */
186  derivative(1, 0) = lin->coeff(0, 1, 0);
187  derivative(0, 1) = lin->coeff(1, 0, 1);
188  derivative(1, 1) = lin->coeff(0, 1, 1);
189  derivative = preDer * derivative;
190  }
191 
193  /* We should put the computation of error propagation and
194  parameter derivatives into the same Gtransfo routine because
195  it could be significantly faster */
196  virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint,
197  Eigen::MatrixX2d &H) const {
198  FatPoint mid;
199  _centerAndScale.transformPosAndErrors(where, mid);
200  transfo->transformPosAndErrors(mid, outPoint);
201  FatPoint tmp;
202  errorProp->transformPosAndErrors(mid, tmp);
203  outPoint.vx = tmp.vx;
204  outPoint.vy = tmp.vy;
205  outPoint.vxy = tmp.vxy;
206  transfo->paramDerivatives(mid, &H(0, 0), &H(0, 1));
207  }
208 
210  void transformPosAndErrors(FatPoint const &where, FatPoint &outPoint) const {
211  FatPoint mid;
212  _centerAndScale.transformPosAndErrors(where, mid);
213  transfo->transformPosAndErrors(mid, outPoint);
214  FatPoint tmp;
215  errorProp->transformPosAndErrors(mid, tmp);
216  outPoint.vx = tmp.vx;
217  outPoint.vy = tmp.vy;
218  outPoint.vxy = tmp.vxy;
219  }
220 
222  Gtransfo const &getTransfo() const {
223  // Cannot fail given the contructor:
224  const GtransfoPoly *fittedPoly = dynamic_cast<const GtransfoPoly *>(&(*transfo));
225  actualResult = (*fittedPoly) * _centerAndScale;
226  return actualResult;
227  }
228 
229 private:
230  /* to better condition the 2nd derivative matrix, the
231  transformed coordinates are mapped (roughly) on [-1,1].
232  We need both the transform and its derivative. */
233  GtransfoLin _centerAndScale;
234  Eigen::Matrix2d preDer;
235 
236  /* Where we store the combination. */
237  mutable GtransfoPoly actualResult;
238 };
239 
240 #ifdef STORAGE
241 
243 class SimpleIdentityMapping : public SimpleGtransfoMapping<GtransfoIdentity> {
244 public:
246  virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint,
247  Eigen::MatrixX2d &H) const {
248  outPoint = where;
249  }
250 };
251 #endif
252 } // namespace jointcal
253 } // namespace lsst
254 
255 #endif // LSST_JOINTCAL_SIMPLE_ASTROMETRY_MAPPING_H
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:414
SimpleGtransfoMapping & operator=(SimpleGtransfoMapping const &)=delete
A point in a plane.
Definition: Point.h:36
Mapping implementation for a polynomial transformation.
virtual Gtransfo const & getTransfo() const
Access to the (fitted) transfo.
SimplePolyMapping(GtransfoLin const &CenterAndScale, GtransfoPoly const &gtransfo)
The transformation will be initialized to gtransfo, so that the effective transformation reads gtrans...
void positionDerivative(Point const &where, Eigen::Matrix2d &derivative, double epsilon) const
The derivative w.r.t. position.
Polynomial transformation class.
Definition: Gtransfo.h:276
A Point with uncertainties.
Definition: FatPoint.h:34
T resize(T... args)
unsigned getNpar() const
Number of parameters in total.
void setToBeFit(bool value)
Set whether this Mapping is to be fit as part of a Model.
Class for a simple mapping implementing a generic Gtransfo.
void transformPosAndErrors(FatPoint const &where, FatPoint &outPoint) const
Implements as well the centering and scaling of coordinates.
void offsetParams(Eigen::VectorXd const &delta)
Remember the error scale and freeze it.
virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint, Eigen::MatrixX2d &H) const
Actually applies the AstrometryMapping and evaluates the derivatives w.r.t the fitted parameters...
Eigen::Matrix< double, Eigen::Dynamic, 2 > MatrixX2d
Definition: Eigenstuff.h:33
void getMappingIndices(std::vector< unsigned > &indices) const
Sets how this set of parameters (of length Npar()) map into the "grand" fit Expects that indices has ...
T size(T... args)
STL class.
unsigned getIndex() const
position of the parameters within the grand fitting scheme
SimpleGtransfoMapping(Gtransfo const &gtransfo, bool toBeFit=true)
virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint, Eigen::MatrixX2d &H) const
Calls the transforms and implements the centering and scaling of coordinates.
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:65
Gtransfo const & getTransfo() const
Access to the (fitted) transfo.
void positionDerivative(Point const &where, Eigen::Matrix2d &derivative, double epsilon) const
The derivative w.r.t. position.
virtual class needed in the abstraction of the distortion model
clone
bool getToBeFit() const
Get whether this mapping is fit as part of a Model.
void transformPosAndErrors(FatPoint const &where, FatPoint &outPoint) const
The same as above but without the parameter derivatives (used to evaluate chi^2)