5252#include < Modules/Legacy/Matlab/Interface/Services/MatlabEngine.h>
5353#include < boost/thread.hpp>
5454#include < Core/Services/FileTransferClient.h>
55+ #include < Core/Datatypes/Matrix.h>
5556
5657#if 0
5758
@@ -145,7 +146,7 @@ namespace MatlabImpl
145146 namespace Matlab {
146147 namespace Interface {
147148
148- class InterfaceWithMatlabImpl // : public ServiceBase
149+ class InterfaceWithMatlabImpl : public ServiceBase
149150 {
150151 public:
151152 explicit InterfaceWithMatlabImpl (InterfaceWithMatlab* module ) :
@@ -168,12 +169,11 @@ namespace MatlabImpl
168169 virtual void presave();
169170 virtual void tcl_command(GuiArgs& args, void* userdata);
170171
171-
172+ # endif
172173 static matlabarray::mitype convertdataformat (const std::string& dataformat);
173174 static std::string totclstring (const std::string& instring);
174175 std::vector<std::string> converttcllist (const std::string& str);
175176
176- #endif
177177 void update_status (const std::string& text);
178178
179179 bool open_matlab_engine ();
@@ -216,7 +216,7 @@ namespace MatlabImpl
216216 GuiString output_string_name_;
217217 GuiString configfile_;
218218
219-
219+ # endif
220220 // Fields per port
221221 std::vector<std::string> input_matrix_name_list_;
222222 std::vector<std::string> input_matrix_name_list_old_;
@@ -250,16 +250,16 @@ namespace MatlabImpl
250250 std::string matlab_code_list_;
251251
252252 // Ports for input and output
253- std::string input_matrix_matfile_[NUM_MATRIX_PORTS] ;
254- std::string input_field_matfile_[NUM_FIELD_PORTS] ;
255- std::string input_nrrd_matfile_[NUM_NRRD_PORTS] ;
256- std::string input_string_matfile_[NUM_STRING_PORTS] ;
257-
258- std::string output_matrix_matfile_[NUM_MATRIX_PORTS] ;
259- std::string output_field_matfile_[NUM_FIELD_PORTS] ;
260- std::string output_nrrd_matfile_[NUM_NRRD_PORTS] ;
261- std::string output_string_matfile_[NUM_STRING_PORTS] ;
262-
253+ std::vector<std:: string> input_matrix_matfile_;
254+ std::vector<std:: string> input_field_matfile_;
255+ std::vector<std:: string> input_nrrd_matfile_;
256+ std::vector<std:: string> input_string_matfile_;
257+
258+ std::vector<std:: string> output_matrix_matfile_;
259+ std::vector<std:: string> output_field_matfile_;
260+ std::vector<std:: string> output_nrrd_matfile_;
261+ std::vector<std:: string> output_string_matfile_;
262+ # if 0
263263 // Internet connectivity stuff
264264 GuiString inet_address_;
265265 GuiString inet_port_;
@@ -465,7 +465,6 @@ namespace MatlabImpl
465465 start_matlab_(context->subVar("start-matlab")),
466466 matlab_timeout_(context->subVar("matlab-timeout")),
467467 matlab_timeout_old_(180),
468- need_file_transfer_(false)
469468 {
470469#ifdef USE_MATLAB_ENGINE_LIBRARY
471470 engine_ = 0;
@@ -525,67 +524,63 @@ namespace MatlabImpl
525524 LOG_DEBUG (module_->get_id ().id_ << " UpdateStatus \" " << text << " \" " );
526525 }
527526
527+ matlabarray::mitype InterfaceWithMatlabImpl::convertdataformat (const std::string& dataformat)
528+ {
529+ matlabarray::mitype type = matlabarray::miUNKNOWN;
530+ if (dataformat == " same as data" ) { type = matlabarray::miSAMEASDATA; }
531+ else if (dataformat == " double" ) { type = matlabarray::miDOUBLE; }
532+ else if (dataformat == " single" ) { type = matlabarray::miSINGLE; }
533+ else if (dataformat == " uint64" ) { type = matlabarray::miUINT64; }
534+ else if (dataformat == " int64" ) { type = matlabarray::miINT64; }
535+ else if (dataformat == " uint32" ) { type = matlabarray::miUINT32; }
536+ else if (dataformat == " int32" ) { type = matlabarray::miINT32; }
537+ else if (dataformat == " uint16" ) { type = matlabarray::miUINT16; }
538+ else if (dataformat == " int16" ) { type = matlabarray::miINT16; }
539+ else if (dataformat == " uint8" ) { type = matlabarray::miUINT8; }
540+ else if (dataformat == " int8" ) { type = matlabarray::miINT8; }
541+ return (type);
542+ }
543+
544+ // converttcllist:
545+ // converts a TCL formatted list into a STL array
546+ // of strings
547+
548+ std::vector<std::string> InterfaceWithMatlabImpl::converttcllist (const std::string& str)
549+ {
550+ std::string result;
551+ std::vector<std::string> list;
528552#if 0
529- matlabarray::mitype InterfaceWithMatlab::convertdataformat(const std::string& dataformat)
530- {
531- matlabarray::mitype type = matlabarray::miUNKNOWN;
532- if (dataformat == "same as data") { type = matlabarray::miSAMEASDATA; }
533- else if (dataformat == "double") { type = matlabarray::miDOUBLE; }
534- else if (dataformat == "single") { type = matlabarray::miSINGLE; }
535- else if (dataformat == "uint64") { type = matlabarray::miUINT64; }
536- else if (dataformat == "int64") { type = matlabarray::miINT64; }
537- else if (dataformat == "uint32") { type = matlabarray::miUINT32; }
538- else if (dataformat == "int32") { type = matlabarray::miINT32; }
539- else if (dataformat == "uint16") { type = matlabarray::miUINT16; }
540- else if (dataformat == "int16") { type = matlabarray::miINT16; }
541- else if (dataformat == "uint8") { type = matlabarray::miUINT8; }
542- else if (dataformat == "int8") { type = matlabarray::miINT8; }
543- return (type);
544- }
553+ int lengthlist = 0;
545554
546- // converttcllist:
547- // converts a TCL formatted list into a STL array
548- // of strings
555+ // Yeah, it is TCL dependent:
556+ // TCL::llength determines the length of the list
557+ TCLInterface::eval("llength { "+str + " }",result);
558+ std::istringstream iss(result);
559+ iss >> lengthlist;
560+ if (lengthlist < 0) return(list);
549561
550- std::vector<std::string> InterfaceWithMatlab::converttcllist(const std::string& str)
562+ list.resize(lengthlist);
563+ for (int p = 0;p<lengthlist;p++)
551564 {
552- std::string result;
553- std::vector<std::string> list(0);
554- int lengthlist = 0;
555-
556- // Yeah, it is TCL dependent:
557- // TCL::llength determines the length of the list
558- TCLInterface::eval("llength { "+str + " }",result);
559- std::istringstream iss(result);
560- iss >> lengthlist;
561- if (lengthlist < 0) return(list);
562-
563- list.resize(lengthlist);
564- for (int p = 0;p<lengthlist;p++)
565- {
566- std::ostringstream oss;
567- // TCL dependency:
568- // TCL::lindex retrieves the p th element from the list
569- oss << "lindex { " << str << " } " << p;
570- TCLInterface::eval(oss.str(),result);
571- list[p] = result;
572- }
573- return(list);
565+ std::ostringstream oss;
566+ // TCL dependency:
567+ // TCL::lindex retrieves the p th element from the list
568+ oss << "lindex { " << str << " } " << p;
569+ TCLInterface::eval(oss.str(),result);
570+ list[p] = result;
574571 }
575572#endif
573+ return (list);
574+ }
576575
577576bool InterfaceWithMatlabImpl::synchronise_input ()
578577{
579- #if 0
580- TCLInterface::execute(get_id()+" Synchronise");
581- get_ctx()->reset();
582-
583578 std::string str;
584579 str = input_matrix_name_.get (); input_matrix_name_list_ = converttcllist (str);
585580 str = input_matrix_type_.get (); input_matrix_type_list_ = converttcllist (str);
586581 str = input_matrix_array_.get (); input_matrix_array_list_ = converttcllist (str);
587582 str = output_matrix_name_.get (); output_matrix_name_list_ = converttcllist (str);
588-
583+ # if 0
589584 str = input_field_name_.get(); input_field_name_list_ = converttcllist(str);
590585 str = input_field_array_.get(); input_field_array_list_ = converttcllist(str);
591586 str = output_field_name_.get(); output_field_name_list_ = converttcllist(str);
@@ -597,10 +592,9 @@ bool InterfaceWithMatlabImpl::synchronise_input()
597592
598593 str = input_string_name_.get(); input_string_name_list_ = converttcllist(str);
599594 str = output_string_name_.get(); output_string_name_list_ = converttcllist(str);
600-
601- TCLInterface::execute(get_id() + " update_text"); // update matlab_code_ before use.
602- matlab_code_list_ = matlab_code_.get();
603595#endif
596+ matlab_code_list_ = matlab_code_.get ();
597+
604598 return (true );
605599}
606600
@@ -1042,17 +1036,17 @@ void InterfaceWithMatlab::execute()
10421036 int sessionn = packet->getparam1 ();
10431037 matlab_engine_->setsession (sessionn);
10441038
1045- // std::string sharehomedir = "yes";
1046- // if (need_file_transfer_) sharehomedir = "no";
1039+ std::string sharehomedir = " yes" ;
1040+ if (need_file_transfer_) sharehomedir = " no" ;
10471041
10481042 std::ostringstream statusStr;
10491043 statusStr << " Matlab engine running\n\n matlabengine version: " << matlab_engine_->getversion ()
10501044 << " \n matlabengine address: " << matlab_engine_->getremoteaddress ()
1051- << " \n matlabengine session: " + matlab_engine_->getsession ();
1052- // << "\nmatlabengine filetransfer version :" << file_transfer_->getversion()
1053- // << "\nshared home directory: " << sharehomedir
1054- // << "\nlocal temp directory: " << file_transfer_->local_file("")
1055- // << "\nremote temp directory: " << file_transfer_->remote_file("") + "\n";
1045+ << " \n matlabengine session: " + matlab_engine_->getsession ()
1046+ << " \n matlabengine filetransfer version :" << file_transfer_->getversion ()
1047+ << " \n shared home directory: " << sharehomedir
1048+ << " \n local temp directory: " << file_transfer_->local_file (" " )
1049+ << " \n remote temp directory: " << file_transfer_->remote_file (" " ) + " \n " ;
10561050 auto status = statusStr.str ();
10571051#else
10581052 std::string status = " InterfaceWithMatlab engine running\n " ;
@@ -1358,30 +1352,29 @@ void InterfaceWithMatlab::execute()
13581352 std::ofstream m_file;
13591353 std::string loadcmd;
13601354
1361- mfile_ = std::string ( " scirun_code.m" ) ;
1355+ mfile_ = " scirun_code.m" ;
13621356 std::string filename = file_transfer_->local_file (mfile_);
13631357
13641358 m_file.open (filename.c_str (),std::ios::out);
13651359
1366- for (int p = 0 ; p < NUM_MATRIX_PORTS; p++)
1360+ auto matrices = module_->getRequiredDynamicInputs (module_->InputMatrix );
1361+ input_matrix_matfile_.clear ();
1362+ input_matrix_name_list_.resize (matrices.size ());
1363+ input_matrix_type_list_.resize (matrices.size ());
1364+ input_matrix_array_list_.resize (matrices.size ());
1365+ input_matrix_name_list_old_.resize (matrices.size ());
1366+ input_matrix_type_list_old_.resize (matrices.size ());
1367+ input_matrix_array_list_old_.resize (matrices.size ());
1368+ input_matrix_generation_old_.resize (matrices.size ());
1369+
1370+ for (const auto & matrix : matrices)
13671371 {
1368- MatrixHandle handle;
1369- if (!(get_input_handle (port,handle,false ))) { port++; continue ; }
1370- port++;
1371-
1372- // if there is no data
1373- if (handle.get_rep () == 0 )
1374- {
1375- // we do not need the old file any more so delete it
1376- input_matrix_matfile_[p] = " " ;
1377- continue ;
1378- }
13791372 // if the data as the same before
13801373 // do nothing
1381- if ((input_matrix_name_list_[p]== input_matrix_name_list_old_[p]) &&
1382- (input_matrix_type_list_[p]== input_matrix_type_list_old_[p]) &&
1383- (input_matrix_array_list_[p]== input_matrix_array_list_old_[p]) &&
1384- (handle-> generation == input_matrix_generation_old_[p ]))
1374+ if ((input_matrix_name_list_[port] == input_matrix_name_list_old_[port]) &&
1375+ (input_matrix_type_list_[port] == input_matrix_type_list_old_[port]) &&
1376+ (input_matrix_array_list_[port] == input_matrix_array_list_old_[port]) &&
1377+ (matrix-> id () == input_matrix_generation_old_[port ]))
13851378 {
13861379 // this one was not created again
13871380 // hence we do not need to translate it again
@@ -1393,21 +1386,21 @@ void InterfaceWithMatlab::execute()
13931386
13941387 // Create a new filename for the input matrix
13951388 std::ostringstream oss;
1396- oss << " input_matrix" << p << " .mat" ;
1397- input_matrix_matfile_[p] = oss.str ();
1389+ oss << " input_matrix" << port << " .mat" ;
1390+ input_matrix_matfile_. push_back ( oss.str () );
13981391
13991392 matlabfile mf;
14001393 matlabarray ma;
14011394
1402- mf.open (file_transfer_->local_file (input_matrix_matfile_[p ])," w" );
1395+ mf.open (file_transfer_->local_file (input_matrix_matfile_[port ]), " w" );
14031396 mf.setheadertext (" InterfaceWithMatlab V5 compatible file generated by SCIRun [module InterfaceWithMatlab version 1.3]" );
14041397
1405- matlabconverter translate (dynamic_cast <SCIRun::ProgressReporter *>( this ));
1398+ matlabconverter translate (module_-> getLogger ( ));
14061399 translate.converttostructmatrix ();
1407- if (input_matrix_array_list_[p ] == " numeric array" )
1400+ if (input_matrix_array_list_[port ] == " numeric array" )
14081401 translate.converttonumericmatrix ();
1409- translate.setdatatype (convertdataformat (input_matrix_type_list_[p ]));
1410- translate.sciMatrixTOmlArray (handle, ma);
1402+ translate.setdatatype (convertdataformat (input_matrix_type_list_[port ]));
1403+ translate.sciMatrixTOmlArray (matrix, ma);
14111404
14121405 mf.putmatlabarray (ma,input_matrix_name_list_[p]);
14131406 mf.close ();
@@ -1423,18 +1416,19 @@ void InterfaceWithMatlab::execute()
14231416 {
14241417 module_->error (" InterfaceWithMatlab: Could not transfer file" );
14251418 std::string err = " Error :" + file_transfer_->geterror ();
1426- error (err);
1419+ module_-> error (err);
14271420 return (false );
14281421 }
14291422 }
14301423
1431- input_matrix_type_list_old_[p ] = input_matrix_type_list_[p ];
1432- input_matrix_array_list_old_[p ] = input_matrix_array_list_[p ];
1433- input_matrix_name_list_old_[p ] = input_matrix_name_list_[p ];
1434- input_matrix_generation_old_[p ] = handle-> generation ;
1424+ input_matrix_type_list_old_[port ] = input_matrix_type_list_[port ];
1425+ input_matrix_array_list_old_[port ] = input_matrix_array_list_[port ];
1426+ input_matrix_name_list_old_[port ] = input_matrix_name_list_[port ];
1427+ input_matrix_generation_old_[port ] = matrix-> id () ;
14351428
1429+ port++;
14361430 }
1437-
1431+ # if 0
14381432 for (int p = 0; p < NUM_FIELD_PORTS; p++)
14391433 {
14401434 FieldHandle handle;
@@ -1505,7 +1499,8 @@ void InterfaceWithMatlab::execute()
15051499 input_field_name_list_old_[p] = input_field_name_list_[p];
15061500 input_field_generation_old_[p] = handle->generation;
15071501 }
1508-
1502+ #endif
1503+ #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
15091504 for (int p = 0 ; p < NUM_NRRD_PORTS; p++)
15101505 {
15111506 NrrdDataHandle handle;
@@ -1574,7 +1569,8 @@ void InterfaceWithMatlab::execute()
15741569 input_nrrd_name_list_old_[p] = input_nrrd_name_list_[p];
15751570 input_nrrd_generation_old_[p] = handle->generation ;
15761571 }
1577-
1572+ #endif
1573+ #if 0
15781574 for (int p = 0; p < NUM_STRING_PORTS; p++)
15791575 {
15801576 StringHandle handle;
@@ -1637,7 +1633,7 @@ void InterfaceWithMatlab::execute()
16371633 input_string_name_list_old_[p] = input_string_name_list_[p];
16381634 input_string_generation_old_[p] = handle->generation;
16391635 }
1640-
1636+ # endif
16411637 }
16421638 catch (matlabfile::could_not_open_file&)
16431639 { // Could not open the temporary file
0 commit comments