From: <bro...@us...> - 2009-10-19 11:57:32
|
Revision: 213 http://mevislabmodules.svn.sourceforge.net/mevislabmodules/?rev=213&view=rev Author: broersen Date: 2009-10-19 11:57:24 +0000 (Mon, 19 Oct 2009) Log Message: ----------- ENH: -Reworked copyInputXMarkerToMatlab and some other code Modified Paths: -------------- trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.cpp trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.h Modified: trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.cpp =================================================================== --- trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.cpp 2009-10-15 11:53:22 UTC (rev 212) +++ trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.cpp 2009-10-19 11:57:24 UTC (rev 213) @@ -126,7 +126,7 @@ (_stringFld[4] = fields->addString("string4"))->setStringValue(""); (_stringNameFld[5] = fields->addString("stringName5"))->setStringValue("string5"); (_stringFld[5] = fields->addString("string5"))->setStringValue(""); - + // Set vector name and value fields. (_vectorNameFld[0] = fields->addString("vectorName0"))->setStringValue("vector0"); (_vectorFld[0] = fields->addVec4f("vector0"))->setStringValue("0 0 0 0"); @@ -149,9 +149,6 @@ (_matrixNameFld[2] = fields->addString("matrixName2"))->setStringValue("matrix2"); (_matrixFld[2] = fields->addMatrix("matrix2"))->setStringValue("1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1"); - //! Reactivate calls of handleNotification on field changes. - handleNotificationOn(); - m_pEngine = engOpen(NULL); if ( !_checkMatlabIsStarted() ) @@ -178,6 +175,9 @@ engGetVisible(m_pEngine, &vis); (_showSessionWindowFld = fields->addBool("showSessionWindow"))->setBoolValue(vis); } + + //! Reactivate calls of handleNotification on field changes. + handleNotificationOn(); } @@ -225,9 +225,9 @@ } } - // Update output only if autoapply is enabled. - if ( ((field == getInField(0))||(field == getInField(1))||(field == getInField(2))) && (_autoCalculationFld->isOn()) - || (field == _calculateFld) ) { + // Update output on an update or if autoapply is enabled. + if( (field == _calculateFld) || + _autoCalculationFld->isOn() && ((field == getInField(0))||(field == getInField(1))||(field == getInField(2))) ) { // Check if Matlab is started. if (!_checkMatlabIsStarted()) { _statusFld->setStringValue("Cannot find Matlab engine!"); @@ -245,8 +245,14 @@ // Execute Matlab script only when the string is valid if(validScriptString) { - // Copy input XMarker to matlab. - _copyInputXMarkerToMatlab(); + if( _inputXMarkerListFld->getBaseValue() != NULL ) { + // Check if a valid XMarkerList is attached to the input. + if( _inputXMarkerListFld->isValidValue() && ML_BASE_IS_A(_inputXMarkerListFld->getBaseValue(), XMarkerList) ) { + // Copy input XMarkerList to Matlab. + _copyInputXMarkerToMatlab(); + } + } + // Copy input image data to matlab. _copyInputImageDataToMatlab(); // Copy scalar values to matlab. @@ -258,7 +264,6 @@ // Copy matrix values to matlab. _copyInputMatricesToMatlab(); - // Insert at the end of the script variable to proof execution status // and run the script in Matlab evaluateString += "\nmevmatscr=1"; @@ -376,7 +381,7 @@ return; } - // Get variable name in the matlab workspace of the output image + // Get variable name in the Matlab workspace of the output image std::string outname = _outDataNameFld[outIndex]->getStringValue(); mxArray *m_pImage = engGetVariable(m_pEngine, outname.c_str()); @@ -417,7 +422,7 @@ } mxDestroyArray(m_pImage); m_pImage = NULL; - // Get min and max values in matlab workspace and set them in MeVisLab + // Get min and max values in Matlab workspace and set them in MeVisLab std::ostringstream minmaxCommand; minmaxCommand << "mevtmpminval = min(" << outname << "(:));" << "mevtmpmaxval = max(" << outname << "(:));"; engEvalString(m_pEngine, minmaxCommand.str().c_str()); @@ -434,9 +439,7 @@ mxDestroyArray(minVal); minVal = NULL; mxDestroyArray(maxVal); maxVal = NULL; engEvalString(m_pEngine, "clear mevtmpminval mevtmpmaxval"); - } - else - { + } else { getOutImg(outIndex)->setOutOfDate(); getOutImg(outIndex)->setStateInfo("Cannot set output size, because variable could not be found in Matlab workspace.",ML_BAD_DATA_TYPE); } @@ -478,7 +481,7 @@ // Check if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "calcOutImageProps(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "calcOutImageProps(): Cannot find Matlab engine!" << std::endl << std::flush; return; } @@ -512,7 +515,7 @@ else { // Throw error, if no data available. - ML_PRINT_ERROR("MatlabScriptWrapper::calcOutSubImage()", ML_BAD_INPUT_IMAGE_POINTER, "Cannot copy from matlab data."); + ML_PRINT_ERROR("MatlabScriptWrapper::calcOutSubImage()", ML_BAD_INPUT_IMAGE_POINTER, "Cannot copy from Matlab data."); } } @@ -520,12 +523,116 @@ // Internal (private) methods. ////////////////////////////////////////////////////////////////////// +//! Loads matlab script from a file, pastes it into script field and saves user written script. +bool MatlabScriptWrapper::_loadMatlabScriptFromFile(std::string& evaluateString) +{ + // Clear input string + evaluateString.clear(); + + // Temporary string for reading input. + std::ostringstream tmpString; + + // Update script window if new script chosen. + std::string pathString = _matlabScriptPathFld->getStringValue(); + + ML_TRY + { + if(pathString.empty()) { + _statusFld->setStringValue("Script path is empty."); + return false; + } + else + { + // File to open. + std::ifstream dat; + dat.open(pathString.c_str()); + if(dat.fail()) { + // Throw error message if file couldn't be opened. + _statusFld->setStringValue("Cannot find .m-file!"); + return false; + } + + // Read script line by line from file. + std::string line; + while(!dat.eof()) + { + getline(dat, line); + tmpString << line << "\n"; + } + + // Close file + dat.close(); + + // Get string + evaluateString = tmpString.str(); + } + } + ML_CATCH_RETHROW; + + return true; +} + +//! Check if Matlab is started. +bool MatlabScriptWrapper::_checkMatlabIsStarted() +{ + if(0 == engEvalString(m_pEngine, "mevTestIfMatlabIsRunning=3.14")) + { + engEvalString(m_pEngine, "clear mevTestIfMatlabIsRunning"); + return true; + } + else + { + return false; + } +} + +void MatlabScriptWrapper::_clearAllVariables() +{ + std::ostringstream clearString; + clearString << "clear "; + + // Clear scalars + for(int i=0; i<6; i++) { + clearString << _scalarNameFld[i]->getStringValue() << " "; + } + + // Clear strings + for(int i=0; i<6; i++) { + clearString << _stringNameFld[i]->getStringValue() << " "; + } + + // Clear matrices + for(int i=0; i<3; i++) { + clearString << _matrixNameFld[i]->getStringValue() << " "; + } + + + // Clear input images + for(int i=0; i<3; i++) { + clearString << _inDataNameFld[i]->getStringValue() << " "; + } + + // Clear output images + for(int i=0; i<3; i++) { + clearString << _outDataNameFld[i]->getStringValue() << " "; + } + + // Clear input XMarker data + clearString << _inXMarkerNameFld->getStringValue() << " "; + + // Clear output XMarker data + clearString << _outXMarkerNameFld->getStringValue(); + + // Evaluate the string in Matlab + engEvalString(m_pEngine, clearString.str().c_str()); +} + //! Copy input image data to matlab. void MatlabScriptWrapper::_copyInputImageDataToMatlab() { if (!_checkMatlabIsStarted()) { - std::cerr << "_copyInputImageDataToMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_copyInputImageDataToMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } @@ -568,7 +675,7 @@ //MLuint32 inDataSize = inImg->getBoxFromImgExt().getExt().getStrides().u; const MLuint32 inDataSize = imgSize.x*imgSize.y*imgSize.z*imgSize.c*imgSize.t*imgSize.u; - // Set matlab image extent. + // Set Matlab image extent. const mwSize insizesArray[6] = {imgSize.x, imgSize.y, imgSize.z, imgSize.c, imgSize.t, imgSize.u}; // Copy different types of images from MeVisLab. @@ -592,12 +699,12 @@ // Create numeric array mxArray *m_pImage = mxCreateNumericArray(6, insizesArray, inputClass, mxREAL); - // Copy data to matlab array. + // Copy data to Matlab array. memcpy((void*)mxGetPr(m_pImage), data, inDataSize*elementSize); // Get input names from gui. std::string inputName = _inDataNameFld[i]->getStringValue(); - // Write data to matlab. + // Write data to Matlab. engPutVariable(m_pEngine, inputName.c_str(), m_pImage); mxDestroyArray(m_pImage); m_pImage = NULL; @@ -612,111 +719,47 @@ //! Copy input XMarkerList to matlab. void MatlabScriptWrapper::_copyInputXMarkerToMatlab() { - // Proof if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_copyInputXMarkerToMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_copyInputXMarkerToMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } + // Get input list. + XMarkerList inputXMarkerList = *((XMarkerList*)_inputXMarkerListFld->getBaseValue()); + // Internal loop. size_t i = 0; - // Get names from GUI. - std::ostringstream inXMarkerStr, outXMarkerStr; - inXMarkerStr << _inXMarkerNameFld->getStringValue(); - outXMarkerStr << _outXMarkerNameFld->getStringValue(); - // Combine string. - std::ostringstream setInXMarkerStr, setOutXMarkerStr; - setInXMarkerStr << inXMarkerStr.str() << "=struct('pos',[],'vec',[],'type',[])"; - setOutXMarkerStr << outXMarkerStr.str() << "=struct('pos',[],'vec',[],'type',[])"; + // Get input list name from GUI. + std::string inXMarkerStr = _inXMarkerNameFld->getStringValue(); - // Input XMarkerList transfered to matlab structure. - engEvalString(m_pEngine, setInXMarkerStr.str().c_str()); - engEvalString(m_pEngine, setOutXMarkerStr.str().c_str()); + // Strings to evaluate. + std::ostringstream setPos, setVec, setType; + setPos << inXMarkerStr.c_str() <<".pos=["; + setVec << inXMarkerStr.c_str() << ".vec=["; + setType << inXMarkerStr.c_str() << ".type=["; - // Check if a valid XMarkerList is attached to the input. - if( _inputXMarkerListFld->isValidValue() && ML_BASE_IS_A(_inputXMarkerListFld->getBaseValue(), XMarkerList)) + // Get XMarkerList size and go through all list step by step. + const size_t listSize = inputXMarkerList.size(); + for(i = 0; i < listSize; i++) { - // Get input list. - XMarkerList inputXMarkerList = *((XMarkerList*)_inputXMarkerListFld->getBaseValue()); + XMarker marker = inputXMarkerList[i]; - // Strings to evaluate. - std::ostringstream setPos, setVec, setType; - setPos << inXMarkerStr.str() <<".pos=["; - setVec << inXMarkerStr.str() << ".vec=["; - setType << inXMarkerStr.str() << ".type=["; - - // Get XMarkerList size and go through all list step by step. - const size_t listSize = inputXMarkerList.size(); - for(i = 0; i < listSize; i++) - { - XMarker marker = inputXMarkerList[i]; - - // Write pos, vec and type to strings. - setPos<<std::dec<<marker.x()<<","<<std::dec<<marker.y()<<","<<std::dec<<marker.z() - <<","<<std::dec<<marker.c()<<","<<std::dec<<marker.t()<<","<<std::dec<<marker.u()<<";"; - setVec<<std::dec<<marker.vx()<<","<<std::dec<<marker.vy()<<","<<std::dec<<marker.vz()<<";"; - setType<<std::dec<<marker.type<<";"; - } - - setPos<<"]"; - setVec<<"]"; - setType<<"]"; - // Put XMarkerList into matlab structure. - engEvalString(m_pEngine, setPos.str().c_str()); - engEvalString(m_pEngine, setVec.str().c_str()); - engEvalString(m_pEngine, setType.str().c_str()); + // Write pos, vec and type to strings. + setPos<<std::dec<<marker.x()<<","<<std::dec<<marker.y()<<","<<std::dec<<marker.z() + <<","<<std::dec<<marker.c()<<","<<std::dec<<marker.t()<<","<<std::dec<<marker.u()<<";"; + setVec<<std::dec<<marker.vx()<<","<<std::dec<<marker.vy()<<","<<std::dec<<marker.vz()<<";"; + setType<<std::dec<<marker.type<<";"; } -} -//! Loads matlab script from a file, pastes it into script field and saves user written script. -bool MatlabScriptWrapper::_loadMatlabScriptFromFile(std::string& evaluateString) -{ - // Clear input string - evaluateString.clear(); - - // Temporary string for reading input. - std::ostringstream tmpString; - - // Update script window if new script chosen. - std::string pathString = _matlabScriptPathFld->getStringValue(); - - ML_TRY - { - if(pathString.empty()) { - _statusFld->setStringValue("Script path is empty."); - return false; - } - else - { - // File to open. - std::ifstream dat; - dat.open(pathString.c_str()); - if(dat.fail()) { - // Throw error message if file couldn't be opened. - _statusFld->setStringValue("Cannot find .m-file!"); - return false; - } - - // Read script line by line from file. - std::string line; - while(!dat.eof()) - { - getline(dat, line); - tmpString << line << "\n"; - } - - // Close file - dat.close(); - - // Get string - evaluateString = tmpString.str(); - } - } - ML_CATCH_RETHROW; - - return true; + setPos<<"]"; + setVec<<"]"; + setType<<"]"; + // Put XMarkerList into matlab structure. + engEvalString(m_pEngine, setPos.str().c_str()); + engEvalString(m_pEngine, setVec.str().c_str()); + engEvalString(m_pEngine, setType.str().c_str()); } //! Gets XMarkerList from Matlab and copies results into output XMarkerList. @@ -728,12 +771,12 @@ // Check if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_getXMarkerBackFromMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_getXMarkerBackFromMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } // Internal loop. size_t i=0, j=0; - // Get names form gui. + // Get names from GUI. std::string outXMarkerStr = _outXMarkerNameFld->getStringValue(); // Compose temp string to execute in matlab. std::ostringstream executeStr; @@ -844,7 +887,7 @@ // Check if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_getScalarsBackFromMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_getScalarsBackFromMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } @@ -870,7 +913,7 @@ // Check if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_copyInputStringsToMatlab(): Cannot find matlab engine!" << std::endl << std::flush; + std::cerr << "_copyInputStringsToMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } // Internal loop. @@ -892,7 +935,7 @@ // Check if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_getStringsBackFromMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_getStringsBackFromMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } @@ -923,7 +966,7 @@ // Proof if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_copyInputVectorsToMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_copyInputVectorsToMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } // Internal loop. @@ -949,7 +992,7 @@ // Check if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_getVectorssBackFromMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_getVectorssBackFromMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } @@ -975,7 +1018,7 @@ // Proof if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_copyInputMatricesToMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_copyInputMatricesToMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } // Internal loop. @@ -1004,7 +1047,7 @@ // Check if Matlab is started. if (!_checkMatlabIsStarted()) { - std::cerr << "_getMatricesBackFromMatlab(): Cannot finding matlab engine!" << std::endl << std::flush; + std::cerr << "_getMatricesBackFromMatlab(): Cannot find Matlab engine!" << std::endl << std::flush; return; } @@ -1027,59 +1070,4 @@ temp = NULL; } -//! Check if Matlab is started. -bool MatlabScriptWrapper::_checkMatlabIsStarted() -{ - if(0 == engEvalString(m_pEngine, "mevTestIfMatlabIsRunning=3.14")) - { - engEvalString(m_pEngine, "clear mevTestIfMatlabIsRunning"); - return true; - } - else - { - return false; - } -} - -void MatlabScriptWrapper::_clearAllVariables() -{ - std::ostringstream clearString; - clearString << "clear "; - - // Clear scalars - for(int i=0; i<6; i++) { - clearString << _scalarNameFld[i]->getStringValue() << " "; - } - - // Clear strings - for(int i=0; i<6; i++) { - clearString << _stringNameFld[i]->getStringValue() << " "; - } - - // Clear matrices - for(int i=0; i<3; i++) { - clearString << _matrixNameFld[i]->getStringValue() << " "; - } - - - // Clear input images - for(int i=0; i<3; i++) { - clearString << _inDataNameFld[i]->getStringValue() << " "; - } - - // Clear output images - for(int i=0; i<3; i++) { - clearString << _outDataNameFld[i]->getStringValue() << " "; - } - - // Clear input XMarker data - clearString << _inXMarkerNameFld->getStringValue() << " "; - - // Clear output XMarker data - clearString << _outXMarkerNameFld->getStringValue(); - - // Evaluate the string in Matlab - engEvalString(m_pEngine, clearString.str().c_str()); -} - ML_END_NAMESPACE Modified: trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.h =================================================================== --- trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.h 2009-10-15 11:53:22 UTC (rev 212) +++ trunk/Community/General/Sources/ML/MLMatlabScriptWrapper/mlMatlabScriptWrapper.h 2009-10-19 11:57:24 UTC (rev 213) @@ -77,11 +77,11 @@ //! \return Region of input image needed to compute the region \c outSubImgBox on output \c outIndex. virtual SubImgBox calcInSubImageBox (int inIndex, const SubImgBox &outSubImgBox, int outIndex); - //! Request input image in fixed datatypes according to user choice + //! Request input image in basic datatypes according to user choice //! \param inIndex The input of which the datatype shall be set. //! \param props The properties of input image upon calling of method. //! \param outIndex The index of the output image. - virtual void calcInSubImageProps(int /*inIndex*/, InSubImageProps &/*props*/, int /*outIndex*/); + virtual void calcInSubImageProps(int inIndex, InSubImageProps &/*props*/, int /*outIndex*/); //! Calculates page \c outSubImg of output image with index \c outIndex by using \c inSubimgs. //! \param outSubImg The subimage of output image \c outIndex calculated from \c inSubImgs. @@ -98,29 +98,29 @@ // void calcOutSubImage (TSubImg<T> *outSubImg, int outIndex, TSubImg<T> *inSubImg1); // virtual void calcOutSubImage (SubImg *outSubImg, int outIndex, SubImg *inSubImg); - //@} private: // ---------------------------------------------------------- //@{ \name Module internal methods declarations // ---------------------------------------------------------- + //! Loads matlab script from a file, pastes it into script field and saves user written script. + bool _loadMatlabScriptFromFile(std::string& evaluateString); + //! Check if Matlab is started. + bool _checkMatlabIsStarted(); + //! Clear all variables that have been put in the Matlab workspace. + void _clearAllVariables(); + //! Copies input image data into Matlab. void _copyInputImageDataToMatlab(); //! Copies input XMarkerList into Matlab. void _copyInputXMarkerToMatlab(); - //! Loads matlab script from a file, pastes it into script field and saves user written script. - bool _loadMatlabScriptFromFile(std::string& evaluateString); - //! Gets XMarkerList from Matlab and copies results into output XMarkerList. + //! Copies XMarkerList from Matlab and copies results into output XMarkerList. void _getXMarkerBackFromMatlab(); //! Copies scalar values to matlab. void _copyInputScalarsToMatlab(); //! Copies scalar values from matlab. void _getScalarsBackFromMatlab(); - //! Check if Matlab is started. - bool _checkMatlabIsStarted(); - //! Clear all variables that have been put in the Matlab workspace. - void _clearAllVariables(); //! Copies string values to matlab. void _copyInputStringsToMatlab(); //! Copies string values from matlab. @@ -133,6 +133,7 @@ void _copyInputMatricesToMatlab(); //! Copies matrix values from matlab. void _getMatricesBackFromMatlab(); + // ---------------------------------------------------------- //@{ \name Module field declarations // ---------------------------------------------------------- @@ -163,7 +164,7 @@ StringField *_inXMarkerNameFld; StringField *_outXMarkerNameFld; //@} - + //! If true, the module updates on field changes. BoolField* _autoCalculationFld; //! Status messages. @@ -172,27 +173,23 @@ NotifyField* _calculateFld; //! Restart Matlab button. NotifyField* _restartMatlabFld; - //{@ Scalar float values. DoubleField *_scalarFld[6]; StringField *_scalarNameFld[6]; //@} - - //{@ String values. - StringField *_stringFld[6]; - StringField *_stringNameFld[6]; - //@} - - //{@ Matrix values. + //{@ Vector values. Vec4fField *_vectorFld[6]; StringField *_vectorNameFld[6]; //@} - //{@ Matrix values. MatrixField *_matrixFld[3]; StringField *_matrixNameFld[3]; //@} + //{@ String values. + StringField *_stringFld[6]; + StringField *_stringNameFld[6]; + //@} //! Implements interface for the runtime type system of the ML. ML_BASEOP_CLASS_HEADER(MatlabScriptWrapper) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |