lsst.afw  16.0-51-gb4b4a8566+2
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 #include <set>
28 
29 #include "boost/optional.hpp"
30 
31 #include "lsst/pex/exceptions.h"
33 
34 namespace lsst {
35 namespace afw {
36 namespace cameraGeom {
37 
39  CameraSys const & reference,
40  Transforms const & transforms
41 ) {
42  return Builder(reference).connect(transforms).build();
43 }
44 
45 lsst::afw::geom::Point2Endpoint TransformMap::_pointConverter;
46 
47 // All resources owned by value or by smart pointer
48 TransformMap::~TransformMap() noexcept = default;
49 
51  CameraSys const &toSys) const {
52  auto mapping = _getMapping(fromSys, toSys);
53  return _pointConverter.pointFromData(mapping->applyForward(_pointConverter.dataFromPoint(point)));
54 }
55 
57  CameraSys const &fromSys,
58  CameraSys const &toSys) const {
59  auto mapping = _getMapping(fromSys, toSys);
60  return _pointConverter.arrayFromData(mapping->applyForward(_pointConverter.dataFromArray(pointList)));
61 }
62 
63 bool TransformMap::contains(CameraSys const &system) const noexcept { return _frameIds.count(system) > 0; }
64 
66  CameraSys const &toSys) const {
67  return std::make_shared<geom::TransformPoint2ToPoint2>(*_getMapping(fromSys, toSys));
68 }
69 
70 int TransformMap::_getFrame(CameraSys const &system) const {
71  try {
72  return _frameIds.at(system);
73  } catch (std::out_of_range const &e) {
74  std::ostringstream buffer;
75  buffer << "Unsupported coordinate system: " << system;
77  }
78 }
79 
80 std::shared_ptr<ast::Mapping const> TransformMap::_getMapping(CameraSys const &fromSys,
81  CameraSys const &toSys) const {
82  return _transforms->getMapping(_getFrame(fromSys), _getFrame(toSys));
83 }
84 
85 size_t TransformMap::size() const noexcept { return _frameIds.size(); }
86 
88  _transforms(std::move(transforms)),
89  _frameIds(std::move(frameIds))
90 {}
91 
92 
93 TransformMap::Builder::Builder(CameraSys const & reference) : _reference(reference) {}
94 
95 TransformMap::Builder::Builder(Builder const &) = default;
97 
100 
101 TransformMap::Builder::~Builder() noexcept = default;
102 
104  CameraSys const & fromSys,
105  CameraSys const & toSys,
107 ) {
108  if (fromSys == toSys) {
109  std::ostringstream buffer;
110  buffer << "Identity connection detected for " << fromSys << ".";
112  }
113  if (!transform->hasForward()) {
114  std::ostringstream buffer;
115  buffer << "Connection from " << fromSys << " to "
116  << toSys << " has no forward transform.";
118  }
119  if (!transform->hasInverse()) {
120  std::ostringstream buffer;
121  buffer << "Connection from " << fromSys << " to "
122  << toSys << " has no inverse transform.";
124  }
125  _connections.push_back(Connection{false, std::move(transform), fromSys, toSys});
126  return *this;
127 }
128 
129 
131  CameraSys const & fromSys,
132  Transforms const & transforms
133 ) {
134  Builder other(_reference); // temporary for strong exception safety
135  for (auto const & item : transforms) {
136  other.connect(fromSys, item.first, item.second);
137  }
138  _connections.insert(_connections.end(), other._connections.begin(), other._connections.end());
139  return *this;
140 }
141 
142 
143 namespace {
144 
145 /*
146  * RAII object that just executes a functor in its destructor.
147  */
148 template <typename Callable>
149 class OnScopeExit {
150 public:
151 
152  explicit OnScopeExit(Callable callable) : _callable(std::move(callable)) {}
153 
154  ~OnScopeExit() noexcept(noexcept(std::declval<Callable>())) { // noexcept iff "_callable()"" is
155  _callable();
156  }
157 
158 private:
159  Callable _callable;
160 };
161 
162 // Factory function for OnScopeExit.
163 template <typename Callable>
164 OnScopeExit<Callable> onScopeExit(Callable callable) {
165  return OnScopeExit<Callable>(std::move(callable));
166 }
167 
168 } // anonymous
169 
170 
172 
173  int nFrames = 0; // tracks frameSet->getNFrame() to avoid those (expensive) calls
174  CameraSysFrameIdMap frameIds; // mapping from CameraSys to Frame ID (int)
175 
176  // Local helper function that looks up the Frame ID for a CameraSys, with
177  // results returned via boost::optional.
178  auto findFrameIdForSys = [&frameIds](CameraSys const & sys) -> boost::optional<int> {
179  auto iter = frameIds.find(sys);
180  if (iter != frameIds.end()) {
181  return boost::optional<int>(iter->second);
182  } else {
183  return boost::none;
184  }
185  };
186 
187  // Local helper function that creates a Frame, updates the nFrames counter,
188  // and adds an entry to the frameIds map. Returns the new Frame.
189  // Should always be called in concert with an update to frameSet.
190  auto addFrameForSys = [&frameIds, &nFrames](CameraSys const & sys) mutable -> ast::Frame {
191  frameIds.emplace(sys, ++nFrames);
192  return ast::Frame(2, "Ident=" + sys.getSysName());
193  };
194 
195  // FrameSet that manages all transforms; should always be updated in
196  // concert with a call to addFrameForSys.
197  auto frameSet = std::make_unique<ast::FrameSet>(addFrameForSys(_reference));
198 
199  // RAII: make sure all 'processed' fields are reset, no matter how we exit
200  auto guard = onScopeExit(
201  [this]() noexcept {
202  for (auto const & c : _connections) { c.processed = false; }
203  }
204  );
205 
206  std::size_t nProcessedTotal = 0;
207  while (nProcessedTotal != _connections.size()) {
208 
209  // Loop over all connections, only inserting those that are connected
210  // to already-inserted connections.
211  std::size_t nProcessedThisPass = 0;
212  for (auto const & c : _connections) {
213  if (c.processed) continue;
214  auto fromId = findFrameIdForSys(c.fromSys);
215  auto toId = findFrameIdForSys(c.toSys);
216  if (fromId && toId) { // We've already inserted both fromSys and toSys. That's a problem.
217  std::ostringstream buffer;
218  buffer << "Duplicate connection from " << c.fromSys << " to " << c.toSys << ".";
220  } else if (fromId) { // We've already inserted fromSys (only)
221  frameSet->addFrame(*fromId, *c.transform->getMapping(), addFrameForSys(c.toSys));
222  c.processed = true;
223  ++nProcessedThisPass;
224  } else if (toId) { // We've already inserted toSys (only)
225  frameSet->addFrame(*toId, *c.transform->inverted()->getMapping(), addFrameForSys(c.fromSys));
226  c.processed = true;
227  ++nProcessedThisPass;
228  }
229  // If we haven't inserted either yet, just continue; hopefully
230  // we'll have inserted one in a future pass.
231  }
232 
233  if (nProcessedThisPass == 0u) { // We're not making progress, so we must have orphans
234  // Use std::set to compile the list of orphaned coordinate systems
235  // for a friendlier (unique, predictably-ordered) error message.
236  std::set<CameraSys> orphaned;
237  for (auto const & c : _connections) {
238  if (!c.processed) {
239  orphaned.insert(c.fromSys);
240  orphaned.insert(c.toSys);
241  }
242  }
243  std::ostringstream buffer;
244  auto o = orphaned.begin();
245  buffer << "Orphaned coordinate system(s) found: " << *o;
246  for (++o; o != orphaned.end(); ++o) {
247  buffer << ", " << *o;
248  }
249  buffer << ".";
251  }
252 
253  nProcessedTotal += nProcessedThisPass;
254  }
255 
256  // We've maintained our own counter for frame IDs for performance and
257  // convenience reasons, but it had better match AST's internal counter.
258  assert(frameSet->getNFrame() == nFrames);
259 
260  // Return the new TransformMap.
261  // We can't use make_shared because TransformMap ctor is private.
263 }
264 
265 
266 } // namespace cameraGeom
267 } // namespace afw
268 } // namespace lsst
Camera coordinate system; used as a key in in TransformMap.
Definition: CameraSys.h:83
Point pointFromData(std::vector< double > const &data) const override
Get a single point from raw data.
Definition: Endpoint.cc:138
T end(T... args)
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.
Definition: TransformMap.cc:50
Helper class used to incrementally construct TransformMap instances.
Definition: TransformMap.h:211
T at(T... args)
Array arrayFromData(ndarray::Array< double, 2, 2 > const &data) const override
Get an array of points from raw data.
Definition: Endpoint.cc:148
T push_back(T... args)
std::shared_ptr< TransformMap const > build() const
Construct a TransformMap from the connections in the builder.
A base class for image defects.
table::Key< table::Array< std::uint8_t > > frameSet
T throw_with_nested(T... args)
Builder(CameraSys const &reference)
Construct an empty builder with no transforms and only the given coordinate system.
Definition: TransformMap.cc:93
T str(T... args)
bool contains(CameraSys const &system) const noexcept
Can this transform to and from the specified coordinate system?
Definition: TransformMap.cc:63
T move(T... args)
std::vector< double > dataFromPoint(Point const &point) const override
Definition: Endpoint.cc:115
T count(T... args)
T insert(T... args)
static std::shared_ptr< TransformMap const > make(CameraSys const &reference, Transforms const &transforms)
Construct a TransformMap with all transforms relative to a single reference CameraSys.
Definition: TransformMap.cc:38
T find(T... args)
T size(T... args)
TransformMap(TransformMap const &other)=delete
STL class.
#define LSST_EXCEPT(type,...)
STL class.
STL class.
T begin(T... args)
size_t size() const noexcept
Get the number of supported coordinate systems.
Definition: TransformMap.cc:85
T emplace(T... args)
Builder & connect(CameraSys const &fromSys, CameraSys const &toSys, std::shared_ptr< geom::TransformPoint2ToPoint2 const > transform)
Add a new coordinate system to the builder.
An endpoint for lsst::geom::Point2D.
Definition: Endpoint.h:261
std::shared_ptr< Mapping > getMapping(int from=BASE, int to=CURRENT) const
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.
Definition: TransformMap.cc:65