lsst.astshim  master-gd1207e8857
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
PolyMap.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_POLYMAP_H
23 #define ASTSHIM_POLYMAP_H
24 
25 #include <algorithm>
26 #include <memory>
27 #include <sstream>
28 #include <stdexcept>
29 #include <vector>
30 
31 #include "astshim/base.h"
32 #include "astshim/Mapping.h"
33 
34 namespace ast {
35 
49 class PolyMap : public Mapping {
50 friend class Object;
51 public:
96  explicit PolyMap(
97  ndarray::Array<double, 2, 2> const & coeff_f,
98  ndarray::Array<double, 2, 2> const & coeff_i,
99  std::string const & options="IterInverse=0"
100  ) :
101  Mapping(reinterpret_cast<AstMapping *>(_makeRawPolyMap(coeff_f, coeff_i, options)))
102  {}
103 
125  explicit PolyMap(
126  ndarray::Array<double, 2, 2> const & coeff_f,
127  int nout,
128  std::string const & options="IterInverse=0"
129  ) :
130  Mapping(reinterpret_cast<AstMapping *>(_makeRawPolyMap(coeff_f, nout, options)))
131  {}
132 
133  virtual ~PolyMap() {}
134 
135  PolyMap(PolyMap const &) = delete;
136  PolyMap(PolyMap &&) = default;
137  PolyMap & operator=(PolyMap const &) = delete;
138  PolyMap & operator=(PolyMap &&) = default;
139 
141  std::shared_ptr<PolyMap> copy() const {
142  return std::static_pointer_cast<PolyMap>(_copyPolymorphic());
143  }
144 
146  bool getIterInverse() const { return getB("IterInverse"); }
147 
149  int getNiterInverse() const { return getI("NiterInverse"); }
150 
152  double getTolInverse() const { return getD("TolInverse"); }
153 
229  bool forward,
230  double acc,
231  double maxacc,
232  int maxorder,
233  std::vector<double> const & lbnd,
234  std::vector<double> const & ubnd
235  ) const {
236  auto const desSize = static_cast<unsigned int>(forward ? getNin() : getNout());
237  if (lbnd.size() != desSize) {
238  std::ostringstream os;
239  os << "lbnd.size() = " << lbnd.size() << " != " << desSize
240  << " = " << (forward ? "getNin()" : "getNout()");
241  throw std::invalid_argument(os.str());
242  }
243  if (ubnd.size() != desSize) {
244  std::ostringstream os;
245  os << "ubnd.size() = " << ubnd.size() << " != " << desSize
246  << " = " << (forward ? "getNin()" : "getNout()");
247  throw std::invalid_argument(os.str());
248  }
249 
250  void *map = astPolyTran(this->getRawPtr(), static_cast<int>(forward), acc, maxacc, maxorder,
251  lbnd.data(), ubnd.data());
252  // Failure should result in a null pointer, so calling assertOK is unlikely to do anything,
253  // but better to be sure and than risk missing an uncaught error.
254  assertOK(reinterpret_cast<AstObject *>(map));
255  if (!map) {
256  throw std::runtime_error("Could not compute an inverse mapping");
257  }
258  return PolyMap(reinterpret_cast<AstPolyMap *>(map));
259  }
260 
261 protected:
262  virtual std::shared_ptr<Object> _copyPolymorphic() const override {
263  return _copyImpl<PolyMap, AstPolyMap>();
264  }
265 
267  PolyMap(AstPolyMap * map) :
268  Mapping(reinterpret_cast<AstMapping *>(map))
269  {
270  if (!astIsAPolyMap(getRawPtr())) {
271  std::ostringstream os;
272  os << "this is a " << getClass() << ", which is not a PolyMap";
273  throw std::invalid_argument(os.str());
274  }
275  }
276 
277 private:
279  AstPolyMap * _makeRawPolyMap(
280  ndarray::Array<double, 2, 2> const & coeff_f,
281  ndarray::Array<double, 2, 2> const & coeff_i,
282  std::string const & options=""
283  ) {
284  const int nin = coeff_f.getSize<1>() - 2;
285  const int ncoeff_f = coeff_f.getSize<0>();
286  const int nout = coeff_i.getSize<1>() - 2;
287  const int ncoeff_i = coeff_i.getSize<0>();
288 
289  if (nin <= 0) {
290  std::ostringstream os;
291  os << "coeff_f row length = " << nin + 2
292  << ", which is too short; length = nin + 2 and nin must be > 0";
293  throw std::invalid_argument(os.str());
294  }
295  if (nout <= 0) {
296  std::ostringstream os;
297  os << "coeff_i row length " << nout + 2
298  << ", which is too short; length = nout + 2 and nout must be > 0";
299  throw std::invalid_argument(os.str());
300  }
301 
302  return astPolyMap(nin, nout, ncoeff_f, coeff_f.getData(),
303  ncoeff_i, coeff_i.getData(), options.c_str());
304  }
305 
307  AstPolyMap * _makeRawPolyMap(
308  ndarray::Array<double, 2, 2> const & coeff_f,
309  int nout,
310  std::string const & options=""
311  ) {
312  const int nin = coeff_f.getSize<1>() - 2;
313  const int ncoeff_f = coeff_f.getSize<0>();
314  if (nin <= 0) {
315  std::ostringstream os;
316  os << "coeff_f row length = " << nin + 2
317  << ", which is too short; length = nin + 2 and nin must be > 0";
318  throw std::invalid_argument(os.str());
319  }
320  if (nout <= 0) {
321  std::ostringstream os;
322  os << "nout = " << nout << " <0 =";
323  throw std::invalid_argument(os.str());
324  }
325 
326  return astPolyMap(nin, nout, ncoeff_f, coeff_f.getData(), 0, nullptr, options.c_str());
327  }
328 };
329 
330 } // namespace ast
331 
332 #endif
std::shared_ptr< PolyMap > copy() const
Return a deep copy of this object.
Definition: PolyMap.h:141
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:53
std::string getClass() const
Definition: Object.h:117
virtual std::shared_ptr< Object > _copyPolymorphic() const override
Definition: PolyMap.h:262
Definition: Mapping.h:59
Definition: PolyMap.h:49
PolyMap(AstPolyMap *map)
Construct a PolyMap from an raw AST pointer.
Definition: PolyMap.h:267
Definition: Object.h:49
PolyMap(ndarray::Array< double, 2, 2 > const &coeff_f, int nout, std::string const &options="IterInverse=0")
Definition: PolyMap.h:125
int getNout() const
Definition: Mapping.h:81
int getNin() const
Definition: Mapping.h:76
bool getIterInverse() const
Get IterInverse: does this provide an iterative inverse transformation?
Definition: PolyMap.h:146
double getD(std::string const &attrib) const
Definition: Object.h:360
int getNiterInverse() const
Get NiterInverse: maximum number of iterations for iterative inverse.
Definition: PolyMap.h:149
double getTolInverse() const
Get TolInverse: target relative error for iterative inverse.
Definition: PolyMap.h:152
PolyMap polyTran(bool forward, double acc, double maxacc, int maxorder, std::vector< double > const &lbnd, std::vector< double > const &ubnd) const
Definition: PolyMap.h:228
int getI(std::string const &attrib) const
Definition: Object.h:386
bool getB(std::string const &attrib) const
Definition: Object.h:334
PolyMap(ndarray::Array< double, 2, 2 > const &coeff_f, ndarray::Array< double, 2, 2 > const &coeff_i, std::string const &options="IterInverse=0")
Definition: PolyMap.h:96
AstObject const * getRawPtr() const
Definition: Object.h:260