aimsalgo 6.0.0
Neuroimaging image processing
momNormStgy.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_MOMNORMSTGY_H
36#define AIMS_MOMENT_MOMNORMSTGY_H
37
38#include <aims/moment/momStgy.h>
39
40
41template< class T >
43{
44 public:
45
47
48 void doit( Moment< T > *, carto::rc_ptr<carto::Volume< T > > &, T, int );
49 void doit( Moment< T > *, const aims::BucketMap<Void> &, int );
50
51 private:
52 void addVoxel2( Moment<T>*, const Point3d & p );
53};
54
55
56template< class T > inline
57void MomentNormalStrategy< T >::addVoxel2( Moment< T > *m, const Point3d & p )
58{
59 double cx = m->cx();
60 double cy = m->cy();
61 double cz = m->cz();
62 double mx = m->m1()[ 0 ] / m->m0();
63 double my = m->m1()[ 1 ] / m->m0();
64 double mz = m->m1()[ 2 ] / m->m0();
65
66 double tx = (double)p[0] * cx - mx;
67 double ty = (double)p[1] * cy - my;
68 double tz = (double)p[2] * cz - mz;
69
70 m->m2()[ 0 ] += tx * tx;
71 m->m2()[ 1 ] += ty * ty;
72 m->m2()[ 2 ] += tz * tz;
73 m->m2()[ 3 ] += tx * ty;
74 m->m2()[ 4 ] += tx * tz;
75 m->m2()[ 5 ] += ty * tz;
76
77 m->m3()[ 0 ] += tx * tx * tx;
78 m->m3()[ 1 ] += ty * ty * ty;
79 m->m3()[ 2 ] += tz * tz * tz;
80 m->m3()[ 3 ] += tx * tx * ty;
81 m->m3()[ 4 ] += tx * tx * tz;
82 m->m3()[ 5 ] += tx * ty * ty;
83 m->m3()[ 6 ] += ty * ty * tz;
84 m->m3()[ 7 ] += tx * tz * tz;
85 m->m3()[ 8 ] += ty * tz * tz;
86 m->m3()[ 9 ] += tx * ty * tz;
87}
88
89
90template< class T > inline
93 T label, int )
94{
95 int x, y, z;
96 int dx = d->getSizeX();
97 int dy = d->getSizeY();
98 int dz = d->getSizeZ();
99 T *it;
100 long o = d->getStrides()[0];
101
102 double cx = m->cx();
103 double cy = m->cy();
104 double cz = m->cz();
105 double ct = m->ct();
106
107 for ( z=0; z<dz; z++ )
108 for ( y=0; y<dy; y++ )
109 {
110 it = &d->at( 0, y, z );
111 for ( x=0; x<dx; x++, it+=o )
112 if ( *it == label )
113 {
114 m->sum()++;
115
116 m->m0()++;
117
118 m->m1()[ 0 ] += (double)x * cx;
119 m->m1()[ 1 ] += (double)y * cy;
120 m->m1()[ 2 ] += (double)z * cz;
121 }
122 }
123
124 double mx = m->m1()[ 0 ] / m->m0();
125 double my = m->m1()[ 1 ] / m->m0();
126 double mz = m->m1()[ 2 ] / m->m0();
127
128 m->gravity()[ 0 ] = mx;
129 m->gravity()[ 1 ] = my;
130 m->gravity()[ 2 ] = mz;
131
132 for ( z=0; z<dz; z++ )
133 for ( y=0; y<dy; y++ )
134 {
135 it = &d->at( 0, y, z );
136 for ( x=0; x<dx; x++, it+=o )
137 if ( *it == label )
138 addVoxel2( m, Point3d( x, y, z ) );
139 }
140
141 m->m0() *= ct; // is is OK or should it have been done before ?
142
143 for ( x=0; x<3; x++ ) m->m1()[ x ] *= ct;
144 for ( x=0; x<6; x++ ) m->m2()[ x ] *= ct;
145 for ( x=0; x<10; x++ ) m->m3()[ x ] *= ct;
146}
147
148
149template <typename T>
151 const aims::BucketMap<Void> & b, int )
152{
153 const aims::BucketMap<Void>::Bucket & bk = b.begin()->second;
154 aims::BucketMap<Void>::Bucket::const_iterator ib, eb = bk.end();
155
156 if( bk.size() == 0 )
157 return;
158
159 double cx = m->cx();
160 double cy = m->cy();
161 double cz = m->cz();
162 double ct = m->ct();
163
164 m->sum() += bk.size();
165 m->m0() += bk.size();
166
167 for( ib=bk.begin(); ib!=eb; ++ib )
168 {
169 m->m1()[ 0 ] += (double)ib->first[0] * cx;
170 m->m1()[ 1 ] += (double)ib->first[1] * cy;
171 m->m1()[ 2 ] += (double)ib->first[2] * cz;
172 }
173
174 double mx = m->m1()[ 0 ] / m->m0();
175 double my = m->m1()[ 1 ] / m->m0();
176 double mz = m->m1()[ 2 ] / m->m0();
177
178 m->gravity()[ 0 ] = mx;
179 m->gravity()[ 1 ] = my;
180 m->gravity()[ 2 ] = mz;
181
182 for( ib=bk.begin(); ib!=eb; ++ib )
183 addVoxel2( m, ib->first );
184
185 m->m0() *= ct;
186
187 int x;
188 for ( x=0; x<3; x++ ) m->m1()[ x ] *= ct;
189 for ( x=0; x<6; x++ ) m->m2()[ x ] *= ct;
190 for ( x=0; x<10; x++ ) m->m3()[ x ] *= ct;
191}
192
193#endif
void doit(Moment< T > *, carto::rc_ptr< carto::Volume< T > > &, T, int)
Definition momNormStgy.h:91
double ct() const
Definition moment.h:101
double & m0()
Definition moment.h:112
double * m3()
Definition moment.h:119
double cx() const
Definition moment.h:98
double cz() const
Definition moment.h:100
double * m2()
Definition moment.h:117
double cy() const
Definition moment.h:99
double * gravity()
Definition moment.h:107
double & sum()
Definition moment.h:110
double * m1()
Definition moment.h:115
std::map< Point3d, T, BucketMapLess > Bucket
AimsVector< int16_t, 3 > Point3d