lsst.astshim  master-gf6b1fd7af3+5
Mapping.h
1 /*
2  * LSST Data Management System
3  * Copyright 2017 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 
62 public:
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(PointD const &lbnd, PointD const &ubnd, double tol) const;
164 
173  SeriesMap then(Mapping const &next) const;
174 
188  ParallelMap under(Mapping const &next) const;
189 
208  double rate(PointD const &at, int ax1, int ax2) const {
209  detail::assertEqual(at.size(), "at.size", static_cast<std::size_t>(getNIn()), "nIn");
210  double result = astRate(getRawPtr(), const_cast<double *>(at.data()), ax1, ax2);
211  assertOK();
212  return result;
213  }
214 
218  void setReport(bool report) { setB("Report", report); }
219 
241  std::shared_ptr<Mapping> simplify() const {
242  AstObject *rawSimpMap = reinterpret_cast<AstObject *>(astSimplify(getRawPtr()));
243  assertOK(rawSimpMap);
244  return Object::fromAstObject<Mapping>(rawSimpMap, true);
245  }
246 
253  void applyForward(ConstArray2D const &from, Array2D const &to) const { _tran(from, true, to); }
254 
261  Array2D applyForward(ConstArray2D const &from) const {
262  Array2D to = ndarray::allocate(getNOut(), from.getSize<1>());
263  _tran(from, true, to);
264  return to;
265  }
266 
273  std::vector<double> applyForward(std::vector<double> const &from) const {
274  auto fromArr = arrayFromVector(from, getNIn());
275  std::vector<double> to(fromArr.getSize<1>() * getNOut());
276  auto toArr = arrayFromVector(to, getNOut());
277  _tran(fromArr, true, toArr);
278  return to;
279  }
280 
287  void applyInverse(ConstArray2D const &from, Array2D const &to) const { _tran(from, false, to); }
288 
295  Array2D applyInverse(ConstArray2D const &from) const {
296  Array2D to = ndarray::allocate(getNIn(), from.getSize<1>());
297  _tran(from, false, to);
298  return to;
299  }
300 
307  std::vector<double> applyInverse(std::vector<double> const &from) const {
308  auto fromArr = arrayFromVector(from, getNOut());
309  std::vector<double> to(fromArr.getSize<1>() * getNIn());
310  auto toArr = arrayFromVector(to, getNIn());
311  _tran(fromArr, false, toArr);
312  return to;
313  }
314 
351  void tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix,
352  Array2D const &to) const {
353  _tranGrid(lbnd, ubnd, tol, maxpix, true, to);
354  }
355 
362  Array2D tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const {
363  Array2D to = ndarray::allocate(nPts, getNOut());
364  _tranGrid(lbnd, ubnd, tol, maxpix, true, to);
365  return to;
366  }
367 
373  void tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix,
374  Array2D const &to) const {
375  _tranGrid(lbnd, ubnd, tol, maxpix, false, to);
376  }
377 
383  Array2D tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const {
384  Array2D to = ndarray::allocate(nPts, getNIn());
385  _tranGrid(lbnd, ubnd, tol, maxpix, false, to);
386  return to;
387  }
388 
389 protected:
393  explicit Mapping(AstMapping *rawMap) : Object(reinterpret_cast<AstObject *>(rawMap)) {
394  assertOK();
395  if (!astIsAMapping(getRawPtr())) {
396  std::ostringstream os;
397  os << "this is a " << getClassName() << ", which is not a Mapping";
398  throw std::invalid_argument(os.str());
399  }
400  }
401 
402  // Protected implementation of deep-copy.
403  virtual std::shared_ptr<Object> copyPolymorphic() const override {
404  return std::static_pointer_cast<Mapping>(copyImpl<Mapping, AstMapping>());
405  }
406 
420  template <typename Class>
421  std::shared_ptr<Class> decompose(int i, bool copy) const;
422 
423 private:
431  void _tran(ConstArray2D const &from, bool doForward, Array2D const &to) const;
432 
436  void _tranGrid(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, bool doForward,
437  Array2D const &to) const;
438 };
439 
440 } // namespace ast
441 
442 #endif
Definition: SeriesMap.h:50
std::vector< double > applyInverse(std::vector< double > const &from) const
Definition: Mapping.h:307
double rate(PointD const &at, int ax1, int ax2) const
Definition: Mapping.h:208
ndarray::Array< double const, 2, 2 > ConstArray2D
Definition: base.h:46
virtual std::shared_ptr< Object > copyPolymorphic() const override
Definition: Mapping.h:403
AstObject const * getRawPtr() const
Definition: Object.h:286
void setB(std::string const &attrib, bool value)
Definition: Object.h:448
Array2D tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const
Definition: Mapping.h:362
int getI(std::string const &attrib) const
Definition: Object.h:399
bool hasInverse() const
Definition: Mapping.h:122
ndarray::Array< double, 2, 2 > Array2D
Definition: base.h:42
AST wrapper classes and functions.
Definition: attributes_channel.dox:1
std::string getClassName() const
Definition: Object.h:133
void tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, Array2D const &to) const
Definition: Mapping.h:351
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:49
SeriesMap then(Mapping const &next) const
Definition: Mapping.cc:37
std::shared_ptr< Mapping > simplify() const
Definition: Mapping.h:241
std::shared_ptr< Class > decompose(int i, bool copy) const
Definition: Mapping.cc:64
bool isInverted() const
Definition: Mapping.h:94
Definition: Mapping.h:59
std::vector< int > PointI
Definition: base.h:50
void tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, Array2D const &to) const
Definition: Mapping.h:373
Mapping(AstMapping *rawMap)
Definition: Mapping.h:393
std::vector< double > applyForward(std::vector< double > const &from) const
Definition: Mapping.h:273
Definition: ParallelMap.h:50
bool getB(std::string const &attrib) const
Definition: Object.h:347
bool getReport() const
Definition: Mapping.h:104
void applyForward(ConstArray2D const &from, Array2D const &to) const
Definition: Mapping.h:253
std::shared_ptr< Mapping > getInverse() const
Definition: Mapping.cc:41
std::shared_ptr< Mapping > copy() const
Return a deep copy of this object.
Definition: Mapping.h:71
bool getIsLinear() const
Definition: Mapping.h:99
Array2D linearApprox(PointD const &lbnd, PointD const &ubnd, double tol) const
Definition: Mapping.cc:49
std::vector< double > PointD
Definition: base.h:57
ParallelMap under(Mapping const &next) const
Definition: Mapping.cc:39
void setReport(bool report)
Definition: Mapping.h:218
void applyInverse(ConstArray2D const &from, Array2D const &to) const
Definition: Mapping.h:287
int getNIn() const
Definition: Mapping.h:76
bool getIsSimple() const
Definition: Mapping.h:86
Array2D applyInverse(ConstArray2D const &from) const
Definition: Mapping.h:295
ConstArray2D arrayFromVector(std::vector< double > const &vec, int nAxes)
Definition: base.cc:65
bool hasForward() const
Definition: Mapping.h:113
Array2D tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const
Definition: Mapping.h:383
Definition: Object.h:49
int getNOut() const
Definition: Mapping.h:81
Array2D applyForward(ConstArray2D const &from) const
Definition: Mapping.h:261