33 #include "lsst/pex/exceptions/Exception.h"
34 #include "lsst/log/Log.h"
44 struct CandidatePtrMore :
public std::binary_function<std::shared_ptr<SpatialCellCandidate>,
45 std::shared_ptr<SpatialCellCandidate>, bool> {
46 bool operator()(std::shared_ptr<SpatialCellCandidate> a, std::shared_ptr<SpatialCellCandidate> b) {
47 return a->getCandidateRating() > b->getCandidateRating();
52 int SpatialCellCandidate::_CandidateId = 0;
65 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
66 (boost::format(
"Saw unknown status %d") % status).str());
69 int SpatialCellImageCandidate::_width = 0;
71 int SpatialCellImageCandidate::_height = 0;
75 : _label(label),
_bbox(bbox), _candidateList(candidateList), _ignoreBad(true) {
76 LOGL_DEBUG(
"afw.math.SpatialCell",
"Cell %s : created with %d candidates", this->_label.c_str(),
77 this->_candidateList.size());
84 CandidateList::iterator pos =
85 std::lower_bound(_candidateList.begin(), _candidateList.end(), candidate, CandidatePtrMore());
86 _candidateList.insert(pos, candidate);
90 CandidateList::iterator pos = std::find(_candidateList.begin(), _candidateList.end(), candidate);
91 if (pos == _candidateList.end()) {
93 lsst::pex::exceptions::NotFoundError,
94 (boost::format(
"Unable to find candidate with ID == %d") % candidate->getId()).str());
96 _candidateList.erase(pos);
105 if (!(_ignoreBad && (*ptr)->isBad())) {
118 return mthis->
end() - mthis->
begin();
123 if ((*ptr)->getId() == id) {
129 return std::shared_ptr<SpatialCellCandidate>();
131 throw LSST_EXCEPT(lsst::pex::exceptions::NotFoundError,
132 (boost::format(
"Unable to find object with ID == %d") %
id).str());
138 bool const ignoreExceptions,
bool const reset) {
146 if (nMaxPerCell > 0 && i == nMaxPerCell) {
152 }
catch (lsst::pex::exceptions::Exception &e) {
153 if (ignoreExceptions) {
156 LSST_EXCEPT_ADD(e,
"Visiting candidate");
164 bool const ignoreExceptions,
bool const reset)
const {
173 for (SpatialCell::const_iterator candidate = (*cell)->begin(), candidateEnd = (*cell)->end();
174 candidate != candidateEnd; ++candidate, ++i) {
175 if (i == nMaxPerCell) {
181 }
catch (lsst::pex::exceptions::LengthError &e) {
182 if (ignoreExceptions) {
185 LSST_EXCEPT_ADD(e,
"Visiting candidate");
204 }
catch (lsst::pex::exceptions::LengthError &e) {
205 if (ignoreExceptions) {
208 LSST_EXCEPT_ADD(e,
"Visiting candidate");
216 bool const reset)
const {
225 for (SpatialCell::const_iterator candidate = (*cell)->begin(
false), candidateEnd = (*cell)->end(
false);
226 candidate != candidateEnd; ++candidate, ++i) {
229 }
catch (lsst::pex::exceptions::LengthError &e) {
230 if (ignoreExceptions) {
233 LSST_EXCEPT_ADD(e,
"Visiting candidate");
242 CandidateList::iterator
end,
bool ignoreBad)
243 : _iterator(iterator),
_end(end), _ignoreBad(ignoreBad) {
244 for (; _iterator != _end; ++_iterator) {
245 (*_iterator)->instantiate();
247 if (!(_ignoreBad && (*_iterator)->isBad())) {
254 CandidateList::iterator
end,
bool ignoreBad,
bool)
255 : _iterator(end),
_end(end), _ignoreBad(ignoreBad) {
263 if (_iterator != _end) {
267 for (; _iterator != _end; ++_iterator) {
268 (*_iterator)->instantiate();
270 if (!(_ignoreBad && (*_iterator)->isBad())) {
279 if (!(_ignoreBad && (*ptr)->isBad())) {
288 if (_iterator == _end) {
289 throw LSST_EXCEPT(lsst::pex::exceptions::NotFoundError,
"Iterator points to end");
296 if (_iterator == _end) {
297 throw LSST_EXCEPT(lsst::pex::exceptions::NotFoundError,
"Iterator points to end");
309 if (xSize <= 0 || ySize <= 0) {
311 lsst::pex::exceptions::LengthError,
312 (boost::format(
"Please specify cells that contain pixels, not %dx%d") % xSize % ySize).str());
316 if (nx * xSize != region.
getWidth()) {
328 for (
int y = 0; y < ny; ++y) {
330 int const y1 = (y == ny - 1) ? region.
getMaxY() : y0 + ySize - 1;
332 for (
int x = 0;
x < nx; ++
x) {
334 int const x1 = (
x == nx - 1) ? region.
getMaxX() : x0 + xSize - 1;
336 std::string label = (boost::format(
"Cell %dx%d") %
x % y).str();
338 _cellList.push_back(std::shared_ptr<SpatialCell>(
new SpatialCell(label, bbox)));
347 struct CellContains :
public std::unary_function<std::shared_ptr<SpatialCell>, bool> {
348 CellContains(std::shared_ptr<SpatialCellCandidate> candidate) :
_candidate(candidate) {}
350 bool operator()(std::shared_ptr<SpatialCell> cell) {
361 CellList::iterator pos = std::find_if(_cellList.begin(), _cellList.end(), CellContains(candidate));
363 if (pos == _cellList.end()) {
364 throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError,
365 (boost::format(
"Unable to insert a candidate at (%.2f, %.2f)") %
366 candidate->getXCenter() % candidate->getYCenter())
370 (*pos)->insertCandidate(candidate);
374 for (CellList::iterator cell = _cellList.begin(),
end = _cellList.end(); cell !=
end; ++cell) {
375 (*cell)->sortCandidates();
380 bool const ignoreExceptions) {
383 for (CellList::iterator cell = _cellList.begin(),
end = _cellList.end(); cell !=
end; ++cell) {
384 (*cell)->visitCandidates(visitor, nMaxPerCell, ignoreExceptions,
false);
389 bool const ignoreExceptions)
const {
392 for (CellList::const_iterator cell = _cellList.begin(),
end = _cellList.end(); cell !=
end; ++cell) {
401 for (CellList::iterator cell = _cellList.begin(),
end = _cellList.end(); cell !=
end; ++cell) {
402 (*cell)->visitAllCandidates(visitor, ignoreExceptions,
false);
409 for (CellList::const_iterator cell = _cellList.begin(),
end = _cellList.end(); cell !=
end; ++cell) {
416 for (CellList::iterator cell = _cellList.begin(),
end = _cellList.end(); cell !=
end; ++cell) {
417 std::shared_ptr<SpatialCellCandidate> cand = (*cell)->getCandidateById(
id,
true);
425 return std::shared_ptr<SpatialCellCandidate>();
427 throw LSST_EXCEPT(lsst::pex::exceptions::NotFoundError,
428 (boost::format(
"Unable to find object with ID == %d") %
id).str());
433 for (CellList::iterator cell = _cellList.begin(),
end = _cellList.end(); cell !=
end; ++cell) {
434 (*cell)->setIgnoreBad(ignoreBad);
void setStatus(Status status)
Set the candidate's status.
std::shared_ptr< SpatialCellCandidate const > operator*() const
Dereference the iterator to return the Candidate (if there is one)
void visitAllCandidates(CandidateVisitor *visitor, bool const ignoreExceptions=false, bool const reset=true)
Call the visitor's processCandidate method for every Candidate in the SpatialCell.
std::vector< std::shared_ptr< SpatialCellCandidate > > CandidateList
bool empty() const
Determine if cell has no usable candidates.
SpatialCellCandidateIterator end()
Return an iterator to (one after) the end of the Candidates.
int positionToIndex(double pos)
Convert image position to nearest integer index.
SpatialCellSet(lsst::afw::geom::Box2I const ®ion, int xSize, int ySize=0)
Constructor.
size_t operator-(SpatialCellCandidateIterator const &rhs) const
Return the number of candidate between this and rhs.
void sortCandidates()
Rearrange the candidates to reflect their current ratings.
void setIgnoreBad(bool ignoreBad)
Set whether we should omit BAD candidates from candidate list when traversing.
SpatialCellCandidateIterator(CandidateList::iterator iterator, CandidateList::iterator end, bool ignoreBad)
ctor; designed to be used to pass begin to SpatialCellCandidateIterator
virtual void processCandidate(SpatialCellCandidate *)
void insertCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Add a candidate to the list, preserving ranking.
An integer coordinate rectangle.
void operator++()
Advance the iterator, maybe skipping over candidates labelled BAD.
metadata import lsst afw display as afwDisplay n
SpatialCell(std::string const &label, lsst::afw::geom::Box2I const &bbox=lsst::afw::geom::Box2I(), CandidateList const &candidateList=CandidateList())
Constructor.
geom::Box2I const & _bbox
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. ...
void removeCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Remove a candidate from the list.
size_t size() const
Return number of usable candidates in Cell.
void sortCandidates()
Rearrange the Candidates in all SpatialCells to reflect their current ratings.
void insertCandidate(std::shared_ptr< SpatialCellCandidate > candidate)
Insert a candidate into the correct cell.
std::vector< std::shared_ptr< SpatialCell > > CellList
Class to ensure constraints for spatial modeling.
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
void visitAllCandidates(CandidateVisitor *visitor, bool const ignoreExceptions=false)
Call the visitor's processCandidate method for every Candidate in the SpatialCellSet.
std::shared_ptr< SpatialCellCandidate > getCandidateById(int id, bool noThrow=false)
Return the SpatialCellCandidate with the specified id.
std::shared_ptr< SpatialCellCandidate > _candidate
geom::Box2I const * _region
void visitCandidates(CandidateVisitor *visitor, int const nMaxPerCell=-1, bool const ignoreExceptions=false)
Call the visitor's processCandidate method for each Candidate in the SpatialCellSet.
SpatialCellCandidateIterator begin()
Return an iterator to the beginning of the Candidates.
An iterator that only returns usable members of the SpatialCell.