-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DM-10542: Replace XYTransform::linearizeTransform #245
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// -*- lsst-c++ -*- | ||
|
||
#ifndef LSST_AFW_GEOM_TRANSFORMFACTORY_H | ||
#define LSST_AFW_GEOM_TRANSFORMFACTORY_H | ||
|
||
/* | ||
* LSST Data Management System | ||
* Copyright 2008-2017 LSST Corporation. | ||
* | ||
* This product includes software developed by the | ||
* LSST Project (http://www.lsst.org/). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the LSST License Statement and | ||
* the GNU General Public License along with this program. If not, | ||
* see <http://www.lsstcorp.org/LegalNotices/>. | ||
*/ | ||
|
||
/* | ||
* Functions for producing Transforms with commonly desired properties. | ||
*/ | ||
|
||
#include "lsst/afw/geom/Transform.h" | ||
|
||
namespace lsst { | ||
namespace afw { | ||
namespace geom { | ||
|
||
/** | ||
* Approximate a Transform by its local linearization. | ||
* | ||
* @tparam FromEndpoint, ToEndpoint The endpoints of the transform. | ||
* | ||
* @param original the Transform to linearize | ||
* @param point the point at which a linear approximation is desired | ||
* @returns a linear Transform whose value and Jacobian at `point` match those | ||
* of `original`. | ||
* | ||
* @throws pex::exceptions::InvalidParameterError Thrown if `original` does not | ||
* have a well-defined value and Jacobian at `point` | ||
* @exceptsafe Provides basic exception safety. | ||
*/ | ||
template <class FromEndpoint, class ToEndpoint> | ||
Transform<FromEndpoint, ToEndpoint> linearizeTransform( | ||
Transform<FromEndpoint, ToEndpoint> const &original, | ||
typename Transform<FromEndpoint, ToEndpoint>::FromPoint const &point); | ||
|
||
/* | ||
* The correct behavior for linearization is unclear where SpherePoints are involved (see discussion on | ||
* DM-10542). Forbid usage until somebody needs it. Note to maintainers: the template specializations MUST | ||
* be deleted in the header for compilers to complain correctly. | ||
*/ | ||
#define DISABLE(From, To) \ | ||
template <> \ | ||
Transform<From, To> linearizeTransform<From, To>(Transform<From, To> const &, \ | ||
Transform<From, To>::FromPoint const &) = delete; | ||
DISABLE(GenericEndpoint, SpherePointEndpoint); | ||
DISABLE(Point2Endpoint, SpherePointEndpoint); | ||
DISABLE(SpherePointEndpoint, GenericEndpoint); | ||
DISABLE(SpherePointEndpoint, Point2Endpoint); | ||
DISABLE(SpherePointEndpoint, SpherePointEndpoint); | ||
#undef DISABLE | ||
|
||
} // geom | ||
} // afw | ||
} // lsst | ||
|
||
#endif // LSST_AFW_GEOM_TRANSFORMFACTORY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* LSST Data Management System | ||
* See COPYRIGHT file at the top of the source tree. | ||
* | ||
* This product includes software developed by the | ||
* LSST Project (http://www.lsst.org/). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the LSST License Statement and | ||
* the GNU General Public License along with this program. If not, | ||
* see <http://www.lsstcorp.org/LegalNotices/>. | ||
*/ | ||
#include "pybind11/pybind11.h" | ||
#include "pybind11/stl.h" // Needed because some Endpoints are vectors | ||
|
||
#include "numpy/arrayobject.h" | ||
#include "ndarray/pybind11.h" | ||
|
||
#include "lsst/afw/geom/Endpoint.h" | ||
#include "lsst/afw/geom/transformFactory.h" | ||
|
||
namespace py = pybind11; | ||
using namespace py::literals; | ||
|
||
namespace lsst { | ||
namespace afw { | ||
namespace geom { | ||
namespace { | ||
|
||
// Declare methods on Transform<FromEndpoint, ToEndpoint> | ||
template <class FromEndpoint, class ToEndpoint> | ||
void declareFunctionTemplates(py::module &mod) { | ||
using Class = Transform<FromEndpoint, ToEndpoint>; | ||
|
||
mod.def("linearizeTransform", | ||
(Class(*)(Class const &, typename Class::FromPoint const &)) & linearizeTransform); | ||
} | ||
|
||
PYBIND11_PLUGIN(transformFactory) { | ||
py::module mod("transformFactory"); | ||
|
||
py::module::import("lsst.afw.geom.transform"); | ||
|
||
// Need to import numpy for ndarray and eigen conversions | ||
if (_import_array() < 0) { | ||
PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); | ||
return nullptr; | ||
} | ||
|
||
declareFunctionTemplates<GenericEndpoint, GenericEndpoint>(mod); | ||
declareFunctionTemplates<GenericEndpoint, Point2Endpoint>(mod); | ||
declareFunctionTemplates<Point2Endpoint, GenericEndpoint>(mod); | ||
declareFunctionTemplates<Point2Endpoint, Point2Endpoint>(mod); | ||
|
||
return mod.ptr(); | ||
} | ||
|
||
} // <anonymous> | ||
} // geom | ||
} // afw | ||
} // lsst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// -*- LSST-C++ -*- | ||
/* | ||
* LSST Data Management System | ||
* See COPYRIGHT file at the top of the source tree. | ||
* | ||
* This product includes software developed by the | ||
* LSST Project (http://www.lsst.org/). | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the LSST License Statement and | ||
* the GNU General Public License along with this program. If not, | ||
* see <http://www.lsstcorp.org/LegalNotices/>. | ||
*/ | ||
|
||
#include <sstream> | ||
#include <cmath> | ||
|
||
#include "astshim.h" | ||
|
||
#include "Eigen/Core" | ||
|
||
#include "lsst/afw/geom/Endpoint.h" | ||
#include "lsst/afw/geom/transformFactory.h" | ||
#include "lsst/pex/exceptions.h" | ||
|
||
#include "ndarray.h" | ||
#include "ndarray/eigen.h" | ||
|
||
namespace lsst { | ||
namespace afw { | ||
namespace geom { | ||
|
||
namespace { | ||
/** | ||
* Print a vector to a stream. | ||
* | ||
* The exact details of the representation are unspecified and subject to | ||
* change, but the following may be regarded as typical: | ||
* | ||
* [1.0, -3.560, 42.0] | ||
* | ||
* @tparam T the element type. Must support stream output. | ||
*/ | ||
template <typename T> | ||
std::ostream &operator<<(std::ostream &os, std::vector<T> const &v) { | ||
os << '['; | ||
bool first = true; | ||
for (T element : v) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
os << ", "; | ||
} | ||
os << element; | ||
} | ||
os << ']'; | ||
return os; | ||
} | ||
|
||
/** | ||
* Convert a Matrix to the equivalent ndarray. | ||
* | ||
* @param matrix The matrix to convert. | ||
* @returns an ndarray containing a copy of the data in `matrix` | ||
*/ | ||
template <class T, int Rows, int Cols> | ||
ndarray::Array<T, 2, 2> toNdArray(Eigen::Matrix<T, Rows, Cols> const &matrix) { | ||
ndarray::Array<T, 2, 2> array = ndarray::allocate(ndarray::makeVector(matrix.rows(), matrix.cols())); | ||
array.asEigen() = matrix; | ||
return array; | ||
} | ||
} // namespace {} | ||
|
||
template <class From, class To> | ||
Transform<From, To> linearizeTransform(Transform<From, To> const &original, | ||
typename Transform<From, To>::FromPoint const &inPoint) { | ||
using Transform = Transform<From, To>; | ||
auto fromEndpoint = original.getFromEndpoint(); | ||
auto toEndpoint = original.getToEndpoint(); | ||
|
||
auto outPoint = original.applyForward(inPoint); | ||
auto jacobian = original.getJacobian(inPoint); | ||
for (int i = 0; i < toEndpoint.getNAxes(); ++i) { | ||
if (!std::isfinite(outPoint[i])) { | ||
std::ostringstream buffer; | ||
buffer << "Transform ill-defined: " << inPoint << " -> " << outPoint; | ||
throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, buffer.str()); | ||
} | ||
} | ||
if (!jacobian.allFinite()) { | ||
std::ostringstream buffer; | ||
buffer << "Transform not continuous at " << inPoint << ": J = " << jacobian; | ||
throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, buffer.str()); | ||
} | ||
|
||
// y(x) = J (x - x0) + y0 | ||
auto map = ast::ShiftMap(fromEndpoint.dataFromPoint(inPoint)) | ||
.getInverse() | ||
->then(ast::MatrixMap(toNdArray(jacobian))) | ||
.then(ast::ShiftMap(toEndpoint.dataFromPoint(outPoint))); | ||
// TODO: remove false flag as part of DM-10947 | ||
return Transform(map, false); | ||
} | ||
|
||
#define INSTANTIATE_FACTORIES(From, To) \ | ||
template Transform<From, To> linearizeTransform<From, To>(Transform<From, To> const &transform, \ | ||
Transform<From, To>::FromPoint const &point); | ||
|
||
// explicit instantiations | ||
INSTANTIATE_FACTORIES(GenericEndpoint, GenericEndpoint); | ||
INSTANTIATE_FACTORIES(GenericEndpoint, Point2Endpoint); | ||
INSTANTIATE_FACTORIES(Point2Endpoint, GenericEndpoint); | ||
INSTANTIATE_FACTORIES(Point2Endpoint, Point2Endpoint); | ||
} | ||
} | ||
} /* namespace lsst::afw::geom */ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be any more efficient to use the template parameters
Rows and
Colshere? I assume they *must* be identical to
matrix.rows()and
matrix.cols()`.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, actually.
Rows
andCols
may have the dummy value -1 to mean "determined at run-time", and in fact they do have this value for the matrix returned bygetJacobian
. (Also, I'm pretty sure that in fixed-size matrices the implementations forrows()
andcols()
are optimized for inlining.)