LSSTApplications  20.0.0
LSSTDataManagementBasePackage
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
lsst::afw::math::SpatialCell::visitCandidates
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
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::afw::math::SpatialCellSet::~SpatialCellSet
virtual ~SpatialCellSet()=default
Destructor.
lsst::afw::math::SpatialCellCandidate
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
lsst::afw::math::SpatialCell::insertCandidate
void insertCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Add a candidate to the list, preserving ranking.
Definition: SpatialCell.cc:83
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::math::SpatialCellImageCandidate::operator=
SpatialCellImageCandidate & operator=(SpatialCellImageCandidate const &)=default
lsst::afw::math::SpatialCellSet::setIgnoreBad
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.cc:433
lsst::afw::math::SpatialCell::operator=
SpatialCell & operator=(SpatialCell const &)=default
lsst::afw::math::SpatialCell::getCandidateById
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
Definition: SpatialCell.cc:121
lsst::afw::math::SpatialCellCandidate::setStatus
void setStatus(Status status)
Set the candidate's status.
Definition: SpatialCell.cc:54
lsst::afw::math::SpatialCellImageCandidate::SpatialCellImageCandidate
SpatialCellImageCandidate(float const xCenter, float const yCenter)
ctor
Definition: SpatialCell.h:129
lsst::afw::math::SpatialCell::setIgnoreBad
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:298
lsst::afw::math::SpatialCellSet::insertCandidate
void insertCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Insert a candidate into the correct cell.
Definition: SpatialCell.cc:361
std::vector
STL class.
lsst::afw::math::SpatialCell::getBBox
lsst::geom::Box2I const & getBBox() const
Get SpatialCell's BBox.
Definition: SpatialCell.h:318
lsst::afw::math::SpatialCell::~SpatialCell
virtual ~SpatialCell()=default
Destructor.
lsst::afw::math::SpatialCell::begin
SpatialCellCandidateIterator begin()
Return an iterator to the beginning of the Candidates.
Definition: SpatialCell.h:267
lsst::afw::math::SpatialCellImageCandidate::~SpatialCellImageCandidate
~SpatialCellImageCandidate() override=default
lsst::afw::math::SpatialCellSet::visitAllCandidates
void visitAllCandidates(CandidateVisitor *visitor, bool const ignoreExceptions=false)
Call the visitor's processCandidate method for every Candidate in the SpatialCellSet.
Definition: SpatialCell.cc:399
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::math::CandidateVisitor
Definition: SpatialCell.h:58
lsst::afw::math::SpatialCell::CandidateList
std::vector< std::shared_ptr< SpatialCellCandidate > > CandidateList
Definition: SpatialCell.h:229
lsst::afw::math::SpatialCellCandidate::operator=
SpatialCellCandidate & operator=(SpatialCellCandidate const &)=default
lsst::afw::math::SpatialCellCandidateIterator::operator++
void operator++()
Advance the iterator, maybe skipping over candidates labelled BAD.
Definition: SpatialCell.cc:262
geom.h
lsst::afw::math::SpatialCellCandidate::operator=
SpatialCellCandidate & operator=(SpatialCellCandidate &&)=default
lsst::afw::math::SpatialCellCandidateIterator::operator!=
bool operator!=(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators unequal?
Definition: SpatialCell.h:190
lsst::afw::math::SpatialCellCandidate::isBad
virtual bool isBad() const
Is this candidate unacceptable?
Definition: SpatialCell.h:110
lsst::afw::math::SpatialCellCandidate::getCandidateRating
virtual double getCandidateRating() const =0
Return candidate's rating.
lsst::afw::math::SpatialCell::operator=
SpatialCell & operator=(SpatialCell &&)=default
end
int end
Definition: BoundedField.cc:105
lsst::afw::math::SpatialCellSet::visitCandidates
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
lsst::afw::math::SpatialCellCandidateIterator::operator==
bool operator==(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators equal?
Definition: SpatialCell.h:188
lsst::afw::math::SpatialCellImageCandidate::setWidth
static void setWidth(int width)
Set the width of the image that getImage should return.
Definition: SpatialCell.h:140
lsst::afw::math::SpatialCellSet::getBBox
lsst::geom::Box2I getBBox() const
Return the bounding box of the image.
Definition: SpatialCell.h:420
lsst::afw::math::SpatialCellCandidate::BAD
@ BAD
Definition: SpatialCell.h:72
lsst::afw::math::SpatialCellCandidateIterator::operator-
size_t operator-(SpatialCellCandidateIterator const &rhs) const
Return the number of candidate between this and rhs.
Definition: SpatialCell.cc:276
lsst::afw::math::SpatialCell::getLabel
std::string const & getLabel() const
Get SpatialCell's label.
Definition: SpatialCell.h:314
lsst::afw::math::SpatialCellSet::SpatialCellSet
SpatialCellSet(SpatialCellSet const &)=default
lsst::afw::math::SpatialCell::SpatialCell
SpatialCell(std::string const &label, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), CandidateList const &candidateList=CandidateList())
Constructor.
Definition: SpatialCell.cc:73
lsst::afw::math::SpatialCellCandidate::getXCenter
float getXCenter() const
Return the object's column-centre.
Definition: SpatialCell.h:90
lsst::afw::math::SpatialCellSet::SpatialCellSet
SpatialCellSet(SpatialCellSet &&)=default
lsst::afw::math::CandidateVisitor::reset
virtual void reset()
Definition: SpatialCell.h:63
lsst::afw::math::SpatialCellCandidate::SpatialCellCandidate
SpatialCellCandidate(SpatialCellCandidate const &)=default
lsst::afw::math::SpatialCell::visitAllCandidates
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
lsst::afw::math::SpatialCellCandidate::SpatialCellCandidate
SpatialCellCandidate(SpatialCellCandidate &&)=default
lsst::afw::math::SpatialCell
Class to ensure constraints for spatial modeling.
Definition: SpatialCell.h:227
lsst::afw::math::SpatialCellImageCandidate
Base class for candidate objects in a SpatialCell that are able to return an Image of some sort (e....
Definition: SpatialCell.h:126
lsst::afw::math::SpatialCell::end
SpatialCellCandidateIterator end(bool ignoreBad)
Definition: SpatialCell.h:280
lsst::afw::math::CandidateVisitor::CandidateVisitor
CandidateVisitor()
Definition: SpatialCell.h:60
lsst::afw::math::SpatialCellImageCandidate::getWidth
static int getWidth()
Return the width of the image that getImage should return.
Definition: SpatialCell.h:142
lsst::afw::math::SpatialCell::SpatialCell
SpatialCell(SpatialCell &&)=default
lsst::afw::math::SpatialCellCandidateIterator
An iterator that only returns usable members of the SpatialCell.
Definition: SpatialCell.h:163
lsst::afw::math::SpatialCellSet::operator=
SpatialCellSet & operator=(SpatialCellSet const &)=default
lsst::afw::math::SpatialCellSet::SpatialCellSet
SpatialCellSet(lsst::geom::Box2I const &region, int xSize, int ySize=0)
Constructor.
Definition: SpatialCell.cc:303
lsst::afw::math::SpatialCellSet::getCellList
CellList & getCellList()
Return our SpatialCells.
Definition: SpatialCell.h:415
lsst::afw::math::SpatialCellCandidate::instantiate
virtual bool instantiate()
Do anything needed to make this candidate usable.
Definition: SpatialCell.h:96
lsst::afw::math::SpatialCellCandidate::GOOD
@ GOOD
Definition: SpatialCell.h:72
lsst::afw::math::SpatialCellImageCandidate::setChi2
void setChi2(double chi2)
Set the candidate's chi^2.
Definition: SpatialCell.h:152
lsst::afw::math::SpatialCellCandidateIterator::SpatialCellCandidateIterator
SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end, bool ignoreBad)
ctor; designed to be used to pass begin to SpatialCellCandidateIterator
Definition: SpatialCell.cc:241
lsst::afw::math::SpatialCellCandidate::getId
int getId() const
Return the candidate's unique ID.
Definition: SpatialCell.h:104
max
int max
Definition: BoundedField.cc:104
lsst::afw::math::SpatialCellImageCandidate::SpatialCellImageCandidate
SpatialCellImageCandidate(SpatialCellImageCandidate &&)=default
lsst::afw::math::SpatialCellCandidateIterator::operator*
std::shared_ptr< SpatialCellCandidate const > operator*() const
Dereference the iterator to return the Candidate (if there is one)
Definition: SpatialCell.cc:287
lsst::afw::math::SpatialCell::removeCandidate
void removeCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Remove a candidate from the list.
Definition: SpatialCell.cc:89
base.h
lsst::afw::math::CandidateVisitor::~CandidateVisitor
virtual ~CandidateVisitor()
Definition: SpatialCell.h:61
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::math::CandidateVisitor::processCandidate
virtual void processCandidate(SpatialCellCandidate *)
Definition: SpatialCell.h:64
LsstImageTypes.h
lsst::afw::math::SpatialCellSet::sortCandidates
void sortCandidates()
Rearrange the Candidates in all SpatialCells to reflect their current ratings.
Definition: SpatialCell.cc:374
lsst::afw::math::SpatialCellImageCandidate::getHeight
static int getHeight()
Return the height of the image that getImage should return.
Definition: SpatialCell.h:147
lsst::afw::math::SpatialCellSet::CellList
std::vector< std::shared_ptr< SpatialCell > > CellList
Definition: SpatialCell.h:389
lsst::afw::math::SpatialCellImageCandidate::SpatialCellImageCandidate
SpatialCellImageCandidate(SpatialCellImageCandidate const &)=default
lsst::afw::math::SpatialCellCandidate::getStatus
Status getStatus() const
Return the candidate's status.
Definition: SpatialCell.h:106
lsst::afw::math::SpatialCell::end
SpatialCellCandidateIterator end()
Return an iterator to (one after) the end of the Candidates.
Definition: SpatialCell.h:277
std::vector::begin
T begin(T... args)
std
STL namespace.
lsst::afw::math::SpatialCell::sortCandidates
void sortCandidates()
Rearrange the candidates to reflect their current ratings.
Definition: SpatialCell.cc:81
lsst::afw::math::SpatialCellSet::getCandidateById
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
Definition: SpatialCell.cc:416
lsst::afw::math::SpatialCellCandidate::UNKNOWN
@ UNKNOWN
Definition: SpatialCell.h:72
lsst::afw::math::SpatialCell::empty
bool empty() const
Determine if cell has no usable candidates.
Definition: SpatialCell.cc:99
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw::math::SpatialCell::begin
SpatialCellCandidateIterator begin(bool ignoreBad)
Definition: SpatialCell.h:270
lsst::afw::math::SpatialCellImageCandidate::setHeight
static void setHeight(int height)
Set the height of the image that getImage should return.
Definition: SpatialCell.h:145
lsst::afw::math::SpatialCell::getIgnoreBad
bool getIgnoreBad() const
Get whether we are omitting BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:300
lsst::afw::math::SpatialCellCandidate::setCandidateRating
virtual void setCandidateRating(double)
Set the candidate's rating.
Definition: SpatialCell.h:101
lsst::afw::math::SpatialCellSet
A collection of SpatialCells covering an entire image.
Definition: SpatialCell.h:387
lsst::afw::math::SpatialCell::size
size_t size() const
Return number of usable candidates in Cell.
Definition: SpatialCell.cc:113
lsst::afw::math::SpatialCell::SpatialCell
SpatialCell(SpatialCell const &)=default
std::vector::end
T end(T... args)
lsst::afw::math::SpatialCellCandidate::SpatialCellCandidate
SpatialCellCandidate(float const xCenter, float const yCenter)
Definition: SpatialCell.h:74
lsst::afw::math::SpatialCell::iterator
SpatialCellCandidateIterator iterator
Definition: SpatialCell.h:230
lsst::afw::math::SpatialCellImageCandidate::operator=
SpatialCellImageCandidate & operator=(SpatialCellImageCandidate &&)=default
lsst::afw::math::SpatialCellCandidate::getYCenter
float getYCenter() const
Return the object's row-centre.
Definition: SpatialCell.h:93
lsst::afw::math::SpatialCellSet::operator=
SpatialCellSet & operator=(SpatialCellSet &&)=default
lsst::afw::math::SpatialCellImageCandidate::getChi2
double getChi2() const
Return the candidate's chi^2.
Definition: SpatialCell.h:150
exceptions.h
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
lsst::afw::math::SpatialCellCandidate::Status
Status
Definition: SpatialCell.h:72
lsst::afw::math::SpatialCellCandidate::~SpatialCellCandidate
virtual ~SpatialCellCandidate()=default
(virtual) destructor – this is a base class you know