Commit a7dd9604 authored by Dipl.-Ing. Jonas Stienen's avatar Dipl.-Ing. Jonas Stienen
Browse files

Refactoring

parent 3614834f
......@@ -55,16 +55,16 @@ endif( )
# Sources
set( VAMATLAB_SRC
"src/FunctionMappings.h"
"src/MatlabHelpers.h"
"src/MatlabHelpers.cpp"
"src/VAMatlabFunctionMapping.h"
"src/VAMatlabHelpers.h"
"src/VAMatlabHelpers.cpp"
"src/VAMatlabConnection.cpp"
"src/VAMatlabConnection.h"
"src/VAMatlabExecutable.cpp"
"src/VAMatlabTracking.h"
"src/VAMatlabTracking.cpp" )
message( ${VAMATLAB_SRC} )
add_library( VAMatlab ${VAMATLAB_SRC} VAMatlab.def )
target_link_libraries( VAMatlab ${VISTA_USE_PACKAGE_LIBRARIES} )
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -88,6 +88,7 @@ public:
, pAddr( pFunctionAddr )
, bPublic( bPublicFunc )
, sDesc( sFunctionDesc )
, sDoc( sFunctionDoc )
{
// Self register the function globally
AddFunctionMapping( this );
......
// $Id: MatlabHelpers.cpp 2403 2012-04-20 18:31:09Z fwefers $
#include "MatlabHelpers.h"
#include "VAMatlabHelpers.h"
#include <VAException.h>
......@@ -10,332 +8,352 @@
#include <sstream>
#include <stdint.h>
bool matlabIsScalar(const mxArray *A) {
mwSize s = mxGetNumberOfDimensions(A);
bool matlabIsScalar( const mxArray *A )
{
mwSize s = mxGetNumberOfDimensions( A );
// Es mssen zwei Dimensionen vorliegen
if (s != 2) return false;
const mwSize* d = mxGetDimensions(A);
if( s != 2 ) return false;
const mwSize* d = mxGetDimensions( A );
// Beide Dimensionen mssen 1 sein
return ((d[0] == 1) && (d[1] == 1));
return ( ( d[ 0 ] == 1 ) && ( d[ 1 ] == 1 ) );
}
bool matlabIsVector(const mxArray *A, int& size) {
mwSize s = mxGetNumberOfDimensions(A);
bool matlabIsVector( const mxArray *A, int& size )
{
mwSize s = mxGetNumberOfDimensions( A );
// Es mssen zwei Dimensionen vorliegen
if (s != 2) return false;
const mwSize* d = mxGetDimensions(A);
if( s != 2 ) return false;
const mwSize* d = mxGetDimensions( A );
// Mindestens eine der beiden Dimensionen muss 1 sein
if( d[0] == 1 && d[1] > 1 )
if( d[ 0 ] == 1 && d[ 1 ] > 1 )
{
size = (int) d[1];
size = ( int ) d[ 1 ];
return true;
}
if( d[1] == 1 && d[0] > 1 )
if( d[ 1 ] == 1 && d[ 0 ] > 1 )
{
size = (int) d[0];
size = ( int ) d[ 0 ];
return true;
}
return false;
}
bool matlabIsVector(const mxArray *A) {
bool matlabIsVector( const mxArray *A ) {
int iDummy;
return matlabIsVector(A, iDummy);
return matlabIsVector( A, iDummy );
}
bool matlabIsRowVector(const mxArray *A) {
mwSize s = mxGetNumberOfDimensions(A);
if (s != 2) return false;
const mwSize* d = mxGetDimensions(A);
return (d[0] == 1);
bool matlabIsRowVector( const mxArray *A )
{
mwSize s = mxGetNumberOfDimensions( A );
if( s != 2 ) return false;
const mwSize* d = mxGetDimensions( A );
return ( d[ 0 ] == 1 );
}
bool matlabIsColumnVector(const mxArray *A) {
mwSize s = mxGetNumberOfDimensions(A);
if (s != 2) return false;
const mwSize* d = mxGetDimensions(A);
return (d[1] == 1);
bool matlabIsColumnVector( const mxArray *A )
{
mwSize s = mxGetNumberOfDimensions( A );
if( s != 2 ) return false;
const mwSize* d = mxGetDimensions( A );
return ( d[ 1 ] == 1 );
}
bool matlabGetBoolScalar(const mxArray* arg, const char* argname) {
bool matlabGetBoolScalar( const mxArray* arg, const char* argname )
{
const int buflen = 1024;
char buf[buflen];
char buf[ buflen ];
if (mxIsLogicalScalar(arg)) {
mxLogical* p = mxGetLogicals(arg);
return (*p == true);
if( mxIsLogicalScalar( arg ) )
{
mxLogical* p = mxGetLogicals( arg );
return ( *p == true );
}
if (mxIsNumeric(arg) && !mxIsComplex(arg) && matlabIsScalar(arg)) {
void* p = mxGetData(arg);
if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsScalar( arg ) )
{
void* p = mxGetData( arg );
if (mxIsInt16(arg)) return (*((int16_t*) p) != 0);
if (mxIsInt32(arg)) return (*((int32_t*) p) != 0);
if (mxIsInt64(arg)) return (*((int64_t*) p) != 0);
if (mxIsSingle(arg)) return (*((float*) p) != 0);
if (mxIsDouble(arg)) return (*((double*) p) != 0);
if( mxIsInt16( arg ) ) return ( *( ( int16_t* ) p ) != 0 );
if( mxIsInt32( arg ) ) return ( *( ( int32_t* ) p ) != 0 );
if( mxIsInt64( arg ) ) return ( *( ( int64_t* ) p ) != 0 );
if( mxIsSingle( arg ) ) return ( *( ( float* ) p ) != 0 );
if( mxIsDouble( arg ) ) return ( *( ( double* ) p ) != 0 );
// Unsupported datatype
sprintf_s(buf, buflen, "Argument '%s' could not be interpreted as a logical scalar", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, buflen, "Argument '%s' could not be interpreted as a logical scalar", argname );
VA_EXCEPT1( buf );
return false;
}
if (mxIsChar(arg) && matlabIsRowVector(arg)) {
mxGetString(arg, buf, buflen);
std::string s(buf);
std::transform(s.begin(), s.end(), s.begin(), tolower);
if( mxIsChar( arg ) && matlabIsRowVector( arg ) )
{
mxGetString( arg, buf, buflen );
std::string s( buf );
std::transform( s.begin(), s.end(), s.begin(), tolower );
if ((s == "true") || (s == "yes")) return true;
if ((s == "false") || (s == "no")) return false;
if( ( s == "true" ) || ( s == "yes" ) ) return true;
if( ( s == "false" ) || ( s == "no" ) ) return false;
// Unsupported datatype
sprintf_s(buf, buflen, "Argument '%s' could not be interpreted as a logical scalar", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, buflen, "Argument '%s' could not be interpreted as a logical scalar", argname );
VA_EXCEPT1( buf );
return false;
}
sprintf_s(buf, buflen, "Argument '%s' must be a logical scalar", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, buflen, "Argument '%s' must be a logical scalar", argname );
VA_EXCEPT1( buf );
return false;
}
int matlabGetIntegerScalar(const mxArray* arg, const char* argname) {
char buf[1024];
int matlabGetIntegerScalar( const mxArray* arg, const char* argname )
{
char buf[ 1024 ];
if (mxIsNumeric(arg) && !mxIsComplex(arg) && matlabIsScalar(arg)) {
void* p = mxGetData(arg);
if (mxIsInt16(arg)) return (int) (*((int16_t*) p));
if (mxIsInt32(arg)) return (int) (*((int32_t*) p));
if (mxIsInt64(arg)) return (int) (*((int64_t*) p)); // [fwe] Possible truncation!
if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsScalar( arg ) )
{
void* p = mxGetData( arg );
if (mxIsSingle(arg)) {
float fValue = *((float*) p);
if( mxIsInt16( arg ) ) return ( int ) ( *( ( int16_t* ) p ) );
if( mxIsInt32( arg ) ) return ( int ) ( *( ( int32_t* ) p ) );
if( mxIsInt64( arg ) ) return ( int ) ( *( ( int64_t* ) p ) ); // [fwe] Possible truncation!
if( mxIsSingle( arg ) ) {
float fValue = *( ( float* ) p );
// Check for integer
if (fValue == floor(fValue))
return (int) fValue;
if( fValue == floor( fValue ) )
return ( int ) fValue;
}
if (mxIsDouble(arg)) {
double dValue = *((double*) p);
if( mxIsDouble( arg ) ) {
double dValue = *( ( double* ) p );
// Check for integer
if (dValue == floor(dValue))
return (int) dValue; // [fwe] Possible truncation!
if( dValue == floor( dValue ) )
return ( int ) dValue; // [fwe] Possible truncation!
}
// Unsupported datatype
sprintf_s(buf, 1024, "Argument '%s' could not be interpreted as an integer scalar", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, 1024, "Argument '%s' could not be interpreted as an integer scalar", argname );
VA_EXCEPT1( buf );
return -1;
}
sprintf_s(buf, 1024, "Argument '%s' must be an integer scalar", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, 1024, "Argument '%s' must be an integer scalar", argname );
VA_EXCEPT1( buf );
return -1;
}
double matlabGetRealScalar(const mxArray* arg, const char* argname) {
char buf[1024];
double matlabGetRealScalar( const mxArray* arg, const char* argname )
{
char buf[ 1024 ];
bool b1 = mxIsNumeric(arg);
bool b2 = !mxIsComplex(arg);
bool b3 = matlabIsScalar(arg);
bool b1 = mxIsNumeric( arg );
bool b2 = !mxIsComplex( arg );
bool b3 = matlabIsScalar( arg );
if( b1 && b2 && b3 )
{
void* p = mxGetData(arg);
void* p = mxGetData( arg );
if( mxIsInt16( arg ) ) return ( double ) ( *( ( int16_t* ) p ) );
if( mxIsInt32( arg ) ) return ( double ) ( *( ( int32_t* ) p ) );
if( mxIsInt64( arg ) ) return ( double ) ( *( ( int64_t* ) p ) ); // [fwe] Possible truncation!
if (mxIsInt16(arg)) return (double) (*((int16_t*) p));
if (mxIsInt32(arg)) return (double) (*((int32_t*) p));
if (mxIsInt64(arg)) return (double) (*((int64_t*) p)); // [fwe] Possible truncation!
if( mxIsSingle( arg ) ) return *( ( float* ) p );
if( mxIsDouble( arg ) ) return *( ( double* ) p );
if (mxIsSingle(arg)) return *((float*) p);
if (mxIsDouble(arg)) return *((double*) p);
// Unsupported datatype
sprintf_s(buf, 1024, "Argument '%s' could not be interpreted as a real-valued scalar", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, 1024, "Argument '%s' could not be interpreted as a real-valued scalar", argname );
VA_EXCEPT1( buf );
return -1;
}
sprintf_s(buf, 1024, "Argument '%s' must be a real-valued scalar", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, 1024, "Argument '%s' must be a real-valued scalar", argname );
VA_EXCEPT1( buf );
return -1;
}
void matlabGetRealVector3(const mxArray* arg, const char* argname, double& x, double& y, double& z) {
char buf[1024];
void matlabGetRealVector3( const mxArray* arg, const char* argname, double& x, double& y, double& z )
{
char buf[ 1024 ];
int size;
if (mxIsNumeric(arg) && !mxIsComplex(arg) && matlabIsVector(arg, size))
if (size == 3) {
if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsVector( arg, size ) )
if( size == 3 ) {
if (mxIsInt16(arg)) {
int16_t* p = (int16_t*) mxGetData(arg);
x = (double) p[0];
y = (double) p[1];
z = (double) p[2];
return;
}
if( mxIsInt16( arg ) ) {
int16_t* p = ( int16_t* ) mxGetData( arg );
x = ( double ) p[ 0 ];
y = ( double ) p[ 1 ];
z = ( double ) p[ 2 ];
return;
}
if (mxIsInt32(arg)) {
int32_t* p = (int32_t*) mxGetData(arg);
x = (double) p[0];
y = (double) p[1];
z = (double) p[2];
return;
}
if( mxIsInt32( arg ) ) {
int32_t* p = ( int32_t* ) mxGetData( arg );
x = ( double ) p[ 0 ];
y = ( double ) p[ 1 ];
z = ( double ) p[ 2 ];
return;
}
if (mxIsInt64(arg)) {
int64_t* p = (int64_t*) mxGetData(arg);
x = (double) p[0];
y = (double) p[1];
z = (double) p[2];
return;
}
if( mxIsInt64( arg ) ) {
int64_t* p = ( int64_t* ) mxGetData( arg );
x = ( double ) p[ 0 ];
y = ( double ) p[ 1 ];
z = ( double ) p[ 2 ];
return;
}
if (mxIsSingle(arg)) {
float* p = (float*) mxGetData(arg);
x = (double) p[0];
y = (double) p[1];
z = (double) p[2];
return;
}
if( mxIsSingle( arg ) ) {
float* p = ( float* ) mxGetData( arg );
x = ( double ) p[ 0 ];
y = ( double ) p[ 1 ];
z = ( double ) p[ 2 ];
return;
}
if (mxIsDouble(arg)) {
double* p = (double*) mxGetData(arg);
x = p[0];
y = p[1];
z = p[2];
return;
}
if( mxIsDouble( arg ) ) {
double* p = ( double* ) mxGetData( arg );
x = p[ 0 ];
y = p[ 1 ];
z = p[ 2 ];
return;
}
}
sprintf_s(buf, 1024, "Argument '%s' must be a real-valued vector with exactly three elements", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, 1024, "Argument '%s' must be a real-valued vector with exactly three elements", argname );
VA_EXCEPT1( buf );
}
void matlabGetQuaternion(const mxArray* arg, const char* argname, double& w, double& i, double& j, double& k )
void matlabGetQuaternion( const mxArray* arg, const char* argname, double& w, double& i, double& j, double& k )
{
char buf[1024];
char buf[ 1024 ];
int size;
if (mxIsNumeric(arg) && !mxIsComplex(arg) && matlabIsVector(arg, size))
if( mxIsNumeric( arg ) && !mxIsComplex( arg ) && matlabIsVector( arg, size ) )
{
if(size == 4 )
if( size == 4 )
{
if (mxIsInt16(arg)) {
int16_t* p = (int16_t*) mxGetData(arg);
w = (double) p[0];
i = (double) p[1];
j = (double) p[2];
k = (double) p[3];
if( mxIsInt16( arg ) ) {
int16_t* p = ( int16_t* ) mxGetData( arg );
w = ( double ) p[ 0 ];
i = ( double ) p[ 1 ];
j = ( double ) p[ 2 ];
k = ( double ) p[ 3 ];
return;
}
if (mxIsInt32(arg)) {
int32_t* p = (int32_t*) mxGetData(arg);
w = (double) p[0];
i = (double) p[1];
j = (double) p[2];
k = (double) p[3];
if( mxIsInt32( arg ) ) {
int32_t* p = ( int32_t* ) mxGetData( arg );
w = ( double ) p[ 0 ];
i = ( double ) p[ 1 ];
j = ( double ) p[ 2 ];
k = ( double ) p[ 3 ];
return;
}
if (mxIsInt64(arg)) {
int64_t* p = (int64_t*) mxGetData(arg);
w = (double) p[0];
i = (double) p[1];
j = (double) p[2];
k = (double) p[3];
if( mxIsInt64( arg ) ) {
int64_t* p = ( int64_t* ) mxGetData( arg );
w = ( double ) p[ 0 ];
i = ( double ) p[ 1 ];
j = ( double ) p[ 2 ];
k = ( double ) p[ 3 ];
return;
}
if (mxIsSingle(arg)) {
float* p = (float*) mxGetData(arg);
w = (double) p[0];
i = (double) p[1];
j = (double) p[2];
k = (double) p[3];
if( mxIsSingle( arg ) ) {
float* p = ( float* ) mxGetData( arg );
w = ( double ) p[ 0 ];
i = ( double ) p[ 1 ];
j = ( double ) p[ 2 ];
k = ( double ) p[ 3 ];
return;
}
if (mxIsDouble(arg)) {
double* p = (double*) mxGetData(arg);
w = (double) p[0];
i = (double) p[1];
j = (double) p[2];
k = (double) p[3];
if( mxIsDouble( arg ) ) {
double* p = ( double* ) mxGetData( arg );
w = ( double ) p[ 0 ];
i = ( double ) p[ 1 ];
j = ( double ) p[ 2 ];
k = ( double ) p[ 3 ];
return;
}
}
}
sprintf_s(buf, 1024, "Argument '%s' must be a real-valued vector with exactly four elements (same order as Matlab quaternion: w (real), i, j, k)", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, 1024, "Argument '%s' must be a real-valued vector with exactly four elements (same order as Matlab quaternion: w (real), i, j, k)", argname );
VA_EXCEPT1( buf );
}
std::string matlabGetString(const mxArray* arg, const char* argname) {
std::string matlabGetString( const mxArray* arg, const char* argname )
{
const int buflen = 1024;
char buf[buflen];
char buf[ buflen ];
// TODO: [fwe] Diese Implementierung ist begrenzt in der Lnge. Sollte man dynamisch machen...
// Test auf Zeichenfolge
if (mxIsChar(arg)) {
if( mxIsChar( arg ) )
{
// Spezialfall: Leere Eingabe
if (mxIsEmpty(arg)) return std::string();
if( mxIsEmpty( arg ) ) return std::string();
if (matlabIsRowVector(arg)) {
mxGetString(arg, buf, buflen);
if( matlabIsRowVector( arg ) )
{
mxGetString( arg, buf, buflen );
return buf;
}
}
sprintf_s(buf, 1024, "Argument '%s' must be a string", argname);
VA_EXCEPT1(buf);
sprintf_s( buf, 1024, "Argument '%s' must be a string", argname );
VA_EXCEPT1( buf );
return std::string();
}
mxArray* matlabCreateRealVector3(double x, double y, double z) {
mxArray* p = mxCreateDoubleMatrix(3, 1, mxREAL);
double* d = mxGetPr(p);
d[0] = x;
d[1] = y;
d[2] = z;
mxArray* matlabCreateRealVector3( double x, double y, double z )
{
mxArray* p = mxCreateDoubleMatrix( 3, 1, mxREAL );
double* d = mxGetPr( p );
d[ 0 ] = x;
d[ 1 ] = y;
d[ 2 ] = z;
return p;
}
mxArray* matlabCreateID(int iID) {
mwSize d[2] = {1, 1};
mxArray* p = mxCreateNumericArray(2, d, mxINT32_CLASS, mxREAL);
*((int*) mxGetData(p)) = iID;
mxArray* matlabCreateID( int iID ) {
mwSize d[ 2 ] = { 1, 1 };
mxArray* p = mxCreateNumericArray( 2, d, mxINT32_CLASS, mxREAL );
*( ( int* ) mxGetData( p ) ) = iID;
return p;
}
mxArray* matlabCreateIDList(const std::vector<int>& viID) {
if (viID.empty()) {
mxArray* matlabCreateIDList( const std::vector<int>& viID )
{
if( viID.empty() )
{
// Special case: Handle empty matrix as 0x0 => Matlab convenience :-}
return mxCreateNumericMatrix(0, 0, mxINT32_CLASS, mxREAL);
} else {
return mxCreateNumericMatrix( 0, 0, mxINT32_CLASS, mxREAL );
}
else {
mxArray* a = mxCreateNumericMatrix( 1, int( viID.size() ), mxINT32_CLASS, mxREAL );
int* p = (int*) mxGetData(a);
for (size_t i=0; i<viID.size(); i++) p[i] = viID[i];
int* p = ( int* ) mxGetData( a );
for( size_t i = 0; i < viID.size(); i++ ) p[ i ] = viID[ i ];
return a;
}
}
mxArray* matlabCreateDirectivityInfo(const CVADirectivityInfo& di) {
mxArray* matlabCreateDirectivityInfo( const CVADirectivityInfo& di )
{
/*
int iID; // ID
std::string sName; // Name (optional)
......@@ -347,19 +365,20 @@ mxArray* matlabCreateDirectivityInfo(const CVADirectivityInfo& di) {
*/
const mwSize nFields = 5;
const char* ppszFieldNames[] = {"id", "name", "filename", "description", "references"};