lsst.astshim  master-g4454501e01
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Frame.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_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;
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 class FrameSet;
106 
157 class Frame : public Mapping {
158  friend class Object;
159 
160 public:
168  explicit Frame(int naxes, std::string const &options = "")
169  : Mapping(reinterpret_cast<AstMapping *>(astFrame(naxes, "%s", options.c_str()))) {}
170 
171  virtual ~Frame() {}
172 
173  Frame(Frame const &) = delete;
174  Frame(Frame &&) = default;
175  Frame &operator=(Frame const &) = delete;
176  Frame &operator=(Frame &&) = default;
177 
179  std::shared_ptr<Frame> copy() const { return std::static_pointer_cast<Frame>(copyPolymorphic()); }
180 
198  double angle(PointD const &a, PointD const &b, PointD const &c) const {
199  assertPointLength(a, "a");
200  assertPointLength(b, "b");
201  assertPointLength(c, "c");
202  return detail::safeDouble(astAngle(getRawPtr(), a.data(), b.data(), c.data()));
203  }
204 
226  double axAngle(PointD const &a, PointD const &b, int axis) const {
227  assertPointLength(a, "a");
228  assertPointLength(b, "b");
229  return detail::safeDouble(astAxAngle(getRawPtr(), a.data(), b.data(), axis));
230  }
231 
244  double axDistance(int axis, double v1, double v2) const {
245  return detail::safeDouble(astAxDistance(getRawPtr(), axis, v1, v2));
246  }
247 
260  double axOffset(int axis, double v1, double dist) const {
261  return detail::safeDouble(astAxOffset(getRawPtr(), axis, v1, dist));
262  }
263 
458  std::shared_ptr<FrameSet> convert(Frame const &to, std::string const &domainlist = "");
459 
473  double distance(PointD const &point1, PointD const &point2) const {
474  assertPointLength(point1, "point1");
475  assertPointLength(point2, "point2");
476  return detail::safeDouble(astDistance(getRawPtr(), point1.data(), point2.data()));
477  }
478 
769  std::shared_ptr<FrameSet> findFrame(Frame const &tmplt, std::string const &domainlist = "");
770 
782  std::string format(int axis, double value) const {
783  char const *rawstr = astFormat(getRawPtr(), axis, value);
784  assertOK();
785  return std::string(rawstr);
786  }
787 
792  bool getActiveUnit() const {
793  bool ret = astGetActiveUnit(getRawPtr());
794  assertOK();
795  return ret;
796  }
797 
802  std::string getAlignSystem() const { return getC("AlignSystem"); }
803 
807  double getBottom(int axis) const { return getD(detail::formatAxisAttr("Bottom", axis)); }
808 
812  int getDigits() const { return getI("Digits"); }
813 
817  int getDigits(int axis) const { return getI(detail::formatAxisAttr("Digits", axis)); }
818 
822  bool getDirection(int axis) const { return getB(detail::formatAxisAttr("Direction", axis)); }
823 
827  std::string getDomain() const { return getC("Domain"); }
828 
832  double getDut1() const { return getD("Dut1"); }
833 
837  double getEpoch() const { return getD("Epoch"); }
838 
842  std::string getFormat(int axis) const { return getC(detail::formatAxisAttr("Format", axis)); }
843 
848  std::string getInternalUnit(int axis) const { return getC(detail::formatAxisAttr("InternalUnit", axis)); }
849 
853  std::string getLabel(int axis) const { return getC(detail::formatAxisAttr("Label", axis)); }
854 
858  bool getMatchEnd() const { return getB("MatchEnd"); }
859 
864  int getMaxAxes() const { return getI("MaxAxes"); }
865 
870  int getMinAxes() const { return getI("MinAxes"); }
871 
876  int getNAxes() const { return getI("NAxes"); }
877 
882  std::string getNormUnit(int axis) const { return getC(detail::formatAxisAttr("NormUnit", axis)); }
883 
887  double getObsAlt() const { return getD("ObsAlt"); }
888 
892  std::string getObsLat() const { return getC("ObsLat"); }
893 
897  std::string getObsLon() const { return getC("ObsLon"); }
898 
902  bool getPermute() const { return getB("Permute"); }
903 
907  bool getPreserveAxes() const { return getB("PreserveAxes"); }
908 
912  std::string getSymbol(int axis) const { return getC(detail::formatAxisAttr("Symbol", axis)); }
913 
918  std::string getSystem() const { return getC("System"); }
919 
923  std::string getTitle() const { return getC("Title"); }
924 
928  double getTop(int axis) const { return getD(detail::formatAxisAttr("Top", axis)); }
929 
933  std::string getUnit(int axis) const { return getC(detail::formatAxisAttr("Unit", axis)); }
934 
966  std::vector<double> intersect(std::vector<double> const &a1, std::vector<double> const &a2,
967  std::vector<double> const &b1, std::vector<double> const &b2) const;
968 
985  std::vector<int> matchAxes(Frame const &other) const {
986  std::vector<int> ret(other.getNIn());
987  astMatchAxes(getRawPtr(), other.getRawPtr(), ret.data());
988  assertOK();
989  return ret;
990  }
991 
1017  CmpFrame over(Frame const &first) const;
1018 
1039  PointD norm(PointD value) const {
1040  astNorm(getRawPtr(), value.data());
1041  assertOK();
1042  detail::astBadToNan(value);
1043  return value;
1044  }
1045 
1069  PointD offset(PointD point1, PointD point2, double offset) const {
1070  assertPointLength(point1, "point1");
1071  assertPointLength(point2, "point2");
1072  PointD ret(getNIn());
1073  astOffset(getRawPtr(), point1.data(), point2.data(), offset, ret.data());
1074  assertOK();
1075  detail::astBadToNan(ret);
1076  return ret;
1077  }
1078 
1111  DirectionPoint offset2(PointD const &point1, double angle, double offset) const {
1112  detail::assertEqual(getNIn(), "naxes", 2, " cannot call offset2");
1113  assertPointLength(point1, "point1");
1114  PointD point2(getNIn());
1115  double offsetAngle = astOffset2(getRawPtr(), point1.data(), angle, offset, point2.data());
1116  assertOK();
1117  detail::astBadToNan(point2);
1118  return DirectionPoint(detail::safeDouble(offsetAngle), point2);
1119  }
1120 
1132  void permAxes(std::vector<int> perm) {
1133  detail::assertEqual(perm.size(), "perm.size()", static_cast<std::size_t>(getNAxes()), "naxes");
1134  astPermAxes(getRawPtr(), perm.data());
1135  assertOK();
1136  }
1137 
1157  FrameMapping pickAxes(std::vector<int> const &axes) const;
1158 
1190  ResolvedPoint resolve(std::vector<double> const &point1, std::vector<double> const &point2,
1191  std::vector<double> const &point3) const;
1192 
1197  void setAlignSystem(std::string const &system) { setC("AlignSystem", system); }
1198 
1202  void setBottom(int axis, double bottom) { setD(detail::formatAxisAttr("Bottom", axis), bottom); }
1203 
1207  void setDigits(int digits) { setI("Digits", digits); }
1208 
1212  void setDigits(int axis, int digits) { setD(detail::formatAxisAttr("Digits", axis), digits); }
1213 
1217  void setDirection(bool direction, int axis) {
1218  setB(detail::formatAxisAttr("Direction", axis), direction);
1219  }
1220 
1224  void setDomain(std::string const &domain) { setC("Domain", domain); }
1225 
1229  void setDut1(double dut1) { setD("Dut1", dut1); }
1230 
1234  void setEpoch(double epoch) { setD("Epoch", epoch); }
1235 
1239  void setEpoch(std::string const &epoch) { setC("Epoch", epoch); }
1240 
1244  void setFormat(int axis, std::string const &format) {
1245  setC(detail::formatAxisAttr("Format", axis), format);
1246  }
1247 
1251  void setLabel(int axis, std::string const &label) { setC(detail::formatAxisAttr("Label", axis), label); }
1252 
1256  void setMatchEnd(bool match) { setB("MatchEnd", match); }
1257 
1262  void setMaxAxes(int maxAxes) { setI("MaxAxes", maxAxes); }
1263 
1268  void setMinAxes(int minAxes) { setI("MinAxes", minAxes); }
1269 
1273  void setObsAlt(double alt) { setD("ObsAlt", alt); }
1274 
1278  void setObsLat(std::string const &lat) { setC("ObsLat", lat); }
1279 
1283  void setObsLon(std::string const &lon) { setC("ObsLon", lon); }
1284 
1289  void setActiveUnit(bool enable) {
1290  astSetActiveUnit(getRawPtr(), enable);
1291  assertOK();
1292  }
1293 
1297  void setPermute(bool permute) { setB("Permute", permute); }
1298 
1302  void setPreserveAxes(bool preserve) { setB("PreserveAxes", preserve); }
1303 
1307  void setSymbol(int axis, std::string const &symbol) {
1308  setC(detail::formatAxisAttr("Symbol", axis), symbol);
1309  }
1310 
1315  void setSystem(std::string const &system) { setC("System", system); }
1316 
1320  void setTitle(std::string const &title) { setC("Title", title); }
1321 
1325  void setTop(int axis, double top) { setD(detail::formatAxisAttr("Top", axis), top); }
1326 
1330  void setUnit(int axis, std::string const &unit) { setC(detail::formatAxisAttr("Unit", axis), unit); }
1331 
1496  NReadValue unformat(int axis, std::string const &str) const {
1497  double value;
1498  int nread = astUnformat(getRawPtr(), axis, str.c_str(), &value);
1499  assertOK();
1500  return NReadValue(nread, detail::safeDouble(value));
1501  }
1502 
1503 protected:
1513  explicit Frame(AstFrame *rawPtr) : Mapping(reinterpret_cast<AstMapping *>(rawPtr)) {
1514  if (!astIsAFrame(getRawPtr())) {
1515  std::ostringstream os;
1516  os << "This is a " << getClassName() << ", which is not a Frame";
1517  throw std::invalid_argument(os.str());
1518  }
1519  }
1520 
1521  virtual std::shared_ptr<Object> copyPolymorphic() const override { return copyImpl<Frame, AstFrame>(); }
1522 
1523 private:
1530  template <typename T>
1531  void assertPointLength(T const &p, char const *name) const {
1532  if (static_cast<int>(p.size()) != getNIn()) {
1533  std::ostringstream os;
1534  os << "point " << name << " has " << p.size() << " axes, but " << getNIn() << " required";
1535  throw std::invalid_argument(os.str());
1536  }
1537  }
1538 };
1539 
1540 } // namespace ast
1541 
1542 #endif
std::string getInternalUnit(int axis) const
Definition: Frame.h:848
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:198
PointD norm(PointD value) const
Definition: Frame.h:1039
FrameMapping(std::shared_ptr< Frame > frame, std::shared_ptr< Mapping > mapping)
Definition: Frame.h:99
std::string getNormUnit(int axis) const
Definition: Frame.h:882
DirectionPoint offset2(PointD const &point1, double angle, double offset) const
Definition: Frame.h:1111
std::string getTitle() const
Definition: Frame.h:923
void setI(std::string const &attrib, int value)
Definition: Object.h:480
int getDigits(int axis) const
Definition: Frame.h:817
int getNAxes() const
Definition: Frame.h:876
double distance(PointD const &point1, PointD const &point2) const
Definition: Frame.h:473
void setSystem(std::string const &system)
Definition: Frame.h:1315
std::string getSymbol(int axis) const
Definition: Frame.h:912
void setTop(int axis, double top)
Definition: Frame.h:1325
std::shared_ptr< FrameSet > convert(Frame const &to, std::string const &domainlist="")
Definition: Frame.cc:31
std::shared_ptr< Frame > copy() const
Return a deep copy of this object.
Definition: Frame.h:179
Definition: Frame.h:157
double axDistance(int axis, double v1, double v2) const
Definition: Frame.h:244
CmpFrame over(Frame const &first) const
Definition: Frame.cc:66
double getObsAlt() const
Definition: Frame.h:887
void setBottom(int axis, double bottom)
Definition: Frame.h:1202
void setMatchEnd(bool match)
Definition: Frame.h:1256
void assertOK(AstObject *rawPtr1=nullptr, AstObject *rawPtr2=nullptr)
Definition: base.cc:49
void setObsAlt(double alt)
Definition: Frame.h:1273
std::string format(int axis, double value) const
Definition: Frame.h:782
void setMaxAxes(int maxAxes)
Definition: Frame.h:1262
PointD offset(PointD point1, PointD point2, double offset) const
Definition: Frame.h:1069
Definition: FrameSet.h:99
std::string getObsLat() const
Definition: Frame.h:892
FrameMapping pickAxes(std::vector< int > const &axes) const
Definition: Frame.cc:68
std::string const getC(std::string const &attrib) const
Definition: Object.h:344
void setPermute(bool permute)
Definition: Frame.h:1297
ResolvedPoint resolve(std::vector< double > const &point1, std::vector< double > const &point2, std::vector< double > const &point3) const
Definition: Frame.cc:84
void setB(std::string const &attrib, bool value)
Definition: Object.h:432
Definition: Mapping.h:59
void setDigits(int axis, int digits)
Definition: Frame.h:1212
bool getActiveUnit() const
Definition: Frame.h:792
Frame(AstFrame *rawPtr)
Definition: Frame.h:1513
std::vector< int > matchAxes(Frame const &other) const
Definition: Frame.h:985
Definition: Object.h:49
std::string getLabel(int axis) const
Definition: Frame.h:853
std::shared_ptr< Frame > frame
Frame.
Definition: Frame.h:101
double value
Value that was read.
Definition: Frame.h:69
int getNIn() const
Definition: Mapping.h:76
std::string getAlignSystem() const
Definition: Frame.h:802
void setEpoch(std::string const &epoch)
Definition: Frame.h:1239
void setObsLon(std::string const &lon)
Definition: Frame.h:1283
virtual std::shared_ptr< Object > copyPolymorphic() const override
Definition: Frame.h:1521
Definition: Frame.h:43
void setTitle(std::string const &title)
Definition: Frame.h:1320
bool getPermute() const
Definition: Frame.h:902
void setSymbol(int axis, std::string const &symbol)
Definition: Frame.h:1307
Frame(int naxes, std::string const &options="")
Definition: Frame.h:168
std::string getUnit(int axis) const
Definition: Frame.h:933
void permAxes(std::vector< int > perm)
Definition: Frame.h:1132
void setC(std::string const &attrib, std::string const &value)
Definition: Object.h:444
double d1
Resolved vector component 1.
Definition: Frame.h:84
std::string getObsLon() const
Definition: Frame.h:897
double getBottom(int axis) const
Definition: Frame.h:807
void setActiveUnit(bool enable)
Definition: Frame.h:1289
bool getDirection(int axis) const
Definition: Frame.h:822
std::string getDomain() const
Definition: Frame.h:827
void setFormat(int axis, std::string const &format)
Definition: Frame.h:1244
double axAngle(PointD const &a, PointD const &b, int axis) const
Definition: Frame.h:226
PointD point
Point.
Definition: Frame.h:53
double getD(std::string const &attrib) const
Definition: Object.h:357
std::vector< double > PointD
Definition: base.h:57
double getDut1() const
Definition: Frame.h:832
Definition: Frame.h:59
NReadValue unformat(int axis, std::string const &str) const
Definition: Frame.h:1496
int getMinAxes() const
Definition: Frame.h:870
bool getPreserveAxes() const
Definition: Frame.h:907
void setLabel(int axis, std::string const &label)
Definition: Frame.h:1251
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:1302
void setD(std::string const &attrib, double value)
Definition: Object.h:456
Definition: CmpFrame.h:60
Definition: Frame.h:91
DirectionPoint(double direction, PointD const &point)
Definition: Frame.h:51
void setDigits(int digits)
Definition: Frame.h:1207
void setDomain(std::string const &domain)
Definition: Frame.h:1224
void setEpoch(double epoch)
Definition: Frame.h:1234
std::shared_ptr< FrameSet > findFrame(Frame const &tmplt, std::string const &domainlist="")
Definition: Frame.cc:41
double direction
Direction, an angle in radians.
Definition: Frame.h:51
double getEpoch() const
Definition: Frame.h:837
double axOffset(int axis, double v1, double dist) const
Definition: Frame.h:260
std::string getSystem() const
Definition: Frame.h:918
bool getMatchEnd() const
Definition: Frame.h:858
void setDirection(bool direction, int axis)
Definition: Frame.h:1217
int getI(std::string const &attrib) const
Definition: Object.h:383
Definition: Frame.h:75
void setAlignSystem(std::string const &system)
Definition: Frame.h:1197
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:331
void setUnit(int axis, std::string const &unit)
Definition: Frame.h:1330
std::string getFormat(int axis) const
Definition: Frame.h:842
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:51
void setMinAxes(int minAxes)
Definition: Frame.h:1268
int getMaxAxes() const
Definition: Frame.h:864
double getTop(int axis) const
Definition: Frame.h:928
int getDigits() const
Definition: Frame.h:812
void setObsLat(std::string const &lat)
Definition: Frame.h:1278
void setDut1(double dut1)
Definition: Frame.h:1229
std::string getClassName() const
Definition: Object.h:118
ResolvedPoint(int naxes)
Definition: Frame.h:82
AstObject const * getRawPtr() const
Definition: Object.h:270