lsst.afw  16.0-12-gf08c31002
TransformMap.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2014 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 <exception>
24 #include <memory>
25 #include <sstream>
26 #include <type_traits>
27 
28 #include "lsst/pex/exceptions.h"
30 
31 namespace lsst {
32 namespace afw {
33 namespace cameraGeom {
34 
35 namespace {
36 
37 /*
38  * Make a FrameSet from a map of camera system: transform
39  *
40  * @tparam Map Any type satisfying the STL map API and mapping CameraSys to
41  * shared_ptr<geom::TransformPoint2ToPoint2>.
42  *
43  * @param reference Coordinate system from which each Transform in `transforms` converts.
44  * @param transforms A map whose keys are camera coordinate systems, and whose values
45  * point to Transforms that convert from `reference` to the corresponding key.
46  * All Transforms must be invertible.
47  * @return an ast::FrameSet containing one ast::Frame(2) for `reference` and an ast::Frame(2)
48  * for each Transform in `transforms`, connected by suitable mappings.
49  *
50  * @throws lsst::pex::exceptions::InvalidParameterError Thrown if `transforms` contains
51  * the `reference` camera system as a key, or if any Transform is not invertible.
52  */
53 template <class Map>
54 std::unique_ptr<ast::FrameSet> makeTransforms(CameraSys const &reference, Map const &transforms) {
55  ast::Frame rootFrame(2, "Ident=" + reference.getSysName());
57 
58  for (auto const &keyValue : transforms) {
59  CameraSys const key = keyValue.first;
60  std::shared_ptr<geom::TransformPoint2ToPoint2> const value = keyValue.second;
61 
62  if (key == reference) {
63  std::ostringstream buffer;
64  buffer << "Cannot specify a Transform from " << reference << " to itself.";
65  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, buffer.str());
66  }
67  if (!value->hasForward()) {
68  std::ostringstream buffer;
69  buffer << *value << " from " << key << " has no forward transform.";
70  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, buffer.str());
71  }
72  if (!value->hasInverse()) {
73  std::ostringstream buffer;
74  buffer << *value << " from " << key << " has no inverse transform.";
75  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, buffer.str());
76  }
77 
78  auto toFrame = ast::Frame(2, "Ident=" + key.getSysName());
79  result->addFrame(ast::FrameSet::BASE, *(value->getMapping()), toFrame);
80  }
81  return result;
82 }
83 
84 /*
85  * Make a map of camera system: frame number for a FrameSet constructed by makeTransforms.
86  *
87  * @tparam Map Any type satisfying the STL map API and mapping CameraSys to
88  * shared_ptr<geom::TransformPoint2ToPoint2>.
89  *
90  * @param reference Coordinate system from which each Transform in `transforms` converts.
91  * @param transforms A map whose keys are camera coordinate systems, and whose values
92  * point to Transforms that convert from `reference` to the corresponding key.
93  * All Transforms must be invertible.
94  * @return a map from `reference` and each key in `transforms` to the corresponding frame number
95  * in the ast::FrameSet returned by `makeTransforms(reference, transforms)`.
96  *
97  * @warning Does not perform input validation.
98  */
99 template <class Map>
100 std::unordered_map<CameraSys, int> makeTranslator(CameraSys const &reference, Map const &transforms) {
102  int nFrames = 1;
103 
104  for (auto const &keyValue : transforms) {
105  CameraSys const key = keyValue.first;
106  result.emplace(key, ++nFrames);
107  }
108  return result;
109 }
110 
111 } // namespace
112 
113 lsst::afw::geom::Point2Endpoint TransformMap::_pointConverter;
114 
116  CameraSys const &reference,
118  : _transforms(makeTransforms(reference, transforms)),
119  _frameIds(makeTranslator(reference, transforms)) {}
120 
121 // TransformMap is immutable, so we can just copy the shared_ptr
123 
124 // Cannot do any move optimizations without breaking immutability
126 
127 // All resources owned by value or by smart pointer
128 TransformMap::~TransformMap() = default;
129 
131  CameraSys const &toSys) const {
132  auto mapping = _getMapping(fromSys, toSys);
133  return _pointConverter.pointFromData(mapping->applyForward(_pointConverter.dataFromPoint(point)));
134 }
135 
137  CameraSys const &fromSys,
138  CameraSys const &toSys) const {
139  auto mapping = _getMapping(fromSys, toSys);
140  return _pointConverter.arrayFromData(mapping->applyForward(_pointConverter.dataFromArray(pointList)));
141 }
142 
143 bool TransformMap::contains(CameraSys const &system) const noexcept { return _frameIds.count(system) > 0; }
144 
146  CameraSys const &toSys) const {
147  return std::make_shared<geom::TransformPoint2ToPoint2>(*_getMapping(fromSys, toSys));
148 }
149 
150 int TransformMap::_getFrame(CameraSys const &system) const {
151  try {
152  return _frameIds.at(system);
153  } catch (std::out_of_range const &e) {
154  std::ostringstream buffer;
155  buffer << "Unsupported coordinate system: " << system;
157  }
158 }
159 
160 std::shared_ptr<ast::Mapping const> TransformMap::_getMapping(CameraSys const &fromSys,
161  CameraSys const &toSys) const {
162  return _transforms->getMapping(_getFrame(fromSys), _getFrame(toSys));
163 }
164 
165 size_t TransformMap::size() const noexcept { return _frameIds.size(); }
166 } // namespace cameraGeom
167 } // namespace afw
168 } // namespace lsst
A registry of 2-dimensional coordinate transforms for a specific camera.
Definition: TransformMap.h:57
Camera coordinate system; used as a key in in TransformMap.
Definition: CameraSys.h:83
TransformMap(CameraSys const &reference, std::unordered_map< CameraSys, std::shared_ptr< geom::TransformPoint2ToPoint2 >> const &transforms)
Define a set of camera transforms.
virtual Point pointFromData(std::vector< double > const &data) const override
Get a single point from raw data.
Definition: Endpoint.cc:138
ItemVariant const * other
Definition: Schema.cc:55
lsst::geom::Point2D transform(lsst::geom::Point2D const &point, CameraSys const &fromSys, CameraSys const &toSys) const
Convert a point from one camera coordinate system to another.
T at(T... args)
virtual Array arrayFromData(ndarray::Array< double, 2, 2 > const &data) const override
Get an array of points from raw data.
Definition: Endpoint.cc:148
A base class for image defects.
T throw_with_nested(T... args)
Key< U > key
Definition: Schema.cc:280
T str(T... args)
T make_pair(T... args)
bool contains(CameraSys const &system) const noexcept
Can this transform to and from the specified coordinate system?
virtual std::vector< double > dataFromPoint(Point const &point) const override
Definition: Endpoint.cc:115
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:194
T count(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
STL class.
STL class.
size_t size() const noexcept
Get the number of supported coordinate systems.
An endpoint for lsst::geom::Point2D.
Definition: Endpoint.h:261
static int constexpr BASE
virtual ndarray::Array< double, 2, 2 > dataFromArray(Array const &arr) const override
Definition: Endpoint.cc:124
std::shared_ptr< geom::TransformPoint2ToPoint2 > getTransform(CameraSys const &fromSys, CameraSys const &toSys) const
Get a Transform from one camera coordinate system to another.