ParameterizedFunction.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @file ParameterizedFunction.cpp
  3. * @brief Simple parameterized multi-dimensional function (Implementation)
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 01/04/2012
  6. */
  7. // STL includes
  8. #include <iostream>
  9. // NICE-core includes
  10. #include "ParameterizedFunction.h"
  11. using namespace NICE;
  12. using namespace std;
  13. ParameterizedFunction::ParameterizedFunction( uint dimension )
  14. {
  15. m_parameters.resize(dimension);
  16. }
  17. void ParameterizedFunction::applyFunctionToDataMatrix ( std::vector< std::vector< double > > & dataMatrix ) const
  18. {
  19. // REMARK: might be inefficient due to virtual calls
  20. int iCnt(0);
  21. for ( vector< vector<double> >::iterator i = dataMatrix.begin() ; i != dataMatrix.end(); i++, iCnt++ )
  22. {
  23. uint index = 0;
  24. for ( vector<double>::iterator j = i->begin(); j != i->end(); j++, index++ )
  25. {
  26. *j = f ( iCnt, *j );
  27. }
  28. }
  29. }
  30. void ParameterizedFunction::restore ( std::istream & is, int format )
  31. {
  32. if (is.good())
  33. {
  34. is.precision (std::numeric_limits<double>::digits10 + 1);
  35. std::string tmp;
  36. bool b_endOfBlock ( false ) ;
  37. while ( !b_endOfBlock )
  38. {
  39. is >> tmp; // start of block
  40. if ( this->isEndTag( tmp, "ParameterizedFunction" ) )
  41. {
  42. b_endOfBlock = true;
  43. continue;
  44. }
  45. tmp = this->removeStartTag ( tmp );
  46. if ( tmp.compare("m_parameters") == 0 )
  47. {
  48. is >> m_parameters;
  49. }
  50. else
  51. {
  52. std::cerr << "WARNING -- unexpected ParameterizedFunction object -- " << tmp << " -- for restoration... aborting" << std::endl;
  53. throw;
  54. }
  55. is >> tmp; // end of block
  56. tmp = this->removeEndTag ( tmp );
  57. }
  58. }
  59. else
  60. {
  61. std::cerr << "ParameterizedFunction::restore -- InStream not initialized - restoring not possible!" << std::endl;
  62. }
  63. }
  64. void ParameterizedFunction::store ( std::ostream & os, int format ) const
  65. {
  66. if (os.good())
  67. {
  68. // show starting point
  69. os << this->createStartTag( "ParameterizedFunction" ) << std::endl;
  70. os.precision (std::numeric_limits<double>::digits10 + 1);
  71. os << this->createStartTag( "m_parameters" ) << std::endl;
  72. os << m_parameters << std::endl;
  73. os << this->createEndTag( "m_parameters" ) << std::endl;
  74. // done
  75. os << this->createEndTag( "ParameterizedFunction" ) << std::endl;
  76. }
  77. };