LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 
33 #ifndef LSST_AFW_MATH_SPATIALCELL_H
34 #define LSST_AFW_MATH_SPATIALCELL_H
35 
36 #include <limits>
37 #include <vector>
38 #include <string>
39 
40 #include "boost/shared_ptr.hpp"
41 #include "lsst/base.h"
42 #include "lsst/pex/exceptions.h"
43 #include "lsst/afw/geom.h"
45 
46 namespace lsst {
47 namespace afw {
48 
49 // forward declarations
50 namespace image {
51  template<typename ImagePixelT> class Image;
52  template<typename ImagePixelT, typename MaskPixelT, typename VariancePixelT> class MaskedImage;
53 }
54 
55 namespace math {
56 
57  /********************************************************************************************************/
59  class SpatialCellCandidate;
60 
62  public:
64  virtual ~CandidateVisitor() {}
65 
66  virtual void reset() {}
68  };
69 
70  /************************************************************************************************************/
75  public:
76  typedef boost::shared_ptr<SpatialCellCandidate> Ptr;
77  typedef boost::shared_ptr<const SpatialCellCandidate> ConstPtr;
78 
79  enum Status {BAD = 0, GOOD = 1, UNKNOWN = 2};
80 
81  SpatialCellCandidate(float const xCenter,
82  float const yCenter
83  ) :
84  _id(++_CandidateId),
86  _xCenter(xCenter), _yCenter(yCenter) {
87  }
88 
92  virtual ~SpatialCellCandidate() {}
93 
95  float getXCenter() const { return _xCenter; }
96 
98  float getYCenter() const { return _yCenter; }
99 
101  virtual bool instantiate() { return true; }
102 
104  virtual double getCandidateRating() const = 0;
106  virtual void setCandidateRating(double) {}
107 
109  int getId() const { return _id; }
111  Status getStatus() const { return _status; }
112  void setStatus(Status status);
114  virtual bool isBad() const {
115  return (_status == BAD);
116  }
117  private:
118  int _id; // Unique ID for object
119  Status _status; // Is this Candidate good?
120  float const _xCenter; // The object's column-centre
121  float const _yCenter; // The object's row-centre
122 
123  static int _CandidateId; // Unique identifier for candidates; useful for preserving current candidate
124  // following insertion
125  };
126 
127  /************************************************************************************************************/
132  template<typename PixelT>
134  public:
135  typedef boost::shared_ptr<SpatialCellImageCandidate> Ptr;
136  typedef boost::shared_ptr<const SpatialCellImageCandidate> ConstPtr;
137 
139  SpatialCellImageCandidate(float const xCenter,
140  float const yCenter
141  ) : SpatialCellCandidate(xCenter, yCenter),
142  _image(PTR(lsst::afw::image::Image<PixelT>)()),
143  _chi2(std::numeric_limits<double>::max()) {
144  }
146 
149 
151  static void setWidth(int width) {
152  _width = width;
153  }
155  static int getWidth() { return _width; }
156 
158  static void setHeight(int height) { _height = height; }
160  static int getHeight() { return _height; }
161 
163  double getChi2() const { return _chi2; }
165  void setChi2(double chi2) { _chi2 = chi2; }
166 
167  protected:
169  private:
170  static int _width; // the width of images to return; may be ignored by subclasses
171  static int _height; // the height of images to return; may be ignored by subclasses
172  double _chi2; // chi^2 for fit
173  };
174 
176  template<typename PixelT>
177  int SpatialCellImageCandidate<PixelT>::_width = 0;
178 
180  template<typename PixelT>
181  int SpatialCellImageCandidate<PixelT>::_height = 0;
182 
183  /************************************************************************************************************/
188  template<typename PixelT>
190  public:
191  typedef boost::shared_ptr<SpatialCellMaskedImageCandidate> Ptr;
192  typedef boost::shared_ptr<const SpatialCellMaskedImageCandidate> ConstPtr;
193 
195  SpatialCellMaskedImageCandidate(float const xCenter,
196  float const yCenter
197  ) : SpatialCellCandidate(xCenter, yCenter),
198  _image(PTR(lsst::afw::image::MaskedImage<PixelT,
199  lsst::afw::image::MaskPixel,
200  lsst::afw::image::VariancePixel>)()),
201  _chi2(std::numeric_limits<double>::max()) {
202  }
204 
207  lsst::afw::image::VariancePixel>) getMaskedImage() const = 0;
208 
210  static void setWidth(int width) {
211  _width = width;
212  }
214  static int getWidth() { return _width; }
215 
217  static void setHeight(int height) { _height = height; }
219  static int getHeight() { return _height; }
220 
222  double getChi2() const { return _chi2; }
224  void setChi2(double chi2) { _chi2 = chi2; }
225 
226  protected:
229  private:
230  static int _width; // the width of images to return; may be ignored by subclasses
231  static int _height; // the height of images to return; may be ignored by subclasses
232  double _chi2; // chi^2 for fit
233  };
234 
236  template<typename PixelT>
237  int SpatialCellMaskedImageCandidate<PixelT>::_width = 0;
238 
240  template<typename PixelT>
241  int SpatialCellMaskedImageCandidate<PixelT>::_height = 0;
242 
243  /************************************************************************************************************/
248  friend class SpatialCell;
249  typedef std::vector<PTR(SpatialCellCandidate)> CandidateList;
250 
251  public:
252  // ctors are protected
253  void operator++();
254  size_t operator-(SpatialCellCandidateIterator const& rhs) const;
255 
256  CONST_PTR(SpatialCellCandidate) operator*() const;
257  PTR(SpatialCellCandidate) operator*();
258 
260  bool operator==(SpatialCellCandidateIterator const& rhs) const {
261  return _iterator == rhs._iterator;
262  }
264  bool operator!=(SpatialCellCandidateIterator const& rhs) const {
265  return _iterator != rhs._iterator;
266  }
267 
268  protected:
269  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end, bool ignoreBad);
270  SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end, bool ignoreBad, bool);
271 
272  private:
273  CandidateList::iterator _iterator;
274  CandidateList::iterator _end;
276  };
277 
278  /************************************************************************************************************/
290  class SpatialCell {
291  public:
292  typedef boost::shared_ptr<SpatialCell> Ptr;
293  typedef boost::shared_ptr<const SpatialCell> ConstPtr;
294  typedef std::vector<PTR(SpatialCellCandidate)> CandidateList;
299  SpatialCell(std::string const& label,
301  CandidateList const& candidateList=CandidateList());
302 
306  virtual ~SpatialCell() {;};
307 
308  bool empty() const;
309  size_t size() const;
310 
311  void sortCandidates();
316  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad);
317  }
319  ) {
320  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad);
321  }
326  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), _ignoreBad, true);
327  }
329  ) {
330  return SpatialCellCandidateIterator(_candidateList.begin(), _candidateList.end(), ignoreBad, true);
331  }
332  //
333  void insertCandidate(PTR(SpatialCellCandidate) candidate);
334 
340  void removeCandidate(PTR(SpatialCellCandidate) candidate);
341 
343  void setIgnoreBad(bool ignoreBad) { _ignoreBad = ignoreBad; }
345  bool getIgnoreBad() const { return _ignoreBad; }
346 
347  PTR(SpatialCellCandidate) getCandidateById(int id, bool noThrow=false);
351  std::string const& getLabel() const { return _label; }
355  lsst::afw::geom::Box2I const& getBBox() const { return _bbox; }
356  /*
357  * Visit our candidates
358  */
359  void visitCandidates(CandidateVisitor * visitor, int const nMaxPerCell=-1,
360  bool const ignoreExceptions=false, bool const reset=true);
361  void visitCandidates(CandidateVisitor * visitor, int const nMaxPerCell=-1,
362  bool const ignoreExceptions=false, bool const reset=true) const;
363  void visitAllCandidates(CandidateVisitor * visitor,
364  bool const ignoreExceptions=false, bool const reset=true);
365  void visitAllCandidates(CandidateVisitor * visitor,
366  bool const ignoreExceptions=false, bool const reset=true) const;
367 
368  private:
369  std::string _label; // Name of cell for logging/trace
370  lsst::afw::geom::Box2I _bbox; // Bounding box of cell in overall image
371  CandidateList _candidateList; // List of all candidates in the cell
372  bool _ignoreBad; // Don't include BAD candidates when traversing the list
373  };
374 
379  public:
380  typedef boost::shared_ptr<SpatialCellSet> Ptr;
381  typedef boost::shared_ptr<const SpatialCellSet> ConstPtr;
382 
383  typedef std::vector<PTR(SpatialCell)> CellList;
384 
385  SpatialCellSet(lsst::afw::geom::Box2I const& region, int xSize, int ySize=0);
386 
390  virtual ~SpatialCellSet() {;};
391 
395  CellList& getCellList() { return _cellList; }
396 
400  lsst::afw::geom::Box2I getBBox() const { return _region; };
401 
402  void insertCandidate(PTR(SpatialCellCandidate) candidate);
403 
404  void sortCandidates();
405 
406  void visitCandidates(CandidateVisitor * visitor, int const nMaxPerCell=-1,
407  bool const ignoreExceptions=false);
408  void visitCandidates(CandidateVisitor * visitor, int const nMaxPerCell=-1,
409  bool const ignoreExceptions=false) const;
410  void visitAllCandidates(CandidateVisitor * visitor, bool const ignoreExceptions=false);
411  void visitAllCandidates(CandidateVisitor * visitor, bool const ignoreExceptions=false) const;
412 
413  PTR(SpatialCellCandidate) getCandidateById(int id, bool noThrow=false);
414 
415  void setIgnoreBad(bool ignoreBad);
416 
417  private:
418  lsst::afw::geom::Box2I _region; // Bounding box of overall image
419  CellList _cellList; // List of SpatialCells
420  };
421 }}}
422 
423 #endif
boost::uint16_t MaskPixel
An include file to include the header files for lsst::afw::geom.
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:343
#define PTR(...)
Definition: base.h:41
SpatialCellImageCandidate(float const xCenter, float const yCenter)
ctor
Definition: SpatialCell.h:139
float getYCenter() const
Return the object&#39;s row-centre.
Definition: SpatialCell.h:98
static void setHeight(int height)
Set the height of the image that getImage should return.
Definition: SpatialCell.h:158
SpatialCellCandidateIterator end(bool ignoreBad)
Definition: SpatialCell.h:328
virtual boost::shared_ptr< lsst::afw::image::Image< PixelT > const > getImage() const =0
Return the Candidate&#39;s Image.
bool operator!=(SpatialCellCandidateIterator const &rhs) const
Are two SpatialCellCandidateIterators unequal?
Definition: SpatialCell.h:264
#define CONST_PTR(...)
Definition: base.h:47
int getId() const
Return the candidate&#39;s unique ID.
Definition: SpatialCell.h:109
boost::shared_ptr< SpatialCellMaskedImageCandidate > Ptr
Definition: SpatialCell.h:191
static int getWidth()
Return the width of the image that getImage should return.
Definition: SpatialCell.h:214
static int _CandidateId
Unique identifier for candidates; useful for preserving current candidate following insertion...
Definition: SpatialCell.h:123
static int _width
The width of images that SpatialCellImageCandidate should return; may be ignored by subclasses...
Definition: SpatialCell.h:170
static int getWidth()
Return the width of the image that getImage should return.
Definition: SpatialCell.h:155
boost::shared_ptr< const SpatialCellSet > ConstPtr
Definition: SpatialCell.h:381
void setStatus(Status status)
Set the candidate&#39;s status.
Definition: SpatialCell.cc:61
std::complex< double > operator-(const std::complex< double > &z1, const Position &p2)
Definition: Bounds.h:70
boost::shared_ptr< const SpatialCellMaskedImageCandidate > ConstPtr
Definition: SpatialCell.h:192
virtual bool instantiate()
Do anything needed to make this candidate usable.
Definition: SpatialCell.h:101
boost::shared_ptr< const SpatialCellCandidate > ConstPtr
Definition: SpatialCell.h:77
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
Status getStatus() const
Return the candidate&#39;s status.
Definition: SpatialCell.h:111
static void setHeight(int height)
Set the height of the image that getImage should return.
Definition: SpatialCell.h:217
A collection of SpatialCells covering an entire image.
Definition: SpatialCell.h:378
boost::shared_ptr< SpatialCellImageCandidate > Ptr
Definition: SpatialCell.h:135
std::vector< boost::shared_ptr< SpatialCell > > CellList
Definition: SpatialCell.h:383
virtual void setCandidateRating(double)
Set the candidate&#39;s rating.
Definition: SpatialCell.h:106
lsst::afw::geom::Box2I const & getBBox() const
Definition: SpatialCell.h:355
geom::Box2I const & _bbox
Definition: fits_io_mpl.h:80
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
bool getIgnoreBad() const
Get whether we are omitting BAD candidates from candidate list when traversing.
Definition: SpatialCell.h:345
double getChi2() const
Return the candidate&#39;s chi^2.
Definition: SpatialCell.h:222
boost::shared_ptr< SpatialCell > Ptr
Definition: SpatialCell.h:292
float VariancePixel
! default type for Masks and MaskedImage Masks
static int getHeight()
Return the height of the image that getImage should return.
Definition: SpatialCell.h:219
boost::shared_ptr< SpatialCellCandidate > Ptr
Definition: SpatialCell.h:76
void setChi2(double chi2)
Set the candidate&#39;s chi^2.
Definition: SpatialCell.h:165
void setChi2(double chi2)
Set the candidate&#39;s chi^2.
Definition: SpatialCell.h:224
lsst::afw::geom::Box2I getBBox() const
Definition: SpatialCell.h:400
SpatialCellCandidateIterator begin(bool ignoreBad)
Definition: SpatialCell.h:318
SpatialCellCandidateIterator end()
Definition: SpatialCell.h:325
CandidateList _candidateList
Definition: SpatialCell.h:371
int status
static int _height
The height of images that SpatialCellImageCandidate should return; may be ignored by subclasses...
Definition: SpatialCell.h:171
std::vector< boost::shared_ptr< SpatialCellCandidate > > CandidateList
Definition: SpatialCell.h:249
double getChi2() const
Return the candidate&#39;s chi^2.
Definition: SpatialCell.h:163
SpatialCellMaskedImageCandidate(float const xCenter, float const yCenter)
ctor
Definition: SpatialCell.h:195
Class to ensure constraints for spatial modeling.
Definition: SpatialCell.h:290
virtual bool isBad() const
Is this candidate unacceptable?
Definition: SpatialCell.h:114
SpatialCellCandidate(float const xCenter, float const yCenter)
Definition: SpatialCell.h:81
boost::shared_ptr< SpatialCellSet > Ptr
Definition: SpatialCell.h:380
virtual void processCandidate(SpatialCellCandidate *)
Definition: SpatialCell.h:67
SpatialCellCandidateIterator begin()
Definition: SpatialCell.h:315
lsst::afw::geom::Box2I _bbox
Definition: SpatialCell.h:370
static int getHeight()
Return the height of the image that getImage should return.
Definition: SpatialCell.h:160
std::vector< boost::shared_ptr< SpatialCellCandidate > > CandidateList
Definition: SpatialCell.h:294
Definition of default types for Masks and Variance Images.
boost::shared_ptr< const SpatialCell > ConstPtr
Definition: SpatialCell.h:293
An iterator that only returns usable members of the SpatialCell.
Definition: SpatialCell.h:247
static void setWidth(int width)
Set the width of the image that getImage should return.
Definition: SpatialCell.h:151
boost::shared_ptr< const SpatialCellImageCandidate > ConstPtr
Definition: SpatialCell.h:136
virtual double getCandidateRating() const =0
Return candidate&#39;s rating.
Include files required for standard LSST Exception handling.
SpatialCellCandidateIterator iterator
Definition: SpatialCell.h:295
boost::shared_ptr< lsst::afw::image::Image< PixelT > > _image
a pointer to the Image, for the use of the base class
Definition: SpatialCell.h:168
float getXCenter() const
Return the object&#39;s column-centre.
Definition: SpatialCell.h:95