|
53 | 53 | #include <boost/thread.hpp> |
54 | 54 | #include <Core/Services/FileTransferClient.h> |
55 | 55 | #include <Core/Datatypes/Matrix.h> |
| 56 | +#include <Core/Datatypes/String.h> |
| 57 | +#include <Core/Datatypes/Legacy/Field/Field.h> |
56 | 58 |
|
57 | 59 | #if 0 |
58 | 60 |
|
@@ -168,23 +170,7 @@ namespace MatlabImpl |
168 | 170 | output_nrrd_matfile_(NUM_OUTPUT_NRRDS), |
169 | 171 | output_string_matfile_(NUM_OUTPUT_STRINGS) |
170 | 172 | {} |
171 | | -#if 0 |
172 | | - |
173 | | - // Constructor |
174 | | - InterfaceWithMatlab(GuiContext* ctx); |
175 | | - |
176 | | - // Destructor |
177 | | - virtual ~InterfaceWithMatlab(); |
178 | 173 |
|
179 | | - // Std functions for each module |
180 | | - // execute(): |
181 | | - // Execute the module and put data on the output port |
182 | | - |
183 | | - virtual void execute(); |
184 | | - virtual void presave(); |
185 | | - virtual void tcl_command(GuiArgs& args, void* userdata); |
186 | | - |
187 | | -#endif |
188 | 174 | static matlabarray::mitype convertdataformat(const std::string& dataformat); |
189 | 175 | static std::string totclstring(const std::string& instring); |
190 | 176 | std::vector<std::string> converttcllist(const Variable::List& str); |
@@ -217,6 +203,9 @@ namespace MatlabImpl |
217 | 203 | bool isMatrixOutputPortConnected(int index) const; |
218 | 204 | bool isFieldOutputPortConnected(int index) const; |
219 | 205 | bool isStringOutputPortConnected(int index) const; |
| 206 | + void sendMatrixOutput(int index, MatrixHandle matrix) const; |
| 207 | + void sendFieldOutput(int index, FieldHandle field) const; |
| 208 | + void sendStringOutput(int index, StringHandle str) const; |
220 | 209 |
|
221 | 210 | // GUI variables |
222 | 211 | #if 0 |
@@ -295,7 +284,6 @@ namespace MatlabImpl |
295 | 284 | TempFileManager tfmanager_; |
296 | 285 | std::string mfile_; |
297 | 286 | #if 0 |
298 | | - GuiString matlab_code_; |
299 | 287 | GuiString matlab_code_file_; |
300 | 288 | GuiString matlab_var_; |
301 | 289 |
|
@@ -1088,49 +1076,48 @@ bool InterfaceWithMatlabImpl::close_matlab_engine() |
1088 | 1076 | return(true); |
1089 | 1077 | } |
1090 | 1078 |
|
1091 | | -bool InterfaceWithMatlab::load_output_matrices() |
| 1079 | +bool InterfaceWithMatlabImpl::load_output_matrices() |
1092 | 1080 | { |
1093 | | - int port = 0; |
1094 | | - try |
| 1081 | + for (int p = 0; p < NUM_OUTPUT_MATRICES; p++) |
1095 | 1082 | { |
1096 | | - for (int p = 0; p < NUM_MATRIX_PORTS; p++) |
1097 | | - { |
1098 | | - if (!oport_connected(port)) { port++; continue; } |
1099 | | - |
1100 | | - // Test whether the matrix port exists |
1101 | | - if (output_matrix_name_list_[p] == "") continue; |
1102 | | - if (output_matrix_matfile_[p] == "") continue; |
1103 | | - |
1104 | | - matlabfile mf; |
1105 | | - matlabarray ma; |
| 1083 | + if (!isMatrixOutputPortConnected(p)) |
| 1084 | + continue; |
| 1085 | + if (output_matrix_name_list_[p].empty()) |
| 1086 | + continue; |
| 1087 | + if (output_matrix_matfile_[p].empty()) |
| 1088 | + continue; |
1106 | 1089 |
|
1107 | | - try |
1108 | | - { |
1109 | | - if (need_file_transfer_) file_transfer_->get_file(file_transfer_->remote_file(output_matrix_matfile_[p]),file_transfer_->local_file(output_matrix_matfile_[p])); |
1110 | | - mf.open(file_transfer_->local_file(output_matrix_matfile_[p]),"r"); |
1111 | | - ma = mf.getmatlabarray(output_matrix_name_list_[p]); |
1112 | | - mf.close(); |
1113 | | - } |
1114 | | - catch(...) |
1115 | | - { |
1116 | | - error("InterfaceWithMatlab: Could not read output matrix"); |
1117 | | - continue; |
1118 | | - } |
| 1090 | + matlabfile mf; |
| 1091 | + matlabarray ma; |
1119 | 1092 |
|
1120 | | - if (ma.isempty()) |
1121 | | - { |
1122 | | - error("InterfaceWithMatlab: Could not read output matrix"); |
1123 | | - continue; |
1124 | | - } |
| 1093 | + try |
| 1094 | + { |
| 1095 | + if (need_file_transfer_) file_transfer_->get_file(file_transfer_->remote_file(output_matrix_matfile_[p]),file_transfer_->local_file(output_matrix_matfile_[p])); |
| 1096 | + mf.open(file_transfer_->local_file(output_matrix_matfile_[p]),"r"); |
| 1097 | + ma = mf.getmatlabarray(output_matrix_name_list_[p]); |
| 1098 | + mf.close(); |
| 1099 | + } |
| 1100 | + catch(...) |
| 1101 | + { |
| 1102 | + module_->error("InterfaceWithMatlab: Could not read output matrix"); |
| 1103 | + continue; |
| 1104 | + } |
1125 | 1105 |
|
1126 | | - MatrixHandle handle; |
1127 | | - std::string info; |
1128 | | - matlabconverter translate(dynamic_cast<SCIRun::ProgressReporter *>(this)); |
1129 | | - if (translate.sciMatrixCompatible(ma,info)) translate.mlArrayTOsciMatrix(ma,handle); |
1130 | | - send_output_handle(port,handle,true); port++; |
| 1106 | + if (ma.isempty()) |
| 1107 | + { |
| 1108 | + module_->error("InterfaceWithMatlab: Could not read output matrix"); |
| 1109 | + continue; |
1131 | 1110 | } |
1132 | 1111 |
|
| 1112 | + MatrixHandle handle; |
| 1113 | + std::string info; |
| 1114 | + matlabconverter translate(module_->getLogger()); |
| 1115 | + if (translate.sciMatrixCompatible(ma,info)) |
| 1116 | + translate.mlArrayTOsciMatrix(ma,handle); |
| 1117 | + sendMatrixOutput(p, handle); |
| 1118 | + } |
1133 | 1119 |
|
| 1120 | +#ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER |
1134 | 1121 | for (int p = 0; p < NUM_FIELD_PORTS; p++) |
1135 | 1122 | { |
1136 | 1123 | if (!oport_connected(port)) { port++; continue; } |
@@ -1241,11 +1228,8 @@ bool InterfaceWithMatlab::load_output_matrices() |
1241 | 1228 | if (translate.sciStringCompatible(ma,info)) translate.mlArrayTOsciString(ma,handle); |
1242 | 1229 | send_output_handle(port,handle,true); port++; |
1243 | 1230 | } |
1244 | | - } |
1245 | | - catch(...) |
1246 | | - { |
1247 | | - return(false); |
1248 | | - } |
| 1231 | +#endif |
| 1232 | + |
1249 | 1233 | return(true); |
1250 | 1234 | } |
1251 | 1235 |
|
@@ -1368,6 +1352,39 @@ bool InterfaceWithMatlabImpl::isStringOutputPortConnected(int index) const |
1368 | 1352 | } |
1369 | 1353 | } |
1370 | 1354 |
|
| 1355 | +void InterfaceWithMatlabImpl::sendMatrixOutput(int index, MatrixHandle matrix) const |
| 1356 | +{ |
| 1357 | + switch (index) |
| 1358 | + { |
| 1359 | + case 0: |
| 1360 | + module_->sendOutput(module_->OutputMatrix0, matrix); |
| 1361 | + case 1: |
| 1362 | + module_->sendOutput(module_->OutputMatrix1, matrix); |
| 1363 | + } |
| 1364 | +} |
| 1365 | + |
| 1366 | +void InterfaceWithMatlabImpl::sendFieldOutput(int index, FieldHandle field) const |
| 1367 | +{ |
| 1368 | + switch (index) |
| 1369 | + { |
| 1370 | + case 0: |
| 1371 | + module_->sendOutput(module_->OutputField0, field); |
| 1372 | + case 1: |
| 1373 | + module_->sendOutput(module_->OutputField1, field); |
| 1374 | + } |
| 1375 | +} |
| 1376 | + |
| 1377 | +void InterfaceWithMatlabImpl::sendStringOutput(int index, StringHandle str) const |
| 1378 | +{ |
| 1379 | + switch (index) |
| 1380 | + { |
| 1381 | + case 0: |
| 1382 | + module_->sendOutput(module_->OutputString0, str); |
| 1383 | + case 1: |
| 1384 | + module_->sendOutput(module_->OutputString1, str); |
| 1385 | + } |
| 1386 | +} |
| 1387 | + |
1371 | 1388 | bool InterfaceWithMatlabImpl::save_input_matrices() |
1372 | 1389 | { |
1373 | 1390 | try |
@@ -1681,52 +1698,53 @@ bool InterfaceWithMatlabImpl::isStringOutputPortConnected(int index) const |
1681 | 1698 | return(true); |
1682 | 1699 | } |
1683 | 1700 |
|
1684 | | - bool InterfaceWithMatlabImpl::create_temp_directory() |
| 1701 | +bool InterfaceWithMatlabImpl::create_temp_directory() |
| 1702 | +{ |
| 1703 | + if (temp_directory_.empty()) |
1685 | 1704 | { |
1686 | | - if (temp_directory_.empty()) |
1687 | | - { |
1688 | | - return (tfmanager_.create_tempdir("matlab-engine.XXXXXX",temp_directory_)); |
1689 | | - } |
1690 | | - return(true); |
| 1705 | + return (tfmanager_.create_tempdir("matlab-engine.XXXXXX",temp_directory_)); |
1691 | 1706 | } |
| 1707 | + return(true); |
| 1708 | +} |
1692 | 1709 |
|
1693 | 1710 |
|
1694 | | - bool InterfaceWithMatlabImpl::delete_temp_directory() |
| 1711 | +bool InterfaceWithMatlabImpl::delete_temp_directory() |
| 1712 | +{ |
| 1713 | + if(temp_directory_ != "") tfmanager_.delete_tempdir(temp_directory_); |
| 1714 | + temp_directory_ = ""; |
| 1715 | + return(true); |
| 1716 | +} |
| 1717 | + |
| 1718 | +std::string InterfaceWithMatlabImpl::totclstring(const std::string &instring) |
| 1719 | +{ |
| 1720 | + size_t strsize = instring.size(); |
| 1721 | + int specchar = 0; |
| 1722 | + for (int p = 0; p < strsize; p++) |
| 1723 | + if ((instring[p]=='\n')||(instring[p]=='\t')||(instring[p]=='\b')||(instring[p]=='\r')||(instring[p]=='{')||(instring[p]=='}') |
| 1724 | + ||(instring[p]=='[')||(instring[p]==']')||(instring[p]=='\\')||(instring[p]=='$')||(instring[p]=='"')) specchar++; |
| 1725 | + |
| 1726 | + std::string newstring; |
| 1727 | + newstring.resize(strsize+specchar); |
| 1728 | + int q = 0; |
| 1729 | + for (int p = 0; p < strsize; p++) |
1695 | 1730 | { |
1696 | | - if(temp_directory_ != "") tfmanager_.delete_tempdir(temp_directory_); |
1697 | | - temp_directory_ = ""; |
1698 | | - return(true); |
| 1731 | + if (instring[p]=='\n') { newstring[q++] = '\\'; newstring[q++] = 'n'; continue; } |
| 1732 | + if (instring[p]=='\t') { newstring[q++] = '\\'; newstring[q++] = 't'; continue; } |
| 1733 | + if (instring[p]=='\b') { newstring[q++] = '\\'; newstring[q++] = 'b'; continue; } |
| 1734 | + if (instring[p]=='\r') { newstring[q++] = '\\'; newstring[q++] = 'r'; continue; } |
| 1735 | + if (instring[p]=='{') { newstring[q++] = '\\'; newstring[q++] = '{'; continue; } |
| 1736 | + if (instring[p]=='}') { newstring[q++] = '\\'; newstring[q++] = '}'; continue; } |
| 1737 | + if (instring[p]=='[') { newstring[q++] = '\\'; newstring[q++] = '['; continue; } |
| 1738 | + if (instring[p]==']') { newstring[q++] = '\\'; newstring[q++] = ']'; continue; } |
| 1739 | + if (instring[p]=='\\') { newstring[q++] = '\\'; newstring[q++] = '\\'; continue; } |
| 1740 | + if (instring[p]=='$') { newstring[q++] = '\\'; newstring[q++] = '$'; continue; } |
| 1741 | + if (instring[p]=='"') { newstring[q++] = '\\'; newstring[q++] = '"'; continue; } |
| 1742 | + newstring[q++] = instring[p]; |
1699 | 1743 | } |
1700 | 1744 |
|
1701 | | - std::string InterfaceWithMatlabImpl::totclstring(const std::string &instring) |
1702 | | - { |
1703 | | - size_t strsize = instring.size(); |
1704 | | - int specchar = 0; |
1705 | | - for (int p = 0; p < strsize; p++) |
1706 | | - if ((instring[p]=='\n')||(instring[p]=='\t')||(instring[p]=='\b')||(instring[p]=='\r')||(instring[p]=='{')||(instring[p]=='}') |
1707 | | - ||(instring[p]=='[')||(instring[p]==']')||(instring[p]=='\\')||(instring[p]=='$')||(instring[p]=='"')) specchar++; |
1708 | | - |
1709 | | - std::string newstring; |
1710 | | - newstring.resize(strsize+specchar); |
1711 | | - int q = 0; |
1712 | | - for (int p = 0; p < strsize; p++) |
1713 | | - { |
1714 | | - if (instring[p]=='\n') { newstring[q++] = '\\'; newstring[q++] = 'n'; continue; } |
1715 | | - if (instring[p]=='\t') { newstring[q++] = '\\'; newstring[q++] = 't'; continue; } |
1716 | | - if (instring[p]=='\b') { newstring[q++] = '\\'; newstring[q++] = 'b'; continue; } |
1717 | | - if (instring[p]=='\r') { newstring[q++] = '\\'; newstring[q++] = 'r'; continue; } |
1718 | | - if (instring[p]=='{') { newstring[q++] = '\\'; newstring[q++] = '{'; continue; } |
1719 | | - if (instring[p]=='}') { newstring[q++] = '\\'; newstring[q++] = '}'; continue; } |
1720 | | - if (instring[p]=='[') { newstring[q++] = '\\'; newstring[q++] = '['; continue; } |
1721 | | - if (instring[p]==']') { newstring[q++] = '\\'; newstring[q++] = ']'; continue; } |
1722 | | - if (instring[p]=='\\') { newstring[q++] = '\\'; newstring[q++] = '\\'; continue; } |
1723 | | - if (instring[p]=='$') { newstring[q++] = '\\'; newstring[q++] = '$'; continue; } |
1724 | | - if (instring[p]=='"') { newstring[q++] = '\\'; newstring[q++] = '"'; continue; } |
1725 | | - newstring[q++] = instring[p]; |
1726 | | - } |
| 1745 | + return(newstring); |
| 1746 | +} |
1727 | 1747 |
|
1728 | | - return(newstring); |
1729 | | - } |
1730 | 1748 | #if 0 |
1731 | 1749 | void InterfaceWithMatlab::tcl_command(GuiArgs& args, void* userdata) |
1732 | 1750 | { |
|
0 commit comments