OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimMatrixProperty.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts (gpotts@imagelinks.com)
6 //
7 //*************************************************************************
8 // $Id: ossimMatrixProperty.cpp 17074 2010-04-14 18:49:11Z dburken $
9 #include <sstream>
11 #include <ossim/base/ossimCommon.h>
12 
13 RTTI_DEF1(ossimMatrixProperty, "ossimMatrixProperty", ossimProperty);
14 
16  const std::vector<double>& values,
17  int numberOfRows,
18  int numberOfCols)
19  :ossimProperty(name),
20  theMinNumberOfCols(-1),
21  theMaxNumberOfCols(-1),
22  theMinNumberOfRows(-1),
23  theMaxNumberOfRows(-1)
24 {
25  resize(numberOfRows, numberOfCols);
26 
27  if((int)values.size() == numberOfRows*numberOfCols)
28  {
29  int rowIdx = 0;
30  int colIdx = 0;
31  int linearIdx = 0;
32  for(rowIdx = 0; rowIdx < getNumberOfRows(); ++rowIdx)
33  {
34  for(colIdx = 0; colIdx < getNumberOfCols(); ++colIdx)
35  {
36  theValueArray[rowIdx][colIdx] = values[linearIdx];
37  ++linearIdx;
38  }
39  }
40  }
41 }
42 
43 
45  :ossimProperty(rhs),
46  theValueArray(rhs.theValueArray),
47  theMinNumberOfCols(rhs.theMinNumberOfCols),
48  theMaxNumberOfCols(rhs.theMaxNumberOfCols),
49  theMinNumberOfRows(rhs.theMaxNumberOfCols),
50  theMaxNumberOfRows(rhs.theMaxNumberOfRows)
51 {
52 }
53 
55 {
56 
57 }
58 
60 {
61  return new ossimMatrixProperty(*this);
62 }
63 
64 
66 {
67  const ossimMatrixProperty* rhsPtr = dynamic_cast<const ossimMatrixProperty*>(&rhs);
68  if(rhsPtr)
69  {
70  theValueArray = rhsPtr->theValueArray;
75  }
76 
77  return ossimProperty::assign(rhs);
78 }
79 
81 {
82  std::istringstream in(value);
83 
84  ossimString numberOfRows;
85  ossimString numberOfCols;
86  ossimString tempValue;
87 
88  in>>numberOfRows >> numberOfCols;
89 
90  if(!in.bad())
91  {
92  resize(numberOfRows.toInt32(), numberOfCols.toInt32());
93 
94  int rowIdx = 0;
95  int colIdx = 0;
96  for(rowIdx = 0; ((rowIdx < getNumberOfRows())&&(!in.bad())); ++rowIdx)
97  {
98  for(colIdx = 0; ((colIdx < getNumberOfCols())&&(!in.bad()));++ colIdx)
99  {
100  in >> tempValue;
101 
102  theValueArray[rowIdx][colIdx] = tempValue.toDouble();
103  }
104  }
105  }
106 
107  return !in.bad();
108 }
109 
111 {
112  if(theValueArray.size()>0)
113  {
114  if(theValueArray[0].size() > 0)
115  {
116  valueResult = "";
117 
118  int rowIdx = 0;
119  int colIdx = 0;
120  valueResult += ossimString::toString(getNumberOfRows()) + " " +
122  for(rowIdx = 0; rowIdx < getNumberOfRows();++rowIdx)
123  {
124  for(colIdx = 0; colIdx < getNumberOfCols(); ++colIdx)
125  {
126  valueResult += ossimString::toString(theValueArray[rowIdx][colIdx]);
127  valueResult += " ";
128  }
129  }
130  }
131  }
132 }
133 
134 void ossimMatrixProperty::resize(int numberOfRows,
135  int numberOfCols)
136 {
137  int tempNumberOfRows = numberOfRows;
138  int tempNumberOfCols = numberOfCols;
139 
140  if(theMinNumberOfCols > 0)
141  {
142  if(tempNumberOfCols < theMinNumberOfCols)
143  {
144  tempNumberOfCols = theMinNumberOfCols;
145  }
146  }
147  if(theMinNumberOfRows > 0)
148  {
149  if(tempNumberOfRows < theMinNumberOfRows)
150  {
151  tempNumberOfRows = theMinNumberOfRows;
152  }
153  }
154  if(theMaxNumberOfCols > 0)
155  {
156  if(tempNumberOfCols > theMaxNumberOfCols)
157  {
158  tempNumberOfCols = theMaxNumberOfCols;
159  }
160  }
161  if(theMaxNumberOfRows > 0)
162  {
163  if(tempNumberOfRows > theMaxNumberOfRows)
164  {
165  tempNumberOfRows = theMaxNumberOfRows;
166  }
167  }
168 
169  int currentNumberOfRows = getNumberOfRows();
170  int currentNumberOfCols = getNumberOfCols();
171 
172  if((currentNumberOfRows != tempNumberOfRows) ||
173  (currentNumberOfCols != tempNumberOfCols))
174  {
175  std::vector< std::vector<double> > tempValue = theValueArray;
176 
177  theValueArray.resize(tempNumberOfRows);
178 
179  int minCols = ossim::min((int)currentNumberOfCols,
180  (int)tempNumberOfCols);
181  int rowIdx = 0;
182  int colIdx = 0;
183 
184  for(rowIdx = 0 ; rowIdx < tempNumberOfRows; ++rowIdx)
185  {
186  theValueArray[rowIdx].resize(tempNumberOfCols);
187  std::fill(theValueArray[rowIdx].begin(), theValueArray[rowIdx].end(), 0);
188 
189  if(tempNumberOfRows < currentNumberOfRows)
190  {
191  for(colIdx = 0 ; colIdx < minCols; ++colIdx)
192  {
193  theValueArray[rowIdx][colIdx] = tempValue[rowIdx][colIdx];
194  }
195  }
196  }
197  }
198 }
199 
201  int colIdx)
202 {
203  return theValueArray[rowIdx][colIdx];
204 }
205 
206 const double& ossimMatrixProperty::operator()(int rowIdx,
207  int colIdx)const
208 {
209  return theValueArray[rowIdx][colIdx];
210 }
211 
213 {
214  return (int)theValueArray.size();
215 }
216 
218 {
219  if(getNumberOfRows())
220  {
221  return (int)theValueArray[0].size();
222  }
223 
224  return 0;
225 }
226 
228 {
229  theMinNumberOfCols = -1;
230  theMaxNumberOfCols = -1;
231  theMinNumberOfRows = -1;
232  theMaxNumberOfRows = -1;
233 }
234 
235 void ossimMatrixProperty::setColConstraints(int minNumberOfCols,
236  int maxNumberOfCols)
237 {
238  theMinNumberOfCols = minNumberOfCols;
239  theMaxNumberOfCols = maxNumberOfCols;
240 }
241 
242 void ossimMatrixProperty::setRowConstraints(int minNumberOfRows,
243  int maxNumberOfRows)
244 {
245  theMinNumberOfRows = minNumberOfRows;
246  theMaxNumberOfRows = maxNumberOfRows;
247 }
248 
249 void ossimMatrixProperty::getColConstraints(int& minNumberOfCols,
250  int& maxNumberOfCols) const
251 {
252  minNumberOfCols = theMinNumberOfCols;
253  maxNumberOfCols = theMaxNumberOfCols;
254 }
255 
256 void ossimMatrixProperty::getRowConstraints(int& minNumberOfRows,
257  int& maxNumberOfRows) const
258 {
259  minNumberOfRows = theMinNumberOfRows;
260  maxNumberOfRows = theMaxNumberOfRows;
261 }
262 
264 {
265  double densityValue = density();
266 
267  if(fabs(densityValue) <= DBL_EPSILON)
268  {
269  return;
270  }
271  int rowIdx = 0;
272  int colIdx = 0;
273  for(rowIdx = 0 ; rowIdx < (int)theValueArray.size(); ++rowIdx)
274  {
275  for(colIdx = 0 ; colIdx < (int)theValueArray[rowIdx].size(); ++colIdx)
276  {
277  theValueArray[rowIdx][colIdx]/=densityValue;
278  }
279  }
280 
281 }
282 
284 {
285  int rowIdx = 0;
286  int colIdx = 0;
287  for(rowIdx = 0 ; rowIdx < (int)theValueArray.size(); ++rowIdx)
288  {
289  for(colIdx = 0 ; colIdx < (int)theValueArray[rowIdx].size(); ++colIdx)
290  {
291  theValueArray[rowIdx][colIdx] = 0.0;
292  }
293  }
294 }
295 
297 {
298  double result = 0.0;
299 
300  if((getNumberOfRows() > 0)&&
301  (getNumberOfCols() > 0))
302  {
303  int rowIdx = 0;
304  int colIdx = 0;
305 
306  for(rowIdx = 0 ; rowIdx < (int)theValueArray.size(); ++rowIdx)
307  {
308  for(colIdx = 0 ; colIdx < (int)theValueArray[rowIdx].size(); ++colIdx)
309  {
310  result += theValueArray[rowIdx][colIdx];
311  }
312  }
313  }
314 
315  return result;
316 }
void getColConstraints(int &minNumberOfColumns, int &maxNumberOfColumns) const
double & operator()(int rowIdx, int colIdx)
void resize(int numberOfRows, int numberOfColumns)
static ossimString toString(bool aValue)
Numeric to string methods.
virtual const ossimProperty & assign(const ossimProperty &rhs)
virtual const ossimProperty & assign(const ossimProperty &rhs)
std::vector< std::vector< double > > theValueArray
virtual ossimString valueToString() const
ossim_int32 toInt32() const
ossimMatrixProperty(const ossimString &name=ossimString(""), const std::vector< double > &values=std::vector< double >(), int numberOfRows=0, int numberOfColumns=0)
yy_size_t size
virtual ossimObject * dup() const
T min(T a, T b)
Definition: ossimCommon.h:203
#define DBL_EPSILON
void getRowConstraints(int &minNumberOfRows, int &maxNumberOfRows) const
void setColConstraints(int minNumberOfColumns, int maxNumberOfColumns)
virtual bool setValue(const ossimString &value)
std::basic_istringstream< char > istringstream
Class for char input memory streams.
Definition: ossimIosFwd.h:32
RTTI_DEF1(ossimMatrixProperty, "ossimMatrixProperty", ossimProperty)
void setRowConstraints(int minNumberOfRows, int maxNumberOfRows)