Quantization.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * @file Quantization.cpp
  3. * @brief Quantization of one-dimensional signals with a standard range of [0,1] (Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 01/09/2012
  6. */
  7. #include <iostream>
  8. #include "Quantization.h"
  9. using namespace NICE;
  10. Quantization::Quantization( )
  11. {
  12. this->ui_numBins = 1;
  13. }
  14. Quantization::Quantization( uint _numBins )
  15. {
  16. this->ui_numBins = _numBins;
  17. }
  18. Quantization::~Quantization()
  19. {
  20. }
  21. uint Quantization::size() const
  22. {
  23. return this->ui_numBins;
  24. }
  25. double Quantization::getPrototype (uint _bin) const
  26. {
  27. return _bin / (double)(this->ui_numBins-1);
  28. }
  29. uint Quantization::quantize (double _value) const
  30. {
  31. if ( _value <= 0.0 )
  32. return 0;
  33. else if ( _value >= 1.0 )
  34. return this->ui_numBins-1;
  35. else
  36. return (uint)( _value * (this->ui_numBins-1) + 0.5 );
  37. }
  38. // ---------------------- STORE AND RESTORE FUNCTIONS ----------------------
  39. void Quantization::restore ( std::istream & _is,
  40. int _format
  41. )
  42. {
  43. if ( _is.good() )
  44. {
  45. std::string tmp;
  46. bool b_endOfBlock ( false ) ;
  47. while ( !b_endOfBlock )
  48. {
  49. _is >> tmp; // start of block
  50. if ( this->isEndTag( tmp, "Quantization" ) )
  51. {
  52. b_endOfBlock = true;
  53. continue;
  54. }
  55. tmp = this->removeStartTag ( tmp );
  56. if ( tmp.compare("ui_numBins") == 0 )
  57. {
  58. _is >> this->ui_numBins;
  59. }
  60. else
  61. {
  62. std::cerr << "WARNING -- unexpected Quantization object -- " << tmp << " -- for restoration... aborting" << std::endl;
  63. throw;
  64. }
  65. _is >> tmp; // end of block
  66. tmp = this->removeEndTag ( tmp );
  67. }
  68. }
  69. else
  70. {
  71. std::cerr << "Quantization::restore -- InStream not initialized - restoring not possible!" << std::endl;
  72. }
  73. }
  74. void Quantization::store ( std::ostream & _os,
  75. int _format
  76. ) const
  77. {
  78. // show starting point
  79. _os << this->createStartTag( "Quantization" ) << std::endl;
  80. _os << this->createStartTag( "ui_numBins" ) << std::endl;
  81. _os << this->ui_numBins << std::endl;
  82. _os << this->createEndTag( "ui_numBins" ) << std::endl;
  83. // done
  84. _os << this->createEndTag( "Quantization" ) << std::endl;
  85. }