LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
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
char * data
Definition: BaseTable.cc:205
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:284
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:268
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.
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