testMatlabConversionFunctionsMex.cpp 3.9 KB

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