OTB  6.7.0
Orfeo Toolbox
mvdAlgorithm.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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"
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
95 
108 template< typename T2, unsigned int N, typename T1 >
109 inline
113 
123 template< typename T2, typename T1, unsigned int N >
124 inline
127 
137 template< typename T2, typename T1, unsigned int N >
138 inline
142 } // end namespace 'otb'
143 
144 /*****************************************************************************/
145 
146 namespace mvd
147 {
156 template< typename T >
157 inline
158 QTextStream&
159 operator << ( QTextStream& stream,
160  const itk::VariableLengthVector< T >& vector );
161 
170 template< typename T >
171 inline
172 QTextStream&
173 operator >> ( QTextStream& stream,
175 
184 template< typename T >
185 inline
186 QDataStream&
187 operator << ( QDataStream& stream,
188  const itk::VariableLengthVector< T >& vector );
189 
198 template< typename T >
199 inline
200 QDataStream&
201 operator >> ( QDataStream& stream,
203 
210 inline
211 QStringList
212 ToQStringList( const StringVector& sv );
213 
221 inline
222 QStringList&
223 AppendToQStringList( QStringList& qsl,
224  const StringVector& sv );
225 
237 inline
238 QString
239 FromStdString( const std::string& str );
240 
243 inline
244 std::string
245 ToLocalStdString( const QString & );
246 
249 inline
250 const char *
251 ToLocalString( const QString & );
252 
263 inline
264 std::string
265 ToStdString( const QString& str );
266 
269 inline
270 const char*
271 ToString( const QString& str );
272 
273 
277 inline
278 std::string
279 ToStdString( unsigned int val );
280 
281 inline
282 std::string
283 ToStdString( const SpacingType & spacing);
284 
285 inline
286 std::string
287 ToStdString( const SizeType & size);
288 
289 inline
290 std::string
291 ToStdString( const PointType & point);
292 
293 template< typename T>
294 std::string
295 ToStdString( const std::vector<T> & vec);
296 
299 template< typename T >
300 inline
301 QString
302 ToQString( const T& val );
303 
306 template<>
307 inline
308 QString
309 ToQString< float >( const float& );
310 
313 template<>
314 inline
315 QString
316 ToQString< double >( const double& );
317 
320 inline
321 QString
322 ToHumanReadableSize( qint64 fize, bool isInBits =true );
323 
324 } // end namespace 'mvd'.
325 
326 /*****************************************************************************/
327 
328 namespace mvd
329 {
340 template< typename TInputIterator, typename TUnaryPredicate >
341 inline
342 bool
343 AllOf( TInputIterator first, TInputIterator last, TUnaryPredicate pred );
344 
355 template< typename TInputIterator, typename TUnaryPredicate >
356 inline
357 bool
358 AnyOf( TInputIterator first, TInputIterator last, TUnaryPredicate pred );
359 
369 template< typename TInputIterator, typename TUnaryPredicate >
370 inline
371 bool
372 NoneOf( TInputIterator first, TInputIterator last, TUnaryPredicate pred );
373 
374 } // end namespace 'mvd'.
375 
376 /*****************************************************************************/
377 /* INLINE SECTION */
378 
379 namespace otb
380 {
381 /*******************************************************************************/
382 template< typename T2, unsigned int N, typename T1 >
383 inline
386 {
387  assert( v.Size()==N );
388 
389  throw itk::RangeError( __FILE__, __LINE__ );
390 
392 
393  for( unsigned int i=0; i<N; ++i )
394  a[ i ] = static_cast< T2 >( v[ i ] );
395 
396  return a;
397 }
398 
399 /*******************************************************************************/
400 template< typename T2, unsigned int N, typename T1 >
401 inline
405 {
406  assert( v.Size()==N && v.Size()==a.Size() );
407 
408  throw itk::RangeError( __FILE__, __LINE__ );
409 
410  for( unsigned int i=0; i<N; ++i )
411  a[ i ] = static_cast< T2>( v[ i ] );
412 
413  return a;
414 }
415 
416 /*******************************************************************************/
417 template< typename T2, typename T1, unsigned int N >
418 inline
421 {
422  assert( a.Size()==N );
423 
424  throw itk::RangeError( __FILE__, __LINE__ );
425 
427 
428  v.Reserve( N );
429 
430  for( unsigned int i=0; i<N; ++i )
431  v[ i ] = static_cast< T2 >( a[ i ] );
432 
433  return v;
434 }
435 
436 /*******************************************************************************/
437 template< typename T2, typename T1, unsigned int N >
438 inline
441  const itk::FixedArray< T1, N >& a )
442 {
443  assert( a.Size()==N );
444 
445  throw itk::RangeError( __FILE__, __LINE__ );
446 
447  v.Reserve( N );
448 
449  for( unsigned int i=0; i<N; ++i )
450  v[ i ] = static_cast< T2 >( a[ i ] );
451 
452  return v;
453 }
454 
455 } // end namespace 'otb'.
456 
457 namespace mvd
458 {
459 
461 inline
462 QStringList
464 {
465  QStringList qsl;
466  return AppendToQStringList( qsl, sv );
467 }
469 
471 inline
472 QStringList&
473 AppendToQStringList( QStringList& qsl, const StringVector& sv )
474 {
475  for( StringVector::const_iterator it( sv.begin() );
476  it!=sv.end();
477  ++ it )
478  {
479  qsl.append( QString(it->c_str()) );
480  }
482 
483  return qsl;
484 }
485 
486 /*******************************************************************************/
487 inline
488 std::string
489 ToLocalStdString( const QString & str )
490 {
491  return std::string( str.toLocal8Bit().constData() );
492 }
493 
494 /*******************************************************************************/
495 inline
496 const char *
497 ToLocalString( const QString & str )
498 {
499  return str.toLocal8Bit().constData();
500 }
501 
502 /*******************************************************************************/
503 inline
504 QString
505 FromStdString( const std::string& str )
506 {
507  return QString( str.c_str() );
508 }
509 
510 /*******************************************************************************/
511 inline
512 std::string
513 ToStdString( const QString& str )
514 {
515  return std::string( str.toLatin1().constData() );
516 }
517 
518 /*******************************************************************************/
519 inline
520 const char*
521 ToString( const QString& str )
522 {
523  return str.toLatin1().constData();
524 }
525 
527 inline
528 std::string
529 ToStdString( unsigned int val )
530 {
531  std::ostringstream oss;
532  oss << val;
533  return oss.str();
534 }
536 
538 inline
539 std::string
540 ToStdString( const SpacingType & spacing)
541 {
542  std::ostringstream oss;
543  oss << spacing[0] << " , " << spacing[1];
544  return oss.str();
545 }
547 
549 inline
550 std::string
551 ToStdString( const SizeType & size)
552 {
553  std::ostringstream oss;
554  oss << size[0] << " , " << size[1];
555  return oss.str();
556 }
558 
560 inline
561 std::string
562 ToStdString( const PointType & point)
563 {
564  std::ostringstream oss;
565  oss << point[0] << " , " << point[1];
566  return oss.str();
567 }
569 
571 template< typename T>
572 inline
573 std::string
574 ToStdString( const std::vector<T> & vec)
575 {
576  std::ostringstream oss;
577  typename std::vector<T>::const_iterator it = vec.begin();
578  while( it != vec.end() )
579  {
580  oss<<*it<< " ";
581  ++it;
582  }
584 
585  return oss.str();
586 }
587 
588 /*******************************************************************************/
589 template< typename T >
590 inline
591 QString
592 ToQString( const T& val )
593 {
594  return QString( "%1" ).arg( val );
595 }
596 
597 /*****************************************************************************/
598 template<>
599 inline
600 QString
601 ToQString< float >( const float& val )
602 {
603  assert( !std::isnan( val ) );
604 
605  QString valf;
606 
607  // See IEEE-754
608  // Ref. http://en.wikipedia.org/wiki/Single-precision_floating-point_format
609  valf.sprintf( "%.9g", val );
610 
611 #if 0
612  std::cout
613  << "ToString< float >(" << val << "): "
614  << ToStdString( valf )
615  << "\t"
616  << valf.toFloat()
617  << std::endl;
618 #endif
619 
620  if( valf.toFloat()!=val )
621  throw std::runtime_error(
622  ToStdString(
623  QCoreApplication::translate(
624  "mvd::DatasetDescritor",
625  "Accuracy loss when converting float (%1) to string."
626  )
627  .arg( valf )
628  )
629  );
630 
631  return valf;
632 }
633 
634 /*****************************************************************************/
635 template<>
636 inline
637 QString
638 ToQString< double >( const double& val )
639 {
640  assert( !std::isnan( val ) );
641 
642  QString vald;
643 
644  // See IEEE-754
645  // Ref. http://en.wikipedia.org/wiki/Double_precision
646  vald.sprintf( "%.17g", val );
647 
648 #if 0
649  std::cout
650  << "ToString< double >(" << val << "): "
651  << ToStdString( vald )
652  << "\t"
653  << vald.toDouble()
654  << std::endl;
655 #endif
656 
657  if( vald.toDouble()!=val )
658  throw std::runtime_error(
659  ToStdString(
660  QCoreApplication::translate(
661  "mvd::DatasetDescriptor",
662  "Accuracy loss when converting double (%1) to string."
663  )
664  .arg( vald )
665  )
666  );
667 
668  return vald;
669 }
670 
671 } // end namespace 'mvd'
672 
673 namespace mvd
674 {
675 
676 /*******************************************************************************/
677 template< typename TInputIterator, typename TUnaryPredicate >
678 inline
679 bool
680 AllOf( TInputIterator first, TInputIterator last, TUnaryPredicate pred )
681 {
682  while( first!=last )
683  {
684  if( !pred( *first ) )
685  return false;
686 
687  ++first;
688  }
689 
690  return true;
691 }
692 
693 /*******************************************************************************/
694 template< typename TInputIterator, typename TUnaryPredicate >
695 inline
696 bool
697 AnyOf( TInputIterator first, TInputIterator last, TUnaryPredicate pred )
698 {
699  while( first!=last )
700  {
701  if( pred( *first ) )
702  return true;
703 
704  ++first;
705  }
706 
707  return false;
708 }
709 
710 /*******************************************************************************/
711 template< typename TInputIterator, typename TUnaryPredicate >
712 inline
713 bool
714 NoneOf( TInputIterator first, TInputIterator last, TUnaryPredicate pred )
715 {
716  while( first!=last )
717  {
718  if( pred( *first ) )
719  return false;
720 
721  ++first;
722  }
723 
724  return true;
725 }
726 
727 /*******************************************************************************/
728 inline
729 QString
730 ToHumanReadableSize( qint64 size, bool isInBits )
731 {
732  double thousand =
733  isInBits ? 1024.0 : 1000.0;
734 
735  double remainder = static_cast< double >( size );
736 
737  if( size < thousand )
738  return QString::number( size );
739 
740  remainder /= thousand;
741 
742  if( remainder < thousand )
743  return QString("%1 KiB" ).arg( QString::number( remainder, 'g', 3 ) );
744 
745  remainder /= thousand;
746 
747  if( remainder < thousand )
748  return QString("%1 MiB" ).arg( QString::number( remainder, 'g', 3 ) );
749 
750  remainder /= thousand;
751 
752  if( remainder < thousand )
753  return QString("%1 GiB" ).arg( QString::number( remainder, 'g', 3 ) );
754 
755  remainder /= thousand;
756 
757  return QString("%1 TiB" ).arg( QString::number( remainder, 'g', 3 ) );
758 }
759 
760 } // end namespace 'mvd'
761 
762 
763 #endif // mvdAlgorithm_h
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
void Reserve(ElementIdentifier size)
QStringList ToQStringList(const StringVector &sv)
Convert a StringVector object to a QStringList object.
Definition: mvdAlgorithm.h:463
QTextStream & operator<<(QTextStream &stream, const itk::VariableLengthVector< T > &vector)
Write an itk::VariableLengthVector< T > into a QTextStream.
itk::FixedArray< T2, N > ToFixedArray(const itk::VariableLengthVector< T1 > &v)
Definition: mvdAlgorithm.h:385
std::string ToStdString(const QString &str)
Convert and copy a QString to a STL std::string.
Definition: mvdAlgorithm.h:513
unsigned int Size(void) const noexcept
QStringList & AppendToQStringList(QStringList &qsl, const StringVector &sv)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:473
std::vector< std::string > StringVector
Definition: mvdTypes.h:99
const char * ToLocalString(const QString &)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:497
bool NoneOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
Test if no element in range fulfills condition.
Definition: mvdAlgorithm.h:714
std::string ToLocalStdString(const QString &)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:489
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:697
const char * ToString(const QString &str)
Append the content of a StringVector object to the content of a QStringList object.
Definition: mvdAlgorithm.h:521
QString FromStdString(const std::string &str)
Convert and copy an STL std::string to a QString.
Definition: mvdAlgorithm.h:505
QString ToQString< double >(const double &)
Definition: mvdAlgorithm.h:638
SizeType Size() const
QString ToQString< float >(const float &)
Definition: mvdAlgorithm.h:601
QString ToQString(const T &val)
Definition: mvdAlgorithm.h:592
bool AllOf(TInputIterator first, TInputIterator last, TUnaryPredicate pred)
Test condition on all elements in range.
Definition: mvdAlgorithm.h:680
QString ToHumanReadableSize(qint64 fize, bool isInBits=true)
Definition: mvdAlgorithm.h:730
itk::VariableLengthVector< T2 > ToVariableLengthVector(const itk::FixedArray< T1, N > &a)
Definition: mvdAlgorithm.h:420
VectorImageType::SpacingType SpacingType
Definition: mvdTypes.h:181
VectorImageType::PointType PointType
Definition: mvdTypes.h:189