LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Bounds.cc
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 #include <vector>
26 
28 
29 namespace lsst {
30 namespace meas {
31 namespace algorithms {
32 namespace shapelet {
33 
34  void Position::read(std::istream& fin)
35  { double x,y; fin >> x >> y; _z = std::complex<double>(x,y); }
36 
37  void Position::write(std::ostream& fout) const
38  { fout << getX() << " " << getY() <<" "; }
39 
40  void Bounds::operator+=(const Position& pos)
41  // Expand the bounds to include the given position.
42  {
43  if (_isDefined) {
44  if (pos.getX() < _xMin) _xMin = pos.getX();
45  else if (pos.getX() > _xMax) _xMax = pos.getX();
46  if (pos.getY() < _yMin) _yMin = pos.getY();
47  else if (pos.getY() > _yMax) _yMax = pos.getY();
48  } else {
49  _xMin = _xMax = pos.getX();
50  _yMin = _yMax = pos.getY();
51  _isDefined = true;
52  }
53  }
54 
55  void Bounds::operator+=(const Bounds& rhs)
56  // Expand the bounds to include the given bounds
57  {
58  if (!rhs.isDefined()) return;
59  if (_isDefined) {
60  if (rhs.getXMin() < _xMin) _xMin = rhs.getXMin();
61  if (rhs.getXMax() > _xMax) _xMax = rhs.getXMax();
62  if (rhs.getYMin() < _yMin) _yMin = rhs.getYMin();
63  if (rhs.getYMax() > _yMax) _yMax = rhs.getYMax();
64  } else {
65  *this = rhs;
66  _isDefined = true;
67  }
68  }
69 
70  bool Bounds::operator==(const Bounds& rhs) const
71  {
72  if (!_isDefined) return (!rhs._isDefined);
73  else return (_xMin == rhs._xMin && _xMax == rhs._xMax &&
74  _yMin == rhs._yMin && _yMax == rhs._yMax);
75  }
76 
77 
79  {
80  return Position((_xMin + _xMax)/2.,(_yMin + _yMax)/2.);
81  }
82 
83  Bounds Bounds::operator&(const Bounds& rhs) const
84  {
85  Bounds temp(_xMin<rhs._xMin ? rhs._xMin : _xMin,
86  _xMax>rhs._xMax ? rhs._xMax : _xMax,
87  _yMin<rhs._yMin ? rhs._yMin : _yMin,
88  _yMax>rhs._yMax ? rhs._yMax : _yMax);
89  if (temp._xMin>temp._xMax || temp._yMin>temp._yMax) return Bounds();
90  else return temp;
91  }
92 
93  void Bounds::snap(double d)
94  {
95  if (_isDefined) {
96  _xMax = d*ceil(_xMax/d);
97  _xMin = d*floor(_xMin/d);
98  _yMax = d*ceil(_yMax/d);
99  _yMin = d*floor(_yMin/d);
100  }
101  }
102 
103  void Bounds::addXBorder(double d)
104  {
105  if (_isDefined) { _xMax += d; _xMin -= d; }
106  }
107 
108  void Bounds::addYBorder(double d)
109  {
110  if (_isDefined) { _yMax += d; _yMin -= d; }
111  }
112 
113  void Bounds::addBorder(double d)
114  {
115  if (_isDefined) { _xMax += d; _xMin -= d; _yMax += d; _yMin -= d; }
116  }
117 
118  bool Bounds::includes(const Position& pos) const
119  {
120  return (_isDefined && pos.getX()<=_xMax && pos.getX()>=_xMin &&
121  pos.getY()<=_yMax && pos.getY()>=_yMin);
122  }
123 
124  bool Bounds::includes(double x, double y) const
125  { return includes(Position(x,y)); }
126 
127  bool Bounds::includes(const Bounds& b2) const
128  {
129  if (!b2.isDefined()) return true;
130  else return (
131  _isDefined &&
132  b2._xMin >= _xMin && b2._xMax <= _xMax &&
133  b2._yMin >= _yMin && b2._yMax <= _yMax);
134  }
135 
136  bool Bounds::intersects(const Bounds& b2) const
137  {
138  return (
139  _isDefined && b2._isDefined &&
140  !(b2._xMin >= _xMax) &&
141  !(b2._xMax <= _xMin) &&
142  !(b2._yMin >= _yMax) &&
143  !(b2._yMax <= _yMin) );
144  }
145 
146  double Bounds::getArea() const
147  { return _isDefined ? (_xMax-_xMin)*(_yMax-_yMin) : 0.; }
148 
149  std::vector<Bounds> Bounds::quarter() const
150  { return divide(2,2); }
151 
152  std::vector<Bounds> Bounds::divide(int nx, int ny) const
153  {
154  if (!_isDefined) return std::vector<Bounds>(nx*ny);
155  std::vector<Bounds> temp;
156  temp.reserve(nx*ny);
157  std::vector<double> x(nx+1);
158  std::vector<double> y(ny+1);
159  x[0] = _xMin; x[nx] = _xMax;
160  y[0] = _yMin; y[ny] = _yMax;
161  double xstep = (_xMax-_xMin)/nx;
162  double ystep = (_yMax-_yMin)/ny;
163  for(int i=1;i<nx;++i) x[i] = x[0]+i*xstep;
164  for(int j=1;j<ny;++j) y[j] = y[0]+j*ystep;
165  for(int i=0;i<nx;++i) for(int j=0;j<ny;++j)
166  temp.push_back(Bounds(x[i],x[i+1],y[j],y[j+1]));
167  return temp;
168  }
169 
170  void Bounds::write(std::ostream& fout) const
171  { fout << _xMin << ' ' << _xMax << ' ' << _yMin << ' ' << _yMax << ' '; }
172 
173  void Bounds::read(std::istream& fin)
174  { fin >> _xMin >> _xMax >> _yMin >> _yMax; _isDefined = true; }
175 
176 }}}}
int y
std::vector< Bounds > divide(int nx, int ny) const
Definition: Bounds.cc:152
bool operator==(const Bounds &rhs) const
Definition: Bounds.cc:70
void operator+=(const Position &pos)
Definition: Bounds.cc:40
std::vector< Bounds > quarter() const
Definition: Bounds.cc:149
void read(std::istream &fin)
Definition: Bounds.cc:34
bool intersects(const Bounds &b2) const
Definition: Bounds.cc:136
Extent< int, N > ceil(Extent< double, N > const &input)
void write(std::ostream &fout) const
Definition: Bounds.cc:170
void write(std::ostream &fout) const
Definition: Bounds.cc:37
double x
bool includes(const Position &pos) const
Definition: Bounds.cc:118
Bounds operator&(const Bounds &rhs) const
Definition: Bounds.cc:83
Extent< int, N > floor(Extent< double, N > const &input)
void read(std::istream &fin)
Definition: Bounds.cc:173