lsst.jointcal  14.0-14-g932474c+8
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 
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 
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 
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 
141 
144 
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; }
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:
197  GtransfoPoly(const unsigned degree = 1);
198 
200  GtransfoPoly(const Gtransfo *gtransfo, const Frame &frame, unsigned degree, unsigned nPoint = 1000);
201 
202  // sets the polynomial degree.
203  void setDegree(const unsigned degree);
204 
205  using Gtransfo::apply; // to unhide Gtransfo::apply(const Point &)
206 
207  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
208 
210  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
211 
213  virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
214 
216  unsigned getDegree() const { return _degree; }
217 
219  int getNpar() const { return 2 * _nterms; }
220 
222  void dump(std::ostream &stream = std::cout) const;
223 
225  double fit(const StarMatchList &starMatchList);
226 
228  GtransfoPoly operator*(const GtransfoPoly &right) const;
229 
231  GtransfoPoly operator+(const GtransfoPoly &right) const;
232 
234  GtransfoPoly operator-(const GtransfoPoly &right) const;
235 
236  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
237 
239 
241  double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
242 
244  double &coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord);
245 
247  double coeffOrZero(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
248 
249  double determinant() const;
250 
252  double paramRef(const int i) const;
253 
255  double &paramRef(const int i);
256 
259  void paramDerivatives(const Point &where, double *dx, double *dy) const;
260 
261  void write(std::ostream &s) const;
262  void read(std::istream &s);
263 
264 private:
265  double computeFit(const StarMatchList &starMatchList, const Gtransfo &InTransfo, const bool UseErrors);
266 
267  unsigned _degree; // the degree
268  unsigned _nterms; // number of parameters per coordinate
269  std::vector<double> _coeffs; // the actual coefficients
270  // both polynomials in a single vector to speed up allocation and copies
271 
272  /* use std::vector rather than double * to avoid
273  writing copy constructor and "operator =".
274  Vect would work as well but introduces a dependence
275  that can be avoided */
276 
277  /* This routine take a double * for the vector because the array can
278  then be allocated on the execution stack, which speeds thing
279  up. However this uses Variable Length Array (VLA) which is not
280  part of C++, but gcc implements it. */
281  void computeMonomials(double xIn, double yIn, double *monomial) const;
282 };
283 
286  const double Prec);
287 
289 
290 /*=============================================================*/
292 class GtransfoLin : public GtransfoPoly {
293 public:
294  using GtransfoPoly::apply; // to unhide Gtransfo::apply(const Point &)
295 
298 
300  explicit GtransfoLin(const GtransfoPoly &gtransfoPoly);
301 
303  GtransfoLin operator*(const GtransfoLin &right) const;
304 
306  GtransfoLin invert() const;
307 
308  // useful? double jacobian(const double x, const double y) const { return determinant();}
309 
311  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
313  GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
314 
315  // void dump(std::ostream &stream = std::cout) const;
316 
317  // double fit(const StarMatchList &starMatchList);
318 
320  GtransfoLin(const double ox, const double oy, const double aa11, const double aa12, const double aa21,
321  const double aa22);
322 
325 
327 
328  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
329 
330  double A11() const { return coeff(1, 0, 0); }
331  double A12() const { return coeff(0, 1, 0); }
332  double A21() const { return coeff(1, 0, 1); }
333  double A22() const { return coeff(0, 1, 1); }
334  double Dx() const { return coeff(0, 0, 0); }
335  double Dy() const { return coeff(0, 0, 1); }
336 
337 protected:
338  double &a11() { return coeff(1, 0, 0); }
339  double &a12() { return coeff(0, 1, 0); }
340  double &a21() { return coeff(1, 0, 1); }
341  double &a22() { return coeff(0, 1, 1); }
342  double &dx() { return coeff(0, 0, 0); }
343  double &dy() { return coeff(0, 0, 1); }
344 
345  friend class Gtransfo;
346  friend class GtransfoIdentity; // for Gtransfo::Derivative
347  friend class GtransfoPoly; // // for Gtransfo::Derivative
348 
349 private:
350  void setDegree(const unsigned degree); // to hide GtransfoPoly::setDegree
351 };
352 
353 /*=============================================================*/
354 
357 public:
358  using Gtransfo::apply; // to unhide Gtransfo::apply(const Point &)
360  GtransfoLinShift(double ox = 0., double oy = 0.) : GtransfoLin(ox, oy, 1., 0., 0., 1.) {}
361  GtransfoLinShift(const Point &point) : GtransfoLin(point.x, point.y, 1., 0., 0., 1.){};
362  double fit(const StarMatchList &starMatchList);
363 
364  int getNpar() const { return 2; }
365 };
366 
367 /*=============================================================*/
369 class GtransfoLinRot : public GtransfoLin {
370 public:
371  using Gtransfo::apply; // to unhide apply(const Point&)
372 
374  GtransfoLinRot(const double angleRad, const Point *center = nullptr, const double scaleFactor = 1.0);
375  double fit(const StarMatchList &starMatchList);
376 
377  int getNpar() const { return 4; }
378 };
379 
380 /*=============================================================*/
381 
384 public:
385  using Gtransfo::apply; // to unhide apply(const Point&)
387  GtransfoLinScale(const double scale = 1) : GtransfoLin(0.0, 0.0, scale, 0., 0., scale){};
389  GtransfoLinScale(const double scaleX, const double scaleY)
390  : GtransfoLin(0.0, 0.0, scaleX, 0., 0., scaleY){};
391 
392  int getNpar() const { return 2; }
393 };
394 
395 /*==================WCS's transfo's =====================================*/
396 
397 class BaseTanWcs : public Gtransfo {
398 public:
399  using Gtransfo::apply; // to unhide apply(const Point&)
400 
401  BaseTanWcs(const GtransfoLin &pix2Tan, const Point &tangentPoint,
402  const GtransfoPoly *corrections = nullptr);
403 
404  BaseTanWcs(const BaseTanWcs &original);
405 
406  void operator=(const BaseTanWcs &original);
407 
408  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
409 
411  Point getTangentPoint() const;
412 
414  GtransfoLin getLinPart() const;
415 
417  const GtransfoPoly *getCorr() const { return corr.get(); }
418 
420  void setCorrections(std::unique_ptr<GtransfoPoly> corrections);
421 
423  Point getCrPix() const;
424 
426  virtual GtransfoPoly getPix2TangentPlane() const = 0;
427 
429  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const = 0;
430 
431  ~BaseTanWcs();
432 
433 protected:
434  GtransfoLin linPix2Tan; // pixels to tangent plane (internally in radians)
436  double ra0, dec0; // in radians
437  double cos0, sin0; // cos(dec0), sin(dec0)
438 };
439 
440 class TanRaDec2Pix; // the inverse of TanPix2RaDec.
441 
443 class TanPix2RaDec : public BaseTanWcs {
444 public:
445  using Gtransfo::apply; // to unhide apply(const Point&)
448  TanPix2RaDec(const GtransfoLin &pix2Tan, const Point &tangentPoint,
449  const GtransfoPoly *corrections = nullptr);
450 
452  GtransfoPoly getPix2TangentPlane() const;
453 
455  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const;
456 
457  TanPix2RaDec();
458 
460  TanPix2RaDec operator*(const GtransfoLin &right) const;
461 
462  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
463 
465  TanRaDec2Pix invert() const;
466 
468  std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
469 
472  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
473 
475 
476  void dump(std::ostream &stream) const;
477 
479  double fit(const StarMatchList &starMatchList);
480 };
481 
483 class TanSipPix2RaDec : public BaseTanWcs {
484 public:
487  TanSipPix2RaDec(const GtransfoLin &pix2Tan, const Point &tangentPoint,
488  const GtransfoPoly *corrections = nullptr);
489 
491  GtransfoPoly getPix2TangentPlane() const;
492 
494  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const;
495 
496  TanSipPix2RaDec();
497 
500  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
501 
503 
504  void dump(std::ostream &stream) const;
505 
507  double fit(const StarMatchList &starMatchList);
508 };
509 
511 
517 class TanRaDec2Pix : public Gtransfo {
518 public:
519  using Gtransfo::apply; // to unhide apply(const Point&)
520 
522  TanRaDec2Pix(const GtransfoLin &tan2Pix, const Point &tangentPoint);
523 
525  TanRaDec2Pix();
526 
528  GtransfoLin getLinPart() const;
529 
531  void setTangentPoint(const Point &tangentPoint);
532 
534  Point getTangentPoint() const;
535 
537  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
538 
540  void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
541 
543  TanPix2RaDec invert() const;
544 
546  std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
547 
549  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
550 
551  void dump(std::ostream &stream) const;
552 
554 
555  double fit(const StarMatchList &starMatchList);
556 
557 private:
558  double ra0, dec0; // tangent point (internally in radians)
559  double cos0, sin0;
560  GtransfoLin linTan2Pix; // tangent plane to pixels (internally in radians)
561 };
562 
564 typedef void(GtransfoFun)(const double, const double, double &, double &, const void *);
565 
567 class UserTransfo : public Gtransfo {
568 public:
569  using Gtransfo::apply; // to unhide apply(const Point&)
570 
572  UserTransfo(GtransfoFun &fun, const void *userData);
573 
574  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
575 
576  void dump(std::ostream &stream = std::cout) const;
577 
578  double fit(const StarMatchList &starMatchList);
579 
581 
582 private:
583  GtransfoFun *_userFun;
584  const void *_userData;
585 };
586 
591 } // namespace jointcal
592 } // namespace lsst
593 
594 #endif // LSST_JOINTCAL_GTRANSFO_H
int y
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:483
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:292
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:324
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:564
the transformation that handles pix to sideral transfos (Gnomonic, possibly with polynomial distortio...
Definition: Gtransfo.h:443
GtransfoLinShift(double ox=0., double oy=0.)
Add ox and oy.
Definition: Gtransfo.h:360
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:387
def scale(algorithm, min, max=None, frame=None)
T endl(T... args)
GtransfoLinShift(const Point &point)
Definition: Gtransfo.h:361
bool isIdentity(const Gtransfo *gtransfo)
Shorthand test to tell if a transfo belongs to the GtransfoIdentity class.
Definition: Gtransfo.cc:29
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:198
constexpr Angle operator+(Angle a, Angle d) noexcept
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
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: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:297
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:326
just here to provide a specialized constructor, and fit.
Definition: Gtransfo.h:369
GtransfoLinScale(const double scaleX, const double scaleY)
Definition: Gtransfo.h:389
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.
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:364
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
T str(T... args)
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:417
unsigned getDegree() const
returns degree
Definition: Gtransfo.h:216
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:356
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
STL class.
This one is the Tangent Plane (called gnomonic) projection (from celestial sphere to tangent plane) ...
Definition: Gtransfo.h:517
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
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:238
constexpr Angle operator-(Angle a, Angle d) noexcept
just here to provide specialized constructors. GtransfoLin fit routine.
Definition: Gtransfo.h:383
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:39
double x
std::unique_ptr< GtransfoPoly > corr
Definition: Gtransfo.h:435
a run-time transfo that allows users to define a Gtransfo with minimal coding (just the transfo routi...
Definition: Gtransfo.h:567
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:377
STL class.
int getNpar() const
total number of parameters
Definition: Gtransfo.h:392
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:219