lsst.jointcal  14.0-23-g746c5c5+4
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 <memory>
7 #include <string>
8 #include <sstream>
9 #include <vector>
10 
11 #include "Eigen/Core"
12 
13 #include "lsst/pex/exceptions.h"
14 #include "lsst/afw/geom/SkyWcs.h"
15 #include "lsst/jointcal/FatPoint.h"
16 
18 
19 namespace lsst {
20 namespace jointcal {
21 
22 class StarMatchList;
23 class Frame;
24 class GtransfoLin;
25 
27 
41 class Gtransfo {
42 public:
44  virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const = 0;
45 
47  void apply(const Point &in, Point &out) const { apply(in.x, in.y, out.x, out.y); }
48 
51  Point apply(const Point &in) const {
52  double xout, yout;
53  apply(in.x, in.y, xout, yout);
54  return Point(xout, yout);
55  }
56 
58  virtual void dump(std::ostream &stream = std::cout) const = 0;
59 
62  dump(s);
63  return s.str();
64  }
65 
67 
71  virtual double fit(const StarMatchList &starMatchList) = 0;
72 
74  void transformStar(FatPoint &in) const { transformPosAndErrors(in, in); }
75 
77  virtual double getJacobian(const Point &point) const { return getJacobian(point.x, point.y); }
78 
80  virtual std::unique_ptr<Gtransfo> clone() const = 0;
81 
85 
87  virtual double getJacobian(const double x, const double y) const;
88 
94  virtual void computeDerivative(const Point &where, GtransfoLin &derivative,
95  const double step = 0.01) const;
96 
98  virtual GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
99 
100  virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
101 
103  virtual void transformErrors(const Point &where, const double *vIn, double *vOut) const;
104 
106 
108  virtual std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
109 
111  void getParams(double *params) const;
112 
114  void offsetParams(Eigen::VectorXd const &delta);
115 
117  virtual double paramRef(const int i) const;
118 
120  virtual double &paramRef(const int i);
121 
124  virtual void paramDerivatives(const Point &where, double *dx, double *dy) const;
125 
127 
129  virtual std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
130 
132  virtual int getNpar() const { return 0; }
133 
134  void write(const std::string &fileName) const;
135 
136  virtual void write(std::ostream &stream) const;
137 
138  virtual ~Gtransfo(){};
139 };
140 
143 
146 
148 
149 /*=============================================================*/
151 
152 class GtransfoIdentity : public Gtransfo {
153 public:
156 
158  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const {
159  xOut = xIn;
160  yOut = yIn;
161  }; // to speed up
162 
163  double fit(const StarMatchList &starMatchList) {
164  throw pexExcept::TypeError(
165  "GtransfoIdentity is the identity transformation: it cannot be fit to anything.");
166  }
167 
168  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const { return right->clone(); }
169  void dump(std::ostream &stream = std::cout) const { stream << "x' = x\ny' = y" << std::endl; }
170 
171  int getNpar() const { return 0; }
173 
174  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
175 
177  virtual GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
178 
179  void write(std::ostream &s) const;
180 
181  void read(std::istream &s);
182 
183  // ClassDef(GtransfoIdentity,1)
184 };
185 
187 bool isIdentity(const Gtransfo *gtransfo);
188 
190 bool isIntegerShift(const Gtransfo *gtransfo);
191 
192 /*==================== GtransfoPoly =======================*/
193 
195 class GtransfoPoly : public Gtransfo {
196 public:
199  GtransfoPoly(const unsigned degree = 1);
200 
202  GtransfoPoly(const Gtransfo *gtransfo, const Frame &frame, unsigned degree, unsigned nPoint = 1000);
203 
204  // sets the polynomial degree.
205  void setDegree(const unsigned degree);
206 
207  using Gtransfo::apply; // to unhide Gtransfo::apply(const Point &)
208 
209  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
210 
212  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
213 
215  virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
216 
218  unsigned getDegree() const { return _degree; }
219 
221  int getNpar() const { return 2 * _nterms; }
222 
224  void dump(std::ostream &stream = std::cout) const;
225 
227  double fit(const StarMatchList &starMatchList);
228 
230  GtransfoPoly operator*(const GtransfoPoly &right) const;
231 
233  GtransfoPoly operator+(const GtransfoPoly &right) const;
234 
236  GtransfoPoly operator-(const GtransfoPoly &right) const;
237 
238  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
239 
241 
243  double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
244 
246  double &coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord);
247 
249  double coeffOrZero(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
250 
251  double determinant() const;
252 
254  double paramRef(const int i) const;
255 
257  double &paramRef(const int i);
258 
261  void paramDerivatives(const Point &where, double *dx, double *dy) const;
262 
263  void write(std::ostream &s) const;
264  void read(std::istream &s);
265 
266 private:
267  double computeFit(const StarMatchList &starMatchList, const Gtransfo &InTransfo, const bool UseErrors);
268 
269  unsigned _degree; // the degree
270  unsigned _nterms; // number of parameters per coordinate
271  std::vector<double> _coeffs; // the actual coefficients
272  // both polynomials in a single vector to speed up allocation and copies
273 
274  /* use std::vector rather than double * to avoid
275  writing copy constructor and "operator =".
276  Vect would work as well but introduces a dependence
277  that can be avoided */
278 
279  /* This routine take a double * for the vector because the array can
280  then be allocated on the execution stack, which speeds thing
281  up. However this uses Variable Length Array (VLA) which is not
282  part of C++, but gcc implements it. */
283  void computeMonomials(double xIn, double yIn, double *monomial) const;
284 };
285 
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 
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 
407 class GtransfoSkyWcs : public Gtransfo {
408 public:
410 
411  using Gtransfo::apply;
412 
413  // Input is x, y pixels; output is ICRS RA, Dec in degrees
414  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const override;
415 
416  void dump(std::ostream &stream = std::cout) const override;
417 
419  double fit(const StarMatchList &starMatchList) override;
420 
421  std::unique_ptr<Gtransfo> clone() const override;
422 
423  std::shared_ptr<afw::geom::SkyWcs> getSkyWcs() const { return _skyWcs; }
424 
425 private:
427 };
428 
429 /*==================WCS's transfo's =====================================*/
430 
431 class BaseTanWcs : public Gtransfo {
432 public:
433  using Gtransfo::apply; // to unhide apply(const Point&)
434 
435  BaseTanWcs(const GtransfoLin &pix2Tan, const Point &tangentPoint,
436  const GtransfoPoly *corrections = nullptr);
437 
438  BaseTanWcs(const BaseTanWcs &original);
439 
440  void operator=(const BaseTanWcs &original);
441 
443  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
444 
446  Point getTangentPoint() const;
447 
449  GtransfoLin getLinPart() const;
450 
452  const GtransfoPoly *getCorr() const { return corr.get(); }
453 
455  void setCorrections(std::unique_ptr<GtransfoPoly> corrections);
456 
458  Point getCrPix() const;
459 
462  virtual GtransfoPoly getPix2TangentPlane() const = 0;
463 
465  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const = 0;
466 
467  ~BaseTanWcs();
468 
469 protected:
470  GtransfoLin linPix2Tan; // transform from pixels to tangent plane (degrees)
471  // a linear approximation centered at the pixel and sky origins
473  double ra0, dec0; // sky origin (radians)
474  double cos0, sin0; // cos(dec0), sin(dec0)
475 };
476 
477 class TanRaDec2Pix; // the inverse of TanPix2RaDec.
478 
480 class TanPix2RaDec : public BaseTanWcs {
481 public:
482  using Gtransfo::apply; // to unhide apply(const Point&)
485  TanPix2RaDec(const GtransfoLin &pix2Tan, const Point &tangentPoint,
486  const GtransfoPoly *corrections = nullptr);
487 
489  GtransfoPoly getPix2TangentPlane() const;
490 
492  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const;
493 
494  TanPix2RaDec();
495 
497  TanPix2RaDec operator*(const GtransfoLin &right) const;
498 
499  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
500 
502  TanRaDec2Pix invert() const;
503 
505  std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
506 
509  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
510 
512 
513  void dump(std::ostream &stream) const;
514 
516  double fit(const StarMatchList &starMatchList);
517 };
518 
520 class TanSipPix2RaDec : public BaseTanWcs {
521 public:
524  TanSipPix2RaDec(const GtransfoLin &pix2Tan, const Point &tangentPoint,
525  const GtransfoPoly *corrections = nullptr);
526 
528  GtransfoPoly getPix2TangentPlane() const;
529 
531  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const;
532 
533  TanSipPix2RaDec();
534 
537  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
538 
540 
541  void dump(std::ostream &stream) const;
542 
544  double fit(const StarMatchList &starMatchList);
545 };
546 
548 
554 class TanRaDec2Pix : public Gtransfo {
555 public:
556  using Gtransfo::apply; // to unhide apply(const Point&)
557 
559  TanRaDec2Pix(const GtransfoLin &tan2Pix, const Point &tangentPoint);
560 
562  TanRaDec2Pix();
563 
565  GtransfoLin getLinPart() const;
566 
568  void setTangentPoint(const Point &tangentPoint);
569 
571  Point getTangentPoint() const;
572 
574  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
575 
577  void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
578 
580  TanPix2RaDec invert() const;
581 
583  std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
584 
586  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
587 
588  void dump(std::ostream &stream) const;
589 
591 
592  double fit(const StarMatchList &starMatchList);
593 
594 private:
595  double ra0, dec0; // tangent point (radians)
596  double cos0, sin0;
597  GtransfoLin linTan2Pix; // tangent plane (probably degrees) to pixels
598 };
599 
601 typedef void(GtransfoFun)(const double, const double, double &, double &, const void *);
602 
604 class UserTransfo : public Gtransfo {
605 public:
606  using Gtransfo::apply; // to unhide apply(const Point&)
607 
609  UserTransfo(GtransfoFun &fun, const void *userData);
610 
611  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
612 
613  void dump(std::ostream &stream = std::cout) const;
614 
615  double fit(const StarMatchList &starMatchList);
616 
618 
619 private:
620  GtransfoFun *_userFun;
621  const void *_userData;
622 };
623 
628 } // namespace jointcal
629 } // namespace lsst
630 
631 #endif // LSST_JOINTCAL_GTRANSFO_H
virtual std::unique_ptr< Gtransfo > roughInverse(const Frame &region) const
Rough inverse.
Definition: Gtransfo.cc:150
Implements the (forward) SIP distorsion scheme.
Definition: Gtransfo.h:520
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:294
A Gtransfo that holds a SkyWcs.
Definition: Gtransfo.h:407
void getParams(double *params) const
params should be at least Npar() long
Definition: Gtransfo.cc:170
std::unique_ptr< Gtransfo > gtransfoCompose(const Gtransfo *left, const Gtransfo *right)
Returns a pointer to a composition.
Definition: Gtransfo.cc:368
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
Definition: Gtransfo.cc:486
std::ostream & operator<<(std::ostream &out, CcdImageKey const &key)
Definition: CcdImage.cc:30
void transformStar(FatPoint &in) const
allows to write MyTransfo(MyStar)
Definition: Gtransfo.h:74
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:175
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:601
the transformation that handles pix to sideral transfos (Gnomonic, possibly with polynomial distortio...
Definition: Gtransfo.h:480
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:132
std::unique_ptr< Gtransfo > clone() const
returns a copy (allocated by new) of the transformation.
Definition: Gtransfo.h:172
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
xOut = xIn; yOut = yIn !
Definition: Gtransfo.h:158
GtransfoLinScale(const double scale=1)
Definition: Gtransfo.h:389
def scale(algorithm, min, max=None, frame=None)
T endl(T... args)
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:30
T right(T... args)
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:199
constexpr Angle operator+(Angle a, Angle d) noexcept
Polynomial transformation class.
Definition: Gtransfo.h:195
GtransfoLin normalizeCoordinatesTransfo(const Frame &frame)
Returns the transformation that maps the input frame along both axes to [-1,1].
Definition: Gtransfo.cc:725
STL class.
A Point with uncertainties.
Definition: FatPoint.h:11
int const step
STL class.
int getNpar() const
returns the number of parameters (to compute chi2&#39;s)
Definition: Gtransfo.h:171
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:189
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:34
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:1013
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:117
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:76
rectangle with sides parallel to axes.
Definition: Frame.h:19
Class for a simple mapping implementing a generic Gtransfo.
virtual double getJacobian(const Point &point) const
returns the local jacobian.
Definition: Gtransfo.h:77
int getNpar() const
total number of parameters
Definition: Gtransfo.h:366
GtransfoIdentity()
constructor.
Definition: Gtransfo.h:155
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:168
std::shared_ptr< afw::geom::SkyWcs > getSkyWcs() const
Definition: Gtransfo.h:423
T str(T... args)
std::string __str__()
Definition: Gtransfo.h:60
virtual double paramRef(const int i) const
Definition: Gtransfo.cc:180
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
Definition: Gtransfo.cc:100
const GtransfoPoly * getCorr() const
Get a non-owning pointer to the correction transform polynomial.
Definition: Gtransfo.h:452
unsigned getDegree() const
returns degree
Definition: Gtransfo.h:218
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:51
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
Definition: Gtransfo.h:152
void dump(std::ostream &stream=std::cout) const
dumps the transfo coefficients to stream.
Definition: Gtransfo.h:169
virtual GtransfoLin linearApproximation(const Point &where, const double step=0.01) const
linear (local) approximation.
Definition: Gtransfo.cc:93
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:53
void apply(const Point &in, Point &out) const
applies the tranfo to in and writes into out. Is indeed virtual.
Definition: Gtransfo.h:47
STL class.
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
Definition: Gtransfo.h:554
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:163
constexpr Angle operator*(Angle a, Angle d) noexcept
std::unique_ptr< Gtransfo > clone() const
returns a copy (allocated by new) of the transformation.
Definition: Gtransfo.h:240
constexpr Angle operator-(Angle a, Angle d) noexcept
just here to provide specialized constructors. GtransfoLin fit routine.
Definition: Gtransfo.h:385
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:41
double x
std::unique_ptr< GtransfoPoly > corr
Definition: Gtransfo.h:472
a run-time transfo that allows users to define a Gtransfo with minimal coding (just the transfo routi...
Definition: Gtransfo.h:604
std::unique_ptr< Gtransfo > gtransfoRead(const std::string &fileName)
The virtual constructor from a file.
Definition: Gtransfo.cc:1608
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
STL class.
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:252
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:221