lsst.jointcal  master-gc5b79683b0+2
Gtransfo.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 #ifndef LSST_JOINTCAL_GTRANSFO_H
3 #define LSST_JOINTCAL_GTRANSFO_H
4 
5 #include <iostream>
6 #include <string>
7 #include <sstream>
8 #include <vector>
9 
10 #include "Eigen/Core"
11 
12 #include "lsst/pex/exceptions.h"
13 #include "lsst/jointcal/FatPoint.h"
14 
15 namespace pexExcept = lsst::pex::exceptions;
16 
17 namespace lsst {
18 namespace jointcal {
19 
20 class StarMatchList;
21 class Frame;
22 class GtransfoLin;
23 
25 
39 class Gtransfo {
40 public:
42  virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const = 0;
43 
45  void apply(const Point &in, Point &out) const { apply(in.x, in.y, out.x, out.y); }
46 
49  Point apply(const Point &in) const {
50  double xout, yout;
51  apply(in.x, in.y, xout, yout);
52  return Point(xout, yout);
53  }
54 
56  virtual void dump(std::ostream &stream = std::cout) const = 0;
57 
58  std::string __str__() {
59  std::stringstream s;
60  dump(s);
61  return s.str();
62  }
63 
65 
69  virtual double fit(const StarMatchList &starMatchList) = 0;
70 
72  void transformStar(FatPoint &in) const { transformPosAndErrors(in, in); }
73 
75  virtual double getJacobian(const Point &point) const { return getJacobian(point.x, point.y); }
76 
78  virtual std::unique_ptr<Gtransfo> clone() const = 0;
79 
82  virtual std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
83 
85  virtual double getJacobian(const double x, const double y) const;
86 
92  virtual void computeDerivative(const Point &where, GtransfoLin &derivative,
93  const double step = 0.01) const;
94 
96  virtual GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
97 
98  virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
99 
101  virtual void transformErrors(const Point &where, const double *vIn, double *vOut) const;
102 
104 
106  virtual std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
107 
109  void getParams(double *params) const;
110 
112  void offsetParams(Eigen::VectorXd const &delta);
113 
115  virtual double paramRef(const int i) const;
116 
118  virtual double &paramRef(const int i);
119 
122  virtual void paramDerivatives(const Point &where, double *dx, double *dy) const;
123 
125 
127  virtual std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
128 
130  virtual int getNpar() const { return 0; }
131 
132  void write(const std::string &fileName) const;
133 
134  virtual void write(std::ostream &stream) const;
135 
136  virtual ~Gtransfo(){};
137 };
138 
140 std::ostream &operator<<(std::ostream &stream, const Gtransfo &gtransfo);
141 
144 
145 std::unique_ptr<Gtransfo> gtransfoCompose(const Gtransfo *left, const Gtransfo *right);
146 
147 /*=============================================================*/
149 
150 class GtransfoIdentity : public Gtransfo {
151 public:
154 
156  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const {
157  xOut = xIn;
158  yOut = yIn;
159  }; // to speed up
160 
161  double fit(const StarMatchList &starMatchList) {
162  throw pexExcept::TypeError(
163  "GtransfoIdentity is the identity transformation: it cannot be fit to anything.");
164  }
165 
166  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const { return right->clone(); }
167  void dump(std::ostream &stream = std::cout) const { stream << "x' = x\ny' = y" << std::endl; }
168 
169  int getNpar() const { return 0; }
170  std::unique_ptr<Gtransfo> clone() const { return std::unique_ptr<Gtransfo>(new GtransfoIdentity); }
171 
172  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
173 
175  virtual GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
176 
177  void write(std::ostream &s) const;
178 
179  void read(std::istream &s);
180 
181  // ClassDef(GtransfoIdentity,1)
182 };
183 
185 bool isIdentity(const Gtransfo *gtransfo);
186 
188 bool isIntegerShift(const Gtransfo *gtransfo);
189 
190 /*==================== GtransfoPoly =======================*/
191 
193 class GtransfoPoly : public Gtransfo {
194 public:
195  using Gtransfo::apply; // to unhide Gtransfo::apply(const Point &)
196 
197 private:
198  unsigned _degree; // the degree
199  unsigned _nterms; // number of parameters per coordinate
200  std::vector<double> _coeffs; // the actual coefficients
201  // both polynomials in a single vector to speed up allocation and copies
202 
203  /* use std::vector rather than double * to avoid
204  writing copy constructor and "operator =".
205  Vect would work as well but introduces a dependence
206  that can be avoided */
207 
208  /* This routine take a double * for the vector because the array can
209  then be allocated on the execution stack, which speeds thing
210  up. However this uses Variable Length Array (VLA) which is not
211  part of C++, but gcc implements it. */
212  void computeMonomials(double xIn, double yIn, double *monomial) const;
213 
214 public:
217  GtransfoPoly(const unsigned degree = 1);
218 
220  GtransfoPoly(const Gtransfo *gtransfo, const Frame &frame, unsigned degree, unsigned nPoint = 1000);
221 
222  // sets the polynomial degree.
223  void setDegree(const unsigned degree);
224 
225  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
226 
228  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
229 
231  virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
232 
234  unsigned getDegree() const { return _degree; }
235 
237  int getNpar() const { return 2 * _nterms; }
238 
240  void dump(std::ostream &stream = std::cout) const;
241 
243  double fit(const StarMatchList &starMatchList);
244 
246  GtransfoPoly operator*(const GtransfoPoly &right) const;
247 
249  GtransfoPoly operator+(const GtransfoPoly &right) const;
250 
252  GtransfoPoly operator-(const GtransfoPoly &right) const;
253 
254  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
255 
256  std::unique_ptr<Gtransfo> clone() const { return std::unique_ptr<Gtransfo>(new GtransfoPoly(*this)); }
257 
259  double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
260 
262  double &coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord);
263 
265  double coeffOrZero(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
266 
267  double determinant() const;
268 
270  double paramRef(const int i) const;
271 
273  double &paramRef(const int i);
274 
277  void paramDerivatives(const Point &where, double *dx, double *dy) const;
278 
279  void write(std::ostream &s) const;
280  void read(std::istream &s);
281 
282 private:
283  double computeFit(const StarMatchList &starMatchList, const Gtransfo &InTransfo, const bool UseErrors);
284 };
285 
287 std::unique_ptr<GtransfoPoly> inversePolyTransfo(const Gtransfo &Direct, const Frame &frame,
288  const double Prec);
289 
291 
292 /*=============================================================*/
294 class GtransfoLin : public GtransfoPoly {
295 public:
296  using GtransfoPoly::apply; // to unhide Gtransfo::apply(const Point &)
297 
300 
302  explicit GtransfoLin(const GtransfoPoly &gtransfoPoly);
303 
305  GtransfoLin operator*(const GtransfoLin &right) const;
306 
308  GtransfoLin invert() const;
309 
310  // useful? double jacobian(const double x, const double y) const { return determinant();}
311 
313  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
315  GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
316 
317  // void dump(std::ostream &stream = std::cout) const;
318 
319  // double fit(const StarMatchList &starMatchList);
320 
322  GtransfoLin(const double ox, const double oy, const double aa11, const double aa12, const double aa21,
323  const double aa22);
324 
327 
328  std::unique_ptr<Gtransfo> clone() const { return std::unique_ptr<Gtransfo>(new GtransfoLin(*this)); }
329 
330  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
331 
332  double A11() const { return coeff(1, 0, 0); }
333  double A12() const { return coeff(0, 1, 0); }
334  double A21() const { return coeff(1, 0, 1); }
335  double A22() const { return coeff(0, 1, 1); }
336  double Dx() const { return coeff(0, 0, 0); }
337  double Dy() const { return coeff(0, 0, 1); }
338 
339 protected:
340  double &a11() { return coeff(1, 0, 0); }
341  double &a12() { return coeff(0, 1, 0); }
342  double &a21() { return coeff(1, 0, 1); }
343  double &a22() { return coeff(0, 1, 1); }
344  double &dx() { return coeff(0, 0, 0); }
345  double &dy() { return coeff(0, 0, 1); }
346 
347  friend class Gtransfo;
348  friend class GtransfoIdentity; // for Gtransfo::Derivative
349  friend class GtransfoPoly; // // for Gtransfo::Derivative
350 
351 private:
352  void setDegree(const unsigned degree); // to hide GtransfoPoly::setDegree
353 };
354 
355 /*=============================================================*/
356 
359 public:
360  using Gtransfo::apply; // to unhide Gtransfo::apply(const Point &)
362  GtransfoLinShift(double ox = 0., double oy = 0.) : GtransfoLin(ox, oy, 1., 0., 0., 1.) {}
363  GtransfoLinShift(const Point &point) : GtransfoLin(point.x, point.y, 1., 0., 0., 1.){};
364  double fit(const StarMatchList &starMatchList);
365 
366  int getNpar() const { return 2; }
367 };
368 
369 /*=============================================================*/
371 class GtransfoLinRot : public GtransfoLin {
372 public:
373  using Gtransfo::apply; // to unhide apply(const Point&)
374 
376  GtransfoLinRot(const double angleRad, const Point *center = nullptr, const double scaleFactor = 1.0);
377  double fit(const StarMatchList &starMatchList);
378 
379  int getNpar() const { return 4; }
380 };
381 
382 /*=============================================================*/
383 
386 public:
387  using Gtransfo::apply; // to unhide apply(const Point&)
389  GtransfoLinScale(const double scale = 1) : GtransfoLin(0.0, 0.0, scale, 0., 0., scale){};
391  GtransfoLinScale(const double scaleX, const double scaleY)
392  : GtransfoLin(0.0, 0.0, scaleX, 0., 0., scaleY){};
393 
394  int getNpar() const { return 2; }
395 };
396 
397 /*==================WCS's transfo's =====================================*/
398 
399 class BaseTanWcs : public Gtransfo {
400 protected:
401  GtransfoLin linPix2Tan; // pixels to tangent plane (internally in radians)
402  std::unique_ptr<GtransfoPoly> corr;
403  double ra0, dec0; // in radians
404  double cos0, sin0; // cos(dec0), sin(dec0)
405 
406 public:
407  using Gtransfo::apply; // to unhide apply(const Point&)
408 
409  BaseTanWcs(const GtransfoLin &pix2Tan, const Point &tangentPoint,
410  const GtransfoPoly *corrections = nullptr);
411 
412  BaseTanWcs(const BaseTanWcs &original);
413 
414  void operator=(const BaseTanWcs &original);
415 
416  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
417 
419  Point getTangentPoint() const;
420 
422  GtransfoLin getLinPart() const;
423 
425  const GtransfoPoly *getCorr() const { return corr.get(); }
426 
428  void setCorrections(std::unique_ptr<GtransfoPoly> corrections);
429 
431  Point getCrPix() const;
432 
434  virtual GtransfoPoly getPix2TangentPlane() const = 0;
435 
437  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const = 0;
438 
439  ~BaseTanWcs();
440 };
441 
442 class TanRaDec2Pix; // the inverse of TanPix2RaDec.
443 
445 class TanPix2RaDec : public BaseTanWcs {
446 public:
447  using Gtransfo::apply; // to unhide apply(const Point&)
450  TanPix2RaDec(const GtransfoLin &pix2Tan, const Point &tangentPoint,
451  const GtransfoPoly *corrections = nullptr);
452 
454  GtransfoPoly getPix2TangentPlane() const;
455 
457  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const;
458 
459  TanPix2RaDec();
460 
462  TanPix2RaDec operator*(const GtransfoLin &right) const;
463 
464  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
465 
467  TanRaDec2Pix invert() const;
468 
470  std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
471 
474  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
475 
476  std::unique_ptr<Gtransfo> clone() const;
477 
478  void dump(std::ostream &stream) const;
479 
481  double fit(const StarMatchList &starMatchList);
482 };
483 
485 class TanSipPix2RaDec : public BaseTanWcs {
486 public:
489  TanSipPix2RaDec(const GtransfoLin &pix2Tan, const Point &tangentPoint,
490  const GtransfoPoly *corrections = nullptr);
491 
493  GtransfoPoly getPix2TangentPlane() const;
494 
496  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const;
497 
498  TanSipPix2RaDec();
499 
502  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
503 
504  std::unique_ptr<Gtransfo> clone() const;
505 
506  void dump(std::ostream &stream) const;
507 
509  double fit(const StarMatchList &starMatchList);
510 };
511 
513 
519 class TanRaDec2Pix : public Gtransfo {
520  double ra0, dec0; // tangent point (internally in radians)
521  double cos0, sin0;
522  GtransfoLin linTan2Pix; // tangent plane to pixels (internally in radians)
523 
524 public:
525  using Gtransfo::apply; // to unhide apply(const Point&)
526 
528  TanRaDec2Pix(const GtransfoLin &tan2Pix, const Point &tangentPoint);
529 
531  TanRaDec2Pix();
532 
534  GtransfoLin getLinPart() const;
535 
537  void setTangentPoint(const Point &tangentPoint);
538 
540  Point getTangentPoint() const;
541 
543  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
544 
546  void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
547 
549  TanPix2RaDec invert() const;
550 
552  std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
553 
555  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
556 
557  void dump(std::ostream &stream) const;
558 
559  std::unique_ptr<Gtransfo> clone() const;
560 
561  double fit(const StarMatchList &starMatchList);
562 };
563 
565 typedef void(GtransfoFun)(const double, const double, double &, double &, const void *);
566 
568 class UserTransfo : public Gtransfo {
569 private:
570  GtransfoFun *_userFun;
571  const void *_userData;
572 
573 public:
574  using Gtransfo::apply; // to unhide apply(const Point&)
575 
577  UserTransfo(GtransfoFun &fun, const void *userData);
578 
579  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
580 
581  void dump(std::ostream &stream = std::cout) const;
582 
583  double fit(const StarMatchList &starMatchList);
584 
585  std::unique_ptr<Gtransfo> clone() const;
586 };
587 
589 std::unique_ptr<Gtransfo> gtransfoRead(const std::string &fileName);
591 std::unique_ptr<Gtransfo> gtransfoRead(std::istream &s);
592 } // namespace jointcal
593 } // namespace lsst
594 
595 #endif // LSST_JOINTCAL_GTRANSFO_H
virtual std::unique_ptr< Gtransfo > roughInverse(const Frame &region) const
Rough inverse.
Definition: Gtransfo.cc:149
Implements the (forward) SIP distorsion scheme.
Definition: Gtransfo.h:485
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:294
void getParams(double *params) const
params should be at least Npar() long
Definition: Gtransfo.cc:169
std::unique_ptr< Gtransfo > gtransfoCompose(const Gtransfo *left, const Gtransfo *right)
Returns a pointer to a composition.
Definition: Gtransfo.cc:367
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
Definition: Gtransfo.cc:485
void transformStar(FatPoint &in) const
allows to write MyTransfo(MyStar)
Definition: Gtransfo.h:72
A point in a plane.
Definition: Point.h:13
GtransfoLin(const GtransfoIdentity &)
Handy converter:
Definition: Gtransfo.h:326
void offsetParams(Eigen::VectorXd const &delta)
Definition: Gtransfo.cc:174
void() GtransfoFun(const double, const double, double &, double &, const void *)
signature of the user-provided routine that actually does the coordinate transfo for UserTransfo...
Definition: Gtransfo.h:565
the transformation that handles pix to sideral transfos (Gnomonic, possibly with polynomial distortio...
Definition: Gtransfo.h:445
GtransfoLinShift(double ox=0., double oy=0.)
Add ox and oy.
Definition: Gtransfo.h:362
virtual int getNpar() const
returns the number of parameters (to compute chi2&#39;s)
Definition: Gtransfo.h:130
std::unique_ptr< Gtransfo > clone() const
returns a copy (allocated by new) of the transformation.
Definition: Gtransfo.h:170
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
xOut = xIn; yOut = yIn !
Definition: Gtransfo.h:156
GtransfoLinScale(const double scale=1)
Definition: Gtransfo.h:389
GtransfoLinShift(const Point &point)
Definition: Gtransfo.h:363
bool isIdentity(const Gtransfo *gtransfo)
Shorthand test to tell if a transfo belongs to the GtransfoIdentity class.
Definition: Gtransfo.cc:29
virtual double fit(const StarMatchList &starMatchList)=0
fits a transfo to a std::list of Point pairs (p1,p2, the Point fields in StarMatch).
void write(const std::string &fileName) const
Definition: Gtransfo.cc:198
std::ostream & operator<<(std::ostream &stream, const Gtransfo &gtransfo)
allows &#39;stream << Transfo;&#39; (by calling gtransfo.dump(stream)).
Definition: Gtransfo.cc:193
Polynomial transformation class.
Definition: Gtransfo.h:193
GtransfoLin normalizeCoordinatesTransfo(const Frame &frame)
Returns the transformation that maps the input frame along both axes to [-1,1].
Definition: Gtransfo.cc:724
A Point with uncertainties.
Definition: FatPoint.h:11
int getNpar() const
returns the number of parameters (to compute chi2&#39;s)
Definition: Gtransfo.h:169
double x
coordinate
Definition: Point.h:18
virtual void paramDerivatives(const Point &where, double *dx, double *dy) const
Derivative w.r.t parameters.
Definition: Gtransfo.cc:188
GtransfoLin()
the default constructor constructs the do-nothing transformation.
Definition: Gtransfo.h:299
bool isIntegerShift(const Gtransfo *gtransfo)
Shorthand test to tell if a transfo is a simple integer shift.
Definition: Gtransfo.cc:33
std::unique_ptr< GtransfoPoly > inversePolyTransfo(const Gtransfo &Direct, const Frame &frame, const double Prec)
approximates the inverse by a polynomial, up to required precision.
Definition: Gtransfo.cc:1012
virtual void transformErrors(const Point &where, const double *vIn, double *vOut) const
transform errors (represented as double[3] in order V(xx),V(yy),Cov(xy))
Definition: Gtransfo.cc:116
std::unique_ptr< Gtransfo > clone() const
returns a copy (allocated by new) of the transformation.
Definition: Gtransfo.h:328
just here to provide a specialized constructor, and fit.
Definition: Gtransfo.h:371
GtransfoLinScale(const double scaleX, const double scaleY)
Definition: Gtransfo.h:391
virtual void computeDerivative(const Point &where, GtransfoLin &derivative, const double step=0.01) const
Computes the local Derivative of a transfo, w.r.t.
Definition: Gtransfo.cc:75
rectangle with sides parallel to axes.
Definition: Frame.h:19
Class for a simple mapping implementing a generic Gtransfo.
Definition: Associations.h:24
virtual double getJacobian(const Point &point) const
returns the local jacobian.
Definition: Gtransfo.h:75
int getNpar() const
total number of parameters
Definition: Gtransfo.h:366
GtransfoIdentity()
constructor.
Definition: Gtransfo.h:153
std::unique_ptr< Gtransfo > reduceCompo(const Gtransfo *right) const
to be overloaded by derived classes if they can really "reduce" the composition (e.g.
Definition: Gtransfo.h:166
std::string __str__()
Definition: Gtransfo.h:58
virtual double paramRef(const int i) const
Definition: Gtransfo.cc:179
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
Definition: Gtransfo.cc:99
const GtransfoPoly * getCorr() const
the "correction" (non-owning pointer)
Definition: Gtransfo.h:425
unsigned getDegree() const
returns degree
Definition: Gtransfo.h:234
Point apply(const Point &in) const
All these apply(..) shadow the virtual one in derived classes, unless one writes "using Gtransfo::app...
Definition: Gtransfo.h:49
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
Definition: Gtransfo.h:150
void dump(std::ostream &stream=std::cout) const
dumps the transfo coefficients to stream.
Definition: Gtransfo.h:167
virtual GtransfoLin linearApproximation(const Point &where, const double step=0.01) const
linear (local) approximation.
Definition: Gtransfo.cc:92
just here to provide a specialized constructor, and fit.
Definition: Gtransfo.h:358
virtual std::unique_ptr< Gtransfo > reduceCompo(const Gtransfo *right) const
to be overloaded by derived classes if they can really "reduce" the composition (e.g.
Definition: Gtransfo.cc:52
void apply(const Point &in, Point &out) const
applies the tranfo to in and writes into out. Is indeed virtual.
Definition: Gtransfo.h:45
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
Definition: Gtransfo.h:519
double fit(const StarMatchList &starMatchList)
fits a transfo to a std::list of Point pairs (p1,p2, the Point fields in StarMatch).
Definition: Gtransfo.h:161
std::unique_ptr< Gtransfo > clone() const
returns a copy (allocated by new) of the transformation.
Definition: Gtransfo.h:256
just here to provide specialized constructors. GtransfoLin fit routine.
Definition: Gtransfo.h:385
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:39
double x
std::unique_ptr< GtransfoPoly > corr
Definition: Gtransfo.h:402
a run-time transfo that allows users to define a Gtransfo with minimal coding (just the transfo routi...
Definition: Gtransfo.h:568
std::unique_ptr< Gtransfo > gtransfoRead(const std::string &fileName)
The virtual constructor from a file.
Definition: Gtransfo.cc:1586
virtual std::unique_ptr< Gtransfo > clone() const =0
returns a copy (allocated by new) of the transformation.
virtual void dump(std::ostream &stream=std::cout) const =0
dumps the transfo coefficients to stream.
int getNpar() const
total number of parameters
Definition: Gtransfo.h:379
int getNpar() const
total number of parameters
Definition: Gtransfo.h:394
virtual std::unique_ptr< Gtransfo > inverseTransfo(const double precision, const Frame &region) const
returns an inverse transfo. Numerical if not overloaded.
Definition: Gtransfo.cc:251
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0
int getNpar() const
total number of parameters
Definition: Gtransfo.h:237