gastro  0.1.0
 All Classes Files Functions Variables Pages
simplepolymapping.h
1 #ifndef SIMPLEPOLYMAPPING__H
2 #define SIMPLEPOLYMAPPING__H
3 
4 #include <memory> // for auto_ptr
5 
6 #include "mapping.h"
7 #include "gtransfo.h"
8 #include "ccdimage.h"
9 
11 
15 {
16  protected:
17  bool toFit;
18  unsigned index;
19  /* inheritance may also work. Perhaps with some trouble because
20  some routines in Mapping and Gtransfo have the same name */
21  CountedRef<Gtransfo> transfo;
22  /* to avoid allocation at every call of PosDerivatives.
23  use a pointer for constness */
24  std::auto_ptr<GtransfoLin> lin;
25 
26 #ifdef STORAGE
27  static_assert(std::is_base_of<Gtransfo, Tr>::value,
29  "SimpleGtransfoMapping requires a class deriving from Gtransfo");
30 #endif
31 
32 
33  public :
34 
35  SimpleGtransfoMapping(const Gtransfo &T, bool ToFit=true) : toFit(ToFit)
36  {
37  transfo = T.Clone();
38  lin.reset(new GtransfoLin); // reserve space for derivative computation
39  }
40 
41  // interface Mapping functions:
42 
44  unsigned Npar() const
45  {
46  if (toFit) return transfo->Npar();
47  else return 0;
48  }
49 
51  void GetMappingIndices(std::vector<unsigned> &Indices) const
52  {
53  if (Indices.size() < Npar()) Indices.resize(Npar());
54  for (unsigned k=0; k<Npar(); ++k) Indices[k] = index+k;
55  }
56 
58  void TransformPosAndErrors(const FatPoint &Where,
59  FatPoint &OutPos) const
60  {
61  transfo->TransformPosAndErrors(Where,OutPos);
62  }
63 
65  void PosDerivative(Point &Where, Eigen::Matrix2d &Der, const double & Eps) const
66  {
67  transfo->Derivative(Where, *lin, Eps);
68  Der(0,0) = lin->Coeff(1,0,0);
69  //
70  /* This does not work : it was proved by rotating the frame
71  see the compilation switch ROTATE_T2 in constrainedpolymodel.cc
72  Der(1,0) = lin->Coeff(1,0,1);
73  Der(0,1) = lin->Coeff(0,1,0);
74  */
75  Der(1,0) = lin->Coeff(0,1,0);
76  Der(0,1) = lin->Coeff(1,0,1);
77  Der(1,1) = lin->Coeff(0,1,1);
78  }
79 
81  void OffsetParams(const double *Delta)
82  {
83  transfo->OffsetParams(Delta);
84  }
85 
87  unsigned Index() const { return index;}
88 
90  void SetIndex(unsigned I) {index=I;}
91 
92  virtual void ComputeTransformAndDerivatives(const FatPoint &Where,
93  FatPoint &OutPos,
94  Eigen::MatrixX2d &H) const
95  {
96  TransformPosAndErrors(Where,OutPos);
97  transfo->ParamDerivatives(Where, &H(0,0), &H(0,1));
98  }
99 
101  const Gtransfo& Transfo() const {return *transfo;}
102 
103 };
104 
105 
106 class SimplePolyMapping : public SimpleGtransfoMapping
107 {
108  // to better condition the 2nd derivative matrix
109  Point center;
110  // could consider scaling as well.
111 
112  public:
113 
114  // ! contructor
115  SimplePolyMapping(const Point& Center, const GtransfoPoly& P):
116  SimpleGtransfoMapping(P), center(Center)
117  {
118  /* We work in centered coordinates: the routines below center
119  coordinates so that the provided initialization has to be
120  shifted the other way around: */
121  GtransfoLinShift shift(center.x, center.y);
122  transfo = new GtransfoPoly(P*shift);
123  // check of matrix indexing (once for all)
124  MatrixX2d H(3,2);
125  assert((&H(1,0) - &H(0,0)) == 1);
126  }
127 
129  /* We should put the computation of error propagation and
130  parameter derivatives into the same Gtransfo routine because
131  it could be significantly faster */
132  virtual void ComputeTransformAndDerivatives(const FatPoint &Where,
133  FatPoint &OutPos,
134  Eigen::MatrixX2d &H) const
135  {
136  TransformPosAndErrors(Where,OutPos);
137  transfo->ParamDerivatives(Where-center, &H(0,0), &H(0,1));
138  }
139 
141  void TransformPosAndErrors(const FatPoint &Where,
142  FatPoint &OutPos) const
143  {
144  FatPoint where = Where;
145  where.x -= center.x;
146  where.y -= center.y;
147  transfo->TransformPosAndErrors(where,OutPos);
148  }
149 
150 
151 
152 
153 };
154 
155 
156 #ifdef STORAGE
157 
159 class SimpleIdentityMapping : public SimpleGtransfoMapping<GtransfoIdentity>
160 {
161  public:
162 
164  virtual void ComputeTransformAndDerivatives(const FatPoint &Where,
165  FatPoint &OutPos,
166  Eigen::MatrixX2d &H) const
167  {
168  OutPos = Where;
169  }
170 
171 };
172 #endif
173 
174 
175 #endif
virtual class needed in the abstraction of the distortion model
Definition: mapping.h:13
void PosDerivative(Point &Where, Eigen::Matrix2d &Der, const double &Eps) const
The derivative w.r.t. position.
Definition: simplepolymapping.h:65
virtual void ComputeTransformAndDerivatives(const FatPoint &Where, FatPoint &OutPos, Eigen::MatrixX2d &H) const
Actually applies the mapping and evaluates the derivatives w.r.t the fitted parameters.
Definition: simplepolymapping.h:92
unsigned Index() const
position of the parameters within the grand fitting scheme
Definition: simplepolymapping.h:87
Class for a simple mapping implementing a generic Gtransfo.
Definition: simplepolymapping.h:14
void TransformPosAndErrors(const FatPoint &Where, FatPoint &OutPos) const
The same as above but without the parameter derivatives (used to evaluate chi^2)
Definition: simplepolymapping.h:58
void GetMappingIndices(std::vector< unsigned > &Indices) const
Provides for this parameter set (of length Npar()) how they map into the "grand" fit.
Definition: simplepolymapping.h:51
const Gtransfo & Transfo() const
Access to the (fitted) transfo.
Definition: simplepolymapping.h:101
unsigned Npar() const
Mumber of parameters in total.
Definition: simplepolymapping.h:44