lsst.afw  13.0-92-g8ecf155+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TransformMapImpl.h
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  * This file must be be included by any code that instantiates a templated version of TransformMap;
24  * failure to do so will result in linker errors.
25  */
26 #include <sstream>
27 #include <utility>
28 #include <memory>
29 #include "lsst/pex/exceptions.h"
30 
31 namespace lsst {
32 namespace afw {
33 namespace geom {
34 
35 template <typename CoordSysT>
36 TransformMap<CoordSysT>::TransformMap(CoordSysT const &nativeCoordSys, Transforms const &transforms)
37  : _nativeCoordSys(nativeCoordSys), _transforms() {
38  for (typename Transforms::const_iterator trIter = transforms.begin(); trIter != transforms.end();
39  ++trIter) {
40  if (_transforms.count(trIter->first) > 0) {
41  std::ostringstream os;
42  os << "Duplicate coordSys \"" << trIter->first << "\"";
43  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
44  } else if (trIter->first == _nativeCoordSys) {
45  std::ostringstream os;
46  os << "coordSys \"" << trIter->first << "\" matches nativeCoordSys";
47  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
48  }
49  _transforms.insert(*trIter);
50  }
51 
52  // insert identity transform for nativeCoordSys, if not already provided
53  if (!contains(nativeCoordSys)) {
54  _transforms.insert(std::make_pair(nativeCoordSys, std::make_shared<IdentityXYTransform>()));
55  }
56 }
57 
58 template <typename CoordSysT>
59 TransformMap<CoordSysT>::TransformMap() : _nativeCoordSys(), _transforms() {}
60 
61 template <typename CoordSysT>
62 Point2D TransformMap<CoordSysT>::transform(Point2D const &fromPoint, CoordSysT const &fromCoordSys,
63  CoordSysT const &toCoordSys) const {
64  if (fromCoordSys == toCoordSys) {
65  return fromPoint;
66  }
67 
68  // transform fromSys -> nativeSys -> toSys
69  std::shared_ptr<XYTransform const> fromTransform = (*this)[fromCoordSys];
70  std::shared_ptr<XYTransform const> toTransform = (*this)[toCoordSys];
71  return toTransform->forwardTransform(fromTransform->reverseTransform(fromPoint));
72 }
73 
74 template <typename CoordSysT>
75 std::vector<Point2D> TransformMap<CoordSysT>::transform(std::vector<Point2D> const &pointList,
76  CoordSysT const &fromCoordSys,
77  CoordSysT const &toCoordSys) const {
78  if (fromCoordSys == toCoordSys) {
79  return pointList;
80  }
81 
82  std::vector<Point2D> outList;
83 
84  // transform pointList from fromCoordSys to native coords, filling outList
85  if (fromCoordSys != _nativeCoordSys) {
86  std::shared_ptr<XYTransform const> fromTransform = (*this)[fromCoordSys];
87  for (std::vector<Point2D>::const_iterator fromPtIter = pointList.begin();
88  fromPtIter != pointList.end(); ++fromPtIter) {
89  outList.push_back(fromTransform->reverseTransform(*fromPtIter));
90  }
91  } else {
92  for (std::vector<Point2D>::const_iterator fromPtIter = pointList.begin();
93  fromPtIter != pointList.end(); ++fromPtIter) {
94  outList.push_back(*fromPtIter);
95  }
96  }
97 
98  // transform outList from native coords to toCoordSys, in place
99  if (toCoordSys != _nativeCoordSys) {
100  std::shared_ptr<XYTransform const> toTransform = (*this)[toCoordSys];
101  for (std::vector<Point2D>::iterator nativePtIter = outList.begin(); nativePtIter != outList.end();
102  ++nativePtIter) {
103  *nativePtIter = toTransform->forwardTransform(*nativePtIter);
104  }
105  }
106  return outList;
107 }
108 
109 template <typename CoordSysT>
110 std::vector<CoordSysT> TransformMap<CoordSysT>::getCoordSysList() const {
111  std::vector<CoordSysT> coordSysList;
112  for (typename Transforms::const_iterator trIter = _transforms.begin(); trIter != _transforms.end();
113  ++trIter) {
114  coordSysList.push_back(trIter->first);
115  }
116  return coordSysList;
117 }
118 
119 template <typename CoordSysT>
120 std::shared_ptr<XYTransform const> TransformMap<CoordSysT>::operator[](CoordSysT const &coordSys) const {
121  typename Transforms::const_iterator const foundIter = _transforms.find(coordSys);
122  if (foundIter == _transforms.end()) {
123  std::ostringstream os;
124  os << "Registry does not support coordSys \"" << coordSys << "\"";
125  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
126  }
127  return foundIter->second;
128 }
129 
130 template <typename CoordSysT>
131 bool TransformMap<CoordSysT>::contains(CoordSysT const &coordSys) const {
132  return _transforms.find(coordSys) != _transforms.end();
133 }
134 }
135 }
136 }
std::shared_ptr< XYTransform const > operator[](CoordSysT const &coordSys) const
Get an XYTransform that transforms from coordSys to nativeCoordSys in the forward direction...
std::vector< CoordSysT > getCoordSysList() const
Get a list of supported coordinate systems.
bool contains(CoordSysT const &coordSys) const
Return true if the coordinate system is supported.
std::map< CoordSysT, std::shared_ptr< XYTransform const > > Transforms
Definition: TransformMap.h:67
Point2D transform(Point2D const &fromPoint, CoordSysT const &fromSys, CoordSysT const &toCoordSys) const
Convert a point from one coordinate system to another.
TransformMap()
null implementation to make SWIG willing to wrap a map that contains these