lsst.afw  14.0-25-g0e8fc950e+3
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());
56  auto result = std::unique_ptr<ast::FrameSet>(new ast::FrameSet(rootFrame));
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 = value->getFrameSet()->getFrame(ast::FrameSet::CURRENT);
79  toFrame->setIdent(key.getSysName());
80  result->addFrame(ast::FrameSet::BASE, *(value->getFrameSet()->getMapping()), *toFrame);
81  }
82  return result;
83 }
84 
85 /*
86  * Make a map of camera system: frame number for a FrameSet constructed by makeTransforms.
87  *
88  * @tparam Map Any type satisfying the STL map API and mapping CameraSys to
89  * shared_ptr<geom::TransformPoint2ToPoint2>.
90  *
91  * @param reference Coordinate system from which each Transform in `transforms` converts.
92  * @param transforms A map whose keys are camera coordinate systems, and whose values
93  * point to Transforms that convert from `reference` to the corresponding key.
94  * All Transforms must be invertible.
95  * @return a map from `reference` and each key in `transforms` to the corresponding frame number
96  * in the ast::FrameSet returned by `makeTransforms(reference, transforms)`.
97  *
98  * @warning Does not perform input validation.
99  */
100 template <class Map>
101 std::unordered_map<CameraSys, int> makeTranslator(CameraSys const &reference, Map const &transforms) {
102  std::unordered_map<CameraSys, int> result({std::make_pair(reference, 1)});
103  int nFrames = 1;
104 
105  for (auto const & keyValue : transforms) {
106  CameraSys const key = keyValue.first;
107  result.emplace(key, ++nFrames);
108  }
109  return result;
110 }
111 
112 } // namespace
113 
114 geom::Point2Endpoint TransformMap::_pointConverter;
115 
118  : _transforms(makeTransforms(reference, transforms)),
119  _frameIds(makeTranslator(reference, transforms)) {}
120 
121 // TransformMap is immutable, so we can just copy the shared_ptr
122 TransformMap::TransformMap(TransformMap const &other) = default;
123 
124 // Cannot do any move optimizations without breaking immutability
126 
128  CameraSys const &toSys) const {
129  auto mapping = _getMapping(fromSys, toSys);
130  return _pointConverter.pointFromData(mapping->applyForward(_pointConverter.dataFromPoint(point)));
131 }
132 
134  CameraSys const &fromSys, CameraSys const &toSys) const {
135  auto mapping = _getMapping(fromSys, toSys);
136  return _pointConverter.arrayFromData(mapping->applyForward(_pointConverter.dataFromArray(pointList)));
137 }
138 
139 bool TransformMap::contains(CameraSys const &system) const noexcept { return _frameIds.count(system) > 0; }
140 
142  CameraSys const &toSys) const {
143  return std::make_shared<geom::TransformPoint2ToPoint2>(*_getMapping(fromSys, toSys));
144 }
145 
146 int TransformMap::_getFrame(CameraSys const &system) const {
147  try {
148  return _frameIds.at(system);
149  } catch (std::out_of_range const &e) {
150  std::ostringstream buffer;
151  buffer << "Unsupported coordinate system: " << system;
153  }
154 }
155 
156 std::shared_ptr<ast::Mapping const> TransformMap::_getMapping(CameraSys const &fromSys,
157  CameraSys const &toSys) const {
158  return _transforms->getMapping(_getFrame(fromSys), _getFrame(toSys));
159 }
160 
161 size_t TransformMap::size() const noexcept { return _frameIds.size(); }
162 } // namespace cameraGeom
163 } // namespace afw
164 } // 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:77
TransformMap(CameraSys const &reference, std::unordered_map< CameraSys, std::shared_ptr< geom::TransformPoint2ToPoint2 >> const &transforms)
Define a set of camera transforms.
virtual ndarray::Array< double, 2, 2 > dataFromArray(Array const &arr) const override
Get raw data from an array of points.
virtual Point pointFromData(std::vector< double > const &data) const override
Get a single point from raw data.
virtual Array arrayFromData(ndarray::Array< double, 2, 2 > const &data) const override
Get an array of points from raw data.
static int constexpr CURRENT
T at(T... args)
A base class for image defects.
T throw_with_nested(T... args)
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?
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.
T emplace(T... args)
static int constexpr BASE
geom::Point2D transform(geom::Point2D const &point, CameraSys const &fromSys, CameraSys const &toSys) const
Convert a point from one camera coordinate system to another.
virtual std::vector< double > dataFromPoint(Point const &point) const override
Get raw data from a single point.
std::shared_ptr< geom::TransformPoint2ToPoint2 > getTransform(CameraSys const &fromSys, CameraSys const &toSys) const
Get a Transform from one camera coordinate system to another.