lsst.astshim  master-gd1207e8857
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Mapping.h
1 /*
2  * LSST Data Management System
3  * Copyright 2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 #ifndef ASTSHIM_MAPPING_H
23 #define ASTSHIM_MAPPING_H
24 
25 #include <memory>
26 #include <vector>
27 
28 #include "ndarray.h"
29 
30 #include "astshim/base.h"
31 #include "astshim/detail/utils.h"
32 #include "astshim/Object.h"
33 
34 namespace ast {
35 
36 class ParallelMap;
37 class SeriesMap;
38 
59 class Mapping : public Object {
60 friend class Object;
61 public:
62 
63  virtual ~Mapping() {}
64 
65  Mapping(Mapping const &) = delete;
66  Mapping(Mapping &&) = default;
67  Mapping & operator=(Mapping const &) = delete;
68  Mapping & operator=(Mapping &&) = default;
69 
71  std::shared_ptr<Mapping> copy() const { return std::static_pointer_cast<Mapping>(_copyPolymorphic()); }
72 
76  int getNin() const { return getI("Nin"); }
77 
81  int getNout() const { return getI("Nout"); }
82 
86  bool getIsSimple() const { return getI("IsSimple"); }
87 
94  bool isInverted() const { return getB("Invert"); }
95 
99  bool getIsLinear() const { return getB("IsLinear"); }
100 
104  bool getReport() const { return getB("Report"); }
105 
113  bool hasForward() const { return getB("TranForward"); }
114 
122  bool hasInverse() const { return getB("TranInverse"); }
123 
137  std::shared_ptr<Mapping> getInverse() const;
138 
163  Array2D linearApprox(
164  PointD const & lbnd,
165  PointD const & ubnd,
166  double tol
167  ) const;
168 
177  SeriesMap of(Mapping const & first) const;
178 
189  ParallelMap over(Mapping const & first) const;
190 
209  double rate(
210  PointD const & at,
211  int ax1,
212  int ax2
213  ) const {
214  detail::assertEqual(at.size(), "at.size", static_cast<std::size_t>(getNin()), "nIn");
215  double result = astRate(getRawPtr(), const_cast<double *>(at.data()), ax1, ax2);
216  assertOK();
217  return result;
218  }
219 
223  void setReport(bool report) { setB("Report", report); }
224 
246  std::shared_ptr<Mapping> simplify() const {
247  AstObject * rawSimpMap = reinterpret_cast<AstObject *>(astSimplify(getRawPtr()));
248  assertOK(rawSimpMap);
249  return Object::fromAstObject<Mapping>(rawSimpMap, true);
250  }
251 
259  ConstArray2D const & from,
260  Array2D const & to
261  ) const {
262  _tran(from, true, to);
263  }
264 
271  Array2D tranForward(
272  ConstArray2D const & from
273  ) const {
274  Array2D to = ndarray::allocate(from.getSize<0>(), getNout());
275  _tran(from, true, to);
276  return to;
277  }
278 
285  std::vector<double> tranForward(
286  std::vector<double> const & from
287  ) const {
288  auto fromArr = arrayFromVector(from, getNin());
289  std::vector<double> to(fromArr.getSize<0>() * getNout());
290  auto toArr = arrayFromVector(to, getNout());
291  _tran(fromArr, true, toArr);
292  return to;
293  }
294 
302  ConstArray2D const & from,
303  Array2D const & to
304  ) const {
305  _tran(from, false, to);
306  }
307 
314  Array2D tranInverse(
315  ConstArray2D const & from
316  ) const {
317  Array2D to = ndarray::allocate(from.getSize<0>(), getNin());
318  _tran(from, false, to);
319  return to;
320  }
321 
328  std::vector<double> tranInverse(
329  std::vector<double> const & from
330  ) const {
331  auto fromArr = arrayFromVector(from, getNout());
332  std::vector<double> to(fromArr.getSize<0>() * getNin());
333  auto toArr = arrayFromVector(to, getNin());
334  _tran(fromArr, false, toArr);
335  return to;
336  }
337 
375  PointI const & lbnd,
376  PointI const & ubnd,
377  double tol,
378  int maxpix,
379  Array2D const & to
380  ) const {
381  _tranGrid(lbnd, ubnd, tol, maxpix, true, to);
382  }
383 
391  PointI const & lbnd,
392  PointI const & ubnd,
393  double tol,
394  int maxpix,
395  int nPts
396  ) const {
397  Array2D to = ndarray::allocate(nPts, getNout());
398  _tranGrid(lbnd, ubnd, tol, maxpix, true, to);
399  return to;
400  }
401 
408  PointI const & lbnd,
409  PointI const & ubnd,
410  double tol,
411  int maxpix,
412  Array2D const & to
413  ) const {
414  _tranGrid(lbnd, ubnd, tol, maxpix, false, to);
415  }
416 
423  PointI const & lbnd,
424  PointI const & ubnd,
425  double tol,
426  int maxpix,
427  int nPts
428  ) const {
429  Array2D to = ndarray::allocate(nPts, getNin());
430  _tranGrid(lbnd, ubnd, tol, maxpix, false, to);
431  return to;
432  }
433 
434 protected:
438  explicit Mapping(AstMapping * rawMap) :
439  Object(reinterpret_cast<AstObject *>(rawMap))
440  {
441  assertOK();
442  if (!astIsAMapping(getRawPtr())) {
443  std::ostringstream os;
444  os << "this is a " << getClass() << ", which is not a Mapping";
445  throw std::invalid_argument(os.str());
446  }
447  }
448 
449  // Protected implementation of deep-copy.
450  virtual std::shared_ptr<Object> _copyPolymorphic() const override {
451  return std::static_pointer_cast<Mapping>(_copyImpl<Mapping, AstMapping>());
452  }
453 
467  template<typename Class>
468  std::shared_ptr<Class> _decompose(int i, bool copy) const;
469 
470 private:
478  void _tran(
479  ConstArray2D const & from,
480  bool doForward,
481  Array2D const & to
482  ) const;
483 
487  void _tranGrid(
488  PointI const & lbnd,
489  PointI const & ubnd,
490  double tol,
491  int maxpix,
492  bool doForward,
493  Array2D const & to
494  ) const;
495 };
496 
497 } // namespace ast
498 
499 #endif
Array2D tranInverse(ConstArray2D const &from) const
Definition: Mapping.h:314
std::shared_ptr< Mapping > getInverse() const
Definition: Mapping.cc:45
void tranInverse(ConstArray2D const &from, Array2D const &to) const
Definition: Mapping.h:301
std::vector< double > tranInverse(std::vector< double > const &from) const
Definition: Mapping.h:328
void tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, Array2D const &to) const
Definition: Mapping.h:407
ParallelMap over(Mapping const &first) const
Definition: Mapping.cc:41
void setReport(bool report)
Definition: Mapping.h:223
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:53
std::shared_ptr< Class > _decompose(int i, bool copy) const
Definition: Mapping.cc:72
Definition: ParallelMap.h:50
bool hasInverse() const
Definition: Mapping.h:122
std::string getClass() const
Definition: Object.h:117
void tranForward(ConstArray2D const &from, Array2D const &to) const
Definition: Mapping.h:258
Mapping(AstMapping *rawMap)
Definition: Mapping.h:438
bool isInverted() const
Definition: Mapping.h:94
void setB(std::string const &attrib, bool value)
Definition: Object.h:435
Definition: Mapping.h:59
bool getReport() const
Definition: Mapping.h:104
Definition: Object.h:49
Array2D tranForward(ConstArray2D const &from) const
Definition: Mapping.h:271
double rate(PointD const &at, int ax1, int ax2) const
Definition: Mapping.h:209
int getNout() const
Definition: Mapping.h:81
int getNin() const
Definition: Mapping.h:76
bool hasForward() const
Definition: Mapping.h:113
Definition: SeriesMap.h:50
std::shared_ptr< Mapping > copy() const
Return a deep copy of this object.
Definition: Mapping.h:71
std::vector< double > tranForward(std::vector< double > const &from) const
Definition: Mapping.h:285
bool getIsLinear() const
Definition: Mapping.h:99
bool getIsSimple() const
Definition: Mapping.h:86
ConstArray2D arrayFromVector(std::vector< double > const &vec, int nAxes)
Definition: base.cc:69
Array2D tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const
Definition: Mapping.h:390
virtual std::shared_ptr< Object > _copyPolymorphic() const override
Definition: Mapping.h:450
int getI(std::string const &attrib) const
Definition: Object.h:386
bool getB(std::string const &attrib) const
Definition: Object.h:334
Array2D tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const
Definition: Mapping.h:422
std::shared_ptr< Mapping > simplify() const
Definition: Mapping.h:246
void tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, Array2D const &to) const
Definition: Mapping.h:374
SeriesMap of(Mapping const &first) const
Definition: Mapping.cc:37
Array2D linearApprox(PointD const &lbnd, PointD const &ubnd, double tol) const
Definition: Mapping.cc:53
AstObject const * getRawPtr() const
Definition: Object.h:260