27 #include <gsl/gsl_matrix.h> 28 #include "IndexValue.h" 29 #include "algorithms/StatFactory.h" 31 template<
class T>
class Vector2d:
public std::vector<std::vector <T> >
39 Vector2d(
int nrow,
int ncol,
const T& value);
43 std::vector< std::vector<T> >::resize(nrow);
45 void resize(
int nrow,
int ncol);
46 int nRows()
const {
return this->size();};
47 int nCols()
const {
if(this->size())
return this->begin()->size();
else return 0;};
48 void selectCol(
int col, std::vector<T> &output)
const;
49 void selectCol(
int col, T* output)
const;
50 std::vector<T> selectCol(
int col);
51 void selectCols(
const std::list<int> &cols,
Vector2d<T> &output)
const;
52 void setMask(
const Vector2d<T> &mask, T msknodata, T nodata=0);
54 output.resize(nCols(),nRows());
55 for(
int irow=0;irow<nRows();++irow){
56 for(
int icol=0;icol<nCols();++icol){
57 output[icol][irow]=(*this)[irow][icol];
61 void selectCols(
const std::list<int> &cols);
63 void scale(
const std::vector<double> &scaleVector,
const std::vector<double> &offsetVector,
Vector2d<T>& scaledOutput);
64 void scale(
const T lbound,
const T ubound, std::vector<double> &scaleVector, std::vector<double> &offsetVector,
Vector2d<T>& scaledOutput);
69 template<
class T1>
friend std::ostream& operator<<(std::ostream & os, const Vector2d<T1>& v);
71 T mymax(
int& x,
int& y,
double maxValue)
const;
77 : std::vector< std::vector<T> >()
87 this->resize(v1.size());
88 for(
int irow=0;irow<v1.size();++irow)
89 this->at(irow)=v1[irow];
97 this->resize(v1.size());
98 for(
int irow=0;irow<v1.size();++irow)
99 this->at(irow)=v1[irow];
105 assert(v1.nRows()==nRows());
106 assert(v1.nCols()==nCols());
107 for(
int irow=0;irow<nRows();++irow)
108 for(
int icol=0;icol<nCols();++icol)
109 (*
this)[irow][icol]+=v1[irow][icol];
114 : std::vector< std::vector<T> >(nrow)
122 for(
int irow=0;irow<nrow;++irow){
123 (this->operator[](irow)).resize(ncol);
131 for(
int irow=0;irow<nrow;++irow){
132 (this->operator[](irow)).resize(ncol);
133 for(
int icol=0;icol<ncol;++icol)
134 (this->
operator[](irow))[icol]=value;
140 this->resize(gsl_m->size1);
141 for(
int irow=0;irow<this->size();++irow){
142 (this->operator[](irow)).resize(gsl_m->size2);
143 for(
int icol=0;icol<this->operator[](irow).size();++icol)
144 (this->
operator[](irow))[icol]=gsl_matrix_get(gsl_m,irow,icol);
151 this->std::vector< std::vector<T> >::resize(nrow);
152 for(
int irow=0;irow<nrow;++irow){
153 (this->operator[](irow)).resize(ncol);
159 output.resize(this->size());
160 std::list<int>::const_iterator it;
161 for(
int irow=0;irow<this->size();++irow){
162 output[irow].resize(cols.size());
164 for(
int icol=0;icol<cols.size();++icol)
165 output[irow][icol]=(*
this)[irow][*(it++)];
172 assert(col<(*
this)[0].size());
173 output.resize(this->size());
174 for(
int irow=0;irow<this->size();++irow){
175 output[irow]=(*this)[irow][col];
182 assert(col<(*
this)[0].size());
183 std::vector<T> output(this->size());
184 for(
int irow=0;irow<this->size();++irow)
185 output[irow]=(*
this)[irow][col];
192 assert(col<(*
this)[0].size());
193 for(
int irow=0;irow<this->size();++irow){
194 output[irow]=(*this)[irow][col];
200 for(
int irow=0;irow<this->size();++irow)
201 for(
int icol=((*
this)[irow]).size()-1;icol>=0;--icol)
202 if(find(cols.begin(),cols.end(),icol)==cols.end())
203 (*
this)[irow].erase(((*
this)[irow]).begin()+icol);
208 assert(mask.nRows()==nRows());
209 assert(mask.nCols()==nCols());
210 for(
int irow=0;irow<this->size();++irow)
211 for(
int icol=0;icol<((*this)[irow]).size()-1;++icol)
212 if(mask[irow][icol]==msknodata)
213 (*this)[irow][icol]=nodata;
216 template<
class T1> std::ostream& operator<<(std::ostream& os, const Vector2d<T1>& v)
218 for(
int irow=0;irow<v.size();++irow){
219 for(
int icol=0;icol<v[irow].size();++icol){
220 os << v[irow][icol] <<
"\t";
235 int nsample=this->size();
236 int nband=(*this)[0].size();
237 std::vector<IndexValue> sortW(nband);
238 for(
int ilevel=0;ilevel<nband;++ilevel){
241 pv.value=(*this)[0][ilevel];
245 output.resize(nsample);
246 for(
int isample=0;isample<nsample;++isample){
247 output[isample].resize(nband);
248 for(
int iband=0;iband<nband;++iband)
249 output[isample][iband]=(*
this)[isample][sortW[iband].position];
253 template<
class T>
void Vector2d<T>::scale(
const std::vector<double> &scaleVector,
const std::vector<double> &offsetVector,
Vector2d<T>& scaledOutput)
255 int nsample=this->size();
256 int nband=(*this)[0].size();
257 assert(scaleVector.size()==nband);
258 assert(offsetVector.size()==nband);
259 std::vector<T> pixel(nband);
260 scaledOutput.resize(nsample,nband);
261 for(
int isample=0;isample<nsample;++isample)
262 for(
int iband=0;iband<nband;++iband)
263 scaledOutput[isample][iband]=((*
this)[isample][iband])*scaleVector[iband]+offsetVector[iband];
266 template<
class T>
void Vector2d<T>::scale(
const T lbound,
const T ubound, std::vector<double> &scaleVector, std::vector<double> &offsetVector,
Vector2d<T>& scaledOutput)
269 int nsample=this->size();
270 int nband=(*this)[0].size();
271 scaleVector.resize(nband);
272 offsetVector.resize(nband);
273 std::vector<T> pixel(nsample);
277 scaledOutput.resize(nsample,nband);
278 for(
int iband=0;iband<nband;++iband){
279 pixel=selectCol(iband);
280 stat.minmax(pixel, pixel.begin(), pixel.end(), theMin, theMax);
281 scaleVector[iband]=
static_cast<double>(ubound-lbound)/(theMax-theMin);
282 offsetVector[iband]=
static_cast<double>(-theMin*scaleVector[iband])-lbound;
283 for(
int isample=0;isample<pixel.size();++isample)
284 scaledOutput[isample][iband]=((*
this)[isample][iband])*scaleVector[iband]+offsetVector[iband];
290 assert(v1.size()==v2.size());
291 for(
int irow=0;irow<v1.size();++irow){
292 assert(v1[irow].size()==v2[irow].size());
293 vsum[irow].resize(v1[irow].size());
294 for(
int icol=0;icol<v1.size();++icol)
295 vsum[irow][icol]=v1[irow][icol]+v2[irow][icol];
302 for(
int irow=0;irow<this->size();++irow){
303 for(
int icol=0;icol<this->operator[](irow).size();++icol)
304 theSum+=(this->
operator[](irow))[icol];
313 for(
int irow=0;irow<this->size();++irow){
314 for(
int icol=0;icol<(this->operator[](irow)).size();++icol){
315 double currentValue=(this->operator[](irow))[icol];
316 if(currentValue<maxValue&¤tValue>theMax){
317 assert(theMax<maxValue);
324 assert(theMax<maxValue);