Debian Codegraph @

src/base/Vector2d.h

raw symbols SCIP
1 /**********************************************************************
2 Vector2d.h: 2-dimensional vector class (inherits from stl vector class)
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #ifndef _VECTOR2D_H_
22 #define _VECTOR2D_H_
23
24 #include <vector>
25 #include <list>
26 #include <algorithm>
27 #include <numeric>
28 #include <gsl/gsl_matrix.h>
29 #include "IndexValue.h"
30 #include "algorithms/StatFactory.h"
31
32 template<class T> class Vector2d: public std::vector<std::vector <T> >
33 {
34 public:
35 Vector2d();
36 Vector2d(const Vector2d<T>& v1);//copy constructor
37 ~Vector2d();
38 Vector2d(int nrow);
39 Vector2d(int nrow, int ncol);
40 Vector2d(int nrow, int ncol, const T& value);
41 Vector2d(const gsl_matrix* gsl_m);
42 void resize(int nrow)
43 {
44 std::vector< std::vector<T> >::resize(nrow);
45 };
46 void resize(int nrow, int ncol);
47 int nRows() const {return this->size();};
48 int nCols() const {if(this->size()) return this->begin()->size(); else return 0;};
49 int nrOfRow() const {return this->size();};
50 int nrOfCol() const {if(this->size()) return this->begin()->size(); else return 0;};
51 void selectCol(int col, std::vector<T> &output) const;
52 void selectCol(int col, T* output) const;
53 std::vector<T> selectCol(int col);
54 void selectCols(const std::list<int> &cols, Vector2d<T> &output) const;
55 void setMask(const Vector2d<T> &mask, T msknodata, T nodata=0);
56 void transpose(Vector2d<T> &output) const{
57 output.resize(nCols(),nRows());
58 for(int irow=0;irow<nRows();++irow){
59 for(int icol=0;icol<nCols();++icol){
60 output[icol][irow]=(*this)[irow][icol];
61 }
62 }
63 };
64 void selectCols(const std::list<int> &cols);
65 void sort(Vector2d<T>& output);
66 void scale(const std::vector<double> &scaleVector, const std::vector<double> &offsetVector, Vector2d<T>& scaledOutput);
67 void scale(const T lbound, const T ubound, std::vector<double> &scaleVector, std::vector<double> &offsetVector, Vector2d<T>& scaledOutput);
68 Vector2d<T>& operator=(const Vector2d<T>& v1);
69 Vector2d<T>& operator+=(const Vector2d<T>& v1);
70 // std::ostream& operator<<(std::ostream& os, const Vector2d<T>& v);
71 // template<class T> std::ostream& operator<<(std::ostream& os, const Vector2d<T>& v);
72 template<class T1> friend std::ostream& operator<<(std::ostream & os, const Vector2d<T1>& v);
73 Vector2d<T> sum(const Vector2d<T>& v1, const Vector2d<T>& v2) const;
74 T mymax(int& x, int& y, double maxValue) const;
75
76 T sum() const;
77 };
78
79 template<class T> Vector2d<T>::Vector2d()
80 : std::vector< std::vector<T> >()
81 {
82 }
83
84 template<class T> Vector2d<T>::~Vector2d()
85 {
86 }
87
88 //copy constructor
89 template<class T> Vector2d<T>::Vector2d(const Vector2d<T>& v1){
90 this->resize(v1.size());
91 for(int irow=0;irow<v1.size();++irow)
92 this->at(irow)=v1[irow];
93 }
94
95 template<class T> Vector2d<T>& Vector2d<T>::operator=(const Vector2d<T>& v1){
96 //check for assignment to self (of the form v=v)
97 if(this==&v1)
98 return *this;
99 else{
100 this->resize(v1.size());
101 for(int irow=0;irow<v1.size();++irow)
102 this->at(irow)=v1[irow];
103 return *this;
104 }
105 }
106
107 template<class T> Vector2d<T>& Vector2d<T>::operator+=(const Vector2d<T>& v1){
108 assert(v1.nRows()==nRows());
109 assert(v1.nCols()==nCols());
110 for(int irow=0;irow<nRows();++irow)
111 for(int icol=0;icol<nCols();++icol)
112 (*this)[irow][icol]+=v1[irow][icol];
113 return *this;
114 }
115
116 template<class T> Vector2d<T>::Vector2d(int nrow)
117 : std::vector< std::vector<T> >(nrow)
118 {
119 }
120
121 template<class T> Vector2d<T>::Vector2d(int nrow, int ncol)
122 // : std::vector< std::vector<T> >(nrow)
123 {
124 this->resize(nrow);
125 for(int irow=0;irow<nrow;++irow){
126 (this->operator[](irow)).resize(ncol);
127 // (*this)[irow].resize(ncol);
128 }
129 }
130
131 template<class T> Vector2d<T>::Vector2d(int nrow, int ncol, const T& value)
132 {
133 this->resize(nrow);
134 for(int irow=0;irow<nrow;++irow){
135 (this->operator[](irow)).resize(ncol);
136 for(int icol=0;icol<ncol;++icol)
137 (this->operator[](irow))[icol]=value;
138 }
139 }
140
141 template<class T> Vector2d<T>::Vector2d(const gsl_matrix* gsl_m)
142 {
143 this->resize(gsl_m->size1);
144 for(int irow=0;irow<this->size();++irow){
145 (this->operator[](irow)).resize(gsl_m->size2);
146 for(int icol=0;icol<this->operator[](irow).size();++icol)
147 (this->operator[](irow))[icol]=gsl_matrix_get(gsl_m,irow,icol);
148 }
149 }
150
151
152 template<class T> void Vector2d<T>::resize(int nrow, int ncol)
153 {
154 this->std::vector< std::vector<T> >::resize(nrow);
155 for(int irow=0;irow<nrow;++irow){
156 (this->operator[](irow)).resize(ncol);
157 }
158 }
159
160 template<class T> void Vector2d<T>::selectCols(const std::list<int> &cols, Vector2d<T> &output) const
161 {
162 output.resize(this->size());
163 std::list<int>::const_iterator it;
164 for(int irow=0;irow<this->size();++irow){
165 output[irow].resize(cols.size());
166 it=cols.begin();
167 for(int icol=0;icol<cols.size();++icol)
168 output[irow][icol]=(*this)[irow][*(it++)];
169 }
170 }
171
172 template<class T> void Vector2d<T>::selectCol(int col, std::vector<T> &output) const
173 {
174 assert(col>=0);
175 assert(col<(*this)[0].size());
176 output.resize(this->size());
177 for(int irow=0;irow<this->size();++irow){
178 output[irow]=(*this)[irow][col];
179 }
180 }
181
182 template<class T> std::vector<T> Vector2d<T>::selectCol(int col)
183 {
184 assert(col>=0);
185 assert(col<(*this)[0].size());
186 std::vector<T> output(this->size());
187 for(int irow=0;irow<this->size();++irow)
188 output[irow]=(*this)[irow][col];
189 return(output);
190 }
191
192 template<class T> void Vector2d<T>::selectCol(int col, T* output) const
193 {
194 assert(col>=0);
195 assert(col<(*this)[0].size());
196 for(int irow=0;irow<this->size();++irow){
197 output[irow]=(*this)[irow][col];
198 }
199 }
200
201 template<class T> void Vector2d<T>::selectCols(const std::list<int> &cols)
202 {
203 for(int irow=0;irow<this->size();++irow)
204 for(int icol=((*this)[irow]).size()-1;icol>=0;--icol)
205 if(find(cols.begin(),cols.end(),icol)==cols.end())
206 (*this)[irow].erase(((*this)[irow]).begin()+icol);
207 }
208
209 template<class T> void Vector2d<T>::setMask(const Vector2d<T> &mask, T msknodata, T nodata)
210 {
211 assert(mask.nRows()==nRows());
212 assert(mask.nCols()==nCols());
213 for(int irow=0;irow<this->size();++irow)
214 for(int icol=0;icol<((*this)[irow]).size()-1;++icol)
215 if(mask[irow][icol]==msknodata)
216 (*this)[irow][icol]=nodata;
217 }
218
219 template<class T1> std::ostream& operator<<(std::ostream& os, const Vector2d<T1>& v)
220 {
221 for(int irow=0;irow<v.size();++irow){
222 for(int icol=0;icol<v[irow].size();++icol){
223 os << v[irow][icol] << "\t";
224 }
225 os << std::endl;
226 }
227 return os;
228 // os << theOption.getLongName() << ": ";
229 // for(int index=0;index<theOption.size();++index)
230 // os << type2string<T>(theOption[index]) << " ";
231 // os << std::endl;
232 // return os;
233 }
234
235 template<class T> void Vector2d<T>::sort(Vector2d<T>& output)
236 {
237 //sort according to first sample (ex. wavelength)
238 int nsample=this->size();//including first sample (ex. wavelength)
239 int nband=(*this)[0].size();
240 std::vector<IndexValue> sortW(nband);
241 for(int ilevel=0;ilevel<nband;++ilevel){
242 IndexValue pv;
243 pv.position=ilevel;
244 pv.value=(*this)[0][ilevel];
245 sortW[ilevel]=pv;
246 }
247 std::sort(sortW.begin(),sortW.end(),Increase_IndexValue());
248 output.resize(nsample);
249 for(int isample=0;isample<nsample;++isample){
250 output[isample].resize(nband);
251 for(int iband=0;iband<nband;++iband)
252 output[isample][iband]=(*this)[isample][sortW[iband].position];
253 }
254 }
255
256 template<class T> void Vector2d<T>::scale(const std::vector<double> &scaleVector,const std::vector<double> &offsetVector, Vector2d<T>& scaledOutput)
257 {
258 int nsample=this->size();//including first sample (ex. wavelength)
259 int nband=(*this)[0].size();
260 assert(scaleVector.size()==nband);
261 assert(offsetVector.size()==nband);
262 std::vector<T> pixel(nband);
263 scaledOutput.resize(nsample,nband);
264 for(int isample=0;isample<nsample;++isample)
265 for(int iband=0;iband<nband;++iband)
266 scaledOutput[isample][iband]=((*this)[isample][iband])*scaleVector[iband]+offsetVector[iband];
267 }
268
269 template<class T> void Vector2d<T>::scale(const T lbound, const T ubound, std::vector<double> &scaleVector, std::vector<double> &offsetVector, Vector2d<T>& scaledOutput)
270 {
271 //scale to lbound and ubound
272 int nsample=this->size();//including first sample (ex. wavelength)
273 int nband=(*this)[0].size();
274 scaleVector.resize(nband);
275 offsetVector.resize(nband);
276 std::vector<T> pixel(nsample);
277 T theMin;
278 T theMax;
279 statfactory::StatFactory stat;
280 scaledOutput.resize(nsample,nband);
281 for(int iband=0;iband<nband;++iband){
282 pixel=selectCol(iband);
283 stat.minmax(pixel, pixel.begin(), pixel.end(), theMin, theMax);
284 scaleVector[iband]=static_cast<double>(ubound-lbound)/(theMax-theMin);
285 offsetVector[iband]=static_cast<double>(-theMin*scaleVector[iband])-lbound;
286 for(int isample=0;isample<pixel.size();++isample)
287 scaledOutput[isample][iband]=((*this)[isample][iband])*scaleVector[iband]+offsetVector[iband];
288 }
289 }
290
291 template<class T> Vector2d<T> Vector2d<T>::sum(const Vector2d<T>& v1, const Vector2d<T>& v2) const{
292 Vector2d<T> vsum(v1.size());
293 assert(v1.size()==v2.size());
294 for(int irow=0;irow<v1.size();++irow){
295 assert(v1[irow].size()==v2[irow].size());
296 vsum[irow].resize(v1[irow].size());
297 for(int icol=0;icol<v1.size();++icol)
298 vsum[irow][icol]=v1[irow][icol]+v2[irow][icol];
299 }
300 return vsum;
301 }
302
303 template<class T> T Vector2d<T>::sum() const{
304 double theSum=0;
305 for(int irow=0;irow<this->size();++irow){
306 for(int icol=0;icol<this->operator[](irow).size();++icol)
307 theSum+=(this->operator[](irow))[icol];
308 }
309 return theSum;
310 }
311
312 template<class T> T Vector2d<T>::mymax(int& x, int& y, double maxValue) const{
313 //todo: what if this->operator[](0)[0] >=maxValue?
314 // double theMax=(this->operator[](0))[0];
315 double theMax=0;
316 for(int irow=0;irow<this->size();++irow){
317 for(int icol=0;icol<(this->operator[](irow)).size();++icol){
318 double currentValue=(this->operator[](irow))[icol];
319 if(currentValue<maxValue&&currentValue>theMax){
320 assert(theMax<maxValue);
321 y=irow;
322 x=icol;
323 theMax=currentValue;
324 }
325 }
326 }
327 assert(theMax<maxValue);
328 return theMax;
329 }
330
331 #endif /* _VECTOR2D_H_ */