LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
SpatialCell.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Class to ensure constraints for spatial modeling
27  */
28 
29 #ifndef LSST_AFW_MATH_SPATIALCELL_H
30 #define LSST_AFW_MATH_SPATIALCELL_H
31 
32 #include <limits>
33 #include <vector>
34 #include <string>
35 
36 #include <memory>
37 #include "lsst/base.h"
38 #include "lsst/pex/exceptions.h"
39 #include "lsst/geom.h"
41 
42 namespace lsst {
43 namespace afw {
44 
45 // forward declarations
46 namespace image {
47 template <typename ImagePixelT>
48 class Image;
49 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
50 class MaskedImage;
51 } // namespace image
52 
53 namespace math {
54 
56 class SpatialCellCandidate;
57 
59 public:
60  CandidateVisitor() = default;
61  virtual ~CandidateVisitor() = default;
62 
63  virtual void reset() {}
65 };
66 
71 public:
72  enum Status { BAD = 0, GOOD = 1, UNKNOWN = 2 };
73 
74  SpatialCellCandidate(float const xCenter,
75  float const yCenter
76  )
77  : _id(++_CandidateId), _status(UNKNOWN), _xCenter(xCenter), _yCenter(yCenter) {}
78 
81 
85  virtual ~SpatialCellCandidate() = default;
86 
88  float getXCenter() const { return _xCenter; }
89 
91  float getYCenter() const { return _yCenter; }
92 
94  virtual bool instantiate() { return true; }
95 
97  virtual double getCandidateRating() const = 0;
99  virtual void setCandidateRating(double) {}
100 
102  int getId() const { return _id; }
104  Status getStatus() const { return _status; }
106  void setStatus(Status status);
108  virtual bool isBad() const { return (_status == BAD); }
109 
110 private:
111  int _id; // Unique ID for object
112  Status _status; // Is this Candidate good?
113  float const _xCenter; // The object's column-centre
114  float const _yCenter; // The object's row-centre
115 
117  static int _CandidateId;
118 };
119 
125 public:
127  SpatialCellImageCandidate(float const xCenter,
128  float const yCenter
129  )
130  : SpatialCellCandidate(xCenter, yCenter), _chi2(std::numeric_limits<double>::max()) {}
133  ~SpatialCellImageCandidate() override = default;
134 
136  static void setWidth(int width) { _width = width; }
138  static int getWidth() { return _width; }
139 
141  static void setHeight(int height) { _height = height; }
143  static int getHeight() { return _height; }
144 
146  double getChi2() const { return _chi2; }
148  void setChi2(double chi2) { _chi2 = chi2; }
149 
150 private:
151  static int _width; // the width of images to return; may be ignored by subclasses
152  static int _height; // the height of images to return; may be ignored by subclasses
153  double _chi2; // chi^2 for fit
154 };
155 
160  friend class SpatialCell;
162 
163 public:
164  // ctors are protected
168  void operator++();
172  size_t operator-(SpatialCellCandidateIterator const& rhs) const;
173 
182 
184  bool operator==(SpatialCellCandidateIterator const& rhs) const { return _iterator == rhs._iterator; }
186  bool operator!=(SpatialCellCandidateIterator const& rhs) const { return _iterator != rhs._iterator; }
187 
188 protected:
195  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end,
196  bool ignoreBad);
203  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end,
204  bool ignoreBad, bool);
205 
206 private:
207  CandidateList::iterator _iterator;
208  CandidateList::iterator _end;
209  bool _ignoreBad;
210 };
211 
223 class SpatialCell {
224 public:
235  CandidateList const& candidateList = CandidateList());
236 
237  SpatialCell(SpatialCell const&) = default;
238  SpatialCell(SpatialCell&&) = default;
239  SpatialCell& operator=(SpatialCell const&) = default;
241 
245  virtual ~SpatialCell() = default;
246 
250  bool empty() const;
254  size_t size() const;
255 
259  void sortCandidates();
264  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad);
265  }
267  ) {
268  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad);
269  }
274  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad, true);
275  }
277  ) {
278  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad, true);
279  }
284 
292 
294  void setIgnoreBad(bool ignoreBad) { _ignoreBad = ignoreBad; }
296  bool getIgnoreBad() const { return _ignoreBad; }
297 
306  std::shared_ptr<SpatialCellCandidate> getCandidateById(int id, bool noThrow = false);
310  std::string const& getLabel() const { return _label; }
314  lsst::geom::Box2I const& getBBox() const { return _bbox; }
315  /*
316  * Visit our candidates
317  */
330  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
331  bool const ignoreExceptions = false, bool const reset = true);
345  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
346  bool const ignoreExceptions = false, bool const reset = true) const;
356  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false,
357  bool const reset = true);
370  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false,
371  bool const reset = true) const;
372 
373 private:
374  std::string _label; // Name of cell for logging/trace
375  lsst::geom::Box2I _bbox; // Bounding box of cell in overall image
376  CandidateList _candidateList; // List of all candidates in the cell
377  bool _ignoreBad; // Don't include BAD candidates when traversing the list
378 };
379 
384 public:
386 
396  SpatialCellSet(lsst::geom::Box2I const& region, int xSize, int ySize = 0);
397 
398  SpatialCellSet(SpatialCellSet const&) = default;
402 
406  virtual ~SpatialCellSet() = default;
407 
411  CellList& getCellList() { return _cellList; }
412 
416  lsst::geom::Box2I getBBox() const { return _region; };
417 
422 
424  void sortCandidates();
425 
437  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
438  bool const ignoreExceptions = false);
448  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
449  bool const ignoreExceptions = false) const;
458  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false);
467  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false) const;
468 
478  std::shared_ptr<SpatialCellCandidate> getCandidateById(int id, bool noThrow = false);
479 
481  void setIgnoreBad(bool ignoreBad);
482 
483 private:
484  lsst::geom::Box2I _region; // Bounding box of overall image
485  CellList _cellList; // List of SpatialCells
486 };
487 } // namespace math
488 } // namespace afw
489 } // namespace lsst
490 
491 #endif
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
int end
int max
Basic LSST definitions.
T begin(T... args)
virtual void processCandidate(SpatialCellCandidate *)
Definition: SpatialCell.h:64
virtual ~CandidateVisitor()=default
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
SpatialCellCandidate(float const xCenter, float const yCenter)
Definition: SpatialCell.h:74
virtual void setCandidateRating(double)
Set the candidate's rating.
Definition: SpatialCell.h:99
SpatialCellCandidate(SpatialCellCandidate const &)=default
virtual bool isBad() const
Is this candidate unacceptable?
Definition: SpatialCell.h:108
float getYCenter() const
Return the object's row-centre.
Definition: SpatialCell.h:91
SpatialCellCandidate(SpatialCellCandidate &&)=default
float getXCenter() const
Return the object's column-centre.
Definition: SpatialCell.h:88
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:102
void setStatus(Status status)
Set the candidate's status.
Definition: SpatialCell.cc:53
virtual double getCandidateRating() const =0
Return candidate's rating.
virtual bool instantiate()
Do anything needed to make this candidate usable.
Definition: SpatialCell.h:94
Status getStatus() const
Return the candidate's status.
Definition: SpatialCell.h:104
virtual ~SpatialCellCandidate()=default
(virtual) destructor – this is a base class you know
An iterator that only returns usable members of the SpatialCell.
Definition: SpatialCell.h:159
SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end, bool ignoreBad)
ctor; designed to be used to pass begin to SpatialCellCandidateIterator
Definition: SpatialCell.cc:240
bool operator==(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators equal?
Definition: SpatialCell.h:184
void operator++()
Advance the iterator, maybe skipping over candidates labelled BAD.
Definition: SpatialCell.cc:261
std::shared_ptr< SpatialCellCandidate const > operator*() const
Dereference the iterator to return the Candidate (if there is one)
Definition: SpatialCell.cc:286
size_t operator-(SpatialCellCandidateIterator const &rhs) const
Return the number of candidate between this and rhs.
Definition: SpatialCell.cc:275
bool operator!=(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators unequal?
Definition: SpatialCell.h:186
Class to ensure constraints for spatial modeling.
Definition: SpatialCell.h:223
lsst::geom::Box2I const & getBBox() const
Get SpatialCell's BBox.
Definition: SpatialCell.h:314
SpatialCell(SpatialCell &&)=default
SpatialCellCandidateIterator end()
Return an iterator to (one after) the end of the Candidates.
Definition: SpatialCell.h:273
size_t size() const
Return number of usable candidates in Cell.
Definition: SpatialCell.cc:112
bool getIgnoreBad() const
Get whether we are omitting BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:296
void sortCandidates()
Rearrange the candidates to reflect their current ratings.
Definition: SpatialCell.cc:80
std::vector< std::shared_ptr< SpatialCellCandidate > > CandidateList
Definition: SpatialCell.h:225
void removeCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Remove a candidate from the list.
Definition: SpatialCell.cc:88
SpatialCell(SpatialCell const &)=default
virtual ~SpatialCell()=default
Destructor.
std::string const & getLabel() const
Get SpatialCell's label.
Definition: SpatialCell.h:310
void visitCandidates(CandidateVisitor *visitor, int const nMaxPerCell=-1, bool const ignoreExceptions=false, bool const reset=true)
Call the visitor's processCandidate method for each Candidate in the SpatialCell.
Definition: SpatialCell.cc:135
SpatialCellCandidateIterator begin()
Return an iterator to the beginning of the Candidates.
Definition: SpatialCell.h:263
SpatialCellCandidateIterator begin(bool ignoreBad)
Definition: SpatialCell.h:266
bool empty() const
Determine if cell has no usable candidates.
Definition: SpatialCell.cc:98
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
Definition: SpatialCell.cc:120
SpatialCell & operator=(SpatialCell const &)=default
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:294
SpatialCell(std::string const &label, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), CandidateList const &candidateList=CandidateList())
Constructor.
Definition: SpatialCell.cc:72
void visitAllCandidates(CandidateVisitor *visitor, bool const ignoreExceptions=false, bool const reset=true)
Call the visitor's processCandidate method for every Candidate in the SpatialCell.
Definition: SpatialCell.cc:192
SpatialCell & operator=(SpatialCell &&)=default
void insertCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Add a candidate to the list, preserving ranking.
Definition: SpatialCell.cc:82
SpatialCellCandidateIterator end(bool ignoreBad)
Definition: SpatialCell.h:276
Base class for candidate objects in a SpatialCell that are able to return an Image of some sort (e....
Definition: SpatialCell.h:124
SpatialCellImageCandidate(float const xCenter, float const yCenter)
ctor
Definition: SpatialCell.h:127
static int getWidth()
Return the width of the image that getImage should return.
Definition: SpatialCell.h:138
double getChi2() const
Return the candidate's chi^2.
Definition: SpatialCell.h:146
SpatialCellImageCandidate(SpatialCellImageCandidate &&)=default
static int getHeight()
Return the height of the image that getImage should return.
Definition: SpatialCell.h:143
static void setWidth(int width)
Set the width of the image that getImage should return.
Definition: SpatialCell.h:136
static void setHeight(int height)
Set the height of the image that getImage should return.
Definition: SpatialCell.h:141
SpatialCellImageCandidate(SpatialCellImageCandidate const &)=default
void setChi2(double chi2)
Set the candidate's chi^2.
Definition: SpatialCell.h:148
A collection of SpatialCells covering an entire image.
Definition: SpatialCell.h:383
SpatialCellSet(SpatialCellSet &&)=default
void sortCandidates()
Rearrange the Candidates in all SpatialCells to reflect their current ratings.
Definition: SpatialCell.cc:373
void visitAllCandidates(CandidateVisitor *visitor, bool const ignoreExceptions=false)
Call the visitor's processCandidate method for every Candidate in the SpatialCellSet.
Definition: SpatialCell.cc:398
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.cc:432
lsst::geom::Box2I getBBox() const
Return the bounding box of the image.
Definition: SpatialCell.h:416
SpatialCellSet & operator=(SpatialCellSet const &)=default
SpatialCellSet & operator=(SpatialCellSet &&)=default
void insertCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Insert a candidate into the correct cell.
Definition: SpatialCell.cc:360
SpatialCellSet(SpatialCellSet const &)=default
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
Definition: SpatialCell.cc:415
std::vector< std::shared_ptr< SpatialCell > > CellList
Definition: SpatialCell.h:385
CellList & getCellList()
Return our SpatialCells.
Definition: SpatialCell.h:411
virtual ~SpatialCellSet()=default
Destructor.
void visitCandidates(CandidateVisitor *visitor, int const nMaxPerCell=-1, bool const ignoreExceptions=false)
Call the visitor's processCandidate method for each Candidate in the SpatialCellSet.
Definition: SpatialCell.cc:379
SpatialCellSet(lsst::geom::Box2I const &region, int xSize, int ySize=0)
Constructor.
Definition: SpatialCell.cc:302
An integer coordinate rectangle.
Definition: Box.h:55
T end(T... args)
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
A base class for image defects.
STL namespace.