LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
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  */
28 #include <sstream>
29 #include <utility>
30 #include "boost/make_shared.hpp"
31 #include "lsst/pex/exceptions.h"
32 
33 namespace lsst {
34 namespace afw {
35 namespace geom {
36 
37 template<typename CoordSysT>
39  CoordSysT const &nativeCoordSys,
40  Transforms const &transforms
41 ) :
42  _nativeCoordSys(nativeCoordSys), _transforms()
43 {
44  for (typename Transforms::const_iterator trIter = transforms.begin();
45  trIter != transforms.end(); ++trIter) {
46  if (_transforms.count(trIter->first) > 0) {
47  std::ostringstream os;
48  os << "Duplicate coordSys \"" << trIter->first << "\"";
49  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
50  } else if (trIter->first == _nativeCoordSys) {
51  std::ostringstream os;
52  os << "coordSys \"" << trIter->first << "\" matches nativeCoordSys";
53  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
54  }
55  _transforms.insert(*trIter);
56  }
57 
58  // insert identity transform for nativeCoordSys, if not already provided
59  if (!contains(nativeCoordSys)) {
60  _transforms.insert(std::make_pair(nativeCoordSys,
61  boost::make_shared<IdentityXYTransform>()));
62  }
63 }
64 
65 template<typename CoordSysT>
66 TransformMap<CoordSysT>::TransformMap() : _nativeCoordSys(), _transforms() {}
67 
68 
69 template<typename CoordSysT>
71  Point2D const &fromPoint,
72  CoordSysT const &fromCoordSys,
73  CoordSysT const &toCoordSys
74 ) const {
75  if (fromCoordSys == toCoordSys) {
76  return fromPoint;
77  }
78 
79  // transform fromSys -> nativeSys -> toSys
80  CONST_PTR(XYTransform) fromTransform = (*this)[fromCoordSys];
81  CONST_PTR(XYTransform) toTransform = (*this)[toCoordSys];
82  return toTransform->forwardTransform(fromTransform->reverseTransform(fromPoint));
83 }
84 
85 template<typename CoordSysT>
87  std::vector<Point2D> const &pointList,
88  CoordSysT const &fromCoordSys,
89  CoordSysT const &toCoordSys
90 ) const {
91  if (fromCoordSys == toCoordSys) {
92  return pointList;
93  }
94 
95  std::vector<Point2D> outList;
96 
97  // transform pointList from fromCoordSys to native coords, filling outList
98  if (fromCoordSys != _nativeCoordSys) {
99  CONST_PTR(XYTransform) fromTransform = (*this)[fromCoordSys];
100  for (std::vector<Point2D>::const_iterator fromPtIter = pointList.begin();
101  fromPtIter != pointList.end(); ++fromPtIter) {
102  outList.push_back(fromTransform->reverseTransform(*fromPtIter));
103  }
104  } else {
105  for (std::vector<Point2D>::const_iterator fromPtIter = pointList.begin();
106  fromPtIter != pointList.end(); ++fromPtIter) {
107  outList.push_back(*fromPtIter);
108  }
109  }
110 
111  // transform outList from native coords to toCoordSys, in place
112  if (toCoordSys != _nativeCoordSys) {
113  CONST_PTR(XYTransform) toTransform = (*this)[toCoordSys];
114  for (std::vector<Point2D>::iterator nativePtIter = outList.begin();
115  nativePtIter != outList.end(); ++nativePtIter) {
116  *nativePtIter = toTransform->forwardTransform(*nativePtIter);
117  }
118  }
119  return outList;
120 }
121 
122 template<typename CoordSysT>
123 std::vector<CoordSysT> TransformMap<CoordSysT>::getCoordSysList() const {
124  std::vector<CoordSysT> coordSysList;
125  for (typename Transforms::const_iterator trIter = _transforms.begin();
126  trIter != _transforms.end(); ++trIter) {
127  coordSysList.push_back(trIter->first);
128  }
129  return coordSysList;
130 }
131 
132 template<typename CoordSysT>
133 CONST_PTR(XYTransform) TransformMap<CoordSysT>::operator[](
134  CoordSysT const &coordSys
135 ) const {
136  typename Transforms::const_iterator const foundIter = _transforms.find(coordSys);
137  if (foundIter == _transforms.end()) {
138  std::ostringstream os;
139  os << "Registry does not support coordSys \"" << coordSys << "\"";
140  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
141  }
142  return foundIter->second;
143 }
144 
145 template<typename CoordSysT>
147  CoordSysT const &coordSys
148 ) const {
149  return _transforms.find(coordSys) != _transforms.end();
150 }
151 
152 }}}
#define CONST_PTR(...)
Definition: base.h:47
std::vector< CoordSysT > getCoordSysList() const
bool contains(CoordSysT const &coordSys) const
CoordSysT _nativeCoordSys
native coordinate system
Definition: TransformMap.h:149
Transforms _transforms
map of coordSys: XYTransform
Definition: TransformMap.h:150
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
TransformMap()
null implementation to make SWIG willing to wrap a map that contains these
Virtual base class for 2D transforms.
Definition: XYTransform.h:48
std::map< CoordSysT, boost::shared_ptr< XYTransform const > > Transforms
Definition: TransformMap.h:67
Point2D transform(Point2D const &fromPoint, CoordSysT const &fromSys, CoordSysT const &toCoordSys) const
Include files required for standard LSST Exception handling.