lsst.afw  14.0-23-g38c323faa+1
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/afw/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 }
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 
82  virtual ~SpatialCellCandidate() {}
83 
85  float getXCenter() const { return _xCenter; }
86 
88  float getYCenter() const { return _yCenter; }
89 
91  virtual bool instantiate() { return true; }
92 
94  virtual double getCandidateRating() const = 0;
96  virtual void setCandidateRating(double) {}
97 
99  int getId() const { return _id; }
101  Status getStatus() const { return _status; }
103  void setStatus(Status status);
105  virtual bool isBad() const { return (_status == BAD); }
106 
107 private:
108  int _id; // Unique ID for object
109  Status _status; // Is this Candidate good?
110  float const _xCenter; // The object's column-centre
111  float const _yCenter; // The object's row-centre
112 
114  static int _CandidateId;
115 };
116 
122 public:
124  SpatialCellImageCandidate(float const xCenter,
125  float const yCenter
126  )
127  : SpatialCellCandidate(xCenter, yCenter), _chi2(std::numeric_limits<double>::max()) {}
129 
131  static void setWidth(int width) { _width = width; }
133  static int getWidth() { return _width; }
134 
136  static void setHeight(int height) { _height = height; }
138  static int getHeight() { return _height; }
139 
141  double getChi2() const { return _chi2; }
143  void setChi2(double chi2) { _chi2 = chi2; }
144 
145 private:
146  static int _width; // the width of images to return; may be ignored by subclasses
147  static int _height; // the height of images to return; may be ignored by subclasses
148  double _chi2; // chi^2 for fit
149 };
150 
155  friend class SpatialCell;
157 
158 public:
159  // ctors are protected
163  void operator++();
167  size_t operator-(SpatialCellCandidateIterator const& rhs) const;
168 
177 
179  bool operator==(SpatialCellCandidateIterator const& rhs) const { return _iterator == rhs._iterator; }
181  bool operator!=(SpatialCellCandidateIterator const& rhs) const { return _iterator != rhs._iterator; }
182 
183 protected:
190  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end,
191  bool ignoreBad);
198  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end,
199  bool ignoreBad, bool);
200 
201 private:
202  CandidateList::iterator _iterator;
203  CandidateList::iterator _end;
204  bool _ignoreBad;
205 };
206 
218 class SpatialCell {
219 public:
230  CandidateList const& candidateList = CandidateList());
231 
235  virtual ~SpatialCell() { ; };
236 
240  bool empty() const;
244  size_t size() const;
245 
249  void sortCandidates();
254  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad);
255  }
257  ) {
258  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad);
259  }
264  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad, true);
265  }
267  ) {
268  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad, true);
269  }
273  void insertCandidate(std::shared_ptr<SpatialCellCandidate> candidate);
274 
281  void removeCandidate(std::shared_ptr<SpatialCellCandidate> candidate);
282 
284  void setIgnoreBad(bool ignoreBad) { _ignoreBad = ignoreBad; }
286  bool getIgnoreBad() const { return _ignoreBad; }
287 
296  std::shared_ptr<SpatialCellCandidate> getCandidateById(int id, bool noThrow = false);
300  std::string const& getLabel() const { return _label; }
304  lsst::afw::geom::Box2I const& getBBox() const { return _bbox; }
305  /*
306  * Visit our candidates
307  */
320  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
321  bool const ignoreExceptions = false, bool const reset = true);
335  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
336  bool const ignoreExceptions = false, bool const reset = true) const;
346  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false,
347  bool const reset = true);
360  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false,
361  bool const reset = true) const;
362 
363 private:
364  std::string _label; // Name of cell for logging/trace
365  lsst::afw::geom::Box2I _bbox; // Bounding box of cell in overall image
366  CandidateList _candidateList; // List of all candidates in the cell
367  bool _ignoreBad; // Don't include BAD candidates when traversing the list
368 };
369 
374 public:
376 
386  SpatialCellSet(lsst::afw::geom::Box2I const& region, int xSize, int ySize = 0);
387 
391  virtual ~SpatialCellSet() { ; };
392 
396  CellList& getCellList() { return _cellList; }
397 
401  lsst::afw::geom::Box2I getBBox() const { return _region; };
402 
406  void insertCandidate(std::shared_ptr<SpatialCellCandidate> candidate);
407 
409  void sortCandidates();
410 
422  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
423  bool const ignoreExceptions = false);
433  void visitCandidates(CandidateVisitor* visitor, int const nMaxPerCell = -1,
434  bool const ignoreExceptions = false) const;
443  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false);
452  void visitAllCandidates(CandidateVisitor* visitor, bool const ignoreExceptions = false) const;
453 
463  std::shared_ptr<SpatialCellCandidate> getCandidateById(int id, bool noThrow = false);
464 
466  void setIgnoreBad(bool ignoreBad);
467 
468 private:
469  lsst::afw::geom::Box2I _region; // Bounding box of overall image
470  CellList _cellList; // List of SpatialCells
471 };
472 }
473 }
474 }
475 
476 #endif
SpatialCellCandidateIterator begin(bool ignoreBad)
Definition: SpatialCell.h:256
static int getWidth()
Return the width of the image that getImage should return.
Definition: SpatialCell.h:133
std::vector< std::shared_ptr< SpatialCellCandidate > > CandidateList
Definition: SpatialCell.h:220
lsst::afw::geom::Box2I getBBox() const
Return the bounding box of the image.
Definition: SpatialCell.h:401
virtual ~SpatialCellCandidate()
(virtual) destructor – this is a base class you know
Definition: SpatialCell.h:82
lsst::afw::geom::Box2I const & getBBox() const
Get SpatialCell&#39;s BBox.
Definition: SpatialCell.h:304
static void setHeight(int height)
Set the height of the image that getImage should return.
Definition: SpatialCell.h:136
SpatialCellCandidateIterator end()
Return an iterator to (one after) the end of the Candidates.
Definition: SpatialCell.h:263
std::shared_ptr< Image< PixelT > > operator*(Image< PixelT > const &img, ImageSlice< PixelT > const &slc)
Overload operator*()
Definition: ImageSlice.cc:104
float getXCenter() const
Return the object&#39;s column-centre.
Definition: SpatialCell.h:85
virtual bool isBad() const
Is this candidate unacceptable?
Definition: SpatialCell.h:105
STL namespace.
virtual void processCandidate(SpatialCellCandidate *)
Definition: SpatialCell.h:64
int getId() const
Return the candidate&#39;s unique ID.
Definition: SpatialCell.h:99
double getChi2() const
Return the candidate&#39;s chi^2.
Definition: SpatialCell.h:141
virtual ~SpatialCell()
Destructor.
Definition: SpatialCell.h:235
bool operator!=(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators unequal?
Definition: SpatialCell.h:181
Rotation angle is unknown.
An integer coordinate rectangle.
Definition: Box.h:55
STL class.
bool operator==(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators equal?
Definition: SpatialCell.h:179
A collection of SpatialCells covering an entire image.
Definition: SpatialCell.h:373
A base class for image defects.
SpatialCellImageCandidate(float const xCenter, float const yCenter)
ctor
Definition: SpatialCell.h:124
int end
virtual ~SpatialCellSet()
Destructor.
Definition: SpatialCell.h:391
int _id
Definition: Mask.cc:567
int max
Definition: BoundedField.cc:99
Status getStatus() const
Return the candidate&#39;s status.
Definition: SpatialCell.h:101
SpatialCellCandidateIterator iterator
Definition: SpatialCell.h:221
virtual bool instantiate()
Do anything needed to make this candidate usable.
Definition: SpatialCell.h:91
std::shared_ptr< Image< PixelT > > operator-(Image< PixelT > const &img, ImageSlice< PixelT > const &slc)
Overload operator-()
Definition: ImageSlice.cc:89
STL class.
virtual void setCandidateRating(double)
Set the candidate&#39;s rating.
Definition: SpatialCell.h:96
static void setWidth(int width)
Set the width of the image that getImage should return.
Definition: SpatialCell.h:131
Base class for candidate objects in a SpatialCell that are able to return an Image of some sort (e...
Definition: SpatialCell.h:121
float getYCenter() const
Return the object&#39;s row-centre.
Definition: SpatialCell.h:88
std::vector< std::shared_ptr< SpatialCell > > CellList
Definition: SpatialCell.h:375
Class to ensure constraints for spatial modeling.
Definition: SpatialCell.h:218
bool getIgnoreBad() const
Get whether we are omitting BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:286
Base class for candidate objects in a SpatialCell.
Definition: SpatialCell.h:70
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:284
SpatialCellCandidateIterator begin()
Return an iterator to the beginning of the Candidates.
Definition: SpatialCell.h:253
An iterator that only returns usable members of the SpatialCell.
Definition: SpatialCell.h:154
SpatialCellCandidateIterator end(bool ignoreBad)
Definition: SpatialCell.h:266
void setChi2(double chi2)
Set the candidate&#39;s chi^2.
Definition: SpatialCell.h:143
std::string const & getLabel() const
Get SpatialCell&#39;s label.
Definition: SpatialCell.h:300
SpatialCellCandidate(float const xCenter, float const yCenter)
Definition: SpatialCell.h:74
static int getHeight()
Return the height of the image that getImage should return.
Definition: SpatialCell.h:138
CellList & getCellList()
Return our SpatialCells.
Definition: SpatialCell.h:396