testMatlabConversionFunctionsMex.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file GPHIKClassifierMex.cpp
  3. * @author Alexander Freytag
  4. * @date 07-01-2014 (dd-mm-yyyy)
  5. * @brief Matlab-Interface of our GPHIKClassifier, allowing for training, classification, optimization, variance prediction, incremental learning, and storing/re-storing.
  6. */
  7. // STL includes
  8. #include <math.h>
  9. #include <matrix.h>
  10. #include <mex.h>
  11. // NICE-core includes
  12. #include <core/vector/MatrixT.h>
  13. #include <core/vector/VectorT.h>
  14. // Interface for conversion between Matlab and C objects
  15. #include "gp-hik-core/matlab/classHandleMtoC.h"
  16. #include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
  17. #include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
  18. using namespace std; //C basics
  19. using namespace NICE; // nice-core
  20. // MAIN MATLAB FUNCTION
  21. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  22. {
  23. // get the command string specifying what to do
  24. if (nrhs < 1)
  25. mexErrMsgTxt("No commands and options passed... Aborting!");
  26. if( !mxIsChar( prhs[0] ) )
  27. mexErrMsgTxt("First argument needs to be the command, ie.e, the unit test method to call... Aborting!");
  28. std::string cmd = MatlabConversion::convertMatlabToString( prhs[0] );
  29. // in all other cases, there should be a second input,
  30. // which the be the class instance handle
  31. if (nrhs < 2)
  32. {
  33. mexErrMsgTxt("Second input should be some kind of matrix variable");
  34. return;
  35. }
  36. if (nlhs < 1)
  37. {
  38. mexErrMsgTxt("No return value defined, possible loss of data... Aborting!");
  39. }
  40. const mxArray *t_pData = prhs[1];
  41. ////////////////////////////////////////
  42. // Check which method to call //
  43. ////////////////////////////////////////
  44. if ( !strcmp("convertInt32", cmd.c_str() ) )
  45. {
  46. int t_iTest = MatlabConversion::convertMatlabToInt32( t_pData );
  47. std::cerr << "convertInt32: " << t_iTest << std::endl;
  48. // output
  49. plhs[0] = mxCreateDoubleScalar( t_iTest );
  50. return;
  51. }
  52. else if ( !strcmp("convertLogical", cmd.c_str() ) )
  53. {
  54. bool t_iTest = MatlabConversion::convertMatlabToBool( t_pData );
  55. std::cerr << "convertLogical: " << t_iTest << std::endl;
  56. // output
  57. plhs[0] = mxCreateLogicalScalar( t_iTest );
  58. return;
  59. }
  60. else if ( !strcmp("convertDouble", cmd.c_str() ) )
  61. {
  62. double t_dTest = MatlabConversion::convertMatlabToDouble( t_pData );
  63. std::cerr << "convertDouble: " << t_dTest << std::endl;
  64. // output
  65. plhs[0] = mxCreateDoubleScalar( t_dTest );
  66. return;
  67. }
  68. /// Matrix/vector functions
  69. else if ( !strcmp("convertDoubleVector", cmd.c_str() ) )
  70. {
  71. NICE::Vector t_vecTest = MatlabConversion::convertDoubleVectorToNice( t_pData );
  72. std::cerr << "convertDoubleVector: " << t_vecTest << std::endl;
  73. // output
  74. plhs[0] = MatlabConversion::convertVectorFromNice( t_vecTest );
  75. return;
  76. }
  77. else if ( !strcmp("convertDoubleMatrix", cmd.c_str() ) )
  78. {
  79. NICE::Matrix t_matTest = MatlabConversion::convertDoubleMatrixToNice( t_pData );
  80. std::cerr << "convertDoubleMatrix: " << t_matTest << std::endl;
  81. // output
  82. plhs[0] = MatlabConversion::convertMatrixFromNice( t_matTest );
  83. return;
  84. }
  85. else if ( !strcmp("convertDoubleSparseVector", cmd.c_str() ) )
  86. {
  87. NICE::SparseVector t_vecTest = MatlabConversion::convertSparseVectorToNice( t_pData );
  88. NICE::Vector t_fullVector;
  89. t_vecTest.convertToVectorT( t_fullVector );
  90. std::cerr << "convertDoubleSparseVector: full version:" << t_fullVector << std::endl;
  91. // output
  92. plhs[0] = MatlabConversion::convertVectorFromNice( t_fullVector );
  93. return;
  94. }
  95. // Got here, so command not recognized
  96. std::string errorMsg (cmd.c_str() );
  97. errorMsg += " -- command not recognized.";
  98. mexErrMsgTxt( errorMsg.c_str() );
  99. }