lsst.afw  13.0-100-ge0459d59f+6
transformUtils.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2017 AURA/LSST.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #ifndef LSST_AFW_GEOM_TRANSFORMUTILS_H
25 #define LSST_AFW_GEOM_TRANSFORMUTILS_H
26 
27 #include <memory>
28 #include <vector>
29 
30 #include "astshim.h"
31 #include "ndarray.h"
32 
33 #include "lsst/afw/coord/Coord.h"
34 #include "lsst/afw/geom/Endpoint.h"
36 #include "lsst/daf/base/PropertyList.h"
37 
38 namespace lsst {
39 namespace afw {
40 namespace geom {
41 namespace detail {
43 constexpr int serializationVersion = 1;
44 
56 template<class Transform>
57 Transform readStream(std::istream & is);
58 
72 template<class Transform>
73 void writeStream(Transform const & transform, std::ostream & os);
74 
75 /*
76  * Provide definitions here in the header file to avoid the need for explicit instantiations
77  */
78 
79 template<class Transform>
80 Transform readStream(std::istream & is) {
81  int version;
82  is >> version;
83  if (version != 1) {
84  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
85  "Unsupported version " + std::to_string(version));
86  }
87  std::string shortClassName;
88  is >> shortClassName;
89  if (shortClassName != Transform::getShortClassName()) {
90  std::ostringstream os;
91  os << "Class name in stream " << shortClassName << " != " << Transform::getShortClassName();
92  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
93  }
94  auto astStream = ast::Stream(&is, nullptr);
95  auto astObjectPtr = ast::Channel(astStream).read();
96  auto frameSet = std::dynamic_pointer_cast<ast::FrameSet>(astObjectPtr);
97  if (!frameSet) {
98  std::ostringstream os;
99  os << "The AST serialization was read as a " << astObjectPtr->getClassName()
100  << " instead of a FrameSet";
101  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, os.str());
102  }
103 
104  return Transform(*frameSet);
105 }
106 
107 template<class Transform>
108 void writeStream(Transform const & transform, std::ostream & os) {
109  os << serializationVersion << " " << Transform::getShortClassName();
110  transform.getFrameSet()->show(os, false); // false = do not write comments
111 }
112 
113 } // namespace detail
114 } // namespace geom
115 } // namespace afw
116 } // namespace lsst
117 
118 #endif
std::shared_ptr< const ast::FrameSet > getFrameSet() const
Get the contained frameset.
Definition: Transform.h:134
void writeStream(Transform const &transform, std::ostream &os)
Serialize a Transform to an output stream.
static std::string getShortClassName()
Return a short version of the class name with no punctuation.
table::Key< std::string > frameSet
A base class for image defects.
Definition: cameraGeom.dox:3
Transform readStream(std::istream &is)
Deserialize a Transform from an input stream.
geom::Transform< geom::Point2Endpoint, geom::Point2Endpoint > Transform
Definition: Orientation.cc:29
constexpr int serializationVersion
version of serialization used when writing (older versions may also be supported when reading) ...
Transform LSST spatial data, such as Point2D and IcrsCoord, using an AST transform.
Definition: Transform.h:64