39 template <
typename RecordT>
51 template <
typename Record1,
typename Record2>
52 bool operator<(RecordPos<Record1>
const &s1, RecordPos<Record2>
const &s2) {
53 return (s1.dec < s2.dec);
58 return s1->getY() < s2->getY();
72 template <
typename Cat>
73 size_t makeRecordPositions(Cat
const &cat, RecordPos<typename Cat::Record> *positions) {
75 Key<Angle> raKey = Cat::Table::getCoordKey().getRa();
76 Key<Angle> decKey = Cat::Table::getCoordKey().getDec();
77 for (
typename Cat::const_iterator i(cat.begin()), e(cat.end()); i != e; ++i) {
84 positions[n].dec = dec.asRadians();
85 positions[n].x =
std::cos(ra) * cosDec;
86 positions[n].y =
std::sin(ra) * cosDec;
93 LOGLS_WARN(
"afw.table.matchRaDec",
"At least one source had ra or dec equal to NaN");
98 template size_t makeRecordPositions(
SimpleCatalog const &, RecordPos<SimpleRecord> *);
99 template size_t makeRecordPositions(
SourceCatalog const &, RecordPos<SourceRecord> *);
101 template <
typename Cat1,
typename Cat2>
102 bool doSelfMatchIfSame(
std::vector<Match<typename Cat1::Record, typename Cat2::Record> > &
result,
103 Cat1
const &cat1, Cat2
const &cat2,
Angle radius) {
108 template <
typename Cat>
109 bool doSelfMatchIfSame(
std::vector<Match<typename Cat::Record, typename Cat::Record> > &
result,
110 Cat
const &cat1, Cat
const &cat2,
Angle radius) {
111 if (&cat1 == &cat2) {
127 return 2. * (1. -
std::cos(theta.asRadians()));
139 Angle fromUnitSphereDistanceSquared(
double d2) noexcept {
146 template <
typename Cat1,
typename Cat2>
148 Cat2
const &cat2,
Angle radius,
156 template <
typename Cat1,
typename Cat2>
158 Cat2
const &cat2,
Angle radius,
163 if (doSelfMatchIfSame(matches, cat1, cat2, radius))
return matches;
168 if (cat1.size() == 0 || cat2.size() == 0) {
172 double const d2Limit = toUnitSphereDistanceSquared(radius);
175 size_t len1 = cat1.size();
176 size_t len2 = cat2.size();
178 typedef RecordPos<typename Cat1::Record> Pos1;
179 typedef RecordPos<typename Cat2::Record> Pos2;
182 len1 = makeRecordPositions(cat1, pos1.
get());
183 len2 = makeRecordPositions(cat2, pos2.
get());
186 for (
size_t i = 0, start = 0; i < len1; ++i) {
187 double minDec = pos1[i].dec - radius.
asRadians();
188 while (start < len2 && pos2[start].
dec < minDec) {
194 double maxDec = pos1[i].dec + radius.
asRadians();
195 size_t closestIndex = -1;
196 double d2Include = d2Limit;
199 for (
size_t j = start; j < len2 && pos2[j].dec <= maxDec; ++j) {
200 double dx = pos1[i].x - pos2[j].x;
201 double dy = pos1[i].y - pos2[j].y;
202 double dz = pos1[i].z - pos2[j].z;
203 double d2 = dx * dx + dy * dy + dz * dz;
204 if (d2 < d2Include) {
210 matches.
push_back(MatchT(pos1[i].
src, pos2[j].src, fromUnitSphereDistanceSquared(d2)));
216 matches.
push_back(MatchT(pos1[i].
src, nullRecord, NAN));
220 MatchT(pos1[i].
src, pos2[closestIndex].src, fromUnitSphereDistanceSquared(d2Include)));
226 #define LSST_MATCH_RADEC(RTYPE, C1, C2) \ 227 template RTYPE matchRaDec(C1 const &, C2 const &, Angle, bool); \ 228 template RTYPE matchRaDec(C1 const &, C2 const &, Angle, MatchControl const &) 234 #undef LSST_MATCH_RADEC 236 template <
typename Cat>
245 template <
typename Cat>
254 if (cat.size() == 0) {
258 double const d2Limit = toUnitSphereDistanceSquared(radius);
261 size_t len = cat.size();
262 typedef RecordPos<typename Cat::Record> Pos;
264 len = makeRecordPositions(cat, pos.
get());
266 for (
size_t i = 0; i < len; ++i) {
267 double maxDec = pos[i].dec + radius.
asRadians();
268 for (
size_t j = i + 1; j < len && pos[j].dec <= maxDec; ++j) {
269 double dx = pos[i].x - pos[j].x;
270 double dy = pos[i].y - pos[j].y;
271 double dz = pos[i].z - pos[j].z;
272 double d2 = dx * dx + dy * dy + dz * dz;
274 Angle d = fromUnitSphereDistanceSquared(d2);
277 matches.
push_back(MatchT(pos[j].src, pos[i].src, d));
285 #define LSST_MATCH_RADEC(RTYPE, C) \ 286 template RTYPE matchRaDec(C const &, Angle, bool); \ 287 template RTYPE matchRaDec(C const &, Angle, MatchControl const &) 292 #undef LSST_MATCH_RADEC 298 return matchXy(cat1, cat2, radius, mc);
303 if (&cat1 == &cat2) {
307 double const r2 = radius * radius;
310 size_t len1 = cat1.size();
311 size_t len2 = cat2.size();
338 for (
size_t i = 0, start = 0; i < len1; ++i) {
339 double y = pos1[i]->getY();
340 double minY = y - radius;
341 while (start < len2 && pos2[start]->getY() < minY) {
347 double x = pos1[i]->getX();
348 double maxY = y + radius;
350 size_t closestIndex = -1;
351 double r2Include = r2;
354 for (
size_t j = start; j < len2 && (y2 = pos2[j]->getY()) <= maxY; ++j) {
355 double dx = x - pos2[j]->getX();
357 double d2 = dx * dx + dy * dy;
358 if (d2 < r2Include) {
370 matches.push_back(
SourceMatch(pos1[i], nullRecord, NAN));
383 return matchXy(cat, radius, mc);
388 double const r2 = radius * radius;
391 size_t len = cat.size();
406 for (
size_t i = 0; i < len; ++i) {
407 double x = pos[i]->getX();
408 double y = pos[i]->getY();
409 double maxY = y + radius;
411 for (
size_t j = i + 1; j < len && (y2 = pos[j]->getY()) <= maxY; ++j) {
412 double dx = x - pos[j]->getX();
414 double d2 = dx * dx + dy * dy;
427 template <
typename Record1,
typename Record2>
432 Key<double> keyD = schema.
addField<
double>(
"distance",
"Distance between matches sources.");
434 result.getTable()->preallocate(matches.size());
435 result.reserve(matches.size());
437 for (Iter i = matches.begin(); i != matches.end(); ++i) {
439 record->set(outKey1, i->first->getId());
440 record->set(outKey2, i->second->getId());
441 record->set(keyD, i->distance);
450 template <
typename Cat1,
typename Cat2>
454 LOG_LOGGER tableLog =
LOG_GET(
"afw.table");
458 if (!first.isSorted() || !second.isSorted())
460 "Catalogs passed to unpackMatches must be sorted.");
466 typename Cat1::const_iterator k1 = first.find(i->get(inKey1));
467 typename Cat2::const_iterator k2 = second.find(i->get(inKey2));
468 if (k1 != first.end()) {
471 LOGLS_WARN(tableLog,
"Persisted match record with ID " << i->get(inKey1)
472 <<
" not found in catalog 1.");
474 if (k2 != second.end()) {
477 LOGLS_WARN(tableLog,
"Persisted match record with ID " << i->get(inKey2)
478 <<
" not found in catalog 2.");
480 j->distance = i->get(keyD);
#define LOGLS_WARN(logger, message)
Defines the fields and offsets for a table.
bool findOnlyClosest
"Return only the closest match if more than one is found " "(default: true)" ;
lsst::afw::geom::Angle Angle
afw::table::Schema schema
AngleUnit constexpr radians
constant with units of radians
AngleUnit constexpr degrees
constant with units of degrees
Schema getSchema() const
Return the schema associated with the catalog's table.
std::vector< Match< typename Cat1::Record, typename Cat2::Record > > unpackMatches(BaseCatalog const &matches, Cat1 const &cat1, Cat2 const &cat2)
Reconstruct a MatchVector from a BaseCatalog representation of the matches and a pair of catalogs...
std::vector< Match< typename Cat1::Record, typename Cat2::Record > > matchRaDec(Cat1 const &cat1, Cat2 const &cat2, Angle radius, MatchControl const &mc=MatchControl())
Compute all tuples (s1,s2,d) where s1 belings to cat1, s2 belongs to cat2 and d, the distance between...
Pass parameters to algorithms that match list of sources.
A class representing an angle.
A base class for image defects.
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
std::shared_ptr< RecordT > src
iterator end()
Iterator access.
Lightweight representation of a geometric match between two records.
std::vector< SimpleMatch > SimpleMatchVector
Iterator class for CatalogT.
std::unique_ptr< SchemaItem< U > > result
BaseCatalog packMatches(std::vector< Match< Record1, Record2 > > const &matches)
Return a table representation of a MatchVector that can be used to persist it.
#define LSST_EXCEPT(type,...)
A class used as a handle to a particular field in a table.
#define LSST_MATCH_RADEC(RTYPE, C1, C2)
SortedCatalogT< SourceRecord > SourceCatalog
constexpr double asRadians() const noexcept
Return an Angle's value in radians.
SourceMatchVector matchXy(SourceCatalog const &cat1, SourceCatalog const &cat2, double radius, MatchControl const &mc=MatchControl())
Compute all tuples (s1,s2,d) where s1 belings to cat1, s2 belongs to cat2 and d, the distance between...
size_type size() const
Return the number of elements in the catalog.
bool includeMismatches
"Include failed matches (i.e. one 'match' is NULL) " "(default: false)" ;
iterator begin()
Iterator access.
SortedCatalogT< SimpleRecord > SimpleCatalog
Match< SourceRecord, SourceRecord > SourceMatch
Key< T > addField(Field< T > const &field, bool doReplace=false)
Add a new field to the Schema, and return the associated Key.
bool symmetricMatch
"Produce symmetric matches (default: true):\n" "i.e. if (s1, s2, d) is reported, then so is (s2...
std::vector< SourceMatch > SourceMatchVector