lsst.jointcal  master-gc935ebf72c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SimplePolyMapping.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 #ifndef LSST_JOINTCAL_SIMPLE_POLY_MAPPING_H
3 #define LSST_JOINTCAL_SIMPLE_POLY_MAPPING_H
4 
5 #include <memory> // for unique_ptr
6 
10 
12 
16 namespace lsst {
17 namespace jointcal {
18 
20 protected:
21  bool toFit;
22  unsigned index;
23  /* inheritance may also work. Perhaps with some trouble because
24  some routines in Mapping and Gtransfo have the same name */
25  std::shared_ptr<Gtransfo> transfo;
26 
27  std::shared_ptr<Gtransfo> errorProp;
28  /* to avoid allocation at every call of positionDerivative.
29  use a pointer for constness */
30  std::unique_ptr<GtransfoLin> lin;
31 
32 public:
33  SimpleGtransfoMapping(const Gtransfo &gtransfo, bool toFit = true)
34  : toFit(toFit), transfo(gtransfo.clone()), errorProp(transfo), lin(new GtransfoLin) {
35  // in this order:
36  // take a copy of the input transfo,
37  // assign the transformation used to propagate errors to the transfo itself
38  // reserve some memory space to compute the derivatives (efficiency).
39  }
40 
41  virtual void freezeErrorScales() {
42  // from there on, updating the transfo does not change the errors.
43  errorProp = transfo->clone();
44  }
45 
46  // interface Mapping functions:
47 
49  unsigned getNpar() const {
50  if (toFit)
51  return transfo->getNpar();
52  else
53  return 0;
54  }
55 
57  void setMappingIndices(std::vector<unsigned> &indices) const {
58  if (indices.size() < getNpar()) indices.resize(getNpar());
59  for (unsigned k = 0; k < getNpar(); ++k) indices[k] = index + k;
60  }
61 
63  void transformPosAndErrors(const FatPoint &where, FatPoint &outPoint) const {
64  transfo->transformPosAndErrors(where, outPoint);
65  FatPoint tmp;
66  errorProp->transformPosAndErrors(where, tmp);
67  outPoint.vx = tmp.vx;
68  outPoint.vy = tmp.vy;
69  outPoint.vxy = tmp.vxy;
70  }
71 
73  void positionDerivative(const Point &where, Eigen::Matrix2d &derivative, double epsilon) const {
74  errorProp->computeDerivative(where, *lin, epsilon);
75  derivative(0, 0) = lin->coeff(1, 0, 0);
76  //
77  /* This does not work : it was proved by rotating the frame
78  see the compilation switch ROTATE_T2 in constrainedpolymodel.cc
79  derivative(1,0) = lin->coeff(1,0,1);
80  derivative(0,1) = lin->coeff(0,1,0);
81  */
82  derivative(1, 0) = lin->coeff(0, 1, 0);
83  derivative(0, 1) = lin->coeff(1, 0, 1);
84  derivative(1, 1) = lin->coeff(0, 1, 1);
85  }
86 
88  void offsetParams(const double *delta) { transfo->offsetParams(delta); }
89 
91  unsigned getIndex() const { return index; }
92 
94  void setIndex(unsigned i) { index = i; }
95 
96  virtual void computeTransformAndDerivatives(const FatPoint &where, FatPoint &outPoint,
97  Eigen::MatrixX2d &H) const {
98  transformPosAndErrors(where, outPoint);
99  transfo->paramDerivatives(where, &H(0, 0), &H(0, 1));
100  }
101 
103  virtual const Gtransfo &getTransfo() const { return *transfo; }
104 };
105 
108  /* to better condition the 2nd derivative matrix, the
109  transformed coordinates are mapped (roughly) on [-1,1].
110  We need both the transform and its derivative. */
111  GtransfoLin _centerAndScale;
112  Eigen::Matrix2d preDer;
113 
114  /* Where we store the combination. */
115  mutable GtransfoPoly actualResult;
116 
117 public:
119 
120  // ! contructor.
123  SimplePolyMapping(const GtransfoLin &CenterAndScale, const GtransfoPoly &gtransfo)
124  : SimpleGtransfoMapping(gtransfo), _centerAndScale(CenterAndScale) {
125  // We assume that the initialization was done properly, for example that
126  // gtransfo = pix2TP*CenterAndScale.invert(), so we do not touch transfo.
127  /* store the (spatial) derivative of _centerAndScale. For the extra
128  diagonal terms, just copied the ones in positionDerivatives */
129  preDer(0, 0) = _centerAndScale.coeff(1, 0, 0);
130  preDer(1, 0) = _centerAndScale.coeff(0, 1, 0);
131  preDer(0, 1) = _centerAndScale.coeff(1, 0, 1);
132  preDer(1, 1) = _centerAndScale.coeff(0, 1, 1);
133 
134  // check of matrix indexing (once for all)
135  MatrixX2d H(3, 2);
136  assert((&H(1, 0) - &H(0, 0)) == 1);
137  }
138 
139  /* The SimpleGtransfoMapping version does not account for the
140  _centerAndScale transfo */
141 
142  void positionDerivative(const Point &where, Eigen::Matrix2d &derivative, double epsilon) const {
143  Point tmp = _centerAndScale.apply(where);
144  errorProp->computeDerivative(tmp, *lin, epsilon);
145  derivative(0, 0) = lin->coeff(1, 0, 0);
146  //
147  /* This does not work : it was proved by rotating the frame
148  see the compilation switch ROTATE_T2 in constrainedpolymodel.cc
149  derivative(1,0) = lin->coeff(1,0,1);
150  derivative(0,1) = lin->coeff(0,1,0);
151  */
152  derivative(1, 0) = lin->coeff(0, 1, 0);
153  derivative(0, 1) = lin->coeff(1, 0, 1);
154  derivative(1, 1) = lin->coeff(0, 1, 1);
155  derivative = preDer * derivative;
156  }
157 
159  /* We should put the computation of error propagation and
160  parameter derivatives into the same Gtransfo routine because
161  it could be significantly faster */
162  virtual void computeTransformAndDerivatives(const FatPoint &where, FatPoint &outPoint,
163  Eigen::MatrixX2d &H) const {
164  FatPoint mid;
165  _centerAndScale.transformPosAndErrors(where, mid);
166  transfo->transformPosAndErrors(mid, outPoint);
167  FatPoint tmp;
168  errorProp->transformPosAndErrors(mid, tmp);
169  outPoint.vx = tmp.vx;
170  outPoint.vy = tmp.vy;
171  outPoint.vxy = tmp.vxy;
172  transfo->paramDerivatives(mid, &H(0, 0), &H(0, 1));
173  }
174 
176  void transformPosAndErrors(const FatPoint &where, FatPoint &outPoint) const {
177  FatPoint mid;
178  _centerAndScale.transformPosAndErrors(where, mid);
179  transfo->transformPosAndErrors(mid, outPoint);
180  FatPoint tmp;
181  errorProp->transformPosAndErrors(mid, tmp);
182  outPoint.vx = tmp.vx;
183  outPoint.vy = tmp.vy;
184  outPoint.vxy = tmp.vxy;
185  }
186 
188  const Gtransfo &getTransfo() const {
189  // Cannot fail given the contructor:
190  const GtransfoPoly *fittedPoly = dynamic_cast<const GtransfoPoly *>(&(*transfo));
191  actualResult = (*fittedPoly) * _centerAndScale;
192  return actualResult;
193  }
194 };
195 
196 #ifdef STORAGE
197 
199 class SimpleIdentityMapping : public SimpleGtransfoMapping<GtransfoIdentity> {
200 public:
202  virtual void computeTransformAndDerivatives(const FatPoint &where, FatPoint &outPoint,
203  Eigen::MatrixX2d &H) const {
204  outPoint = where;
205  }
206 };
207 #endif
208 } // namespace jointcal
209 } // namespace lsst
210 
211 #endif // LSST_JOINTCAL_SIMPLE_POLY_MAPPING_H
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:288
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
a mix of apply and Derivative
Definition: Gtransfo.cc:567
void transformPosAndErrors(const FatPoint &where, FatPoint &outPoint) const
The same as above but without the parameter derivatives (used to evaluate chi^2)
virtual class needed in the abstraction of the distortion model
Definition: Mapping.h:15
A point in a plane.
Definition: Point.h:13
unsigned getIndex() const
position of the parameters within the grand fitting scheme
double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const
access to coefficients (read only)
Definition: Gtransfo.cc:651
virtual void computeTransformAndDerivatives(const FatPoint &where, FatPoint &outPoint, Eigen::MatrixX2d &H) const
Actually applies the mapping and evaluates the derivatives w.r.t the fitted parameters.
Mapping implementation for a polynomial transformation.
void setMappingIndices(std::vector< unsigned > &indices) const
Sets how this set of parameters (of length Npar()) map into the &quot;grand&quot; fit Expects that indices has ...
void transformPosAndErrors(const FatPoint &where, FatPoint &outPoint) const
Implements as well the centering and scaling of coordinates.
Polynomial transformation class.
Definition: Gtransfo.h:187
unsigned getNpar() const
Number of parameters in total.
void offsetParams(const double *delta)
Remember the error scale and freeze it.
A Point with uncertainties.
Definition: FatPoint.h:11
virtual const Gtransfo & getTransfo() const
Access to the (fitted) transfo.
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
Definition: Gtransfo.cc:486
SimplePolyMapping(const GtransfoLin &CenterAndScale, const GtransfoPoly &gtransfo)
The transformation will be initialized to gtransfo, so that the effective transformation reads gtrans...
virtual void computeTransformAndDerivatives(const FatPoint &where, FatPoint &outPoint, Eigen::MatrixX2d &H) const
Calls the transforms and implements the centering and scaling of coordinates.
const Gtransfo & getTransfo() const
Access to the (fitted) transfo.
Eigen::Matrix< double, Eigen::Dynamic, 2 > MatrixX2d
Definition: Eigenstuff.h:6
void positionDerivative(const Point &where, Eigen::Matrix2d &derivative, double epsilon) const
The derivative w.r.t. position.
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:37
void positionDerivative(const Point &where, Eigen::Matrix2d &derivative, double epsilon) const
The derivative w.r.t. position.
SimpleGtransfoMapping(const Gtransfo &gtransfo, bool toFit=true)
std::unique_ptr< GtransfoLin > lin
std::shared_ptr< Gtransfo > errorProp
std::shared_ptr< Gtransfo > transfo