Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
rectangle2d.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_WIN_RECTANGLE2D_HH
27 # define MLN_WIN_RECTANGLE2D_HH
28 
34 
35 # include <mln/core/internal/classical_window_base.hh>
36 # include <mln/core/alias/dpoint2d.hh>
37 # include <mln/core/def/coord.hh>
38 
39 
40 namespace mln
41 {
42 
43  mln_internal_add_classical_window_trait(rectangle2d);
44 
45 
46  namespace win
47  {
48 
64  struct rectangle2d : public internal::classical_window_base< dpoint2d, rectangle2d >
65  {
73  rectangle2d(unsigned height, unsigned width);
74 
75 
77  unsigned height() const;
78 
80  unsigned width() const;
81 
83  unsigned area() const;
84 
88  unsigned delta_() const;
89 
90 
92  const std::vector<dpoint2d>& std_vector() const;
93 
94  void print_(std::ostream& ostr) const;
95 
96  protected:
97 
98  unsigned height_, width_;
99  };
100 
101 
102 
103 # ifndef MLN_INCLUDE_ONLY
104 
105  inline
106  rectangle2d::rectangle2d(unsigned height, unsigned width)
107  : height_(height),
108  width_(width)
109  {
110  mln_precondition(height % 2 == 1 && width % 2 == 1);
111  const def::coord drow = (def::coord) (height / 2);
112  const def::coord dcol = (def::coord) (width / 2);
113 
114  for (def::coord row = (def::coord) -drow; row <= drow; ++row)
115  for (def::coord col = (def::coord) -dcol; col <= dcol; ++col)
116  this->insert(dpoint2d(row, col));
117  }
118 
119  inline
120  unsigned rectangle2d::height() const
121  {
122  return height_;
123  }
124 
125  inline
126  unsigned rectangle2d::width() const
127  {
128  return width_;
129  }
130 
131  inline
132  unsigned rectangle2d::area() const
133  {
134  return width_ * height_;
135  }
136 
137  inline
138  unsigned rectangle2d::delta_() const
139  {
140  return width_ > height_ ? width_ / 2 : height_ / 2;
141  }
142 
143  inline
144  const std::vector<dpoint2d>&
146  {
147  return win_.std_vector();
148  }
149 
150  inline
151  void
152  rectangle2d::print_(std::ostream& ostr) const
153  {
154  ostr << "[rectangle2d: width=" << width_ << ", height=" << height_ << ']';
155  }
156 
157 # endif // ! MLN_INCLUDE_ONLY
158 
159  } // end of namespace mln::win
160 
161 } // end of namespace mln
162 
163 
164 
165 // When rectangle2d is involved, one surely also wants:
166 # include <mln/win/hline2d.hh>
167 # include <mln/win/vline2d.hh>
168 
169 
170 #endif // ! MLN_WIN_RECTANGLE2D_HH