lsst.jointcal  16.0-3-g21cc1d5+4
SimpleAstrometryMapping.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 #ifndef LSST_JOINTCAL_SIMPLE_ASTROMETRY_MAPPING_H
3 #define LSST_JOINTCAL_SIMPLE_ASTROMETRY_MAPPING_H
4 
5 #include <memory> // for unique_ptr
6 
10 
12 
16 namespace lsst {
17 namespace jointcal {
18 
20 public:
22  : toBeFit(toBeFit), transfo(gtransfo.clone()), errorProp(transfo), lin(new GtransfoLin) {
23  // in this order:
24  // take a copy of the input transfo,
25  // assign the transformation used to propagate errors to the transfo itself
26  // reserve some memory space to compute the derivatives (efficiency).
27  }
28 
34 
35  virtual void freezeErrorTransform() {
36  // from there on, updating the transfo does not change the errors.
37  errorProp = transfo->clone();
38  }
39 
40  // interface Mapping functions:
41 
43  unsigned getNpar() const {
44  if (toBeFit)
45  return transfo->getNpar();
46  else
47  return 0;
48  }
49 
52  if (indices.size() < getNpar()) indices.resize(getNpar());
53  for (unsigned k = 0; k < getNpar(); ++k) indices[k] = index + k;
54  }
55 
57  void transformPosAndErrors(FatPoint const &where, FatPoint &outPoint) const {
58  transfo->transformPosAndErrors(where, outPoint);
59  FatPoint tmp;
60  errorProp->transformPosAndErrors(where, tmp);
61  outPoint.vx = tmp.vx;
62  outPoint.vy = tmp.vy;
63  outPoint.vxy = tmp.vxy;
64  }
65 
67  void positionDerivative(Point const &where, Eigen::Matrix2d &derivative, double epsilon) const {
68  errorProp->computeDerivative(where, *lin, epsilon);
69  derivative(0, 0) = lin->coeff(1, 0, 0);
70  //
71  /* This does not work : it was proved by rotating the frame
72  see the compilation switch ROTATE_T2 in constrainedAstrometryModel.cc
73  derivative(1,0) = lin->coeff(1,0,1);
74  derivative(0,1) = lin->coeff(0,1,0);
75  */
76  derivative(1, 0) = lin->coeff(0, 1, 0);
77  derivative(0, 1) = lin->coeff(1, 0, 1);
78  derivative(1, 1) = lin->coeff(0, 1, 1);
79  }
80 
82  void offsetParams(Eigen::VectorXd const &delta) {
83  if (toBeFit) transfo->offsetParams(delta);
84  }
85 
87  unsigned getIndex() const { return index; }
88 
90  void setIndex(unsigned i) { index = i; }
91 
92  virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint,
93  Eigen::MatrixX2d &H) const {
94  transformPosAndErrors(where, outPoint);
95  transfo->paramDerivatives(where, &H(0, 0), &H(0, 1));
96  }
97 
99  virtual Gtransfo const &getTransfo() const { return *transfo; }
100 
102  bool getToBeFit() const { return toBeFit; }
104  void setToBeFit(bool value) { toBeFit = value; }
105 
106 protected:
107  // Whether this Mapping is fit as part of a Model.
108  bool toBeFit;
109  unsigned index;
110  /* inheritance may also work. Perhaps with some trouble because
111  some routines in Mapping and Gtransfo have the same name */
113 
115  /* to avoid allocation at every call of positionDerivative.
116  use a pointer for constness */
118 };
119 
122 public:
124 
125  // ! contructor.
128  SimplePolyMapping(GtransfoLin const &CenterAndScale, GtransfoPoly const &gtransfo)
129  : SimpleGtransfoMapping(gtransfo), _centerAndScale(CenterAndScale) {
130  // We assume that the initialization was done properly, for example that
131  // gtransfo = pix2TP*CenterAndScale.invert(), so we do not touch transfo.
132  /* store the (spatial) derivative of _centerAndScale. For the extra
133  diagonal terms, just copied the ones in positionDerivatives */
134  preDer(0, 0) = _centerAndScale.coeff(1, 0, 0);
135  preDer(1, 0) = _centerAndScale.coeff(0, 1, 0);
136  preDer(0, 1) = _centerAndScale.coeff(1, 0, 1);
137  preDer(1, 1) = _centerAndScale.coeff(0, 1, 1);
138 
139  // check of matrix indexing (once for all)
140  MatrixX2d H(3, 2);
141  assert((&H(1, 0) - &H(0, 0)) == 1);
142  }
143 
145  SimplePolyMapping(SimplePolyMapping const &) = delete;
147  SimplePolyMapping &operator=(SimplePolyMapping const &) = delete;
149 
150  /* The SimpleGtransfoMapping version does not account for the
151  _centerAndScale transfo */
152 
153  void positionDerivative(Point const &where, Eigen::Matrix2d &derivative, double epsilon) const {
154  Point tmp = _centerAndScale.apply(where);
155  errorProp->computeDerivative(tmp, *lin, epsilon);
156  derivative(0, 0) = lin->coeff(1, 0, 0);
157  //
158  /* This does not work : it was proved by rotating the frame
159  see the compilation switch ROTATE_T2 in constrainedAstrometryModel.cc
160  derivative(1,0) = lin->coeff(1,0,1);
161  derivative(0,1) = lin->coeff(0,1,0);
162  */
163  derivative(1, 0) = lin->coeff(0, 1, 0);
164  derivative(0, 1) = lin->coeff(1, 0, 1);
165  derivative(1, 1) = lin->coeff(0, 1, 1);
166  derivative = preDer * derivative;
167  }
168 
170  /* We should put the computation of error propagation and
171  parameter derivatives into the same Gtransfo routine because
172  it could be significantly faster */
173  virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint,
174  Eigen::MatrixX2d &H) const {
175  FatPoint mid;
176  _centerAndScale.transformPosAndErrors(where, mid);
177  transfo->transformPosAndErrors(mid, outPoint);
178  FatPoint tmp;
179  errorProp->transformPosAndErrors(mid, tmp);
180  outPoint.vx = tmp.vx;
181  outPoint.vy = tmp.vy;
182  outPoint.vxy = tmp.vxy;
183  transfo->paramDerivatives(mid, &H(0, 0), &H(0, 1));
184  }
185 
187  void transformPosAndErrors(FatPoint const &where, FatPoint &outPoint) const {
188  FatPoint mid;
189  _centerAndScale.transformPosAndErrors(where, mid);
190  transfo->transformPosAndErrors(mid, outPoint);
191  FatPoint tmp;
192  errorProp->transformPosAndErrors(mid, tmp);
193  outPoint.vx = tmp.vx;
194  outPoint.vy = tmp.vy;
195  outPoint.vxy = tmp.vxy;
196  }
197 
199  Gtransfo const &getTransfo() const {
200  // Cannot fail given the contructor:
201  const GtransfoPoly *fittedPoly = dynamic_cast<const GtransfoPoly *>(&(*transfo));
202  actualResult = (*fittedPoly) * _centerAndScale;
203  return actualResult;
204  }
205 
206 private:
207  /* to better condition the 2nd derivative matrix, the
208  transformed coordinates are mapped (roughly) on [-1,1].
209  We need both the transform and its derivative. */
210  GtransfoLin _centerAndScale;
211  Eigen::Matrix2d preDer;
212 
213  /* Where we store the combination. */
214  mutable GtransfoPoly actualResult;
215 };
216 
217 #ifdef STORAGE
218 
220 class SimpleIdentityMapping : public SimpleGtransfoMapping<GtransfoIdentity> {
221 public:
223  virtual void computeTransformAndDerivatives(FatPoint const &where, FatPoint &outPoint,
224  Eigen::MatrixX2d &H) const {
225  outPoint = where;
226  }
227 };
228 #endif
229 } // namespace jointcal
230 } // namespace lsst
231 
232 #endif // LSST_JOINTCAL_SIMPLE_ASTROMETRY_MAPPING_H
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:391
SimpleGtransfoMapping & operator=(SimpleGtransfoMapping const &)=delete
A point in a plane.
Definition: Point.h:13
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:253
A Point with uncertainties.
Definition: FatPoint.h:11
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:10
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:42
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)