OTB  7.2.0
Orfeo Toolbox
mvdAlgorithm.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef mvdAlgorithm_h
22 #define mvdAlgorithm_h
23 
24 //
25 // Configuration include.
27 #include "ConfigureMonteverdi.h"
28 
29 #include "OTBMonteverdiCoreExport.h"
30 
31 /*****************************************************************************/
32 /* INCLUDE SECTION */
33 
34 //
35 // Qt includes (sorted by alphabetic order)
37 #include <QtCore>
38 
39 //
40 // System includes (sorted by alphabetic order)
41 #include <string>
42 
43 //
44 // ITK includes (sorted by alphabetic order)
45 #include "itkFixedArray.h"
46 #include "itkExceptionObject.h"
47 #include "itkVariableLengthVector.h"
48 
49 //
50 // OTB includes (sorted by alphabetic order)
51 
52 //
53 // Monteverdi includes (sorted by alphabetic order)
54 #include "mvdTypes.h"
55 
56 
57 /*****************************************************************************/
58 /* PRE-DECLARATION SECTION */
59 
60 //
61 // External classes pre-declaration.
62 namespace
63 {
64 }
65 
66 namespace mvd
67 {
68 //
69 // Internal classes pre-declaration.
70 } // end of namespace 'mvd'.
71 
72 
73 /*****************************************************************************/
74 /* FUNCTIONS DECLARATION. */
75 
76 namespace otb
77 {
78 
91 template <typename T2, unsigned int N, typename T1>
92 inline itk::FixedArray<T2, N> ToFixedArray(const itk::VariableLengthVector<T1>& v);
93 
106 template <typename T2, unsigned int N, typename T1>
107 inline itk::FixedArray<T2, N>& ToFixedArray(itk::FixedArray<T2, N>& a, const itk::VariableLengthVector<T1>& v);
108 
118 template <typename T2, typename T1, unsigned int N>
119 inline itk::VariableLengthVector<T2> ToVariableLengthVector(const itk::FixedArray<T1, N>& a);
120 
130 template <typename T2, typename T1, unsigned int N>
131 inline itk::VariableLengthVector<T2>& ToVariableLengthVector(itk::FixedArray<T1, N>& a, const itk::VariableLengthVector<T1>& v);
132 } // end namespace 'otb'
133 
134 /*****************************************************************************/
135 
136 namespace mvd
137 {
146 template <typename T>
147 inline QTextStream& operator<<(QTextStream& stream, const itk::VariableLengthVector<T>& vector);
148 
157 template <typename T>
158 inline QTextStream& operator>>(QTextStream& stream, itk::VariableLengthVector<T>& vector);
159 
168 template <typename T>
169 inline QDataStream& operator<<(QDataStream& stream, const itk::VariableLengthVector<T>& vector);
170 
179 template <typename T>
180 inline QDataStream& operator>>(QDataStream& stream, itk::VariableLengthVector<T>& vector);
181 
188 inline QStringList ToQStringList(const StringVector& sv);
189 
197 inline QStringList& AppendToQStringList(QStringList& qsl, const StringVector& sv);
198 
210 inline QString FromStdString(const std::string& str);
211 
214 inline std::string ToLocalStdString(const QString&);
215 
218 inline const char* ToLocalString(const QString&);
219 
230 inline std::string ToStdString(const QString& str);
231 
234 inline const char* ToString(const QString& str);
235 
236 
240 inline std::string ToStdString(unsigned int val);
241 
242 inline std::string ToStdString(const SpacingType& spacing);
243 
244 inline std::string ToStdString(const SizeType& size);
245 
246 inline std::string ToStdString(const PointType& point);
247 
248 template <typename T>
249 std::string ToStdString(const std::vector<T>& vec);
250 
253 template <typename T>
254 inline QString ToQString(const T& val);
255 
258 template <>
259 inline QString ToQString<float>(const float&);
260 
263 template <>
264 inline QString ToQString<double>(const double&);
265 
268 inline QString ToHumanReadableSize(qint64 fize, bool isInBits = true);
269 
270 } // end namespace 'mvd'.
271 
272 /*****************************************************************************/
273 
274 namespace mvd
275 {
286 template <typename TInputIterator, typename TUnaryPredicate>
287 inline bool AllOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred);
288 
299 template <typename TInputIterator, typename TUnaryPredicate>
300 inline bool AnyOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred);
301 
311 template <typename TInputIterator, typename TUnaryPredicate>
312 inline bool NoneOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred);
313 
314 } // end namespace 'mvd'.
315 
316 /*****************************************************************************/
317 /* INLINE SECTION */
318 
319 namespace otb
320 {
321 /*******************************************************************************/
322 template <typename T2, unsigned int N, typename T1>
323 inline itk::FixedArray<T2, N> ToFixedArray(const itk::VariableLengthVector<T1>& v)
324 {
325  assert(v.Size() == N);
326 
327  throw itk::RangeError(__FILE__, __LINE__);
328 
329  itk::FixedArray<T2, N> a;
330 
331  for (unsigned int i = 0; i < N; ++i)
332  a[i] = static_cast<T2>(v[i]);
333 
334  return a;
335 }
336 
337 /*******************************************************************************/
338 template <typename T2, unsigned int N, typename T1>
339 inline itk::FixedArray<T2, N>& ToFixedArray(itk::FixedArray<T2, N>& a, const itk::VariableLengthVector<T1>& v)
340 {
341  assert(v.Size() == N && v.Size() == a.Size());
342 
343  throw itk::RangeError(__FILE__, __LINE__);
344 
345  for (unsigned int i = 0; i < N; ++i)
346  a[i] = static_cast<T2>(v[i]);
347 
348  return a;
349 }
350 
351 /*******************************************************************************/
352 template <typename T2, typename T1, unsigned int N>
353 inline itk::VariableLengthVector<T2> ToVariableLengthVector(const itk::FixedArray<T1, N>& a)
354 {
355  assert(a.Size() == N);
356 
357  throw itk::RangeError(__FILE__, __LINE__);
358 
359  itk::VariableLengthVector<T2> v;
360 
361  v.Reserve(N);
362 
363  for (unsigned int i = 0; i < N; ++i)
364  v[i] = static_cast<T2>(a[i]);
365 
366  return v;
367 }
368 
369 /*******************************************************************************/
370 template <typename T2, typename T1, unsigned int N>
371 inline itk::VariableLengthVector<T2>& ToVariableLengthVector(itk::VariableLengthVector<T2>& v, const itk::FixedArray<T1, N>& a)
372 {
373  assert(a.Size() == N);
374 
375  throw itk::RangeError(__FILE__, __LINE__);
376 
377  v.Reserve(N);
378 
379  for (unsigned int i = 0; i < N; ++i)
380  v[i] = static_cast<T2>(a[i]);
381 
382  return v;
383 }
384 
385 } // end namespace 'otb'.
386 
387 namespace mvd
388 {
389 
391 inline QStringList ToQStringList(const StringVector& sv)
392 {
393  QStringList qsl;
394  return AppendToQStringList(qsl, sv);
395 }
397 
399 inline QStringList& AppendToQStringList(QStringList& qsl, const StringVector& sv)
400 {
401  for (StringVector::const_iterator it(sv.begin()); it != sv.end(); ++it)
402  {
403  qsl.append(QString(it->c_str()));
404  }
406 
407  return qsl;
408 }
409 
410 /*******************************************************************************/
411 inline std::string ToLocalStdString(const QString& str)
412 {
413  return std::string(str.toLocal8Bit().constData());
414 }
415 
416 /*******************************************************************************/
417 inline const char* ToLocalString(const QString& str)
418 {
419  return str.toLocal8Bit().constData();
420 }
421 
422 /*******************************************************************************/
423 inline QString FromStdString(const std::string& str)
424 {
425  return QString(str.c_str());
426 }
427 
428 /*******************************************************************************/
429 inline std::string ToStdString(const QString& str)
430 {
431  return std::string(str.toLatin1().constData());
432 }
433 
434 /*******************************************************************************/
435 inline const char* ToString(const QString& str)
436 {
437  return str.toLatin1().constData();
438 }
439 
441 inline std::string ToStdString(unsigned int val)
442 {
443  std::ostringstream oss;
444  oss << val;
445  return oss.str();
446 }
448 
450 inline std::string ToStdString(const SpacingType& spacing)
451 {
452  std::ostringstream oss;
453  oss << spacing[0] << " , " << spacing[1];
454  return oss.str();
455 }
457 
459 inline std::string ToStdString(const SizeType& size)
460 {
461  std::ostringstream oss;
462  oss << size[0] << " , " << size[1];
463  return oss.str();
464 }
466 
468 inline std::string ToStdString(const PointType& point)
469 {
470  std::ostringstream oss;
471  oss << point[0] << " , " << point[1];
472  return oss.str();
473 }
475 
477 template <typename T>
478 inline std::string ToStdString(const std::vector<T>& vec)
479 {
480  std::ostringstream oss;
481  typename std::vector<T>::const_iterator it = vec.begin();
482  while (it != vec.end())
483  {
484  oss << *it << " ";
485  ++it;
486  }
488 
489  return oss.str();
490 }
491 
492 /*******************************************************************************/
493 template <typename T>
494 inline QString ToQString(const T& val)
495 {
496  return QString("%1").arg(val);
497 }
498 
499 /*****************************************************************************/
500 template <>
501 inline QString ToQString<float>(const float& val)
502 {
503  assert(!std::isnan(val));
504 
505  // See IEEE-754
506  // Ref. http://en.wikipedia.org/wiki/Single-precision_floating-point_format
507 
508  QString valf = QString("%1").arg(val, 0, 'g', 9);
509 
510 #if 0
511  std::cout
512  << "ToString< float >(" << val << "): "
513  << ToStdString( valf )
514  << "\t"
515  << valf.toFloat()
516  << std::endl;
517 #endif
518 
519  if (valf.toFloat() != val)
520  throw std::runtime_error(
521  ToStdString(QCoreApplication::translate("mvd::DatasetDescritor", "Accuracy loss when converting float (%1) to string.").arg(valf)));
522 
523  return valf;
524 }
525 
526 /*****************************************************************************/
527 template <>
528 inline QString ToQString<double>(const double& val)
529 {
530  assert(!std::isnan(val));
531 
532  // See IEEE-754
533  // Ref. http://en.wikipedia.org/wiki/Double_precision
534 
535  QString vald = QString("%1").arg(val, 0, 'g', 17);
536 
537 
538 #if 0
539  std::cout
540  << "ToString< double >(" << val << "): "
541  << ToStdString( vald )
542  << "\t"
543  << vald.toDouble()
544  << std::endl;
545 #endif
546 
547  if (vald.toDouble() != val)
548  throw std::runtime_error(
549  ToStdString(QCoreApplication::translate("mvd::DatasetDescriptor", "Accuracy loss when converting double (%1) to string.").arg(vald)));
550 
551  return vald;
552 }
553 
554 } // end namespace 'mvd'
555 
556 namespace mvd
557 {
558 
559 /*******************************************************************************/
560 template <typename TInputIterator, typename TUnaryPredicate>
561 inline bool AllOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
562 {
563  while (first != last)
564  {
565  if (!pred(*first))
566  return false;
567 
568  ++first;
569  }
570 
571  return true;
572 }
573 
574 /*******************************************************************************/
575 template <typename TInputIterator, typename TUnaryPredicate>
576 inline bool AnyOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
577 {
578  while (first != last)
579  {
580  if (pred(*first))
581  return true;
582 
583  ++first;
584  }
585 
586  return false;
587 }
588 
589 /*******************************************************************************/
590 template <typename TInputIterator, typename TUnaryPredicate>
591 inline bool NoneOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
592 {
593  while (first != last)
594  {
595  if (pred(*first))
596  return false;
597 
598  ++first;
599  }
600 
601  return true;
602 }
603 
604 /*******************************************************************************/
605 inline QString ToHumanReadableSize(qint64 size, bool isInBits)
606 {
607  double thousand = isInBits ? 1024.0 : 1000.0;
608 
609  double remainder = static_cast<double>(size);
610 
611  if (size < thousand)
612  return QString::number(size);
613 
614  remainder /= thousand;
615 
616  if (remainder < thousand)
617  return QString("%1 KiB").arg(QString::number(remainder, 'g', 3));
618 
619  remainder /= thousand;
620 
621  if (remainder < thousand)
622  return QString("%1 MiB").arg(QString::number(remainder, 'g', 3));
623 
624  remainder /= thousand;
625 
626  if (remainder < thousand)
627  return QString("%1 GiB").arg(QString::number(remainder, 'g', 3));
628 
629  remainder /= thousand;
630 
631  return QString("%1 TiB").arg(QString::number(remainder, 'g', 3));
632 }
633 
634 } // end namespace 'mvd'
635 
636 
637 #endif // mvdAlgorithm_h
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
itk::FixedArray< T2, N > & ToFixedArray(itk::FixedArray< T2, N > &a, const itk::VariableLengthVector< T1 > &v)
Definition: mvdAlgorithm.h:339
itk::VariableLengthVector< T2 > & ToVariableLengthVector(itk::VariableLengthVector< T2 > &v, const itk::FixedArray< T1, N > &a)
Definition: mvdAlgorithm.h:371
std::vector< std::string > StringVector
Definition: mvdTypes.h:99
QStringList ToQStringList(const StringVector &sv)
Convert a StringVector object to a QStringList object.
Definition: mvdAlgorithm.h:391
std::string ToStdString(const QString &str)
Convert and copy a QString to a STL std::string.
Definition: mvdAlgorithm.h:429
QStringList & AppendToQStringList(QStringList &qsl, const StringVector &sv)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:399
const char * ToLocalString(const QString &)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:417
bool NoneOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
Test if no element in range fulfills condition.
Definition: mvdAlgorithm.h:591
std::string ToLocalStdString(const QString &)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:411
QTextStream & operator>>(QTextStream &stream, itk::VariableLengthVector< T > &vector)
Write an itk::VariableLengthVector< T > into a QTextStream.
bool AnyOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
Test if any of element in range fulfills condition.
Definition: mvdAlgorithm.h:576
const char * ToString(const QString &str)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:435
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
QString FromStdString(const std::string &str)
Convert and copy an STL std::string to a QString.
Definition: mvdAlgorithm.h:423
QString ToQString< double >(const double &)
Definition: mvdAlgorithm.h:528
QString ToQString< float >(const float &)
Definition: mvdAlgorithm.h:501
QString ToQString(const T &val)
Definition: mvdAlgorithm.h:494
bool AllOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
Test condition on all elements in range.
Definition: mvdAlgorithm.h:561
QString ToHumanReadableSize(qint64 fize, bool isInBits=true)
Definition: mvdAlgorithm.h:605
VectorImageType::SpacingType SpacingType
Definition: mvdTypes.h:175
VectorImageType::PointType PointType
Definition: mvdTypes.h:183