27 #ifndef MLN_CORE_SITE_SET_BOX_HH
28 # define MLN_CORE_SITE_SET_BOX_HH
36 # include <mln/core/concept/box.hh>
37 # include <mln/core/internal/box_impl.hh>
38 # include <mln/core/point.hh>
39 # include <mln/literal/origin.hh>
46 template <
typename P>
class box;
47 template <
typename P>
class box_fwd_piter_;
48 template <
typename P>
class box_bkd_piter_;
55 struct site_set_<
box<P> >
57 typedef trait::site_set::nsites::known
nsites;
58 typedef trait::site_set::bbox::straight
bbox;
59 typedef trait::site_set::contents::fixed
contents;
60 typedef trait::site_set::arity::unique
arity;
64 struct set_precise_unary_< op::ord,
box<P> >
66 typedef set_precise_unary_< op::ord, box<P> >
ret;
67 bool strict(
const box<P>& lhs,
const box<P>&
rhs)
const;
82 public internal::box_impl_< P::dim, mln_coord(P), box<P> >,
83 private mlc_is_unqualif(P)::check_t
87 enum {
dim = P::dim };
129 box(mln_coord(P)
nslis, mln_coord(P) nrows, mln_coord(P)
ncols);
136 bool has(
const P&
p)
const;
178 template <
typename P>
179 std::ostream& operator<<(std::ostream& ostr, const box<P>& b);
184 template <
typename P>
191 # ifndef MLN_INCLUDE_ONLY
193 template <
typename P>
203 template <
typename P>
208 if (pmin_.col() < ref.pmin().col())
209 pmin_.col() = ref.pmin().col();
210 if (pmin_.row() < ref.pmin().row())
211 pmin_.row() = ref.pmin().row();
213 if (pmax_.col() > ref.pmax().col())
214 pmax_.col() = ref.pmax().col();
215 if (pmax_.row() > ref.pmax().row())
216 pmax_.row() = ref.pmax().row();
219 template <
typename P>
224 mln_precondition(is_valid());
231 for (
unsigned i = 0; i < P::dim; ++i)
233 if (b.pmin()[i] < pmin_[i])
234 pmin_[i] = b.pmin()[i];
235 if (b.pmax()[i] > pmax_[i])
236 pmax_[i] = b.pmax()[i];
240 template <
typename P>
245 mln_precondition(is_valid());
249 template <
typename P>
257 template <
typename P>
262 mln_precondition(is_valid());
266 template <
typename P>
274 template <
typename P>
277 : pmin_(P::plus_infty()),
278 pmax_(P::minus_infty())
283 template <
typename P>
289 mln_precondition(is_valid());
292 template <
typename P>
296 metal::bool_<(dim == 1)>::check();
297 pmin_ = literal::origin;
298 pmax_ = P(ninds - 1);
301 template <
typename P>
305 metal::bool_<(dim == 2)>::check();
306 mln_precondition(nrows != 0 && ncols != 0);
308 pmin_ = literal::origin;
309 pmax_ = P(--nrows, --ncols);
310 mln_postcondition(is_valid());
313 template <
typename P>
315 box<P>::box(mln_coord(P) nslis, mln_coord(P) nrows, mln_coord(P) ncols)
317 metal::bool_<(dim == 3)>::check();
318 pmin_ = literal::origin;
319 pmax_ = P(nslis - 1, nrows - 1, ncols - 1);
320 mln_postcondition(is_valid());
323 template <
typename P>
326 box<P>::has(
const P& p)
const
328 mln_precondition(is_valid());
329 for (
unsigned i = 0; i < P::dim; ++i)
330 if (p[i] < pmin_[i] || p[i] > pmax_[i])
335 template <
typename P>
338 box<P>::enlarge(
unsigned b)
340 mln_precondition(is_valid());
341 for (
unsigned i = 0; i < P::dim; ++i)
343 pmin_[i] =
static_cast<mln_coord(P)
>(pmin_[i] - b);
344 pmax_[i] =
static_cast<mln_coord(P)
>(pmax_[i] + b);
346 mln_postcondition(is_valid());
349 template <
typename P>
352 box<P>::enlarge(
unsigned dim,
unsigned b)
354 mln_precondition(is_valid());
355 pmin_[dim] =
static_cast<mln_coord(P)
>(pmin_[dim] - b);
356 pmax_[dim] =
static_cast<mln_coord(P)
>(pmax_[dim] + b);
357 mln_postcondition(is_valid());
360 template <
typename P>
367 for (
unsigned i = 0; i < P::dim; i++)
369 pmin[i] = (a.pmin()[i] < b.pmin()[i]) ? a.pmin()[i] : b.pmin()[i];
370 pmax[i] = (a.pmax()[i] > b.pmax()[i]) ? a.pmax()[i] : b.pmax()[i];
373 return box<P>(pmin, pmax);
376 template <
typename P>
379 box<P>::to_larger(
unsigned b)
const
381 mln_precondition(is_valid());
384 for (
unsigned i = 0; i < P::dim; ++i)
386 tmp.pmin_[i] =
static_cast<mln_coord(P)
>(tmp.pmin_[i] - b);
387 tmp.pmax_[i] =
static_cast<mln_coord(P)
>(tmp.pmax_[i] + b);
389 mln_postcondition(tmp.is_valid());
393 template <
typename P>
396 box<P>::pcenter()
const
398 mln_precondition(is_valid());
400 for (
unsigned i = 0; i < P::dim; ++i)
401 center[i] =
static_cast<mln_coord(P)
>(pmin_[i] + ((pmax_[i] - pmin_[i]) / 2));
405 template <
typename P>
408 box<P>::memory_size()
const
410 return sizeof(*this);
413 template <
typename P>
415 std::ostream& operator<<(std::ostream& ostr, const box<P>& b)
417 mln_precondition(b.is_valid());
418 return ostr <<
"[" << b.pmin() <<
".." << b.pmax() <<
']';
424 template <
typename P>
427 set_precise_unary_< op::ord, box<P> >::strict(
const box<P>& lhs,
const box<P>& rhs)
const
430 return util::ord_lexi_strict(lhs.pmin(), lhs.pmax(),
431 rhs.pmin(), rhs.pmax());
436 # endif // ! MLN_INCLUDE_ONLY
441 # include <mln/core/site_set/box_piter.hh>
444 #endif // ! MLN_CORE_SITE_SET_BOX_HH