aimsdata 6.0.0
Neuroimaging data handling
linearcomb.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 * Linear combination beetween two datas.
36 */
37#ifndef AIMS_UTILITY_LINEARCOMB_H
38#define AIMS_UTILITY_LINEARCOMB_H
39
41#include <cartodata/volume/volume.h>
42#include <cartobase/containers/nditerator.h>
43
44namespace aims
45{
46
52 template <class T>
54 {
55 protected:
57 float _num1;
59 float _den1;
61 float _num2;
63 float _den2;
64
65 public :
72 AimsLinearCombination(float num1,float den1,
73 float num2=0.0,float den2=1.0)
74 { ASSERT(den1!=0 && den2!=0);
75 _num1 = num1;
76 _den1 = den1;
77 _num2 = num2;
78 _den2 = den2;
79 }
80
82
85 const carto::rc_ptr<carto::Volume<T> > &data1,
86 const carto::rc_ptr<carto::Volume<T> > &data2 );
89 const carto::rc_ptr<carto::Volume<T> > &data1);
90 };
91
92
99 template <class T>
101 {
102 public:
103
104 // [NS-2024-01-15] - std::map<T>::find does not work with multichannel (i.e.: VoxelValue<uint8_t, 3>)
105 // because std::map<T>::find uses std::less<T> functor to compare multichannel VoxelValue but it
106 // implicitely convert VoxelValue to boolean. To fix this bug we uses an std::map type with
107 // VoxelValue comparison support
108 typedef typename std::map<T, T, carto::KeyComparatorLess<T> > MapType;
109
112
122 inline static void replace( const carto::Volume<T> & ivol,
123 carto::Volume<T> & ovol,
124 const aims::Replacer<T>::MapType & repl );
125 };
126
127
128 template <class T> inline
130 const carto::rc_ptr<carto::Volume<T> > &data1,
131 const carto::rc_ptr<carto::Volume<T> > &data2 )
132 {
133 if( data1->getSize() != data2->getSize() )
134 AimsError("VolumeRef<T> AimsLinearCombination(const rc_ptr<Volume<T> > &,\
135 float,float,const rc_ptr<Volume<T> > &,float,float) : \
136 sizes must be the same");
137
138 carto::VolumeRef<T> comb( data1->getSize(), data1->getBorders() );
139 comb.setVoxelSize( data1->getVoxelSize() );
140 typename carto::Volume<T>::iterator it;
141 typename carto::Volume<T>::const_iterator it1, it2;
142 for( it=comb->begin(), it1=data1->begin(), it2=data2->begin();
143 it!=comb->end(); it++, it1++, it2++ )
144 *it = (T)((double)*it1 * (double)_num1 / (double)_den1 +
145 (double)*it2 * (double)_num2 / (double)_den2 );
146 return comb;
147 }
148
149
150 template <class T> inline
152 const carto::rc_ptr<carto::Volume<T> > &data1 )
153 {
154 carto::VolumeRef<T> comb( data1->getSize(), data1->getBorders() );
155 comb.setVoxelSize( data1->getVoxelSize() );
156 typename carto::Volume<T>::iterator it;
158 for( it=comb->begin(), it1=data1->begin(); it!=comb->end(); it++, it1++ )
159 *it = (T)((double)*it1 * (double)_num1 / (double)_den1);
160 return comb;
161 }
162
163
164
165 template <typename T>
166 inline void Replacer<T>::replace( const carto::Volume<T> & ivol,
167 carto::Volume<T> & ovol,
168 const aims::Replacer<T>::MapType & repl )
169 {
170 carto::const_line_NDIterator<T> it( &ivol.at(0), ivol.getSize(),
171 ivol.getStrides(), true );
172 const T *p, *pp;
173 T *dp;
174 int ds;
175 std::vector<int> pos;
176
177 typename aims::Replacer<T>::MapType::const_iterator im, em = repl.end();
178
179 for( ; !it.ended(); ++it )
180 {
181 p = &*it;
182 pos = it.position();
183 dp = &ovol.at(pos);
184 pos[it.line_direction()]++;
185 ds = &ovol.at(pos) - dp;
186
187 for( pp=p + it.line_length(); p!=pp; it.inc_line_ptr( p ), dp+=ds )
188 {
189 im = repl.find( *p );
190 if( im != em )
191 {
192 // if (im->first != (*p))
193 // std::cout << "Found value: " << carto::toString(im->first) << " did not match searched key: " << carto::toString(*p) << std::endl << std::flush;
194 *dp = im->second;
195 }
196 }
197 }
198 }
199
200}
201
202#endif
#define ASSERT(EX)
float _den2
dividor for the second data
Definition linearcomb.h:63
float _num1
multiplicator for the first data
Definition linearcomb.h:57
float _num2
multiplicator for the second data
Definition linearcomb.h:61
AimsLinearCombination(float num1, float den1, float num2=0.0, float den2=1.0)
The programmer should provide at least 2 parameters to the constructor:
Definition linearcomb.h:72
virtual ~AimsLinearCombination()
Destructor does nothing.
Definition linearcomb.h:81
float _den1
dividor for the first data
Definition linearcomb.h:59
carto::VolumeRef< T > operator()(const carto::rc_ptr< carto::Volume< T > > &data1, const carto::rc_ptr< carto::Volume< T > > &data2)
Return the combination of data1 and data2.
Definition linearcomb.h:129
std::map< T, T, carto::KeyComparatorLess< T > > MapType
Definition linearcomb.h:108
static void replace(const carto::Volume< T > &ivol, carto::Volume< T > &ovol, const aims::Replacer< T >::MapType &repl)
Replace values from "repl" keys, from the input volume ivol, to the correspodinf repl values in the o...
Definition linearcomb.h:166
const std::vector< int > & position() const
std::vector< int > getSize() const
void setVoxelSize(float vx, float vy=1., float vz=1., float vt=1.)
iterator end()
iterator begin()
iterator begin()
std::vector< long > getStrides() const
const T & at(long x, long y=0, long z=0, long t=0) const
std::ptrdiff_t line_length() const
void inc_line_ptr(const T *&p) const
AIMSDATA_API void AimsError(const std::string &message) __attribute__((__noreturn__))
Give an error message on display.
The class for EcatSino data write operation.