aimstil  5.0.5
boxTools.h
Go to the documentation of this file.
1 #ifndef TIL_BOXTOOLS_H
2 #define TIL_BOXTOOLS_H
3 
6 
7 
8 namespace til
9 {
10 
12  template < typename T, typename TStorage, std::size_t D >
13  inline
14  typename boost::enable_if<is_numeric_container<TStorage>, bool>::type
15  contains(const Box<T,D> & box, const TStorage & v)
16  {
17  return all_greater_equal(v, box.min_bounds()) && all_less_equal(v, box.max_bounds());
18  }
19 
21  template < typename T1, typename T2, std::size_t D >
22  inline bool contains(const Box<T1,D> & boxOutside, const Box<T2,D> & boxInside)
23  {
24  return (contains(boxOutside, boxInside.min_bounds()) &&
25  contains(boxOutside, boxInside.max_bounds()));
26  }
27 
28 
31  // NB: the range checking is done by the box object itself; if bounds are
32  // not consistent, box throws an exception
33  template < typename T, std::size_t D >
34  Box<T,D> intersection(const Box<T,D> & b1, const Box<T,D> & b2)
35  {
36  // NB: we first collect the numbers, and then assign them to the box,
37  // because using Box.setX/Y/ZMin/Max individually will most
38  // of the time break the box consistency rule (having min's < max's)
39  // and hence throw an exception
40  return Box<T,D>(
41  max(b1.min_bounds(), b2.min_bounds()),
42  min(b1.max_bounds(), b2.max_bounds())
43  );
44  }
45 
47  template < typename T, std::size_t D >
48  std::ostream& operator<<(std::ostream& os, const Box<T,D> &box)
49  {
50  return os << "(" <<" "<< box.min_bounds() <<" "<< "," <<" "<< box.max_bounds() << " )" << std::endl;
51  }
52 
53 
55  template < typename T, std::size_t D>
57  {
58  return box.max_bounds() - box.min_bounds();
59  }
60 
62  // TODO: return type might not be appropriate (e.g. char)
63  template < typename T, std::size_t D >
64  T volume(const Box<T,D> &box)
65  {
66  T res = 1;
67  for (std::size_t i = 0; i < D; ++i)
68  {
69  res *= box.max_bounds()[i] - box.min_bounds()[i];
70  }
71  return res;
72  }
73 
74 } // namespace til
75 
76 
77 #endif
78 
boost::enable_if< is_Image< TImage >, typename TImage::value_type >::type min(const TImage &im)
Box< T, D > intersection(const Box< T, D > &b1, const Box< T, D > &b2)
Computes the intersection of two boxes.
Definition: boxTools.h:34
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
numeric_array< T, D > size(const Box< T, D > &box)
Return the size of a box.
Definition: boxTools.h:56
const numeric_array< T, D > & min_bounds() const
Get min bounds.
Definition: Box.h:41
const numeric_array< T, D > & max_bounds() const
Get max bounds.
Definition: Box.h:43
TImage::value_type max(const TImage &im)
Returns the maximum intensity of the input image.
boost::enable_if< is_numeric_container< TStorage >, bool >::type contains(const Box< T, D > &box, const TStorage &v)
Check whether a point lies within box.
Definition: boxTools.h:15
T volume(const Box< T, D > &box)
Return the volume of a box.
Definition: boxTools.h:64
A 3D box parallel to canonical axes.
Definition: Box.h:25