lsst.astshim  master-gd1207e8857
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Frame.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_FRAME_H
23 #define ASTSHIM_FRAME_H
24 
25 #include <memory>
26 #include <sstream>
27 #include <stdexcept>
28 #include <utility>
29 
30 #include "astshim/Mapping.h"
31 #include "astshim/Object.h"
32 #include "astshim/base.h"
33 #include "astshim/detail/utils.h"
34 
35 namespace ast {
36 
37 class CmpFrame;
38 class Frame;
39 
44 public:
51  DirectionPoint(double direction, PointD const &point) : direction(direction), point(point){};
52  double direction;
53  PointD point;
54 };
55 
59 class NReadValue {
60 public:
67  NReadValue(int nread, double value) : nread(nread), value(value){};
68  int nread;
69  double value;
70 };
71 
76 public:
82  explicit ResolvedPoint(int naxes) : point(naxes), d1(), d2() {}
83  std::vector<double> point;
84  double d1;
85  double d2;
86 };
87 
91 class FrameMapping {
92 public:
99  FrameMapping(std::shared_ptr<Frame> frame, std::shared_ptr<Mapping> mapping) :
100  frame(frame), mapping(mapping) {}
101  std::shared_ptr<Frame> frame;
102  std::shared_ptr<Mapping> mapping;
103 };
104 
105 
106 class FrameSet;
107 
158 class Frame : public Mapping {
159  friend class Object;
160 
161 public:
169  explicit Frame(int naxes, std::string const &options = "")
170  : Mapping(reinterpret_cast<AstMapping *>(astFrame(naxes, options.c_str()))) {}
171 
172  virtual ~Frame() {}
173 
174  Frame(Frame const &) = delete;
175  Frame(Frame &&) = default;
176  Frame &operator=(Frame const &) = delete;
177  Frame &operator=(Frame &&) = default;
178 
180  std::shared_ptr<Frame> copy() const { return std::static_pointer_cast<Frame>(_copyPolymorphic()); }
181 
199  double angle(PointD const &a, PointD const &b, PointD const &c) const {
200  assertPointLength(a, "a");
201  assertPointLength(b, "b");
202  assertPointLength(c, "c");
203  return detail::safeDouble(astAngle(getRawPtr(), a.data(), b.data(), c.data()));
204  }
205 
227  double axAngle(PointD const &a, PointD const &b, int axis) const {
228  assertPointLength(a, "a");
229  assertPointLength(b, "b");
230  return detail::safeDouble(astAxAngle(getRawPtr(), a.data(), b.data(), axis));
231  }
232 
245  double axDistance(int axis, double v1, double v2) const {
246  return detail::safeDouble(astAxDistance(getRawPtr(), axis, v1, v2));
247  }
248 
261  double axOffset(int axis, double v1, double dist) const {
262  return detail::safeDouble(astAxOffset(getRawPtr(), axis, v1, dist));
263  }
264 
460  FrameSet convert(Frame const &to, std::string const &domainlist="");
461 
475  double distance(PointD const &point1, PointD const &point2) const {
476  assertPointLength(point1, "point1");
477  assertPointLength(point2, "point2");
478  return detail::safeDouble(astDistance(getRawPtr(), point1.data(), point2.data()));
479  }
480 
772  FrameSet findFrame(Frame const &tmplt, std::string const &domainlist = "");
773 
785  std::string format(int axis, double value) const {
786  char const *rawstr = astFormat(getRawPtr(), axis, value);
787  assertOK();
788  return std::string(rawstr);
789  }
790 
795  bool getActiveUnit() const {
796  bool ret = astGetActiveUnit(getRawPtr());
797  assertOK();
798  return ret;
799  }
800 
805  std::string getAlignSystem() const { return getC("AlignSystem"); }
806 
810  double getBottom(int axis) const { return getD(detail::formatAxisAttr("Bottom", axis)); }
811 
815  int getDigits() const { return getI("Digits"); }
816 
820  bool getDirection(int axis) const { return getB(detail::formatAxisAttr("Direction", axis)); }
821 
825  std::string getDomain() const { return getC("Domain"); }
826 
830  double getDut1() const { return getD("Dut1"); }
831 
835  double getEpoch() const { return getD("Epoch"); }
836 
840  std::string getFormat(int axis) const { return getC(detail::formatAxisAttr("Format", axis)); }
841 
846  std::string getInternalUnit(int axis) const { return getC(detail::formatAxisAttr("InternalUnit", axis)); }
847 
851  std::string getLabel(int axis) const { return getC(detail::formatAxisAttr("Label", axis)); }
852 
856  bool getMatchEnd() const { return getB("MatchEnd"); }
857 
862  int getMaxAxes() const { return getI("MaxAxes"); }
863 
868  int getMinAxes() const { return getI("MinAxes"); }
869 
874  int getNaxes() const { return getI("Naxes"); }
875 
880  std::string getNormUnit(int axis) const { return getC(detail::formatAxisAttr("NormUnit", axis)); }
881 
885  double getObsAlt() const { return getD("ObsAlt"); }
886 
890  std::string getObsLat() const { return getC("ObsLat"); }
891 
895  std::string getObsLon() const { return getC("ObsLon"); }
896 
900  bool getPermute() const { return getB("Permute"); }
901 
905  bool getPreserveAxes() const { return getB("PreserveAxes"); }
906 
910  std::string getSymbol(int axis) const { return getC(detail::formatAxisAttr("Symbol", axis)); }
911 
916  std::string getSystem() const { return getC("System"); }
917 
921  std::string getTitle() const { return getC("Title"); }
922 
926  double getTop(int axis) const { return getD(detail::formatAxisAttr("Top", axis)); }
927 
931  std::string getUnit(int axis) const { return getC(detail::formatAxisAttr("Unit", axis)); }
932 
964  std::vector<double> intersect(std::vector<double> const &a1, std::vector<double> const &a2,
965  std::vector<double> const &b1, std::vector<double> const &b2) const;
966 
983  std::vector<int> matchAxes(Frame const &other) const {
984  std::vector<int> ret(other.getNin());
985  astMatchAxes(getRawPtr(), other.getRawPtr(), ret.data());
986  assertOK();
987  return ret;
988  }
989 
1015  CmpFrame over(Frame const &first) const;
1016 
1037  PointD norm(PointD value) const {
1038  astNorm(getRawPtr(), value.data());
1039  assertOK();
1040  detail::astBadToNan(value);
1041  return value;
1042  }
1043 
1067  PointD offset(PointD point1, PointD point2, double offset) const {
1068  assertPointLength(point1, "point1");
1069  assertPointLength(point2, "point2");
1070  PointD ret(getNin());
1071  astOffset(getRawPtr(), point1.data(), point2.data(), offset, ret.data());
1072  assertOK();
1073  detail::astBadToNan(ret);
1074  return ret;
1075  }
1076 
1109  DirectionPoint offset2(PointD const &point1, double angle, double offset) const {
1110  detail::assertEqual(getNin(), "naxes", 2, " cannot call offset2");
1111  assertPointLength(point1, "point1");
1112  PointD point2(getNin());
1113  double offsetAngle = astOffset2(getRawPtr(), point1.data(), angle, offset, point2.data());
1114  assertOK();
1115  detail::astBadToNan(point2);
1116  return DirectionPoint(detail::safeDouble(offsetAngle), point2);
1117  }
1118 
1130  void permAxes(std::vector<int> perm) {
1131  detail::assertEqual(static_cast<int>(perm.size()), "perm.size()", getNaxes(), "naxes");
1132  astPermAxes(getRawPtr(), perm.data());
1133  assertOK();
1134  }
1135 
1155  FrameMapping pickAxes(std::vector<int> const &axes) const;
1156 
1188  ResolvedPoint resolve(std::vector<double> const &point1, std::vector<double> const &point2,
1189  std::vector<double> const &point3) const;
1190 
1195  void setAlignSystem(std::string const &system) { setC("AlignSystem", system); }
1196 
1200  void setBottom(int axis, double bottom) { setD(detail::formatAxisAttr("Bottom", axis), bottom); }
1201 
1205  void setDigits(int digits) { setI("Digits", digits); }
1206 
1210  void setDigits(int digits, int axis) { setD(detail::formatAxisAttr("Digits", axis), digits); }
1211 
1215  void setDirection(bool direction, int axis) {
1216  setB(detail::formatAxisAttr("Direction", axis), direction);
1217  }
1218 
1222  void setDomain(std::string const &domain) { setC("Domain", domain); }
1223 
1227  void setDut1(double dut1) { setD("Dut1", dut1); }
1228 
1232  void setEpoch(double epoch) { setD("Epoch", epoch); }
1233 
1237  void setEpoch(std::string const &epoch) { setC("Epoch", epoch); }
1238 
1242  void setFormat(int axis, std::string const &format) {
1243  setC(detail::formatAxisAttr("Format", axis), format);
1244  }
1245 
1249  void setLabel(int axis, std::string const &label) { setC(detail::formatAxisAttr("Label", axis), label); }
1250 
1254  void setMatchEnd(bool match) { setB("MatchEnd", match); }
1255 
1260  void setMaxAxes(int maxAxes) { setI("MaxAxes", maxAxes); }
1261 
1266  void setMinAxes(int minAxes) { setI("MinAxes", minAxes); }
1267 
1271  void setObsAlt(double alt) { setD("ObsAlt", alt); }
1272 
1276  void setObsLat(std::string const &lat) { setC("ObsLat", lat); }
1277 
1281  void setObsLon(std::string const &lon) { setC("ObsLon", lon); }
1282 
1287  void setActiveUnit(bool enable) {
1288  astSetActiveUnit(getRawPtr(), enable);
1289  assertOK();
1290  }
1291 
1295  void setPermute(bool permute) { setB("Permute", permute); }
1296 
1300  void setPreserveAxes(bool preserve) { setB("PreserveAxes", preserve); }
1301 
1305  void setSymbol(int axis, std::string const &symbol) {
1306  setC(detail::formatAxisAttr("Symbol", axis), symbol);
1307  }
1308 
1313  void setSystem(std::string const &system) { setC("System", system); }
1314 
1318  void setTitle(std::string const &title) { setC("Title", title); }
1319 
1323  void setTop(int axis, double top) { setD(detail::formatAxisAttr("Top", axis), top); }
1324 
1328  void setUnit(int axis, std::string const &unit) { setC(detail::formatAxisAttr("Unit", axis), unit); }
1329 
1494  NReadValue unformat(int axis, std::string const &str) const {
1495  double value;
1496  int nread = astUnformat(getRawPtr(), axis, str.c_str(), &value);
1497  assertOK();
1498  return NReadValue(nread, detail::safeDouble(value));
1499  }
1500 
1501 protected:
1511  explicit Frame(AstFrame *rawPtr) : Mapping(reinterpret_cast<AstMapping *>(rawPtr)) {
1512  if (!astIsAFrame(getRawPtr())) {
1513  std::ostringstream os;
1514  os << "This is a " << getClass() << ", which is not a Frame";
1515  throw std::invalid_argument(os.str());
1516  }
1517  }
1518 
1519  virtual std::shared_ptr<Object> _copyPolymorphic() const override { return _copyImpl<Frame, AstFrame>(); }
1520 
1521 private:
1528  template <typename T>
1529  void assertPointLength(T const &p, char const *name) const {
1530  if (static_cast<int>(p.size()) != getNin()) {
1531  std::ostringstream os;
1532  os << "point " << name << " has " << p.size() << " axes, but " << getNin() << " required";
1533  throw std::invalid_argument(os.str());
1534  }
1535  }
1536 };
1537 
1538 } // namespace ast
1539 
1540 #endif
std::string getInternalUnit(int axis) const
Definition: Frame.h:846
std::shared_ptr< Mapping > mapping
Mapping.
Definition: Frame.h:102
double angle(PointD const &a, PointD const &b, PointD const &c) const
Definition: Frame.h:199
PointD norm(PointD value) const
Definition: Frame.h:1037
FrameMapping(std::shared_ptr< Frame > frame, std::shared_ptr< Mapping > mapping)
Definition: Frame.h:99
std::string getNormUnit(int axis) const
Definition: Frame.h:880
DirectionPoint offset2(PointD const &point1, double angle, double offset) const
Definition: Frame.h:1109
std::string getTitle() const
Definition: Frame.h:921
void setI(std::string const &attrib, int value)
Definition: Object.h:483
double distance(PointD const &point1, PointD const &point2) const
Definition: Frame.h:475
void setSystem(std::string const &system)
Definition: Frame.h:1313
FrameSet findFrame(Frame const &tmplt, std::string const &domainlist="")
Definition: Frame.cc:40
std::string getSymbol(int axis) const
Definition: Frame.h:910
void setTop(int axis, double top)
Definition: Frame.h:1323
std::shared_ptr< Frame > copy() const
Return a deep copy of this object.
Definition: Frame.h:180
Definition: Frame.h:158
double axDistance(int axis, double v1, double v2) const
Definition: Frame.h:245
CmpFrame over(Frame const &first) const
Definition: Frame.cc:65
double getObsAlt() const
Definition: Frame.h:885
void setBottom(int axis, double bottom)
Definition: Frame.h:1200
void setMatchEnd(bool match)
Definition: Frame.h:1254
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:53
void setObsAlt(double alt)
Definition: Frame.h:1271
std::string getClass() const
Definition: Object.h:117
std::string format(int axis, double value) const
Definition: Frame.h:785
void setMaxAxes(int maxAxes)
Definition: Frame.h:1260
PointD offset(PointD point1, PointD point2, double offset) const
Definition: Frame.h:1067
Definition: FrameSet.h:99
std::string getObsLat() const
Definition: Frame.h:890
FrameMapping pickAxes(std::vector< int > const &axes) const
Definition: Frame.cc:67
std::string const getC(std::string const &attrib) const
Definition: Object.h:347
void setPermute(bool permute)
Definition: Frame.h:1295
ResolvedPoint resolve(std::vector< double > const &point1, std::vector< double > const &point2, std::vector< double > const &point3) const
Definition: Frame.cc:83
void setB(std::string const &attrib, bool value)
Definition: Object.h:435
Definition: Mapping.h:59
bool getActiveUnit() const
Definition: Frame.h:795
Frame(AstFrame *rawPtr)
Definition: Frame.h:1511
std::vector< int > matchAxes(Frame const &other) const
Definition: Frame.h:983
Definition: Object.h:49
std::string getLabel(int axis) const
Definition: Frame.h:851
std::shared_ptr< Frame > frame
Frame.
Definition: Frame.h:101
double value
Value that was read.
Definition: Frame.h:69
FrameSet convert(Frame const &to, std::string const &domainlist="")
Definition: Frame.cc:30
std::string getAlignSystem() const
Definition: Frame.h:805
void setEpoch(std::string const &epoch)
Definition: Frame.h:1237
void setObsLon(std::string const &lon)
Definition: Frame.h:1281
Definition: Frame.h:43
void setTitle(std::string const &title)
Definition: Frame.h:1318
bool getPermute() const
Definition: Frame.h:900
void setSymbol(int axis, std::string const &symbol)
Definition: Frame.h:1305
Frame(int naxes, std::string const &options="")
Definition: Frame.h:169
std::string getUnit(int axis) const
Definition: Frame.h:931
void permAxes(std::vector< int > perm)
Definition: Frame.h:1130
void setC(std::string const &attrib, std::string const &value)
Definition: Object.h:447
double d1
Resolved vector component 1.
Definition: Frame.h:84
std::string getObsLon() const
Definition: Frame.h:895
double getBottom(int axis) const
Definition: Frame.h:810
int getNaxes() const
Definition: Frame.h:874
void setActiveUnit(bool enable)
Definition: Frame.h:1287
bool getDirection(int axis) const
Definition: Frame.h:820
int getNin() const
Definition: Mapping.h:76
std::string getDomain() const
Definition: Frame.h:825
void setFormat(int axis, std::string const &format)
Definition: Frame.h:1242
double axAngle(PointD const &a, PointD const &b, int axis) const
Definition: Frame.h:227
PointD point
Point.
Definition: Frame.h:53
double getD(std::string const &attrib) const
Definition: Object.h:360
double getDut1() const
Definition: Frame.h:830
Definition: Frame.h:59
NReadValue unformat(int axis, std::string const &str) const
Definition: Frame.h:1494
int getMinAxes() const
Definition: Frame.h:868
bool getPreserveAxes() const
Definition: Frame.h:905
void setLabel(int axis, std::string const &label)
Definition: Frame.h:1249
NReadValue(int nread, double value)
Definition: Frame.h:67
double d2
Resolved vector component 2.
Definition: Frame.h:85
void setPreserveAxes(bool preserve)
Definition: Frame.h:1300
void setD(std::string const &attrib, double value)
Definition: Object.h:459
Definition: CmpFrame.h:60
Definition: Frame.h:91
virtual std::shared_ptr< Object > _copyPolymorphic() const override
Definition: Frame.h:1519
DirectionPoint(double direction, PointD const &point)
Definition: Frame.h:51
void setDigits(int digits)
Definition: Frame.h:1205
void setDomain(std::string const &domain)
Definition: Frame.h:1222
void setEpoch(double epoch)
Definition: Frame.h:1232
double direction
Direction, an angle in radians.
Definition: Frame.h:51
double getEpoch() const
Definition: Frame.h:835
double axOffset(int axis, double v1, double dist) const
Definition: Frame.h:261
void setDigits(int digits, int axis)
Definition: Frame.h:1210
std::string getSystem() const
Definition: Frame.h:916
bool getMatchEnd() const
Definition: Frame.h:856
void setDirection(bool direction, int axis)
Definition: Frame.h:1215
int getI(std::string const &attrib) const
Definition: Object.h:386
Definition: Frame.h:75
void setAlignSystem(std::string const &system)
Definition: Frame.h:1195
std::vector< double > point
Point.
Definition: Frame.h:83
int nread
Number of characters that was read.
Definition: Frame.h:67
bool getB(std::string const &attrib) const
Definition: Object.h:334
void setUnit(int axis, std::string const &unit)
Definition: Frame.h:1328
std::string getFormat(int axis) const
Definition: Frame.h:840
std::vector< double > intersect(std::vector< double > const &a1, std::vector< double > const &a2, std::vector< double > const &b1, std::vector< double > const &b2) const
Definition: Frame.cc:50
void setMinAxes(int minAxes)
Definition: Frame.h:1266
int getMaxAxes() const
Definition: Frame.h:862
double getTop(int axis) const
Definition: Frame.h:926
int getDigits() const
Definition: Frame.h:815
void setObsLat(std::string const &lat)
Definition: Frame.h:1276
void setDut1(double dut1)
Definition: Frame.h:1227
ResolvedPoint(int naxes)
Definition: Frame.h:82
AstObject const * getRawPtr() const
Definition: Object.h:260