aimsalgo  5.1.2
Neuroimaging image processing
momIncStgy.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 #ifndef AIMS_MOMENT_MOMINCSTGY_H
36 #define AIMS_MOMENT_MOMINCSTGY_H
37 
38 #include <aims/moment/momStgy.h>
39 
40 
41 template< class T >
43 {
44  public:
45 
47 
48  void update( Moment< T > *, double, double, double, int );
49  void doit( Moment< T > *, carto::rc_ptr<carto::Volume< T > > &, T, int );
50  void doit( Moment< T > *, const aims::BucketMap<Void> &, int );
51 };
52 
53 
54 template< class T > inline
56  double y, double z, int dir )
57 {
58  double ct = m->ct();
59  double c2 = ct * ct;
60  double sum = m->sum();
61  double a = ( dir == Moment< T >::mSub && sum < 2.0 ) ? 0.0 : ct;
62  double d = ( dir == Moment< T >::mAdd && sum < 1.0 ) ? ct : sum * ct;
63  double d2 = ( dir == Moment< T >::mAdd ) ? ( sum + 1.0 ) * ct :
64  ( sum < 2.0 ) ? ct : ( sum - 1.0 ) * ct;
65  double d4 = d2 * d2;
66  double s = (double)dir;
67  double d1 = s - ct / d;
68 
69  x *= m->cx();
70  y *= m->cy();
71  z *= m->cz();
72  double x2 = x * x;
73  double y2 = y * y;
74  double z2 = z * z;
75  double x3 = x * x2;
76  double y3 = y * y2;
77  double z3 = z * z2;
78  double xy = x * y;
79  double xz = x * z;
80  double yz = y * z;
81  double x2y = x2 * y;
82  double x2z = x2 * z;
83  double xy2 = x * y2;
84  double y2z = y2 * z;
85  double xz2 = x * z2;
86  double yz2 = y * z2;
87  double xyz = x * y * z;
88 
89  double mx = m->m1()[ 0 ];
90  double my = m->m1()[ 1 ];
91  double mz = m->m1()[ 2 ];
92  double mx2 = mx * mx;
93  double my2 = my * my;
94  double mz2 = mz * mz;
95 
96  double m2x = m->m2()[ 0 ];
97  double m2y = m->m2()[ 1 ];
98  double m2z = m->m2()[ 2 ];
99  double m2xy = m->m2()[ 3 ];
100  double m2xz = m->m2()[ 4 ];
101  double m2yz = m->m2()[ 5 ];
102 
103  double xymz = xy * mz;
104  double xzmy = xz * my;
105  double yzmx = yz * mx;
106 
107  double mxd = mx / d;
108  double myd = my / d;
109  double mzd = mz / d;
110 
111  double mxmy = mx * my;
112  double mxmz = mx * mz;
113  double mymz = my * mz;
114  double mxyzd = mxmy * mz / d;
115 
116  double mxmyz = mxmy * z;
117  double mxymz = mxmz * y;
118  double xmymz = mymz * x;
119 
120  m->m3()[ 0 ] += s*(x3-3.0*(m2x*(x-mxd)+s*x3*ct+x2*mx)/d2+(2.0*x3*c2+
121  6.0*s*x2*mx*ct+3.0*x*mx2-mx2/d*(3.0*s*x*ct+mx-
122  s*mxd*ct))/d4)*a;
123  m->m3()[ 1 ] += s*(y3-3.0*(m2y*(y-myd)+s*y3*ct+y2*my)/d2+(2.0*y3*c2+
124  6.0*s*y2*my*ct+3.0*y*my2-my2/d*(3.0*s*y*ct+my-
125  s*myd*ct))/d4)*a;
126  m->m3()[ 2 ] += s*(z3-3.0*(m2z*(z-mzd)+s*z3*ct+z2*mz)/d2+(2.0*z3*c2+
127  6.0*s*z2*mz*ct+3.0*z*mz2-mz2/d*(3.0*s*z*ct+mz-
128  s*mzd*ct))/d4)*a;
129  m->m3()[ 3 ] += s*(x2y-(2.0*m2xy*(x-mxd)+m2x*(y-myd)+2.0*xy*mx+3.0*s*
130  x2y*ct+x2*my)/d2+(2.0*x*mxmy+y*mx2+4.0*s*xy*mx*ct+2.0*
131  s*x2*my*ct+2.0*x2y*c2-s*mxd*(mxmy*d1+2.0*x*my*ct+
132  y*mx*ct))/d4)*a;
133  m->m3()[ 4 ] += s*(x2z-(2.0*m2xz*(x-mxd)+m2x*(z-mzd)+2.0*xz*mx+3.0*s*
134  x2z*ct+x2*mz)/d2+(2.0*x*mxmz+z*mx2+4.0*s*xz*mx*ct+2.0*
135  s*x2*mz*ct+2.0*x2z*c2-s*mxd*(mxmz*d1+2.0*x*mz*ct+
136  z*mx*ct))/d4)*a;
137  m->m3()[ 5 ] += s*(xy2-(2.0*m2xy*(y-myd)+m2y*(x-mxd)+2.0*xy*my+3.0*s*
138  xy2*ct+y2*mx)/d2+(2.0*y*mxmy+x*my2+4.0*s*xy*my*ct+2.0*
139  s*y2*mx*ct+2.0*xy2*c2-s*myd*(mxmy*d1+2.0*y*mx*ct+
140  x*my*ct))/d4)*a;
141  m->m3()[ 6 ] += s*(y2z-(2.0*m2yz*(y-myd)+m2y*(z-mzd)+2.0*yz*my+3.0*s*
142  y2z*ct+y2*mz)/d2+(2.0*y*mymz+z*my2+4.0*s*yz*my*ct+2.0*
143  s*y2*mz*ct+2.0*y2z*c2-s*myd*(mymz*d1+2.0*y*mz*ct+
144  z*my*ct))/d4)*a;
145  m->m3()[ 7 ] += s*(xz2-(2.0*m2xz*(z-mzd)+m2z*(x-mxd)+2.0*xz*mz+3.0*s*
146  xz2*ct+z2*mx)/d2+(2.0*z*mxmz+x*mz2+4.0*s*xz*mz*ct+2.0*
147  s*z2*mx*ct+2.0*xz2*c2-s*mzd*(mxmz*d1+2.0*z*mx*ct+
148  x*mz*ct))/d4)*a;
149  m->m3()[ 8 ] += s*(yz2-(2.0*m2yz*(z-mzd)+m2z*(y-myd)+2.0*yz*mz+3.0*s*
150  yz2*ct+z2*my)/d2+(2.0*z*mymz+y*mz2+4.0*s*yz*mz*ct+2.0*
151  s*z2*my*ct+2.0*yz2*c2-s*mzd*(mymz*d1+2.0*z*my*ct+
152  y*mz*ct))/d4)*a;
153  m->m3()[ 9 ] += s*(xyz-(m2yz*(x-mxd)+m2xz*(y-myd)+m2xy*(z-mzd)+3.0*s*
154  xyz*ct+xymz+xzmy+yzmx)/d2+(2.0*xyz*c2+2.0*s*(yzmx+xymz+
155  xzmy)*ct+xmymz+mxymz+mxmyz-mxyzd-s*ct*(xmymz+mxymz+mxmyz-
156  mxyzd)/d)/d4)*a;
157 
158  m->m2()[ 0 ] += s * ( x2 - ( 2*x*mx + s*x2*ct - mx2/d ) / d2 ) * a;
159  m->m2()[ 1 ] += s * ( y2 - ( 2*y*my + s*y2*ct - my2/d ) / d2 ) * a;
160  m->m2()[ 2 ] += s * ( z2 - ( 2*z*mz + s*z2*ct - mz2/d ) / d2 ) * a;
161  m->m2()[ 3 ] += s * ( xy - ( x*my + y*mx + s*xy*ct - mxmy/d ) / d2 ) * a;
162  m->m2()[ 4 ] += s * ( xz - ( x*mz + z*mx + s*xz*ct - mxmz/d ) / d2 ) * a;
163  m->m2()[ 5 ] += s * ( yz - ( y*mz + z*my + s*yz*ct - mymz/d ) / d2 ) * a;
164 
165  m->m1()[ 0 ] += s * x * ct;
166  m->m1()[ 1 ] += s * y * ct;
167  m->m1()[ 2 ] += s * z * ct;
168 
169  m->m0() += s * ct;
170 
171  m->sum() += s;
172 
173  double dd = ( m->sum() < 2 ) ? ct : m->sum() * ct;
174  m->gravity()[ 0 ] = m->m1()[ 0 ] / dd;
175  m->gravity()[ 1 ] = m->m1()[ 1 ] / dd;
176  m->gravity()[ 2 ] = m->m1()[ 2 ] / dd;
177 }
178 
179 
180 template< class T > inline
182  Moment< T > *m, carto::rc_ptr<carto::Volume< T > > & d, T label, int dir )
183 {
184  int x, y, z;
185  int dx = d->getSizeX();
186  int dy = d->getSizeY();
187  int dz = d->getSizeZ();
188  int o = d->getStrides()[0];
189  T *it;
190 
191  for ( z=0; z<dz; z++ )
192  for ( y=0; y<dy; y++ )
193  {
194  it = &d->at( 0, y, z );
195  for ( x=0; x<dx; x++, it+=o )
196  if ( *it == label )
197  update( m, (double)x, (double)y, (double)z, dir );
198  }
199 }
200 
201 
202 template< class T > inline
204  const aims::BucketMap<Void> & b,
205  int dir )
206 {
207  if (!b.empty())
208  {
209  const aims::BucketMap<Void>::Bucket & bk = b.begin()->second;
210 
211  if (!bk.empty())
212  {
214 
215  for( ib=bk.begin(); ib!=eb; ++ib )
216  {
217  update( m, ib->first[0], ib->first[1], ib->first[2], dir );
218  }
219  }
220  }
221 }
222 
223 #endif
void doit(Moment< T > *, carto::rc_ptr< carto::Volume< T > > &, T, int)
Definition: momIncStgy.h:181
void update(Moment< T > *, double, double, double, int)
Definition: momIncStgy.h:55
Definition: moment.h:56
double ct() const
Definition: moment.h:101
double * m2()
Definition: moment.h:117
double * gravity()
Definition: moment.h:107
double * m1()
Definition: moment.h:115
double & m0()
Definition: moment.h:112
double cx() const
Definition: moment.h:98
double cz() const
Definition: moment.h:100
double & sum()
Definition: moment.h:110
double * m3()
Definition: moment.h:119
double cy() const
Definition: moment.h:99
std::map< Point3d, T, BucketMapLess > Bucket
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)