/********************************************************************** Vector2d.h: 2-dimensional vector class (inherits from stl vector class) Author(s): Pieter.Kempeneers@ec.europa.eu Copyright (C) 2016-2020 European Union (Joint Research Centre) This file is part of jiplib. jiplib is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. jiplib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with jiplib. If not, see . ***********************************************************************/ #ifndef _VECTOR2D_H_ #define _VECTOR2D_H_ #include #include #include #include #include #include "IndexValue.h" #include "algorithms/StatFactory.h" template class Vector2d: public std::vector > { public: Vector2d(); Vector2d(const Vector2d& v1);//copy constructor ~Vector2d(); Vector2d(int nrow); Vector2d(int nrow, int ncol); Vector2d(int nrow, int ncol, const T& value); Vector2d(const gsl_matrix* gsl_m); void resize(int nrow) { std::vector< std::vector >::resize(nrow); }; void resize(int nrow, int ncol); int nRows() const {return this->size();}; int nCols() const {if(this->size()) return this->begin()->size(); else return 0;}; int nrOfRow() const {return this->size();}; int nrOfCol() const {if(this->size()) return this->begin()->size(); else return 0;}; void selectCol(int col, std::vector &output) const; void selectCol(int col, T* output) const; std::vector selectCol(int col); void selectCols(const std::list &cols, Vector2d &output) const; void setMask(const Vector2d &mask, T msknodata, T nodata=0); void transpose(Vector2d &output) const{ output.resize(nCols(),nRows()); for(int irow=0;irow &cols); void sort(Vector2d& output); void scale(const std::vector &scaleVector, const std::vector &offsetVector, Vector2d& scaledOutput); void scale(const T lbound, const T ubound, std::vector &scaleVector, std::vector &offsetVector, Vector2d& scaledOutput); Vector2d& operator=(const Vector2d& v1); Vector2d& operator+=(const Vector2d& v1); // std::ostream& operator<<(std::ostream& os, const Vector2d& v); // template std::ostream& operator<<(std::ostream& os, const Vector2d& v); template friend std::ostream& operator<<(std::ostream & os, const Vector2d& v); Vector2d sum(const Vector2d& v1, const Vector2d& v2) const; T mymax(int& x, int& y, double maxValue) const; T sum() const; }; template Vector2d::Vector2d() : std::vector< std::vector >() { } template Vector2d::~Vector2d() { } //copy constructor template Vector2d::Vector2d(const Vector2d& v1){ this->resize(v1.size()); for(int irow=0;irowat(irow)=v1[irow]; } template Vector2d& Vector2d::operator=(const Vector2d& v1){ //check for assignment to self (of the form v=v) if(this==&v1) return *this; else{ this->resize(v1.size()); for(int irow=0;irowat(irow)=v1[irow]; return *this; } } template Vector2d& Vector2d::operator+=(const Vector2d& v1){ assert(v1.nRows()==nRows()); assert(v1.nCols()==nCols()); for(int irow=0;irow Vector2d::Vector2d(int nrow) : std::vector< std::vector >(nrow) { } template Vector2d::Vector2d(int nrow, int ncol) // : std::vector< std::vector >(nrow) { this->resize(nrow); for(int irow=0;irowoperator[](irow)).resize(ncol); // (*this)[irow].resize(ncol); } } template Vector2d::Vector2d(int nrow, int ncol, const T& value) { this->resize(nrow); for(int irow=0;irowoperator[](irow)).resize(ncol); for(int icol=0;icoloperator[](irow))[icol]=value; } } template Vector2d::Vector2d(const gsl_matrix* gsl_m) { this->resize(gsl_m->size1); for(int irow=0;irowsize();++irow){ (this->operator[](irow)).resize(gsl_m->size2); for(int icol=0;icoloperator[](irow).size();++icol) (this->operator[](irow))[icol]=gsl_matrix_get(gsl_m,irow,icol); } } template void Vector2d::resize(int nrow, int ncol) { this->std::vector< std::vector >::resize(nrow); for(int irow=0;irowoperator[](irow)).resize(ncol); } } template void Vector2d::selectCols(const std::list &cols, Vector2d &output) const { output.resize(this->size()); std::list::const_iterator it; for(int irow=0;irowsize();++irow){ output[irow].resize(cols.size()); it=cols.begin(); for(int icol=0;icol void Vector2d::selectCol(int col, std::vector &output) const { assert(col>=0); assert(col<(*this)[0].size()); output.resize(this->size()); for(int irow=0;irowsize();++irow){ output[irow]=(*this)[irow][col]; } } template std::vector Vector2d::selectCol(int col) { assert(col>=0); assert(col<(*this)[0].size()); std::vector output(this->size()); for(int irow=0;irowsize();++irow) output[irow]=(*this)[irow][col]; return(output); } template void Vector2d::selectCol(int col, T* output) const { assert(col>=0); assert(col<(*this)[0].size()); for(int irow=0;irowsize();++irow){ output[irow]=(*this)[irow][col]; } } template void Vector2d::selectCols(const std::list &cols) { for(int irow=0;irowsize();++irow) for(int icol=((*this)[irow]).size()-1;icol>=0;--icol) if(find(cols.begin(),cols.end(),icol)==cols.end()) (*this)[irow].erase(((*this)[irow]).begin()+icol); } template void Vector2d::setMask(const Vector2d &mask, T msknodata, T nodata) { assert(mask.nRows()==nRows()); assert(mask.nCols()==nCols()); for(int irow=0;irowsize();++irow) for(int icol=0;icol<((*this)[irow]).size()-1;++icol) if(mask[irow][icol]==msknodata) (*this)[irow][icol]=nodata; } template std::ostream& operator<<(std::ostream& os, const Vector2d& v) { for(int irow=0;irow(theOption[index]) << " "; // os << std::endl; // return os; } template void Vector2d::sort(Vector2d& output) { //sort according to first sample (ex. wavelength) int nsample=this->size();//including first sample (ex. wavelength) int nband=(*this)[0].size(); std::vector sortW(nband); for(int ilevel=0;ilevel void Vector2d::scale(const std::vector &scaleVector,const std::vector &offsetVector, Vector2d& scaledOutput) { int nsample=this->size();//including first sample (ex. wavelength) int nband=(*this)[0].size(); assert(scaleVector.size()==nband); assert(offsetVector.size()==nband); std::vector pixel(nband); scaledOutput.resize(nsample,nband); for(int isample=0;isample void Vector2d::scale(const T lbound, const T ubound, std::vector &scaleVector, std::vector &offsetVector, Vector2d& scaledOutput) { //scale to lbound and ubound int nsample=this->size();//including first sample (ex. wavelength) int nband=(*this)[0].size(); scaleVector.resize(nband); offsetVector.resize(nband); std::vector pixel(nsample); T theMin; T theMax; statfactory::StatFactory stat; scaledOutput.resize(nsample,nband); for(int iband=0;iband(ubound-lbound)/(theMax-theMin); offsetVector[iband]=static_cast(-theMin*scaleVector[iband])-lbound; for(int isample=0;isample Vector2d Vector2d::sum(const Vector2d& v1, const Vector2d& v2) const{ Vector2d vsum(v1.size()); assert(v1.size()==v2.size()); for(int irow=0;irow T Vector2d::sum() const{ double theSum=0; for(int irow=0;irowsize();++irow){ for(int icol=0;icoloperator[](irow).size();++icol) theSum+=(this->operator[](irow))[icol]; } return theSum; } template T Vector2d::mymax(int& x, int& y, double maxValue) const{ //todo: what if this->operator[](0)[0] >=maxValue? // double theMax=(this->operator[](0))[0]; double theMax=0; for(int irow=0;irowsize();++irow){ for(int icol=0;icol<(this->operator[](irow)).size();++icol){ double currentValue=(this->operator[](irow))[icol]; if(currentValuetheMax){ assert(theMax