LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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:
61  virtual ~CandidateVisitor() {}
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 
83 
87  virtual ~SpatialCellCandidate() = default;
88 
90  float getXCenter() const { return _xCenter; }
91 
93  float getYCenter() const { return _yCenter; }
94 
96  virtual bool instantiate() { return true; }
97 
99  virtual double getCandidateRating() const = 0;
101  virtual void setCandidateRating(double) {}
102 
104  int getId() const { return _id; }
106  Status getStatus() const { return _status; }
108  void setStatus(Status status);
110  virtual bool isBad() const { return (_status == BAD); }
111 
112 private:
113  int _id; // Unique ID for object
114  Status _status; // Is this Candidate good?
115  float const _xCenter; // The object's column-centre
116  float const _yCenter; // The object's row-centre
117 
119  static int _CandidateId;
120 };
121 
127 public:
129  SpatialCellImageCandidate(float const xCenter,
130  float const yCenter
131  )
132  : SpatialCellCandidate(xCenter, yCenter), _chi2(std::numeric_limits<double>::max()) {}
137  ~SpatialCellImageCandidate() override = default;
138 
140  static void setWidth(int width) { _width = width; }
142  static int getWidth() { return _width; }
143 
145  static void setHeight(int height) { _height = height; }
147  static int getHeight() { return _height; }
148 
150  double getChi2() const { return _chi2; }
152  void setChi2(double chi2) { _chi2 = chi2; }
153 
154 private:
155  static int _width; // the width of images to return; may be ignored by subclasses
156  static int _height; // the height of images to return; may be ignored by subclasses
157  double _chi2; // chi^2 for fit
158 };
159 
164  friend class SpatialCell;
166 
167 public:
168  // ctors are protected
172  void operator++();
176  size_t operator-(SpatialCellCandidateIterator const& rhs) const;
177 
186 
188  bool operator==(SpatialCellCandidateIterator const& rhs) const { return _iterator == rhs._iterator; }
190  bool operator!=(SpatialCellCandidateIterator const& rhs) const { return _iterator != rhs._iterator; }
191 
192 protected:
199  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end,
200  bool ignoreBad);
207  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end,
208  bool ignoreBad, bool);
209 
210 private:
211  CandidateList::iterator _iterator;
212  CandidateList::iterator _end;
213  bool _ignoreBad;
214 };
215 
227 class SpatialCell {
228 public:
239  CandidateList const& candidateList = CandidateList());
240 
241  SpatialCell(SpatialCell const&) = default;
242  SpatialCell(SpatialCell&&) = default;
243  SpatialCell& operator=(SpatialCell const&) = default;
245 
249  virtual ~SpatialCell() = default;
250 
254  bool empty() const;
258  size_t size() const;
259 
263  void sortCandidates();
268  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad);
269  }
271  ) {
272  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad);
273  }
278  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad, true);
279  }
281  ) {
282  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad, true);
283  }
288 
296 
298  void setIgnoreBad(bool ignoreBad) { _ignoreBad = ignoreBad; }
300  bool getIgnoreBad() const { return _ignoreBad; }
301 
310  std::shared_ptr<SpatialCellCandidate> getCandidateById(int id, bool noThrow = false);
314  std::string const& getLabel() const { return _label; }
318  lsst::geom::Box2I const& getBBox() const { return _bbox; }
319  /*
320  * Visit our candidates
321  */
334  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
335  bool const ignoreExceptions = false, bool const reset = true);
349  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
350  bool const ignoreExceptions = false, bool const reset = true) const;
360  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false,
361  bool const reset = true);
374  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false,
375  bool const reset = true) const;
376 
377 private:
378  std::string _label; // Name of cell for logging/trace
379  lsst::geom::Box2I _bbox; // Bounding box of cell in overall image
380  CandidateList _candidateList; // List of all candidates in the cell
381  bool _ignoreBad; // Don't include BAD candidates when traversing the list
382 };
383 
388 public:
390 
400  SpatialCellSet(lsst::geom::Box2I const& region, int xSize, int ySize = 0);
401 
402  SpatialCellSet(SpatialCellSet const&) = default;
406 
410  virtual ~SpatialCellSet() = default;
411 
415  CellList& getCellList() { return _cellList; }
416 
420  lsst::geom::Box2I getBBox() const { return _region; };
421 
426 
428  void sortCandidates();
429 
441  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
442  bool const ignoreExceptions = false);
452  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
453  bool const ignoreExceptions = false) const;
462  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false);
471  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false) const;
472 
482  std::shared_ptr<SpatialCellCandidate> getCandidateById(int id, bool noThrow = false);
483 
485  void setIgnoreBad(bool ignoreBad);
486 
487 private:
488  lsst::geom::Box2I _region; // Bounding box of overall image
489  CellList _cellList; // List of SpatialCells
490 };
491 } // namespace math
492 } // namespace afw
493 } // namespace lsst
494 
495 #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
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:101
SpatialCellCandidate(SpatialCellCandidate const &)=default
virtual bool isBad() const
Is this candidate unacceptable?
Definition: SpatialCell.h:110
SpatialCellCandidate & operator=(SpatialCellCandidate &&)=default
float getYCenter() const
Return the object's row-centre.
Definition: SpatialCell.h:93
SpatialCellCandidate(SpatialCellCandidate &&)=default
float getXCenter() const
Return the object's column-centre.
Definition: SpatialCell.h:90
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:104
void setStatus(Status status)
Set the candidate's status.
Definition: SpatialCell.cc:54
SpatialCellCandidate & operator=(SpatialCellCandidate const &)=default
virtual double getCandidateRating() const =0
Return candidate's rating.
virtual bool instantiate()
Do anything needed to make this candidate usable.
Definition: SpatialCell.h:96
Status getStatus() const
Return the candidate's status.
Definition: SpatialCell.h:106
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:163
SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end, bool ignoreBad)
ctor; designed to be used to pass begin to SpatialCellCandidateIterator
Definition: SpatialCell.cc:241
bool operator==(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators equal?
Definition: SpatialCell.h:188
void operator++()
Advance the iterator, maybe skipping over candidates labelled BAD.
Definition: SpatialCell.cc:262
std::shared_ptr< SpatialCellCandidate const > operator*() const
Dereference the iterator to return the Candidate (if there is one)
Definition: SpatialCell.cc:287
size_t operator-(SpatialCellCandidateIterator const &rhs) const
Return the number of candidate between this and rhs.
Definition: SpatialCell.cc:276
bool operator!=(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators unequal?
Definition: SpatialCell.h:190
Class to ensure constraints for spatial modeling.
Definition: SpatialCell.h:227
lsst::geom::Box2I const & getBBox() const
Get SpatialCell's BBox.
Definition: SpatialCell.h:318
SpatialCell(SpatialCell &&)=default
SpatialCellCandidateIterator end()
Return an iterator to (one after) the end of the Candidates.
Definition: SpatialCell.h:277
size_t size() const
Return number of usable candidates in Cell.
Definition: SpatialCell.cc:113
bool getIgnoreBad() const
Get whether we are omitting BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:300
void sortCandidates()
Rearrange the candidates to reflect their current ratings.
Definition: SpatialCell.cc:81
void removeCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Remove a candidate from the list.
Definition: SpatialCell.cc:89
SpatialCell(SpatialCell const &)=default
virtual ~SpatialCell()=default
Destructor.
std::string const & getLabel() const
Get SpatialCell's label.
Definition: SpatialCell.h:314
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:136
std::vector< std::shared_ptr< SpatialCellCandidate > > CandidateList
Definition: SpatialCell.h:229
SpatialCellCandidateIterator iterator
Definition: SpatialCell.h:230
SpatialCellCandidateIterator begin()
Return an iterator to the beginning of the Candidates.
Definition: SpatialCell.h:267
SpatialCellCandidateIterator begin(bool ignoreBad)
Definition: SpatialCell.h:270
bool empty() const
Determine if cell has no usable candidates.
Definition: SpatialCell.cc:99
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
Definition: SpatialCell.cc:121
SpatialCell & operator=(SpatialCell const &)=default
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:298
SpatialCell(std::string const &label, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), CandidateList const &candidateList=CandidateList())
Constructor.
Definition: SpatialCell.cc:73
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:193
SpatialCell & operator=(SpatialCell &&)=default
void insertCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Add a candidate to the list, preserving ranking.
Definition: SpatialCell.cc:83
SpatialCellCandidateIterator end(bool ignoreBad)
Definition: SpatialCell.h:280
Base class for candidate objects in a SpatialCell that are able to return an Image of some sort (e....
Definition: SpatialCell.h:126
SpatialCellImageCandidate(float const xCenter, float const yCenter)
ctor
Definition: SpatialCell.h:129
static int getWidth()
Return the width of the image that getImage should return.
Definition: SpatialCell.h:142
double getChi2() const
Return the candidate's chi^2.
Definition: SpatialCell.h:150
SpatialCellImageCandidate(SpatialCellImageCandidate &&)=default
static int getHeight()
Return the height of the image that getImage should return.
Definition: SpatialCell.h:147
static void setWidth(int width)
Set the width of the image that getImage should return.
Definition: SpatialCell.h:140
static void setHeight(int height)
Set the height of the image that getImage should return.
Definition: SpatialCell.h:145
SpatialCellImageCandidate & operator=(SpatialCellImageCandidate &&)=default
SpatialCellImageCandidate & operator=(SpatialCellImageCandidate const &)=default
SpatialCellImageCandidate(SpatialCellImageCandidate const &)=default
void setChi2(double chi2)
Set the candidate's chi^2.
Definition: SpatialCell.h:152
A collection of SpatialCells covering an entire image.
Definition: SpatialCell.h:387
SpatialCellSet(SpatialCellSet &&)=default
void sortCandidates()
Rearrange the Candidates in all SpatialCells to reflect their current ratings.
Definition: SpatialCell.cc:374
void visitAllCandidates(CandidateVisitor *visitor, bool const ignoreExceptions=false)
Call the visitor's processCandidate method for every Candidate in the SpatialCellSet.
Definition: SpatialCell.cc:399
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.cc:433
lsst::geom::Box2I getBBox() const
Return the bounding box of the image.
Definition: SpatialCell.h:420
std::vector< std::shared_ptr< SpatialCell > > CellList
Definition: SpatialCell.h:389
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:361
SpatialCellSet(SpatialCellSet const &)=default
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
Definition: SpatialCell.cc:416
CellList & getCellList()
Return our SpatialCells.
Definition: SpatialCell.h:415
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:380
SpatialCellSet(lsst::geom::Box2I const &region, int xSize, int ySize=0)
Constructor.
Definition: SpatialCell.cc:303
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.