Loading [MathJax]/extensions/tex2jax.js
LSST Applications g04a91732dc+7fec47d7bc,g07dc498a13+5ab4d22ec3,g0fba68d861+565de8e5d5,g1409bbee79+5ab4d22ec3,g1a7e361dbc+5ab4d22ec3,g1fd858c14a+11200c7927,g20f46db602+25d63fd678,g35bb328faa+fcb1d3bbc8,g4d2262a081+61302e889d,g4d39ba7253+d05e267ece,g4e0f332c67+5d362be553,g53246c7159+fcb1d3bbc8,g60b5630c4e+d05e267ece,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g8048e755c2+a1301e4c20,g8852436030+163ceb82d8,g89139ef638+5ab4d22ec3,g89e1512fd8+fbb808ebce,g8d6b6b353c+d05e267ece,g9125e01d80+fcb1d3bbc8,g989de1cb63+5ab4d22ec3,g9f33ca652e+8abe617c77,ga9baa6287d+d05e267ece,gaaedd4e678+5ab4d22ec3,gabe3b4be73+1e0a283bba,gb1101e3267+fefe9ce5b1,gb58c049af0+f03b321e39,gb90eeb9370+824c420ec4,gc741bbaa4f+77ddc33078,gcf25f946ba+163ceb82d8,gd315a588df+0f88d5218e,gd6cbbdb0b4+c8606af20c,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+e6bd566e97,ge278dab8ac+932305ba37,ge82c20c137+76d20ab76d,w.2025.10
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Transform.cc
Go to the documentation of this file.
1/*
2 * LSST Data Management System
3 * Copyright 2008-2017 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 <memory>
24#include <iostream>
25#include <sstream>
26#include <vector>
27
28#include "astshim.h"
38
39namespace lsst {
40namespace afw {
41namespace geom {
42
43template <class FromEndpoint, class ToEndpoint>
45 : _fromEndpoint(mapping.getNIn()),
46 _mapping(simplify ? mapping.simplified() : mapping.copy()),
47 _toEndpoint(mapping.getNOut()) {}
48
49template <typename FromEndpoint, typename ToEndpoint>
51 : _fromEndpoint(frameSet.getNIn()), _mapping(), _toEndpoint(frameSet.getNOut()) {
52 auto frameSetCopy = frameSet.copy();
53 // Normalize the base and current frame in a way that affects its behavior as a mapping.
54 // To do this one must set the current frame to the frame to be normalized
55 // and normalize the frame set as a frame (i.e. normalize the frame "in situ").
56 // The obvious alternative of normalizing a shallow copy of the frame does not work;
57 // the frame is altered but not the associated mapping!
58
59 // Normalize the current frame by normalizing the frameset as a frame
60 _toEndpoint.normalizeFrame(frameSetCopy);
61
62 // Normalize the base frame by temporarily making it the current frame,
63 // normalizing the frameset as a frame, then making it the base frame again
64 const int baseIndex = frameSetCopy->getBase();
65 const int currentIndex = frameSetCopy->getCurrent();
66 frameSetCopy->setCurrent(baseIndex);
67 _fromEndpoint.normalizeFrame(frameSetCopy);
68 frameSetCopy->setBase(baseIndex);
69 frameSetCopy->setCurrent(currentIndex);
70 _mapping = simplify ? frameSetCopy->getMapping()->simplified() : frameSetCopy->getMapping();
71}
72
73template <typename FromEndpoint, typename ToEndpoint>
75 : _fromEndpoint(mapping->getNIn()), _mapping(mapping), _toEndpoint(mapping->getNOut()) {}
76
77template <class FromEndpoint, class ToEndpoint>
79 typename FromEndpoint::Point const &point) const {
80 auto const rawFromData = _fromEndpoint.dataFromPoint(point);
81 auto rawToData = _mapping->applyForward(rawFromData);
82 return _toEndpoint.pointFromData(rawToData);
83}
84
85template <class FromEndpoint, class ToEndpoint>
87 typename FromEndpoint::Array const &array) const {
88 auto const rawFromData = _fromEndpoint.dataFromArray(array);
89 auto rawToData = _mapping->applyForward(rawFromData);
90 return _toEndpoint.arrayFromData(rawToData);
92
93template <class FromEndpoint, class ToEndpoint>
94typename FromEndpoint::Point Transform<FromEndpoint, ToEndpoint>::applyInverse(
95 typename ToEndpoint::Point const &point) const {
96 auto const rawFromData = _toEndpoint.dataFromPoint(point);
97 auto rawToData = _mapping->applyInverse(rawFromData);
98 return _fromEndpoint.pointFromData(rawToData);
99}
100
101template <class FromEndpoint, class ToEndpoint>
102typename FromEndpoint::Array Transform<FromEndpoint, ToEndpoint>::applyInverse(
103 typename ToEndpoint::Array const &array) const {
104 auto const rawFromData = _toEndpoint.dataFromArray(array);
105 auto rawToData = _mapping->applyInverse(rawFromData);
106 return _fromEndpoint.arrayFromData(rawToData);
107}
108
109template <class FromEndpoint, class ToEndpoint>
111 auto inverse = std::dynamic_pointer_cast<ast::Mapping>(_mapping->inverted());
112 if (!inverse) {
113 // don't throw std::bad_cast because it doesn't let you provide debugging info
114 std::ostringstream buffer;
115 buffer << "Mapping.inverted() does not return a Mapping. Called from: " << _mapping;
117 }
119}
120
121template <class FromEndpoint, class ToEndpoint>
123 int const nIn = _fromEndpoint.getNAxes();
124 int const nOut = _toEndpoint.getNAxes();
125 std::vector<double> const point = _fromEndpoint.dataFromPoint(x);
126
127 Eigen::MatrixXd jacobian(nOut, nIn);
128 for (int i = 0; i < nOut; ++i) {
129 for (int j = 0; j < nIn; ++j) {
130 jacobian(i, j) = _mapping->rate(point, i + 1, j + 1);
131 }
132 }
133 return jacobian;
134}
135
136template <class FromEndpoint, class ToEndpoint>
139 os << "Transform" << FromEndpoint::getClassPrefix() << "To" << ToEndpoint::getClassPrefix();
140 return os.str();
141}
142
143template <class FromEndpoint, class ToEndpoint>
148
149template <class FromEndpoint, class ToEndpoint>
155
156template <class FromEndpoint, class ToEndpoint>
160
161template <class FromEndpoint, class ToEndpoint>
167
168template <class FromEndpoint, class ToEndpoint>
169template <class NextToEndpoint>
171 Transform<ToEndpoint, NextToEndpoint> const &next, bool simplify) const {
172 if (_toEndpoint.getNAxes() == next.getFromEndpoint().getNAxes()) {
173 auto nextMapping = next.getMapping();
174 auto combinedMapping = getMapping()->then(*next.getMapping());
175 if (simplify) {
176 return std::make_shared<Transform<FromEndpoint, NextToEndpoint>>(*combinedMapping.simplified());
177 } else {
179 }
180 } else {
181 auto message = "Cannot match " + std::to_string(_toEndpoint.getNAxes()) + "-D to-endpoint to " +
182 std::to_string(next.getFromEndpoint().getNAxes()) + "-D from-endpoint.";
184 }
185}
186
187template <class FromEndpoint, class ToEndpoint>
189 os << "Transform<" << transform.getFromEndpoint() << ", " << transform.getToEndpoint() << ">";
190 return os;
191};
192
193namespace {
194
195class TransformPersistenceHelper {
196public:
197 table::Schema schema;
199
200 static TransformPersistenceHelper const &get() {
201 static TransformPersistenceHelper instance;
202 return instance;
203 }
204
205 // No copying
206 TransformPersistenceHelper(TransformPersistenceHelper const &) = delete;
207 TransformPersistenceHelper &operator=(TransformPersistenceHelper const &) = delete;
208
209 // No moving
210 TransformPersistenceHelper(TransformPersistenceHelper &&) = delete;
211 TransformPersistenceHelper &operator=(TransformPersistenceHelper &&) = delete;
212
213private:
214 TransformPersistenceHelper()
215 : schema(),
216 bytes(schema.addField<table::Array<std::uint8_t>>(
217 "bytes", "a bytestring containing the output of Transform.writeString", "")) {}
218};
219
220template <typename FromEndpoint, typename ToEndpoint>
221class TransformFactory : public table::io::PersistableFactory {
222public:
223 explicit TransformFactory(std::string const &name) : table::io::PersistableFactory(name) {}
226 CatalogVector const &catalogs) const override {
227 auto const &keys = TransformPersistenceHelper::get();
228 LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
229 LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
230 LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
231 auto const &record = catalogs.front().front();
232 std::string stringRep = formatters::bytesToString(record.get(keys.bytes));
234 }
236
237} // namespace
238
239template <class FromEndpoint, class ToEndpoint>
241 auto const &keys = TransformPersistenceHelper::get();
242 table::BaseCatalog cat = handle.makeCatalog(keys.schema);
244 record->set(keys.bytes, formatters::stringToBytes(writeString()));
245 handle.saveCatalog(cat);
246}
247
248#define INSTANTIATE_OVERLOADS(FromEndpoint, ToEndpoint, NextToEndpoint) \
249 template std::shared_ptr<Transform<FromEndpoint, NextToEndpoint>> \
250 Transform<FromEndpoint, ToEndpoint>::then<NextToEndpoint>( \
251 Transform<ToEndpoint, NextToEndpoint> const &next, bool) const;
252
253#define INSTANTIATE_TRANSFORM(FromEndpoint, ToEndpoint) \
254 } /* namespace geom */ \
255 template std::shared_ptr<geom::Transform<geom::FromEndpoint, geom::ToEndpoint>> \
256 table::io::PersistableFacade<geom::Transform<geom::FromEndpoint, geom::ToEndpoint>>::dynamicCast( \
257 std::shared_ptr<table::io::Persistable> const &); \
258 namespace geom { \
259 template class Transform<FromEndpoint, ToEndpoint>; \
260 template std::ostream &operator<<<FromEndpoint, ToEndpoint>( \
261 std::ostream &os, Transform<FromEndpoint, ToEndpoint> const &transform); \
262 namespace { \
263 TransformFactory<FromEndpoint, ToEndpoint> registration##FromEndpoint##ToEndpoint( \
264 Transform<FromEndpoint, ToEndpoint>::getShortClassName()); \
265 } /* namespace */ \
266 INSTANTIATE_OVERLOADS(FromEndpoint, ToEndpoint, GenericEndpoint) \
267 INSTANTIATE_OVERLOADS(FromEndpoint, ToEndpoint, Point2Endpoint) \
268 INSTANTIATE_OVERLOADS(FromEndpoint, ToEndpoint, SpherePointEndpoint)
269
270// explicit instantiations
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition Persistable.h:48
#define INSTANTIATE_TRANSFORM(FromEndpoint, ToEndpoint)
Definition Transform.cc:253
A FrameSet consists of a set of one or more Frames (which describe coordinate systems),...
Definition FrameSet.h:99
std::shared_ptr< FrameSet > copy() const
Return a deep copy of this object.
Definition FrameSet.h:147
An abstract base class for objects which transform one set of coordinates to another.
Definition Mapping.h:59
A generic endpoint for data in the format used by ast::Mapping.
Definition Endpoint.h:226
An endpoint for lsst::geom::Point2D.
Definition Endpoint.h:261
An endpoint for lsst::geom::SpherePoint.
Definition Endpoint.h:315
Transform LSST spatial data, such as lsst::geom::Point2D and lsst::geom::SpherePoint,...
Definition Transform.h:68
typename FromEndpoint::Point FromPoint
Definition Transform.h:74
std::shared_ptr< const ast::Mapping > getMapping() const
Get the contained mapping.
Definition Transform.h:135
static std::shared_ptr< Transform< FromEndpoint, ToEndpoint > > readStream(std::istream &is)
Deserialize a Transform of this type from an input stream.
Definition Transform.cc:144
FromPoint applyInverse(ToPoint const &point) const
Transform one point in the inverse direction ("to" to "from")
static std::string getShortClassName()
Return a short version of the class name with no punctuation.
Definition Transform.cc:137
std::shared_ptr< Transform< ToEndpoint, FromEndpoint > > inverted() const
The inverse of this Transform.
Definition Transform.cc:110
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition Transform.cc:240
Eigen::MatrixXd getJacobian(FromPoint const &x) const
The Jacobian matrix of this Transform.
Definition Transform.cc:122
std::string writeString() const
Serialize this Transform to a string, using the same format as writeStream.
Definition Transform.cc:162
Transform(Transform const &)=default
static std::shared_ptr< Transform< FromEndpoint, ToEndpoint > > readString(std::string &str)
Deserialize a Transform of this type from a string, using the same format as readStream.
Definition Transform.cc:150
std::shared_ptr< Transform< FromEndpoint, NextToEndpoint > > then(Transform< ToEndpoint, NextToEndpoint > const &next, bool simplify=true) const
Concatenate two Transforms.
Definition Transform.cc:170
ToPoint applyForward(FromPoint const &point) const
Transform one point in the forward direction ("from" to "to")
void writeStream(std::ostream &os) const
Serialize this Transform to an output stream.
Definition Transform.cc:157
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition Catalog.h:489
A class used as a handle to a particular field in a table.
Definition Key.h:53
Defines the fields and offsets for a table.
Definition Schema.h:51
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
io::OutputArchiveHandle OutputArchiveHandle
Reports invalid arguments.
Definition Runtime.h:66
Reports errors in the logical structure of the program.
Definition Runtime.h:46
T make_shared(T... args)
ndarray::Array< std::uint8_t, 1, 1 > stringToBytes(std::string const &str)
Encode a std::string as a vector of uint8.
Definition Utils.cc:43
std::string bytesToString(ndarray::Array< std::uint8_t const, 1, 1 > const &bytes)
Decode a std::string from a vector of uint8 returned by stringToBytes.
Definition Utils.cc:54
void writeStream(Transform const &transform, std::ostream &os)
Serialize a Transform to an output stream.
std::shared_ptr< Transform > readStream(std::istream &is)
Deserialize a Transform from an input stream.
std::ostream & operator<<(std::ostream &os, GenericEndpoint const &endpoint)
Print "GenericEndpoint(_n_)" to the ostream where _n_ is the number of axes, e.g. "GenericAxes(4)".
Definition Endpoint.cc:239
CatalogT< BaseRecord > BaseCatalog
Definition fwd.h:72
STL namespace.
T dynamic_pointer_cast(T... args)
T str(T... args)
T to_string(T... args)
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override