LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
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/geom/Point.h"
32 #include "lsst/geom/SpherePoint.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<lsst::geom::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 
149  ndarray::Array<double, 2, 2> const& data) const {
150  this->_assertNAxes(this->_getNAxes(data));
151  int const nPoints = this->_getNPoints(data);
152  Array array;
153  array.reserve(nPoints);
154  for (auto const& dataCol : data.transpose()) {
155  array.emplace_back(dataCol[0], dataCol[1]);
156  }
157  return array;
158 }
159 
161  // use getCurrentFrame because if framePtr points to a FrameSet we want the name of its current frame
162  std::string className = getCurrentFrame(framePtr)->getClassName();
163  if (className != "Frame") {
165  os << "frame is a " << className << ", not a Frame";
167  }
168 }
169 
171  if (nAxes != 2) {
173  os << "nAxes = " << nAxes << " != 2";
175  }
176 }
177 
179  const int nAxes = this->getNAxes();
181  for (int axInd = 0; axInd < nAxes; ++axInd) {
182  result[axInd] = point[axInd].asRadians();
183  }
184  return result;
185 }
186 
187 ndarray::Array<double, 2, 2> SpherePointEndpoint::dataFromArray(Array const& arr) const {
188  const int nAxes = this->getNAxes();
189  const int nPoints = this->getNPoints(arr);
190  ndarray::Array<double, 2, 2> data = ndarray::allocate(ndarray::makeVector(nAxes, nPoints));
191  auto dataColIter = data.transpose().begin();
192  for (auto const& point : arr) {
193  for (int axInd = 0; axInd < nAxes; ++axInd) {
194  (*dataColIter)[axInd] = point[axInd].asRadians();
195  }
196  ++dataColIter;
197  }
198  return data;
199 }
200 
202  this->_assertNAxes(this->_getNAxes(data));
203  return lsst::geom::SpherePoint(data[0], data[1], lsst::geom::radians);
204 }
205 
207  ndarray::Array<double, 2, 2> const& data) const {
208  this->_assertNAxes(this->_getNAxes(data));
209  int const nPoints = this->_getNPoints(data);
210  Array array;
211  array.reserve(nPoints);
212  for (auto const& dataCol : data.transpose()) {
213  array.emplace_back(lsst::geom::SpherePoint(dataCol[0], dataCol[1], lsst::geom::radians));
214  }
215  return array;
216 }
217 
219  return std::make_shared<ast::SkyFrame>();
220 }
221 
223  // use getCurrentFrame because if framePtr points to a FrameSet we want its current frame
224  auto currentFramePtr = getCurrentFrame(framePtr);
225  auto skyFramePtr = std::dynamic_pointer_cast<ast::SkyFrame>(currentFramePtr);
226  if (!skyFramePtr) {
228  os << "frame is a " << currentFramePtr->getClassName() << ", not a SkyFrame";
230  }
231  if (skyFramePtr->getLonAxis() != 1) {
232  // axes are swapped to Lat, Lon; swap them back to the usual Lon, Lat
233  // warning: be sure to call permAxes on the original framePtr argument,
234  // as otherwise it will have no effect if framePtr points to a FrameSet
235  std::vector<int> perm = {2, 1};
236  framePtr->permAxes(perm);
237  }
238 }
239 
241  os << "GenericEndpoint(" << endpoint.getNAxes() << ")";
242  return os;
243 }
244 
246  os << "Point2Endpoint()";
247  return os;
248 }
249 
251  os << "SpherePointEndpoint()";
252  return os;
253 }
254 
255 // explicit instantiations
256 template class BaseEndpoint<std::vector<double>, ndarray::Array<double, 2, 2>>;
259 
262 
263 } // namespace geom
264 } // namespace afw
265 } // namespace lsst
int _getNPoints(ndarray::Array< double, 2, 2 > const &data) const
Definition: Endpoint.h:183
A coordinate class intended to represent absolute positions.
ndarray::Array< double, 2, 2 > dataFromArray(Array const &arr) const override
Definition: Endpoint.cc:187
T to_string(T... args)
py::object result
Definition: schema.cc:418
std::ostream & operator<<(std::ostream &os, GenericEndpoint const &endpoint)
Print "GenericEndpoint(_n_)" to the ostream where _n_ is the number of axes, e.g. "GenericAxes(4)"...
Definition: Endpoint.cc:240
std::shared_ptr< Frame > getFrame(int iframe, bool copy=true) const
Obtain a deep copy of the specified Frame.
Definition: FrameSet.h:270
Point pointFromData(std::vector< double > const &data) const override
Get a single point from raw data.
Definition: Endpoint.cc:138
Base class for endpoints with Array = std::vector<Point> where Point has 2 dimensions.
Definition: Endpoint.h:195
static int constexpr CURRENT
index of current frame
Definition: FrameSet.h:105
Point pointFromData(std::vector< double > const &data) const override
Get a single point from raw data.
Definition: Endpoint.cc:201
AngleUnit constexpr radians
constant with units of radians
Definition: Angle.h:108
STL class.
Array arrayFromData(ndarray::Array< double, 2, 2 > const &data) const override
Get an array of points from raw data.
Definition: Endpoint.cc:148
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:222
SkyFrame is a specialised form of Frame which describes celestial longitude/latitude coordinate syste...
Definition: SkyFrame.h:66
A base class for image defects.
char * data
Definition: BaseRecord.cc:62
A generic endpoint for data in the format used by ast::Mapping.
Definition: Endpoint.h:226
std::vector< double > dataFromPoint(Point const &point) const override
Definition: Endpoint.cc:178
T str(T... args)
An endpoint for lsst::geom::SpherePoint.
Definition: Endpoint.h:315
T dynamic_pointer_cast(T... args)
SpherePointEndpoint()
Construct a SpherePointEndpoint.
Definition: Endpoint.h:325
std::vector< double > dataFromPoint(Point const &point) const override
Definition: Endpoint.cc:115
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:218
int getNPoints(Array const &arr) const override
Definition: Endpoint.cc:83
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Array arrayFromData(ndarray::Array< double, 2, 2 > const &data) const override
Get an array of points from raw data.
Definition: Endpoint.cc:206
STL class.
Point in an unspecified spherical coordinate system.
Definition: SpherePoint.h:57
Reports invalid arguments.
Definition: Runtime.h:66
lsst::geom::SpherePoint SpherePoint
Definition: misc.h:35
An endpoint for lsst::geom::Point2D.
Definition: Endpoint.h:261
ItemVariant const * other
Definition: Schema.cc:56
void normalizeFrame(std::shared_ptr< ast::Frame > framePtr) const override
Check that framePtr points to a Frame, not a subclass.
Definition: Endpoint.cc:160
STL class.
A FrameSet consists of a set of one or more Frames (which describe coordinate systems), connected together by Mappings (which describe how the coordinate systems are inter-related).
Definition: FrameSet.h:99
BaseEndpoint(BaseEndpoint const &)=default
std::ostream * os
Definition: Schema.cc:746
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