lsst.jointcal  master-ga8493ae4fe+1
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 "lsst/pex/exceptions.h"
11 #include "lsst/jointcal/FatPoint.h"
12 
13 namespace pexExcept = lsst::pex::exceptions;
14 
15 namespace lsst {
16 namespace jointcal {
17 
18 class StarMatchList;
19 class Frame;
20 class GtransfoLin;
21 
23 
37 class Gtransfo {
38 public:
40  virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const = 0;
41 
43  void apply(const Point &in, Point &out) const { apply(in.x, in.y, out.x, out.y); }
44 
47  Point apply(const Point &in) const {
48  double xout, yout;
49  apply(in.x, in.y, xout, yout);
50  return Point(xout, yout);
51  }
52 
54  virtual void dump(std::ostream &stream = std::cout) const = 0;
55 
56  std::string __str__() {
57  std::stringstream s;
58  dump(s);
59  return s.str();
60  }
61 
63 
67  virtual double fit(const StarMatchList &starMatchList) = 0;
68 
70  void transformStar(FatPoint &in) const { transformPosAndErrors(in, in); }
71 
73  virtual double getJacobian(const Point &point) const { return getJacobian(point.x, point.y); }
74 
76  virtual std::unique_ptr<Gtransfo> clone() const = 0;
77 
80  virtual std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
81 
83  virtual double getJacobian(const double x, const double y) const;
84 
90  virtual void computeDerivative(const Point &where, GtransfoLin &derivative,
91  const double step = 0.01) const;
92 
94  virtual GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
95 
96  virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
97 
99  virtual void transformErrors(const Point &where, const double *vIn, double *vOut) const;
100 
102 
104  virtual std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
105 
107  void getParams(double *params) const;
108 
110  void offsetParams(const double *params);
111 
113  virtual double paramRef(const int i) const;
114 
116  virtual double &paramRef(const int i);
117 
120  virtual void paramDerivatives(const Point &where, double *dx, double *dy) const;
121 
123 
125  virtual std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
126 
128  virtual int getNpar() const { return 0; }
129 
130  void write(const std::string &fileName) const;
131 
132  virtual void write(std::ostream &stream) const;
133 
134  virtual ~Gtransfo(){};
135 };
136 
138 std::ostream &operator<<(std::ostream &stream, const Gtransfo &gtransfo);
139 
142 
143 std::unique_ptr<Gtransfo> gtransfoCompose(const Gtransfo *left, const Gtransfo *right);
144 
145 /*=============================================================*/
147 
148 class GtransfoIdentity : public Gtransfo {
149 public:
152 
154  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const {
155  xOut = xIn;
156  yOut = yIn;
157  }; // to speed up
158 
159  double fit(const StarMatchList &starMatchList) {
160  throw pexExcept::TypeError(
161  "GtransfoIdentity is the identity transformation: it cannot be fit to anything.");
162  }
163 
164  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const { return right->clone(); }
165  void dump(std::ostream &stream = std::cout) const { stream << "x' = x\ny' = y" << std::endl; }
166 
167  int getNpar() const { return 0; }
168  std::unique_ptr<Gtransfo> clone() const { return std::unique_ptr<Gtransfo>(new GtransfoIdentity); }
169 
170  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
171 
173  virtual GtransfoLin linearApproximation(const Point &where, const double step = 0.01) const;
174 
175  void write(std::ostream &s) const;
176 
177  void read(std::istream &s);
178 
179  // ClassDef(GtransfoIdentity,1)
180 };
181 
183 bool isIdentity(const Gtransfo *gtransfo);
184 
186 bool isIntegerShift(const Gtransfo *gtransfo);
187 
188 /*==================== GtransfoPoly =======================*/
189 
191 class GtransfoPoly : public Gtransfo {
192 public:
193  using Gtransfo::apply; // to unhide Gtransfo::apply(const Point &)
194 
195 private:
196  unsigned _degree; // the degree
197  unsigned _nterms; // number of parameters per coordinate
198  std::vector<double> _coeffs; // the actual coefficients
199  // both polynomials in a single vector to speed up allocation and copies
200 
201  /* use std::vector rather than double * to avoid
202  writing copy constructor and "operator =".
203  Vect would work as well but introduces a dependence
204  that can be avoided */
205 
206  /* This routine take a double * for the vector because the array can
207  then be allocated on the execution stack, which speeds thing
208  up. However this uses Variable Length Array (VLA) which is not
209  part of C++, but gcc implements it. */
210  void computeMonomials(double xIn, double yIn, double *monomial) const;
211 
212 public:
215  GtransfoPoly(const unsigned degree = 1);
216 
218  GtransfoPoly(const Gtransfo *gtransfo, const Frame &frame, unsigned degree, unsigned nPoint = 1000);
219 
220  // sets the polynomial degree.
221  void setDegree(const unsigned degree);
222 
223  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
224 
226  void computeDerivative(const Point &where, GtransfoLin &derivative, const double step = 0.01) const;
227 
229  virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
230 
232  unsigned getDegree() const { return _degree; }
233 
235  int getNpar() const { return 2 * _nterms; }
236 
238  void dump(std::ostream &stream = std::cout) const;
239 
241  double fit(const StarMatchList &starMatchList);
242 
244  GtransfoPoly operator*(const GtransfoPoly &right) const;
245 
247  GtransfoPoly operator+(const GtransfoPoly &right) const;
248 
250  GtransfoPoly operator-(const GtransfoPoly &right) const;
251 
252  std::unique_ptr<Gtransfo> reduceCompo(const Gtransfo *right) const;
253 
254  std::unique_ptr<Gtransfo> clone() const { return std::unique_ptr<Gtransfo>(new GtransfoPoly(*this)); }
255 
257  double coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
258 
260  double &coeff(const unsigned powX, const unsigned powY, const unsigned whichCoord);
261 
263  double coeffOrZero(const unsigned powX, const unsigned powY, const unsigned whichCoord) const;
264 
265  double determinant() const;
266 
268  double paramRef(const int i) const;
269 
271  double &paramRef(const int i);
272 
275  void paramDerivatives(const Point &where, double *dx, double *dy) const;
276 
277  void write(std::ostream &s) const;
278  void read(std::istream &s);
279 
280 private:
281  double computeFit(const StarMatchList &starMatchList, const Gtransfo &InTransfo, const bool UseErrors);
282 };
283 
285 std::unique_ptr<GtransfoPoly> inversePolyTransfo(const Gtransfo &Direct, const Frame &frame,
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 
326  std::unique_ptr<Gtransfo> clone() const { return std::unique_ptr<Gtransfo>(new GtransfoLin(*this)); }
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 protected:
399  GtransfoLin linPix2Tan; // pixels to tangent plane (internally in radians)
400  std::unique_ptr<GtransfoPoly> corr;
401  double ra0, dec0; // in radians
402  double cos0, sin0; // cos(dec0), sin(dec0)
403 
404 public:
405  using Gtransfo::apply; // to unhide apply(const Point&)
406 
407  BaseTanWcs(const GtransfoLin &pix2Tan, const Point &tangentPoint,
408  const GtransfoPoly *corrections = nullptr);
409 
410  BaseTanWcs(const BaseTanWcs &original);
411 
412  void operator=(const BaseTanWcs &original);
413 
414  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
415 
417  Point getTangentPoint() const;
418 
420  GtransfoLin getLinPart() const;
421 
423  const GtransfoPoly *getCorr() const { return corr.get(); }
424 
426  void setCorrections(std::unique_ptr<GtransfoPoly> corrections);
427 
429  Point getCrPix() const;
430 
432  virtual GtransfoPoly getPix2TangentPlane() const = 0;
433 
435  virtual void pix2TP(double xPixel, double yPixel, double &xTangentPlane, double &yTangentPlane) const = 0;
436 
437  ~BaseTanWcs();
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 
474  std::unique_ptr<Gtransfo> clone() const;
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 
502  std::unique_ptr<Gtransfo> clone() const;
503 
504  void dump(std::ostream &stream) const;
505 
507  double fit(const StarMatchList &starMatchList);
508 };
509 
511 
517 class TanRaDec2Pix : public Gtransfo {
518  double ra0, dec0; // tangent point (internally in radians)
519  double cos0, sin0;
520  GtransfoLin linTan2Pix; // tangent plane to pixels (internally in radians)
521 
522 public:
523  using Gtransfo::apply; // to unhide apply(const Point&)
524 
526  TanRaDec2Pix(const GtransfoLin &tan2Pix, const Point &tangentPoint);
527 
529  TanRaDec2Pix();
530 
532  GtransfoLin getLinPart() const;
533 
535  void setTangentPoint(const Point &tangentPoint);
536 
538  Point getTangentPoint() const;
539 
541  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
542 
544  void transformPosAndErrors(const FatPoint &in, FatPoint &out) const;
545 
547  TanPix2RaDec invert() const;
548 
550  std::unique_ptr<Gtransfo> roughInverse(const Frame &region) const;
551 
553  std::unique_ptr<Gtransfo> inverseTransfo(const double precision, const Frame &region) const;
554 
555  void dump(std::ostream &stream) const;
556 
557  std::unique_ptr<Gtransfo> clone() const;
558 
559  double fit(const StarMatchList &starMatchList);
560 };
561 
563 typedef void(GtransfoFun)(const double, const double, double &, double &, const void *);
564 
566 class UserTransfo : public Gtransfo {
567 private:
568  GtransfoFun *_userFun;
569  const void *_userData;
570 
571 public:
572  using Gtransfo::apply; // to unhide apply(const Point&)
573 
575  UserTransfo(GtransfoFun &fun, const void *userData);
576 
577  void apply(const double xIn, const double yIn, double &xOut, double &yOut) const;
578 
579  void dump(std::ostream &stream = std::cout) const;
580 
581  double fit(const StarMatchList &starMatchList);
582 
583  std::unique_ptr<Gtransfo> clone() const;
584 };
585 
587 std::unique_ptr<Gtransfo> gtransfoRead(const std::string &fileName);
589 std::unique_ptr<Gtransfo> gtransfoRead(std::istream &s);
590 } // namespace jointcal
591 } // namespace lsst
592 
593 #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: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:70
A point in a plane.
Definition: Point.h:13
GtransfoLin(const GtransfoIdentity &)
Handy converter:
Definition: Gtransfo.h:324
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:563
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:128
std::unique_ptr< Gtransfo > clone() const
returns a copy (allocated by new) of the transformation.
Definition: Gtransfo.h:168
void apply(const double xIn, const double yIn, double &xOut, double &yOut) const
xOut = xIn; yOut = yIn !
Definition: Gtransfo.h:154
GtransfoLinScale(const double scale=1)
Definition: Gtransfo.h:387
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:27
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:191
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:167
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.
Definition: Associations.h:24
virtual double getJacobian(const Point &point) const
returns the local jacobian.
Definition: Gtransfo.h:73
int getNpar() const
total number of parameters
Definition: Gtransfo.h:364
GtransfoIdentity()
constructor.
Definition: Gtransfo.h:151
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:164
std::string __str__()
Definition: Gtransfo.h:56
virtual double paramRef(const int i) const
Definition: Gtransfo.cc:179
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
Definition: Gtransfo.cc:99
void offsetParams(const double *params)
Definition: Gtransfo.cc:174
const GtransfoPoly * getCorr() const
the "correction" (non-owning pointer)
Definition: Gtransfo.h:423
unsigned getDegree() const
returns degree
Definition: Gtransfo.h:232
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:47
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
Definition: Gtransfo.h:148
void dump(std::ostream &stream=std::cout) const
dumps the transfo coefficients to stream.
Definition: Gtransfo.h:165
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:43
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:159
std::unique_ptr< Gtransfo > clone() const
returns a copy (allocated by new) of the transformation.
Definition: Gtransfo.h:254
just here to provide specialized constructors. GtransfoLin fit routine.
Definition: Gtransfo.h:383
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:37
std::unique_ptr< GtransfoPoly > corr
Definition: Gtransfo.h:400
a run-time transfo that allows users to define a Gtransfo with minimal coding (just the transfo routi...
Definition: Gtransfo.h:566
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
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:235