lsst.afw  13.0-92-g8ecf155+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TransformBoundedField.cc
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 #include <memory>
25 #include <string>
26 
27 #include "ndarray/eigen.h"
28 #include "astshim.h"
34 
35 namespace lsst {
36 namespace afw {
37 namespace math {
38 
40  : BoundedField(bbox), _transform(transform) {
41  if (transform.getToEndpoint().getNAxes() != 1) {
42  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
43  "To EndPoint of transform must have 1 axis but has " +
44  std::to_string(transform.getToEndpoint().getNAxes()));
45  }
46 }
47 
48 double TransformBoundedField::evaluate(geom::Point2D const& position) const {
49  return _transform.applyForward(position)[0];
50 }
51 
52 ndarray::Array<double, 1, 1> TransformBoundedField::evaluate(ndarray::Array<double const, 1> const& x,
53  ndarray::Array<double const, 1> const& y) const {
54  if (x.getSize<0>() != y.getSize<0>()) {
55  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
56  "x length " + std::to_string(x.getSize<0>()) + "!= y length " +
57  std::to_string(x.getSize<0>()));
58  }
59 
60  // TODO if Mapping.applyForward gains support for x, y (DM-11226) then use that instead of copying data
61  int const nPoints = x.getSize<0>();
62  ndarray::Array<double, 2, 2> xy = ndarray::allocate(ndarray::makeVector(2, nPoints));
63  for (int col = 0; col < nPoints; ++col) {
64  xy[0][col] = x[col];
65  xy[1][col] = y[col];
66  }
67 
68  auto res2D = _transform.getFrameSet()->applyForward(xy);
69 
70  // res2D has shape 1 x N; return a 1-D view with the extra dimension stripped
71  auto resShape = ndarray::makeVector(nPoints);
72  auto resStrides = ndarray::makeVector(1);
73  return ndarray::external(res2D.getData(), resShape, resStrides, res2D);
74 }
75 
76 // ------------------ persistence ---------------------------------------------------------------------------
77 
78 namespace {
79 
80 struct PersistenceHelper {
81  table::Schema schema;
82  table::PointKey<int> bboxMin;
83  table::PointKey<int> bboxMax;
84  table::Key<std::string> frameSet;
85 
86  PersistenceHelper()
87  : schema(),
88  bboxMin(table::PointKey<int>::addFields(schema, "bbox_min", "lower-left corner of bounding box",
89  "pixel")),
90  bboxMax(table::PointKey<int>::addFields(schema, "bbox_max",
91  "upper-right corner of bounding box", "pixel")),
92  frameSet(schema.addField<std::string>("frameSet", "FrameSet contained in the Transform", 0,
93  false)) {}
94 
95  PersistenceHelper(table::Schema const& s)
96  : schema(s), bboxMin(s["bbox_min"]), bboxMax(s["bbox_max"]), frameSet(s["frameSet"]) {}
97 };
98 
99 class TransformBoundedFieldFactory : public table::io::PersistableFactory {
100 public:
101  explicit TransformBoundedFieldFactory(std::string const& name)
102  : afw::table::io::PersistableFactory(name) {}
103 
104  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
105  CatalogVector const& catalogs) const override {
106  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
107  LSST_ARCHIVE_ASSERT(catalogs.front().size() == 1u);
108  table::BaseRecord const& record = catalogs.front().front();
109  PersistenceHelper const keys(record.getSchema());
110  // NOTE: needed invert=false in case min=-1, max=0 (empty bbox). See RFC-324 and DM-10200
111  geom::Box2I bbox(record.get(keys.bboxMin), record.get(keys.bboxMax), false);
112  auto frameSetStr = record.get(keys.frameSet);
113  auto transform =
115  return std::make_shared<TransformBoundedField>(bbox, transform);
116  }
117 };
118 
119 std::string getTransformBoundedFieldPersistenceName() { return "TransformBoundedField"; }
120 
121 TransformBoundedFieldFactory registration(getTransformBoundedFieldPersistenceName());
122 
123 } // namespace
124 
126  return getTransformBoundedFieldPersistenceName();
127 }
128 
129 std::string TransformBoundedField::getPythonModule() const { return "lsst.afw.math"; }
130 
132  PersistenceHelper const keys;
133  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
134  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
135  record->set(keys.bboxMin, getBBox().getMin());
136  record->set(keys.bboxMax, getBBox().getMax());
137  record->set(keys.frameSet, getTransform().writeString());
138  handle.saveCatalog(catalog);
139 }
140 
141 std::shared_ptr<BoundedField> TransformBoundedField::operator*(double const scale) const {
142  auto zoomMap = ast::ZoomMap(1, scale);
143  auto newMapping = getTransform().getFrameSet()->then(zoomMap);
144  auto newTransform = Transform(newMapping);
145  return std::make_shared<TransformBoundedField>(getBBox(), newTransform);
146 }
147 
149  auto rhsCasted = dynamic_cast<TransformBoundedField const*>(&rhs);
150  if (!rhsCasted) return false;
151 
152  return getBBox() == rhsCasted->getBBox() &&
153  *(getTransform().getFrameSet()) == *(rhsCasted->getTransform().getFrameSet());
154 }
155 
156 std::string TransformBoundedField::toString() const {
157  std::ostringstream os;
158  os << "TransformBoundedField (containing " << _transform.getFrameSet()->getNFrame() << " frames)";
159  return os.str();
160 }
161 
162 } // namespace math
163 } // namespace afw
164 } // namespace lsst
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
An object passed to Persistable::write to allow it to persist itself.
A custom container class for records, based on std::vector.
Definition: Catalog.h:96
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
table::PointKey< int > bboxMin
table::PointKey< int > bboxMax
TransformBoundedField(geom::Box2I const &bbox, Transform const &transform)
Create a TransformBoundedField from a bounding box and transform.
A BoundedField based on geom::Transform&lt;Poin2Endpoint, GenericEndpoint&lt;1&gt;&gt;.
geom::Box2I getBBox() const
Return the bounding box that defines the region where the field is valid.
Definition: BoundedField.h:112
An integer coordinate rectangle.
Definition: Box.h:55
std::shared_ptr< BoundedField > operator*(double const scale) const override
Return a scaled BoundedField.
bool operator==(BoundedField const &rhs) const override
BoundedFields (of the same sublcass) are equal if their bounding boxes and parameters are equal...
geom::Transform< geom::Point2Endpoint, geom::GenericEndpoint > Transform
Transform getTransform() const
Get the contained Transform.
table::Key< std::string > frameSet
table::Key< std::string > name
Definition: ApCorrMap.cc:71
double x
ToEndpoint getToEndpoint() const
Get the &quot;to&quot; endpoint.
Definition: Transform.h:139
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
static Transform< FromEndpoint, ToEndpoint > readString(std::string &str)
Deserialize a Transform of this type from a string, using the same format as readStream.
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:55
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
std::shared_ptr< const ast::FrameSet > getFrameSet() const
Get the contained frameset.
Definition: Transform.h:134
ToPoint applyForward(FromPoint const &point) const
Transform one point in the forward direction (&quot;from&quot; to &quot;to&quot;)
double evaluate(geom::Point2D const &position) const override
Evaluate the field at the given point.
afw::table::Schema schema
Definition: GaussianPsf.cc:43
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:463