Commit 0b80a89c by Stefan Philips

[BRANCH] matlab - branch for matlab wrapper (doesn't compile, yet, due to linker problems)

parent a3e882c0
......@@ -171,3 +171,30 @@ SETUP_SHADERS( "${TARGET_GLSL_FILES}" "${OW_SHARE_DIR_RELATIVE}/shaders" "CORE"
SETUP_STYLECHECKER( "${LibName}"
"${TARGET_CPP_FILES};${TARGET_H_FILES};${TARGET_TEST_FILES};${TARGET_GLSL_FILES}" # add all these files to the stylechecker
"" ) # exlude some ugly files
FIND_PACKAGE( Matlab )
IF( MATLAB_FOUND )
MESSAGE( STATUS "Found matlab" )
INCLUDE_DIRECTORIES( ${MATLAB_INCLUDE_DIR} )
MESSAGE( STATUS MATLAB_INCLUDE_DIR: ${MATLAB_INCLUDE_DIR} ) # include path for mex.h, engine.h
MESSAGE( STATUS MATLAB_LIBRARIES: ${MATLAB_LIBRARIES} ) # required libraries: libmex, etc
MESSAGE( STATUS MATLAB_MEX_LIBRARY: ${MATLAB_MEX_LIBRARY} ) # path to libmex.lib
MESSAGE( STATUS MATLAB_MX_LIBRARY: ${MATLAB_MX_LIBRARY} ) # path to libmx.lib
MESSAGE( STATUS MATLAB_ENG_LIBRARY: ${MATLAB_ENG_LIBRARY} ) # path to libeng.lib
# MESSAGE( TARGET_LINK_LIBRARIES: ${MATLAB_ENG_LIBRARY} ) # path to libeng.lib
# LIST( APPEND MODULE_DEPENDENCIES ${MATLAB_MEX_LIBRARY} ${MATLAB_MX_LIBRARY} ${MATLAB_ENG_LIBRARY} )
MESSAGE( STATUS LINK_DIRECTORIES: ${LINK_DIRECTORIES} )
ADD_DEFINITIONS( -DUSE_MATLAB )
# ADD_DEFINITIONS( -l${MATLAB_MEX_LIBRARY} -l${MATLAB_MX_LIBRARY} -l${MATLAB_ENG_LIBRARY} )
# LINK_DIRECTORIES( ${MATLAB_LIBRARIES} )
# set_property(TARGET ${LibName} PROPERTY LINK_SEARCH_START_STATIC 1)
# TARGET_LINK_LIBRARIES( ${LibName} /tmp/bla/libmex.so /tmp/bla/libmx.so /tmp/bla/libeng.so )
TARGET_LINK_LIBRARIES( ${LibName} ${MATLAB_LIBRARIES} )
# TARGET_LINK_LIBRARIES( ${LibName} ${Boost_LIBRARIES} ${CMAKE_STANDARD_LIBRARIES} ${CMAKE_DL_LIBS} ${OPENGL_gl_LIBRARY} ${OPENSCENEGRAPH_LIBRARIES} ${MATLAB_MEX_LIBRARY} ${MATLAB_MX_LIBRARY} ${MATLAB_ENG_LIBRARY} )
# ADD_LIBRARY( ${MATLAB_MEX_LIBRARY} SHARED ${MATLAB_INCLUDE_DIR}/mex.h )
# ADD_LIBRARY( ${MATLAB_MX_LIBRARY} SHARED ${MATLAB_INCLUDE_DIR}/mex.h )
# ADD_LIBRARY( ${MATLAB_ENG_LIBRARY} SHARED ${MATLAB_INCLUDE_DIR}/mex.h )
ENDIF( MATLAB_FOUND )
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
// #include "/local/apps/matlab/extern/include/engine.h"
#include <engine.h>
#include "WMatlab.h"
WMatlab::WMatlab( const std::string startCommand ) : m_engine( NULL )
{
m_engine = engOpen( startCommand.c_str() );
WAssert( m_engine, "Matlab start failed." );
}
WMatlab::~WMatlab()
{
engClose( m_engine );
}
void WMatlab::putVariable( const std::string& varName, const WMatrix<double>& matrix )
{
size_t cols = matrix.getNbCols();
size_t rows = matrix.getNbRows();
mxArray* matlabMatrix = mxCreateDoubleMatrix( rows, cols, mxREAL );
// TODO(philips): do this by std::copy (we use c++)
memcpy( ( void* ) mxGetPr( matlabMatrix ), ( void* ) &matrix[0], sizeof( double ) * cols * rows );
// for ( size_t col = 0; col < cols; ++col )
// {
// for ( size_t row = 0; row < rows; ++row )
// {
// matlabMatrixPtr[ row * cols + col ] = matrix( row, col );
// }
// }
engPutVariable( m_engine, varName.c_str(), matlabMatrix );
mxDestroyArray( matlabMatrix );
}
void WMatlab::putVariable( const std::string& varName, const WValue<double>& vector )
{
size_t cols = 1;
size_t rows = vector.size();
mxArray* matlabMatrix = mxCreateDoubleMatrix( rows, cols, mxREAL );
// TODO(philips): do this by std::copy (we use c++)
memcpy( ( void* ) mxGetPr( matlabMatrix ), ( void* ) &vector[0], sizeof( double ) * cols * rows );
engPutVariable( m_engine, varName.c_str(), matlabMatrix );
mxDestroyArray( matlabMatrix );
}
void WMatlab::putVariable( const std::string& varName, const WVector3d& vector )
{
mxArray* matlabMatrix = mxCreateDoubleMatrix( 3, 1, mxREAL );
memcpy( ( void* ) mxGetPr( matlabMatrix ), ( void* ) &vector[0], sizeof( double ) * 3 );
engPutVariable( m_engine, varName.c_str(), matlabMatrix );
mxDestroyArray( matlabMatrix );
}
void WMatlab::putVariable( const std::string& varName, double val )
{
// mxArray* matlabMatrix = mxCreateDoubleMatrix( 1, 1, mxREAL );
mxArray* matlabMatrix = mxCreateDoubleScalar( val );
// double* matlabMatrixPtr = mxGetPr( matlabMatrix );
// ( *matlabMatrixPtr ) = val;
// matlabMatrixPtr = NULL;
engPutVariable( m_engine, varName.c_str(), matlabMatrix );
mxDestroyArray( matlabMatrix );
}
void WMatlab::putVariable( const std::string& varName, mxArray* matlabArray )
{
engPutVariable( m_engine, varName.c_str(), matlabArray );
mxDestroyArray( matlabArray );
}
WMatrix<double> WMatlab::getVariable( const std::string& varName ) const
{
// TODO(philips): is this with memcpy possible? - faster
// memcpy( (void *)mxGetPr(matlabMatrix), (void *)time, sizeof(time) );
mxArray* matlabMatrix = engGetVariable( m_engine, varName.c_str() );
WAssert( matlabMatrix, "Variable doesn't exist!" );
size_t rows = mxGetM( matlabMatrix );
size_t cols = mxGetN( matlabMatrix );
double* matlabMatrixPtr = mxGetPr( matlabMatrix );
WMatrix<double> result( rows, cols );
// TODO(philips): do this by std::copy (we use c++)
memcpy( ( void* ) &result[0], ( void* )mxGetPr( matlabMatrix ), sizeof( double ) * cols * rows );
// for ( size_t col = 0; col < cols; ++col )
// {
// for ( size_t row = 0; row < rows; ++row )
// {
// result( row, col ) = matlabMatrixPtr[ row * cols + col ];
// }
// }
matlabMatrixPtr = NULL;
mxDestroyArray( matlabMatrix );
return result;
}
double WMatlab::getScalarVariable( const std::string& varName ) const
{
mxArray* matlabMatrix = engGetVariable( m_engine, varName.c_str() );
WAssert( matlabMatrix, "Variable doesn't exist!" );
WAssert( mxGetM( matlabMatrix ) == 1, "Variable isn't a scalar!" );
WAssert( mxGetN( matlabMatrix ) == 1, "Variable isn't a scalar!" );
double result( mxGetScalar( matlabMatrix ) );
mxDestroyArray( matlabMatrix );
return result;
}
void WMatlab::evalString( const std::string& function )
{
int ret = engEvalString( m_engine, function.c_str() );
WAssert( !ret, "Evaluation failed." );
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WMATLAB_H
#define WMATLAB_H
#include <engine.h>
#include <string>
#include "core/common/math/WMatrix.h"
// TODO(philips): use forward declaration
// forward declaration
// typedef struct Engine;
/**
* Class to interfacing matlab from open walnut.
*/
class WMatlab
{
public:
/**
* Default constructor.
*/
explicit WMatlab( const std::string startCommand = "" );
/**
* Destructor.
*/
~WMatlab();
/**
*
*/
void putVariable( const std::string& varName, const WMatrix<double>& matrix );
/**
*
*/
void putVariable( const std::string& varName, const WValue<double>& vector );
/**
*
*/
void putVariable( const std::string& varName, const WVector3d& vector );
/**
*
*/
void putVariable( const std::string& varName, double val );
/**
*
*/
void putVariable( const std::string& varName, mxArray* matlabArray );
/**
*
*/
WMatrix<double> getVariable( const std::string& varName ) const;
/**
*
*/
double getScalarVariable( const std::string& varName ) const;
/**
*
*/
void evalString( const std::string& function );
Engine* getEngine();
protected:
private:
Engine* m_engine;
};
inline Engine* WMatlab::getEngine()
{
return m_engine;
}
#endif // WMATLAB_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WMATLAB_TEST_H
#define WMATLAB_TEST_H
#include <cxxtest/TestSuite.h>
#include "../WMatlab.h"
/**
* Unit tests for the matlab wrapper.
*/
class WMatlabTest : public CxxTest::TestSuite
{
public:
/**
*
*/
void testFoundMatlab( void )
{
TS_ASSERT_THROWS_NOTHING( WMatlab() );
}
/**
*
*/
void testInAndOut( void )
{
WMatlab matlab;
WMatrix<double> input( 3, 4 );
for ( size_t i = 0; i < input.getNbRows(); ++i )
{
for ( size_t j = 0; j < input.getNbCols(); ++j )
{
input( i, j ) = static_cast<double>( i * 10 + j );
}
}
matlab.putVariable( "A", input );
WMatrix<double> output( matlab.getVariable( "A" ) );
TS_ASSERT_EQUALS( input, output );
}
/**
*
*/
void testFunctionEvaluation( void )
{
WMatlab matlab;
WMatrix<double> A( 3, 4 );
WMatrix<double> B( 3, 4 );
for ( size_t i = 0; i < A.getNbRows(); ++i )
{
for ( size_t j = 0; j < A.getNbCols(); ++j )
{
A( i, j ) = static_cast<double>( i * 10 + j );
B( i, j ) = static_cast<double>( j * 10 + i );
}
}
matlab.putVariable( "A", A );
matlab.putVariable( "B", B );
matlab.evalString( "C=A+B" );
WMatrix<double> C( matlab.getVariable( "C" ) );
TS_ASSERT_EQUALS( A+B, C );
}
};
#endif // WMATLAB_TEST_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment