lsst.astshim  master-ga73c468035
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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 of(Mapping const &first) const;
174 
185  ParallelMap over(Mapping const &first) const;
186 
205  double rate(PointD const &at, int ax1, int ax2) const {
206  detail::assertEqual(at.size(), "at.size", static_cast<std::size_t>(getNin()), "nIn");
207  double result = astRate(getRawPtr(), const_cast<double *>(at.data()), ax1, ax2);
208  assertOK();
209  return result;
210  }
211 
215  void setReport(bool report) { setB("Report", report); }
216 
238  std::shared_ptr<Mapping> simplify() const {
239  AstObject *rawSimpMap = reinterpret_cast<AstObject *>(astSimplify(getRawPtr()));
240  assertOK(rawSimpMap);
241  return Object::fromAstObject<Mapping>(rawSimpMap, true);
242  }
243 
250  void tranForward(ConstArray2D const &from, Array2D const &to) const { _tran(from, true, to); }
251 
258  Array2D tranForward(ConstArray2D const &from) const {
259  Array2D to = ndarray::allocate(getNout(), from.getSize<1>());
260  _tran(from, true, to);
261  return to;
262  }
263 
270  std::vector<double> tranForward(std::vector<double> const &from) const {
271  auto fromArr = arrayFromVector(from, getNin());
272  std::vector<double> to(fromArr.getSize<1>() * getNout());
273  auto toArr = arrayFromVector(to, getNout());
274  _tran(fromArr, true, toArr);
275  return to;
276  }
277 
284  void tranInverse(ConstArray2D const &from, Array2D const &to) const { _tran(from, false, to); }
285 
292  Array2D tranInverse(ConstArray2D const &from) const {
293  Array2D to = ndarray::allocate(getNin(), from.getSize<1>());
294  _tran(from, false, to);
295  return to;
296  }
297 
304  std::vector<double> tranInverse(std::vector<double> const &from) const {
305  auto fromArr = arrayFromVector(from, getNout());
306  std::vector<double> to(fromArr.getSize<1>() * getNin());
307  auto toArr = arrayFromVector(to, getNin());
308  _tran(fromArr, false, toArr);
309  return to;
310  }
311 
348  void tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix,
349  Array2D const &to) const {
350  _tranGrid(lbnd, ubnd, tol, maxpix, true, to);
351  }
352 
359  Array2D tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const {
360  Array2D to = ndarray::allocate(nPts, getNout());
361  _tranGrid(lbnd, ubnd, tol, maxpix, true, to);
362  return to;
363  }
364 
370  void tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix,
371  Array2D const &to) const {
372  _tranGrid(lbnd, ubnd, tol, maxpix, false, to);
373  }
374 
380  Array2D tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const {
381  Array2D to = ndarray::allocate(nPts, getNin());
382  _tranGrid(lbnd, ubnd, tol, maxpix, false, to);
383  return to;
384  }
385 
386 protected:
390  explicit Mapping(AstMapping *rawMap) : Object(reinterpret_cast<AstObject *>(rawMap)) {
391  assertOK();
392  if (!astIsAMapping(getRawPtr())) {
393  std::ostringstream os;
394  os << "this is a " << getClass() << ", which is not a Mapping";
395  throw std::invalid_argument(os.str());
396  }
397  }
398 
399  // Protected implementation of deep-copy.
400  virtual std::shared_ptr<Object> copyPolymorphic() const override {
401  return std::static_pointer_cast<Mapping>(copyImpl<Mapping, AstMapping>());
402  }
403 
417  template <typename Class>
418  std::shared_ptr<Class> decompose(int i, bool copy) const;
419 
420 private:
428  void _tran(ConstArray2D const &from, bool doForward, Array2D const &to) const;
429 
433  void _tranGrid(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, bool doForward,
434  Array2D const &to) const;
435 };
436 
437 } // namespace ast
438 
439 #endif
Array2D tranInverse(ConstArray2D const &from) const
Definition: Mapping.h:292
std::shared_ptr< Mapping > getInverse() const
Definition: Mapping.cc:41
ndarray::Array< double const, 2, 2 > ConstArray2D
Definition: base.h:46
void tranInverse(ConstArray2D const &from, Array2D const &to) const
Definition: Mapping.h:284
std::vector< double > tranInverse(std::vector< double > const &from) const
Definition: Mapping.h:304
virtual std::shared_ptr< Object > copyPolymorphic() const override
Definition: Mapping.h:400
void tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, Array2D const &to) const
Definition: Mapping.h:370
ParallelMap over(Mapping const &first) const
Definition: Mapping.cc:39
ndarray::Array< double, 2, 2 > Array2D
Definition: base.h:42
void setReport(bool report)
Definition: Mapping.h:215
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:49
Definition: ParallelMap.h:50
bool hasInverse() const
Definition: Mapping.h:122
std::string getClass() const
Definition: Object.h:118
void tranForward(ConstArray2D const &from, Array2D const &to) const
Definition: Mapping.h:250
Mapping(AstMapping *rawMap)
Definition: Mapping.h:390
bool isInverted() const
Definition: Mapping.h:94
void setB(std::string const &attrib, bool value)
Definition: Object.h:423
Definition: Mapping.h:59
bool getReport() const
Definition: Mapping.h:104
Definition: Object.h:49
std::vector< int > PointI
Definition: base.h:50
Array2D tranForward(ConstArray2D const &from) const
Definition: Mapping.h:258
double rate(PointD const &at, int ax1, int ax2) const
Definition: Mapping.h:205
int getNout() const
Definition: Mapping.h:81
int getNin() const
Definition: Mapping.h:76
std::vector< double > PointD
Definition: base.h:57
bool hasForward() const
Definition: Mapping.h:113
Definition: SeriesMap.h:50
std::shared_ptr< Class > decompose(int i, bool copy) const
Definition: Mapping.cc:64
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:270
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:65
Array2D tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const
Definition: Mapping.h:359
int getI(std::string const &attrib) const
Definition: Object.h:374
bool getB(std::string const &attrib) const
Definition: Object.h:322
Array2D tranGridInverse(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, int nPts) const
Definition: Mapping.h:380
std::shared_ptr< Mapping > simplify() const
Definition: Mapping.h:238
void tranGridForward(PointI const &lbnd, PointI const &ubnd, double tol, int maxpix, Array2D const &to) const
Definition: Mapping.h:348
SeriesMap of(Mapping const &first) const
Definition: Mapping.cc:37
Array2D linearApprox(PointD const &lbnd, PointD const &ubnd, double tol) const
Definition: Mapping.cc:49
AstObject const * getRawPtr() const
Definition: Object.h:261