primatologist-gpl  5.1.2
volume_d.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef PRIMATOLOGIST_UTILITY_VOLUME_D_H
12 #define PRIMATOLOGIST_UTILITY_VOLUME_D_H
13 
15 #include <aims/vector/vector.h>
16 #include <cartodata/volume/volume.h>
17 
18 namespace aims {
19 namespace vol {
20 
21  template <typename T>
22  carto::VolumeRef<T> empty()
23  {
24  carto::VolumeRef<T> vol( (carto::Volume<T>*)0 );
25  return vol;
26  }
27 
28  template <typename T>
29  carto::VolumeRef<T> select( const carto::VolumeRef<T> & in, int t )
30  {
31  return in.view( Point4di( 0, 0, 0, t ),
32  Point4di( in.getSizeX(), in.getSizeY(),
33  in.getSizeZ(), 1 ) );
34  }
35 
36  template <typename P, typename T>
37  bool inside( const P & p, const carto::VolumeRef<T> & input )
38  {
39  std::vector<int> size = input.getSize();
40  for( int i = 0; i < p.size(); ++i )
41  if( p[i] < 0 || p[i] >= size[i] )
42  return false;
43  return true;
44  }
45 
46  template <typename P, typename T>
47  bool insideAllocated( const P & p, const carto::VolumeRef<T> & input )
48  {
49  std::vector<int> size = input.getSize();
50  std::vector<int> b = input.getBorders();
51  for( int i = 0; i < p.size(); ++i )
52  if( p[i] < -b[i/2] || p[i] >= size[i] + b[i/2+1] )
53  return false;
54  return true;
55  }
56 
57  template <typename T>
58  bool inside( const Point4di & p, const carto::VolumeRef<T> & input )
59  {
60  return 0 <= p[0] && p[0] < input.getSizeX() &&
61  0 <= p[1] && p[1] < input.getSizeY() &&
62  0 <= p[2] && p[2] < input.getSizeZ() &&
63  0 <= p[3] && p[3] < input.getSizeT();
64  }
65 
66  template <typename T>
67  bool insideAllocated( const Point4di & p, const carto::VolumeRef<T> & input )
68  {
69  std::vector<int> b = input.getBorders();
70  return -b[0] <= p[0] && p[0] < input.getSizeX() + b[1] &&
71  -b[2] <= p[1] && p[1] < input.getSizeY() + b[3] &&
72  -b[4] <= p[2] && p[2] < input.getSizeZ() + b[5] &&
73  -b[6] <= p[3] && p[3] < input.getSizeT() + b[7];
74  }
75 
76  template <typename T, typename M>
77  carto::VolumeRef<T> &
78  clip( carto::VolumeRef<T> & in,
79  const typename type::identity<T>::type & min,
80  const typename type::identity<T>::type & max,
81  const carto::VolumeRef<M> & mask )
82  {
83  for( int z = 0; z < in.getSizeZ(); ++z )
84  for( int y = 0; y < in.getSizeY(); ++y )
85  for( int x = 0; x < in.getSizeX(); ++x )
86  if( !mask.get() || mask( x, y, z ) != 0 )
87  for( int t = 0; t < in.getSizeT(); ++t )
88  {
89  if( in( x, y,z , t) < min )
90  in( x, y, z, t ) = min;
91  else if( in( x, y, z, t ) > max )
92  in( x, y, z, t ) = max;
93  }
94  return in;
95  }
96 
97  template <typename T>
98  carto::VolumeRef<T> &
99  clip( carto::VolumeRef<T> & in,
100  const typename type::identity<T>::type & min,
101  const typename type::identity<T>::type & max )
102  {
103  return clip( in, min, max, vol::empty<int>() );
104  }
105 
106  template <typename T, typename M>
107  carto::VolumeRef<T>
108  newClip( carto::VolumeRef<T> & in,
109  const typename type::identity<T>::type & min,
110  const typename type::identity<T>::type & max,
111  const carto::VolumeRef<M> & mask )
112  {
113  carto::VolumeRef<T> copy = in.copy();
114  clip( copy, min, max, mask );
115  return copy;
116  }
117 
118  template <typename T>
119  carto::VolumeRef<T>
120  newClip( carto::VolumeRef<T> & in,
121  const typename type::identity<T>::type & min,
122  const typename type::identity<T>::type & max )
123  {
124  carto::VolumeRef<T> copy = in.copy();
125  clip( copy, min, max );
126  return copy;
127  }
128 
129  template <typename T1, typename T2, typename M>
130  uintmax_t
131  sumLogicalDiff( const carto::VolumeRef<T1> & one,
132  const carto::VolumeRef<T2> & two,
133  const carto::VolumeRef<M> & mask )
134  {
135  if( one.getSizeX() != two.getSizeX() ||
136  one.getSizeY() != two.getSizeY() ||
137  one.getSizeZ() != two.getSizeZ() ||
138  one.getSizeT() != two.getSizeT() )
139  throw std::invalid_argument( "aims::vol::sumLogicalDiff: volumes "
140  "should have similar dimensions" );
141 
142  uintmax_t sum = 0;
143  for( int z = 0; z < one.getSizeZ(); ++z )
144  for( int y = 0; y < one.getSizeY(); ++y )
145  for( int x = 0; x < one.getSizeX(); ++x )
146  if( !mask.get() || mask( x, y, z ) != 0 )
147  for( int t = 0; t < one.getSizeT(); ++t )
148  if( one( x, y, z, t ) != two( x, y, z, t ) )
149  ++sum;
150  return sum;
151  }
152 
153  template <typename T1, typename T2>
154  uintmax_t
155  sumLogicalDiff( const carto::VolumeRef<T1> & one,
156  const carto::VolumeRef<T2> & two )
157  {
158  return sumLogicalDiff( one, two, empty<int>() );
159  }
160 
161  template <typename T, typename M>
162  typename carto::DataTypeTraits<T>::LongType
163  sum( const carto::VolumeRef<T> & in,
164  const carto::VolumeRef<M> & mask )
165  {
166  typename carto::DataTypeTraits<T>::LongType aggregate = 0;
167  for( int z = 0; z < in.getSizeZ(); ++z )
168  for( int y = 0; y < in.getSizeY(); ++y )
169  for( int x = 0; x < in.getSizeX(); ++x )
170  if( !mask.get() || mask( x, y, z ) != 0 )
171  for( int t = 0; t < in.getSizeT(); ++t )
172  {
173  aggregate += in( x, y, z, t );
174  }
175  return aggregate;
176  }
177 
178 } // namespace vol
179 } // namespace aims
180 
181 #endif // PRIMATOLOGIST_UTILITY_VOLUME_D_H
float min(float x, float y)
float max(float x, float y)
carto::DataTypeTraits< T >::LongType sum(const carto::VolumeRef< T > &in, const carto::VolumeRef< M > &mask)
Definition: volume_d.h:163
uintmax_t sumLogicalDiff(const carto::VolumeRef< T1 > &one, const carto::VolumeRef< T2 > &two, const carto::VolumeRef< M > &mask)
Definition: volume_d.h:131
bool insideAllocated(const P &p, const carto::VolumeRef< T > &input)
Definition: volume_d.h:47
carto::VolumeRef< T > empty()
Definition: volume_d.h:22
carto::VolumeRef< T > select(const carto::VolumeRef< T > &in, int t)
Definition: volume_d.h:29
carto::VolumeRef< T > newClip(carto::VolumeRef< T > &in, const typename type::identity< T >::type &min, const typename type::identity< T >::type &max, const carto::VolumeRef< M > &mask)
Definition: volume_d.h:108
bool inside(const P &p, const carto::VolumeRef< T > &input)
Definition: volume_d.h:37
carto::VolumeRef< T > & clip(carto::VolumeRef< T > &in, const typename type::identity< T >::type &min, const typename type::identity< T >::type &max, const carto::VolumeRef< M > &mask)
Definition: volume_d.h:78