lsst.afw  15.0-17-g0b88e3da8+6
Endpoint.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2017 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include <ostream>
24 #include <sstream>
25 #include <string>
26 #include <memory>
27 #include <typeinfo>
28 #include <vector>
29 
30 #include "astshim.h"
31 #include "lsst/afw/geom/Point.h"
33 #include "lsst/afw/geom/Endpoint.h"
34 
35 namespace lsst {
36 namespace afw {
37 namespace geom {
38 namespace {
39 
40 /*
41 Get a pointer to a frame
42 
43 If frame is a FrameSet then return a copy of its current frame, else return the original argument
44 */
46  auto frameSetPtr = std::dynamic_pointer_cast<ast::FrameSet>(framePtr);
47  if (frameSetPtr) {
48  return frameSetPtr->getFrame(ast::FrameSet::CURRENT);
49  }
50  return framePtr;
51 }
52 
53 } // namespace
54 
55 template <typename Point, typename Array>
56 BaseEndpoint<Point, Array>::BaseEndpoint(int nAxes) : _nAxes(nAxes) {
57  if (nAxes <= 0) {
59  "nAxes = " + std::to_string(nAxes) + "; must be > 0");
60  }
61 }
62 
63 template <typename Point, typename Array>
65  return this->getNAxes() == other.getNAxes() && typeid(*this) == typeid(other);
66 }
67 
68 template <typename Point, typename Array>
70  return std::make_shared<ast::Frame>(getNAxes());
71 }
72 
73 template <typename Point, typename Array>
75  if (nAxes != this->getNAxes()) {
77  os << "number of axes provided " << nAxes << " != " << this->getNAxes() << " required";
78  throw std::invalid_argument(os.str());
79  }
80 }
81 
82 template <typename Point>
84  return arr.size();
85 }
86 
87 std::vector<double> GenericEndpoint::dataFromPoint(Point const& point) const {
88  this->_assertNAxes(_getNAxes(point));
89  return point;
90 }
91 
92 ndarray::Array<double, 2, 2> GenericEndpoint::dataFromArray(Array const& arr) const {
93  this->_assertNAxes(_getNAxes(arr));
94  return ndarray::copy(arr);
95 }
96 
97 std::vector<double> GenericEndpoint::pointFromData(std::vector<double> const& data) const {
98  this->_assertNAxes(data.size());
99  return data;
100 }
101 
102 ndarray::Array<double, 2, 2> GenericEndpoint::arrayFromData(ndarray::Array<double, 2, 2> const& data) const {
103  this->_assertNAxes(_getNAxes(data));
104  return ndarray::copy(data);
105 }
106 
107 Point2Endpoint::Point2Endpoint(int nAxes) : BaseVectorEndpoint<Point2D>(2) {
108  if (nAxes != 2) {
110  os << "nAxes = " << nAxes << " != 2";
112  }
113 }
114 
116  const int nAxes = this->getNAxes();
118  for (int axInd = 0; axInd < nAxes; ++axInd) {
119  result[axInd] = point[axInd];
120  }
121  return result;
122 }
123 
124 ndarray::Array<double, 2, 2> Point2Endpoint::dataFromArray(Array const& arr) const {
125  const int nAxes = this->getNAxes();
126  const int nPoints = this->getNPoints(arr);
127  ndarray::Array<double, 2, 2> data = ndarray::allocate(ndarray::makeVector(nAxes, nPoints));
128  auto dataColIter = data.transpose().begin();
129  for (auto const& point : arr) {
130  for (int axInd = 0; axInd < nAxes; ++axInd) {
131  (*dataColIter)[axInd] = point[axInd];
132  }
133  ++dataColIter;
134  }
135  return data;
136 }
137 
139  const int nAxes = this->getNAxes();
140  this->_assertNAxes(this->_getNAxes(data));
141  Point result;
142  for (int axInd = 0; axInd < nAxes; ++axInd) {
143  result[axInd] = data[axInd];
144  }
145  return result;
146 }
147 
148 std::vector<Point2D> Point2Endpoint::arrayFromData(ndarray::Array<double, 2, 2> const& data) const {
149  this->_assertNAxes(this->_getNAxes(data));
150  int const nPoints = this->_getNPoints(data);
151  Array array;
152  array.reserve(nPoints);
153  for (auto const& dataCol : data.transpose()) {
154  array.emplace_back(dataCol[0], dataCol[1]);
155  }
156  return array;
157 }
158 
160  // use getCurrentFrame because if framePtr points to a FrameSet we want the name of its current frame
161  std::string className = getCurrentFrame(framePtr)->getClassName();
162  if (className != "Frame") {
164  os << "frame is a " << className << ", not a Frame";
166  }
167 }
168 
170  if (nAxes != 2) {
172  os << "nAxes = " << nAxes << " != 2";
174  }
175 }
176 
178  const int nAxes = this->getNAxes();
180  for (int axInd = 0; axInd < nAxes; ++axInd) {
181  result[axInd] = point[axInd].asRadians();
182  }
183  return result;
184 }
185 
186 ndarray::Array<double, 2, 2> SpherePointEndpoint::dataFromArray(Array const& arr) const {
187  const int nAxes = this->getNAxes();
188  const int nPoints = this->getNPoints(arr);
189  ndarray::Array<double, 2, 2> data = ndarray::allocate(ndarray::makeVector(nAxes, nPoints));
190  auto dataColIter = data.transpose().begin();
191  for (auto const& point : arr) {
192  for (int axInd = 0; axInd < nAxes; ++axInd) {
193  (*dataColIter)[axInd] = point[axInd].asRadians();
194  }
195  ++dataColIter;
196  }
197  return data;
198 }
199 
201  this->_assertNAxes(this->_getNAxes(data));
202  return SpherePoint(data[0], data[1], radians);
203 }
204 
206  ndarray::Array<double, 2, 2> const& data) const {
207  this->_assertNAxes(this->_getNAxes(data));
208  int const nPoints = this->_getNPoints(data);
209  Array array;
210  array.reserve(nPoints);
211  for (auto const& dataCol : data.transpose()) {
212  array.emplace_back(SpherePoint(dataCol[0], dataCol[1], radians));
213  }
214  return array;
215 }
216 
217 std::shared_ptr<ast::Frame> SpherePointEndpoint::makeFrame() const { return std::make_shared<ast::SkyFrame>(); }
218 
220  // use getCurrentFrame because if framePtr points to a FrameSet we want its current frame
221  auto currentFramePtr = getCurrentFrame(framePtr);
222  auto skyFramePtr = std::dynamic_pointer_cast<ast::SkyFrame>(currentFramePtr);
223  if (!skyFramePtr) {
225  os << "frame is a " << currentFramePtr->getClassName() << ", not a SkyFrame";
227  }
228  if (skyFramePtr->getLonAxis() != 1) {
229  // axes are swapped to Lat, Lon; swap them back to the usual Lon, Lat
230  // warning: be sure to call permAxes on the original framePtr argument,
231  // as otherwise it will have no effect if framePtr points to a FrameSet
232  std::vector<int> perm = {2, 1};
233  framePtr->permAxes(perm);
234  }
235 }
236 
238  os << "GenericEndpoint(" << endpoint.getNAxes() << ")";
239  return os;
240 }
241 
243  os << "Point2Endpoint()";
244  return os;
245 }
246 
248  os << "SpherePointEndpoint()";
249  return os;
250 }
251 
252 // explicit instantiations
253 template class BaseEndpoint<std::vector<double>, ndarray::Array<double, 2, 2>>;
256 
257 template class BaseVectorEndpoint<Point2D>;
258 template class BaseVectorEndpoint<SpherePoint>;
259 
260 } // namespace geom
261 } // namespace afw
262 } // namespace lsst
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:59
char * data
Definition: BaseTable.cc:204
int _getNPoints(ndarray::Array< double, 2, 2 > const &data) const
Definition: Endpoint.h:185
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
virtual Point pointFromData(std::vector< double > const &data) const override
Get a single point from raw data.
Definition: Endpoint.cc:200
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:87
virtual ndarray::Array< double, 2, 2 > dataFromArray(Array const &arr) const override
Definition: Endpoint.cc:186
T to_string(T... args)
std::shared_ptr< Frame > getFrame(int iframe, bool copy=true) const
bool operator==(CoordKey const &lhs, CoordKey const &rhs)
Compare CoordKeys for equality using the constituent Keys.
Base class for endpoints with Array = std::vector<Point> where Point has 2 dimensions.
Definition: Endpoint.h:198
ItemVariant const * other
Definition: Schema.cc:55
static int constexpr CURRENT
A coordinate class intended to represent absolute positions.
virtual Array arrayFromData(ndarray::Array< double, 2, 2 > const &data) const override
Get an array of points from raw data.
Definition: Endpoint.cc:205
STL class.
lsst::afw::geom::SpherePoint SpherePoint
Definition: misc.h:35
virtual void normalizeFrame(std::shared_ptr< ast::Frame > framePtr) const override
Check that framePtr points to a SkyFrame and set longitude axis to 0, latitude to 1...
Definition: Endpoint.cc:219
A base class for image defects.
A generic endpoint for data in the format used by ast::Mapping.
Definition: Endpoint.h:229
virtual std::vector< double > dataFromPoint(Point const &point) const override
Definition: Endpoint.cc:177
T str(T... args)
An endpoint for SpherePoint.
Definition: Endpoint.h:318
T dynamic_pointer_cast(T... args)
SpherePointEndpoint()
Construct a SpherePointEndpoint.
Definition: Endpoint.h:328
virtual std::vector< double > dataFromPoint(Point const &point) const override
Definition: Endpoint.cc:115
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:194
virtual std::shared_ptr< ast::Frame > makeFrame() const override
Create a Frame that can be used with this end point in a Transform.
Definition: Endpoint.cc:217
virtual int getNPoints(Array const &arr) const override
Definition: Endpoint.cc:83
int _getNAxes(ndarray::Array< double, 2, 2 > const &data) const
Definition: Endpoint.h:179
T size(T... args)
#define LSST_EXCEPT(type,...)
STL class.
An endpoint for Point2D.
Definition: Endpoint.h:264
virtual Array arrayFromData(ndarray::Array< double, 2, 2 > const &data) const override
Get an array of points from raw data.
Definition: Endpoint.cc:148
virtual void normalizeFrame(std::shared_ptr< ast::Frame > framePtr) const override
Check that framePtr points to a Frame, not a subclass.
Definition: Endpoint.cc:159
std::ostream * os
Definition: Schema.cc:736
virtual Point pointFromData(std::vector< double > const &data) const override
Get a single point from raw data.
Definition: Endpoint.cc:138
STL class.
BaseEndpoint(BaseEndpoint const &)=default
virtual ndarray::Array< double, 2, 2 > dataFromArray(Array const &arr) const override
Definition: Endpoint.cc:124
Virtual base class for endpoints, which are helper classes for Transform.
Definition: Endpoint.h:67