/usr/include/vigra/matlab.hxx is in libvigraimpex-dev 1.10.0+dfsg-3ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 | /************************************************************************/
/* */
/* Copyright 2008 by Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_MATLAB_HXX
#define VIGRA_MATLAB_HXX
#include <string>
#include "array_vector.hxx"
#include "sized_int.hxx"
#include "matrix.hxx"
#include <map>
#include <time.h>
// This is needed with visual studio 10
#ifdef _CHAR16T
#define CHAR16_T
#endif
#include <mex.h>
#include "matlab_FLEXTYPE.hxx"
namespace vigra {
namespace matlab {
template <class T>
struct ValueType;
#define VIGRA_MATLAB_VALUETYPE_UTIL(type, functionName, typeID, matTypeName) \
template <> \
struct ValueType<type> \
{ \
static bool check(mxArray const * t) \
{ \
return mxIs##functionName(t); \
} \
\
static mxClassID const classID = typeID; \
\
static std::string typeName() \
{ \
return #matTypeName; \
} \
};
VIGRA_MATLAB_VALUETYPE_UTIL(double, Double, mxDOUBLE_CLASS, double)
VIGRA_MATLAB_VALUETYPE_UTIL(float, Single, mxSINGLE_CLASS, single)
VIGRA_MATLAB_VALUETYPE_UTIL(Int8, Int8, mxINT8_CLASS, int8)
VIGRA_MATLAB_VALUETYPE_UTIL(UInt8, Uint8, mxUINT8_CLASS, uint8)
VIGRA_MATLAB_VALUETYPE_UTIL(Int16, Int16, mxINT16_CLASS, int16)
VIGRA_MATLAB_VALUETYPE_UTIL(UInt16, Uint16, mxUINT16_CLASS, uint16)
#if VIGRA_BITSOF_INT == 32
VIGRA_MATLAB_VALUETYPE_UTIL(int, Int32, mxINT32_CLASS, int32)
VIGRA_MATLAB_VALUETYPE_UTIL(unsigned int, Uint32, mxUINT32_CLASS, uint32)
#elif VIGRA_BITSOF_INT == 64
VIGRA_MATLAB_VALUETYPE_UTIL(int, Int64, mxINT64_CLASS, int64)
VIGRA_MATLAB_VALUETYPE_UTIL(unsigned int, Uint64, mxUINT64_CLASS, uint64)
#endif
#if VIGRA_BITSOF_LONG == 32
VIGRA_MATLAB_VALUETYPE_UTIL(long, Int32, mxINT32_CLASS, int32)
VIGRA_MATLAB_VALUETYPE_UTIL(unsigned long, Uint32, mxUINT32_CLASS, uint32)
#elif VIGRA_BITSOF_LONG == 64
VIGRA_MATLAB_VALUETYPE_UTIL(long, Int64, mxINT64_CLASS, int64)
VIGRA_MATLAB_VALUETYPE_UTIL(unsigned long, Uint64, mxUINT64_CLASS, uint64)
#endif
#if VIGRA_BITSOF_LONG_LONG == 32
VIGRA_MATLAB_VALUETYPE_UTIL(long long, Int32, mxINT32_CLASS, int32)
VIGRA_MATLAB_VALUETYPE_UTIL(unsigned long long, Uint32, mxUINT32_CLASS, uint32)
#elif VIGRA_BITSOF_LONG_LONG == 64
VIGRA_MATLAB_VALUETYPE_UTIL(long long, Int64, mxINT64_CLASS, int64)
VIGRA_MATLAB_VALUETYPE_UTIL(unsigned long long, Uint64, mxUINT64_CLASS, uint64)
#endif
#undef VIGRA_MATLAB_VALUETYPE_UTIL
class ConstStructArray
{
protected:
mxArray * matPointer_;
public:
struct Proxy
{
mxArray * matPointer_;
int index_;
Proxy(mxArray * matPointer, int index)
: matPointer_(matPointer),
index_(index)
{}
operator const mxArray *() const
{
return mxGetFieldByNumber(matPointer_, 0, index_);
}
};
ConstStructArray(const mxArray * matPointer = 0)
: matPointer_(const_cast<mxArray *>(matPointer))
{
if(matPointer != 0 && !mxIsStruct(matPointer))
mexErrMsgTxt("StructArray(mxArray *): Argument must be a Matlab struct array.");
}
Proxy operator[](const char * field_name) const
{
if(matPointer_ == 0)
mexErrMsgTxt("StructArray::operator[]: Cannot access uninitialized struct array.");
int i = mxGetFieldNumber(matPointer_, field_name);
if(i == -1)
mexErrMsgTxt("StructArray::operator[]: Unknown field name.");
return Proxy(matPointer_, i);
}
Proxy operator[](std::string field_name) const
{
return operator[](field_name.c_str());
}
bool isValid() const
{
return matPointer_ != 0;
}
bool isValid(const char * field_name) const
{
return isValid() && mxGetFieldNumber(matPointer_, field_name) != -1;
}
bool isValid(std::string field_name) const
{
return isValid(field_name.c_str());
}
};
class ConstCellArray
{
protected:
mxArray * matPointer_;
int size_;
public:
struct Proxy
{
mxArray * matPointer_;
int index_;
Proxy(mxArray * matPointer, int index)
: matPointer_(matPointer),
index_(index)
{}
operator const mxArray *() const
{
return mxGetCell(matPointer_, index_);
}
};
ConstCellArray(const mxArray * matPointer = 0)
: matPointer_(const_cast<mxArray *>(matPointer)),
size_(0)
{
if(matPointer != 0 && !mxIsCell(matPointer))
mexErrMsgTxt("CellArray(mxArray *): Argument must be a Matlab cell array.");
if(matPointer != 0)
size_ = static_cast<int>(mxGetNumberOfElements(matPointer));
else
size_ = -1;
}
Proxy operator[](int i) const
{
if(!isValid(i))
mexErrMsgTxt("CellArray::operator[]: Index out of range.");
return Proxy(matPointer_, i);
}
int size() const
{
return size_;
}
bool isValid( int i ) const
{
return i >= 0 && i < size_;
}
};
class CellArray
: public ConstCellArray
{
public:
struct Proxy
: public ConstCellArray::Proxy
{
Proxy(mxArray * matPointer, int index)
: ConstCellArray::Proxy(matPointer, index)
{}
void operator=(mxArray * v)
{
mxDestroyArray(mxGetCell(matPointer_, index_));
mxSetCell(matPointer_, index_, v);
}
};
CellArray(const mxArray * matPointer)
: ConstCellArray(matPointer)
{}
Proxy operator[](int i)
{
if(!isValid(i))
mexErrMsgTxt("CellArray::operator[]: Index out of range.");
return Proxy(matPointer_, i);
}
ConstCellArray::Proxy operator[](int i) const
{
if(!isValid(i))
mexErrMsgTxt("CellArray::operator[]: Index out of range.");
return ConstCellArray::Proxy(matPointer_, i);
}
};
template <class T, unsigned int SIZE>
TinyVectorView<T, SIZE>
getTinyVector(mxArray const * t)
{
if(!ValueType<T>::check(t))
{
std::string msg = std::string("Input array must have type ") +
ValueType<T>::typeName() + ".";
mexErrMsgTxt(msg.c_str());
}
if(SIZE != mxGetNumberOfElements(t))
{
mexErrMsgTxt("getTinyVector(): Input array has wrong number of elements.");
}
return TinyVectorView<T, SIZE>((T *)mxGetData(t));
}
template <unsigned int SIZE>
typename MultiArrayShape<SIZE>::type
getShape(mxArray const * t)
{
if(!ValueType<Int32>::check(t))
{
std::string msg = std::string("Input array must have type 'int32'.");
mexErrMsgTxt(msg.c_str());
}
if(SIZE != mxGetNumberOfElements(t))
{
mexErrMsgTxt("getShape(): Input array has wrong number of elements.");
}
TinyVectorView<Int32, SIZE> res((MultiArrayIndex *)mxGetData(t));
return typename MultiArrayShape<SIZE>::type(res);
}
template <int DIM, class T>
MultiArrayView<DIM, T>
getMultiArray(mxArray const * t)
{
typedef typename MultiArrayView<DIM, T>::difference_type Shape;
if(!ValueType<T>::check(t))
{
std::string msg = std::string("getMultiArray(): Input array must have type ") +
ValueType<T>::typeName() + ".";
mexErrMsgTxt(msg.c_str());
}
Shape shape;
if(DIM > 1)
{
int mdim = mxGetNumberOfDimensions(t);
if(DIM < mdim)
{
mexErrMsgTxt("getMultiArray(): Input array has too many dimensions.");
}
const mwSize * matlabShape = mxGetDimensions(t);
for(int k=0; k<mdim; ++k)
{
shape[k] = static_cast<typename Shape::value_type>(matlabShape[k]);
}
for(int k=mdim; k<DIM; ++k)
{
shape[k] = 1;
}
}
else
{
shape[0] = static_cast<typename Shape::value_type>(mxGetNumberOfElements(t));
}
return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
}
template <int DIM, class T>
MultiArrayView<DIM, T>
createMultiArray(typename MultiArrayShape<DIM>::type const & shape, mxArray * & t)
{
mwSize matlabShape[DIM];
for(int k=0; k<DIM; ++k)
matlabShape[k] = static_cast<mwSize>(shape[k]);
t = mxCreateNumericArray(DIM, matlabShape, ValueType<T>::classID, mxREAL);
return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
}
template <int DIM, class T>
MultiArrayView<DIM, T>
createMultiArray(typename MultiArrayShape<DIM>::type const & shape, CellArray::Proxy t)
{
mwSize matlabShape[DIM];
for(int k=0; k<DIM; ++k)
matlabShape[k] = static_cast<mwSize>(shape[k]);
t = mxCreateNumericArray(DIM, matlabShape, ValueType<T>::classID, mxREAL);
return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
}
template <class T>
inline MultiArrayView<1, T>
getArray(mxArray const * t)
{
return getMultiArray<1, T>(t);
}
template <class T>
inline MultiArrayView<1, T>
createArray(MultiArrayIndex size, mxArray * & t)
{
return createMultiArray<1, T>(MultiArrayShape<1>::type(size), t);
}
template <class T>
inline MultiArrayView<1, T>
createArray(MultiArrayIndex size, CellArray::Proxy t)
{
return createMultiArray<1, T>(MultiArrayShape<1>::type(size), t);
}
template <class T>
MultiArrayView<2, T>
getMatrix(mxArray const * t)
{
typedef typename MultiArrayView<2, T>::difference_type Shape;
if(!ValueType<T>::check(t))
{
std::string msg = std::string("getMatrix(): Input matrix must have type ") +
ValueType<T>::typeName() + ".";
mexErrMsgTxt(msg.c_str());
}
if(2 != mxGetNumberOfDimensions(t))
mexErrMsgTxt("getMatrix(): Input matrix must have 2 dimensions.");
const mwSize * matlabShape = mxGetDimensions(t);
Shape shape(static_cast<MultiArrayIndex>(matlabShape[0]),
static_cast<MultiArrayIndex>(matlabShape[1]));
return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
}
template <class T>
MultiArrayView<2, T>
createMatrix(mwSize rowCount, mwSize columnCount, mxArray * & t)
{
typedef typename MultiArrayView<2, T>::difference_type Shape;
Shape shape(rowCount, columnCount);
t = mxCreateNumericMatrix(rowCount, columnCount, ValueType<T>::classID, mxREAL);
return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
}
template <class T>
MultiArrayView<2, T>
createMatrix(mwSize rowCount, mwSize columnCount, CellArray::Proxy t)
{
typedef typename MultiArrayView<2, T>::difference_type Shape;
Shape shape(rowCount, columnCount);
t = mxCreateNumericMatrix(rowCount, columnCount, ValueType<T>::classID, mxREAL);
return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
}
template <class T>
BasicImageView<T>
getImage(mxArray const * t)
{
if(!ValueType<T>::check(t))
{
std::string msg = std::string("getImage(): Input matrix must have type ") +
ValueType<T>::typeName() + ".";
mexErrMsgTxt(msg.c_str());
}
if(2 != mxGetNumberOfDimensions(t))
mexErrMsgTxt("getImage(): Input matrix must have 2 dimensions.");
const mwSize * matlabShape = mxGetDimensions(t);
return BasicImageView<T>((T *)mxGetData(t), static_cast<int>(matlabShape[0]),
static_cast<int>(matlabShape[1]));
}
template <class T>
BasicImageView<T>
createImage(mwSize width, mwSize height, mxArray * & t)
{
t = mxCreateNumericMatrix(width, height, ValueType<T>::classID, mxREAL);
return BasicImageView<T>((T *)mxGetData(t), width, height);
}
template <class T>
BasicImageView<T>
createImage(mwSize width, mwSize height, CellArray::Proxy t)
{
t = mxCreateNumericMatrix(width, height, ValueType<T>::classID, mxREAL);
return BasicImageView<T>((T *)mxGetData(t), width, height);
}
inline ConstCellArray
getCellArray(mxArray const * t)
{
return ConstCellArray(t);
}
inline CellArray
createCellArray(mwSize size, mxArray * & t)
{
mwSize matSize[] = { size };
t = mxCreateCellArray(1, matSize);
return CellArray(t);
}
inline CellArray
createCellArray(mwSize size, CellArray::Proxy t)
{
mwSize matSize[] = { size };
t = mxCreateCellArray(1, matSize);
return CellArray(t);
}
inline ConstStructArray
getStructArray(mxArray const * t)
{
return ConstStructArray(t);
}
template<class T>
T
getScalar(mxArray const * t)
{
if(mxIsEmpty(t))
mexErrMsgTxt("getScalar() on empty input.");
if(!mxIsNumeric(t) && !mxIsLogical(t))
mexErrMsgTxt("getScalar(): argument is not numeric.");
return static_cast<T>(mxGetScalar(t));
}
template<class T>
mxArray *
createScalar(T v)
{
mxArray * m;
createMatrix<double>(1, 1, m)(0,0) = static_cast<double>(v);
return m;
}
inline std::string
getString(mxArray const * t)
{
if(mxIsEmpty(t))
mexErrMsgTxt("getString() on empty input.");
if(!mxIsChar(t))
mexErrMsgTxt("getString(): argument is not a string.");
int size = static_cast<int>(mxGetNumberOfElements(t) + 1);
ArrayVector<char> buf(size);
mxGetString(t, buf.begin(), size);
return std::string(buf.begin());
}
class CompileTimeError;
namespace detail {
class Required
{
public:
void argumentWasProvided() const { /* empty because required arguments are always provided */ }
};
template<class T>
class DefaultImpl
{
public:
T defaultValue_;
mutable bool * argumentWasProvided_;
DefaultImpl(T v, bool * argFlag = 0)
: defaultValue_(v),
argumentWasProvided_(argFlag)
{
if(argumentWasProvided_ != 0)
*argumentWasProvided_ = false;
}
void argumentWasProvided() const
{
if(argumentWasProvided_ != 0)
*argumentWasProvided_ = true;
}
};
class OptionalImpl
{
public:
mutable bool * argumentWasProvided_;
OptionalImpl(bool * argFlag = 0)
: argumentWasProvided_(argFlag)
{
if(argumentWasProvided_ != 0)
*argumentWasProvided_ = false;
}
void argumentWasProvided() const
{
if(argumentWasProvided_ != 0)
*argumentWasProvided_ = true;
}
};
} // namespace detail
inline detail::Required v_required()
{
return detail::Required();
}
template<class T>
inline detail::DefaultImpl<T> v_default(T in)
{
return detail::DefaultImpl<T>(in);
}
template<class T>
inline detail::DefaultImpl<T> v_default(T in, bool & argFlag)
{
return detail::DefaultImpl<T>(in, &argFlag);
}
inline detail::OptionalImpl v_optional()
{
return detail::OptionalImpl();
}
inline detail::OptionalImpl v_optional(bool& argFlag)
{
return detail::OptionalImpl(&argFlag);
}
// TODO:
// * handle rgb images
// * handle complex matrices
// * handle sparse matrices
class InputArray
{
int size_;
const mxArray ** data_;
std::string createErrMsg(std::string name)
{
std::string s1;
s1 = "Required input '" + name + "' not found in option struct!";
return s1;
}
std::string createErrMsg(int pos)
{
char tmp[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
std::string oi(1, tmp[pos%10]);
std::string s1 = "Required input in signature of function at position: '"+ oi+"' has not been supplied";
return s1;
}
public:
ConstStructArray options_;
/* Local Typedefs */
typedef const mxArray * value_type;
typedef value_type & reference;
typedef value_type const & const_reference;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef int size_type;
typedef int difference_type;
/*Constructor*/
InputArray(size_type size, pointer data)
: size_(size),
data_(data),
options_(isValid(size-1) && mxIsStruct(data_[size-1])
? data_[size-1]
: 0)
{}
/*Operators*/
const_reference operator[]( difference_type i ) const
{
if(!isValid(i))
mexErrMsgTxt("Too few input arguments.");
return data_[i];
}
value_type operator[]( std::string name) const
{
std::string errMsg = "Not Found " + name +" in OptionStruct or OptionStruct not set";
if(!isValid(name))
mexErrMsgTxt(errMsg.c_str());
return options_[name];
}
/*Some More Helper Func*/
size_type size() const
{
return size_;
}
bool isValid( difference_type i ) const
{
return i >= 0 && i < size_;
}
bool isValid(std::string name) const
{
return options_.isValid(name);
}
bool isEmpty(difference_type i) const
{
return mxIsEmpty(data_[i]);
}
bool isEmpty(std::string name) const
{
return mxIsEmpty(options_[name]);
}
bool hasData(difference_type i) const
{
return isValid(i) && !isEmpty(i);
}
bool hasData(std::string name) const
{
return isValid(name) && !isEmpty(name);
}
template<class Place>
mxClassID typeOf(Place posOrName)
{
return mxGetClassID((*this)[posOrName]);
}
/*Action to take if value not set*/
template <class T, class U, class Place>
T errorOrDefault(detail::DefaultImpl<U> const & o, Place posOrName)
{
return o.defaultValue_;
}
template <class T, class Place>
T errorOrDefault(detail::OptionalImpl, Place posOrName)
{
return T();
}
template <class T, class Place>
T errorOrDefault(detail::Required r, Place posOrName)
{
std::string a = createErrMsg(posOrName);
mexErrMsgTxt( a.c_str());
return T();
}
/*getter Func*/
template <class Place, class ReqType>
int getEnum(Place posOrName, ReqType req, std::map<std::string, int> const & converter)
{
if(!hasData(posOrName))
{
return errorOrDefault<int>(req, posOrName);
}
std::string enumAsString = matlab::getString((*this)[posOrName]);
typename std::map<std::string, int>::const_iterator m = converter.find(enumAsString);
if(m == converter.end())
{
std::string msg = std::string("Unknown option: ") + enumAsString + ".";
mexErrMsgTxt(msg.c_str());
}
req.argumentWasProvided();
return (*m).second;
}
/*String Type*/
template <class Place, class ReqType>
std::string getString(Place posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault<std::string>(req, posOrName);
}
else
{
req.argumentWasProvided();
return matlab::getString((*this)[posOrName]);
}
}
/*Scalar Type*/
template <class T,class Place, class ReqType>
T getScalar(Place posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault<T>(req, posOrName);
}
else
{
req.argumentWasProvided();
return matlab::getScalar<T>((*this)[posOrName]);
}
}
template <class T, class Place, class ReqType, class minClass, class maxClass>
T getScalarMinMax(Place posOrName, ReqType req, minClass min_, maxClass max_)
{
T temp = this->getScalar<T>(posOrName, req);
if (!is_in_range(temp, min_, max_))
mexErrMsgTxt("Value out of bounds.");
return temp;
}
template <class T, class Place, class ReqType, class iteratorType>
T getScalarVals(Place posOrName, ReqType req, iteratorType begin_, iteratorType end_)
{
T temp = this->getScalar<T>(posOrName, req);
for(iteratorType iter = begin_; iter != end_; ++iter)
{
if((*iter) == temp) return temp;
}
mexErrMsgTxt("Value not allowed");
}
template <class T, class Place, class ReqType, class iteratorType>
T getScalarVals2D3D(Place posOrName, ReqType req, iteratorType begin2D_, iteratorType end2D_,
iteratorType begin3D_, iteratorType end3D_,
int dimVar)
{
T temp = this->getScalar<T>(posOrName, req);
switch(dimVar)
{
case 2:
for(iteratorType iter = begin2D_; iter != end2D_; ++iter)
{
if((*iter) == temp) return temp;
}
break;
case 3:
for(iteratorType iter = begin3D_; iter != end3D_; ++iter)
{
if((*iter) == temp) return temp;
}
break;
default:
mexErrMsgTxt("dimVar specified must be 2 or 3");
}
mexErrMsgTxt("Value not allowed");
}
template <class Place, class ReqType>
bool getBool(Place posOrName, ReqType req)
{
return this->getScalarMinMax<int>(posOrName, req, 0, 1) != 0;
}
/*Array Type*/
template <unsigned int N, class T, class Place, class ReqType>
MultiArrayView<N,T> getMultiArray(Place posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault< MultiArrayView<N,T> >(req, posOrName);
}
else
{
req.argumentWasProvided();
value_type temp = (*this)[posOrName];
return matlab::getMultiArray<N,T>(temp);
}
}
template < class T, class Place, class ReqType>
BasicImageView<T> getImage(Place posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault<BasicImageView<T> >(req, posOrName);
}
else
{
req.argumentWasProvided();
value_type temp = (*this)[posOrName];
return matlab::getImage<T>(temp);
}
}
template<class T,unsigned int sze, class Place, class ReqType>
TinyVectorView< T, sze> getTinyVector(Place posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault<TinyVectorView< T, sze> >(req, posOrName);
}
else
{
req.argumentWasProvided();
value_type temp = (*this)[posOrName];
return matlab::getTinyVector< T, sze>(temp);
}
}
template< unsigned int sze, class Place, class ReqType>
TinyVectorView<MultiArrayIndex, sze> getShape(Place posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault<TinyVectorView<MultiArrayIndex, sze> >(req, posOrName);
}
else
{
req.argumentWasProvided();
value_type temp = (*this)[posOrName];
return matlab::getShape<sze>(temp);
}
}
template< class Place, class ReqType>
int getDimOfInput(Place posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault<int>(req, posOrName);
}
else
{
req.argumentWasProvided();
return mxGetNumberOfDimensions((*this)[posOrName]);
}
}
template<class ReqType>
ConstCellArray getCellArray(int posOrName, ReqType req)
{
if(!hasData(posOrName))
{
return errorOrDefault<ConstCellArray>(req, posOrName);
}
else
{
req.argumentWasProvided();
value_type temp = (*this)[posOrName];
return matlab::getCellArray(temp);
}
}
template<class ReqType>
ConstCellArray getCellArray(std::string posOrName, ReqType req)
{
CompileTimeError ERROR__Const_Cell_Array_May_Not_Be_In_Option_Struct;
return ConstCellArray(); //avoid compiler warning
}
};
class OutputArray
{
int size_;
mxArray ** data_;
std::string createErrMsgOut(int pos)
{
char tmp[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
std::string oi(1, tmp[pos%10]);
std::string s1 = "Required Output at position: '" + oi + "' has not been supplied";
return s1;
}
public:
typedef mxArray * value_type;
typedef value_type & reference;
typedef value_type const & const_reference;
typedef value_type * pointer;
typedef value_type const * const_pointer;
typedef int size_type;
typedef int difference_type;
OutputArray(size_type size, pointer data)
: size_(size),
data_(data)
{}
reference operator[]( difference_type i )
{
if(!isValid(i))
mexErrMsgTxt("Too few output arguments.");
return data_[i];
}
const_reference operator[]( difference_type i ) const
{
if(!isValid(i))
mexErrMsgTxt("Too few output arguments.");
return data_[i];
}
size_type size() const
{
return size_;
}
bool isValid( difference_type i ) const
{
return i >= 0 && i < size_;
}
bool isEmpty(difference_type i){
return mxIsEmpty(data_[i]);
}
template <class T>
T errorOrDefault(detail::OptionalImpl const & o, int Pos)
{
return T();
}
template <class T>
T errorOrDefault(detail::Required r, int Pos)
{
mexErrMsgTxt(createErrMsgOut(Pos).c_str());
return T();
}
/* creating func */
template <unsigned int DIM, class T, class ReqType>
MultiArrayView<DIM, T> createMultiArray(int pos,ReqType req,
const TinyVector<int, DIM> & shape)
{
if(!isValid(pos))
return errorOrDefault<MultiArrayView<DIM, T> >(req, pos);
req.argumentWasProvided();
return matlab::createMultiArray<DIM, T>(shape, (*this)[pos]);
}
template <class T, class ReqType>
BasicImageView<T> createImage(int pos, ReqType req,
mwSize width, mwSize height)
{
if(!isValid(pos))
return errorOrDefault<BasicImageView<T> >(req, pos);
req.argumentWasProvided();
return matlab::createImage<T>(width, height, (*this)[pos]);
}
template <class T, class ReqType>
BasicImageView<T> createImage( int pos, ReqType req,
typename MultiArrayShape<2>::type const & shape)
{
return createImage<T>(pos, req, shape[1], shape[0]);
}
template <class T, class ReqType>
T* createScalar(int pos, ReqType req)
{
if(!isValid(pos))
return errorOrDefault<T*>(req, pos);
req.argumentWasProvided();
BasicImageView<T> temp = matlab::createImage<T>(1, 1, (*this)[pos]);
return &temp(0,0);
}
template <class T, class ReqType>
void createScalar(int pos, ReqType req, T val)
{
if(!isValid(pos))
{
errorOrDefault<T>(req, pos);
return;
}
req.argumentWasProvided();
BasicImageView<T> temp = matlab::createImage<T>(1, 1, (*this)[pos]);
temp(0,0) = val;
}
template <class ReqType>
ConstCellArray createCellArray(int pos, ReqType req, mwSize sze)
{
if(!isValid(pos))
return errorOrDefault<ConstCellArray>(req, pos);
return matlab::createCellArray(sze, (*this)[pos]);
}
};
/***********************************
Rahuls code starts here
************************************/
using namespace vigra;
/*++++++++++++++++++++++++++HELPERFUNC+++++++++++++++++++++++++++++++*
* This is used for better readability of the test cases .
* Nothing to be done here.
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
int cantorPair(int x, int y){
return (int)(((x+y)*(x+y+1))/2+y);
}
int cantorPair(int x, int y, int z){
return cantorPair(cantorPair(x,y),z);
}
template <int x, int y>
struct cP{
enum { value = (int)(((x+y)*(x+y+1))/2+y)};
};
template <int x, int y, int z>
struct cP3{
enum { value = cP<cP<x, y>::value, z>::value};
};
template <class T>
inline bool is_in_range(T in, T min, T max)
{
return (in >= min && in <= max);
}
template<class T>
inline bool is_in_range(T in, std::string min, T max)
{
return(in <= max);
}
template<class T>
inline bool is_in_range(T in, T min, std::string max)
{
return (in >= min);
}
//Wrapper classes to STL-Map for use as a sparse array.
//This is used for the ordering of the map. Lexicographical ordering of the index pairs.
struct ShapeCmp {
bool operator()( TinyVector<int,2> s1, TinyVector<int,2> s2 ) const {
if(s1[0] != s2[0]){
return (s1[0] < s2[0]);
} else {
return s1[1] < s2[1];
}
}
};
template<class T>
class SparseArray
{
std::map<TinyVector<int,2>, T,ShapeCmp> data;
int width, length;
public:
void assign(int i = 1, int j = 1){
width = j;
length = i;
}
SparseArray(int i = 1 , int j = 1){
width = j;
length = i;
}
//Any better idea? i would like to unify the get and operator() functions.
// Problem is that operator() always passes a reference or creates one.
template<class indexType>
T& operator()(indexType i_, indexType j_){
Int32 i = static_cast<Int32>(i_);
Int32 j = static_cast<Int32>(j_);
TinyVector<int,2> newShapew(i, j);
typename std::map<TinyVector<int,2>, T, ShapeCmp>::iterator iter;
TinyVector<int,2> newShape;
return data[newShapew];
}
template<class indexType>
const T get(indexType i_, indexType j_){
Int32 i = static_cast<Int32>(i_);
Int32 j = static_cast<Int32>(j_);
TinyVector<int,2> newShape(i, j);
if(data.find(newShape) == data.end()) return 0;
else return data.find(newShape)->second;
}
//see documentation of mxCreateSparse and the mxGet functions to understand this.
void mapToMxArray(mxArray * & in){
int len = data.size();
in = mxCreateSparse(width, length, len, mxREAL);
int* jc = mxGetJc(in);
int* ir = mxGetIr(in);
double* pr = mxGetPr(in);
if(len == 0){
jc[0] = 1;
return;
}
typename std::map<TinyVector<int,2>, T, ShapeCmp>::iterator iter;
TinyVector<int,2> newShape;
int ii = 0;
int jj = 0;
int curjc = -1;
for( iter = data.begin(); iter != data.end(); ++iter ) {
newShape = iter->first;
ir[ii] = newShape[1];
pr[ii] = iter->second;
if(newShape[0] != curjc){
curjc = newShape[0] ;
jc[jj] = ii;
jj++;
}
ii++;
}
jc[jj] = len;
}
};
enum DataDimension {IMAGE = 2, VOLUME = 3};
} // namespace matlab
} // namespace vigra
void vigraMexFunction(vigra::matlab::OutputArray, vigra::matlab::InputArray);
#ifndef VIGRA_CUSTOM_MEXFUNCTION
/*
DO NOT Comment out this function. If you are using a
custom mexfunction just #define VIGRA_CUSTOM_MEXFUNCTION
before #including matlab.hxx.
*/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
try
{
vigra::matlab::InputArray inputs(nrhs, prhs);
vigra::matlab::OutputArray outputs(nlhs, plhs);
vigraMexFunction(outputs, inputs);
}
catch(std::exception & e)
{
mexErrMsgTxt(e.what());
}
}
#endif /*CUSTOM_MEXFUNCTION*/
#define VIGRA_CREATE_ENUM_AND_STD_MAP2(mapName, item1, item2) \
const int item1 = 1;\
const int item2 = 2;\
std::map<std::string,int> mapName;\
mapName[#item1] = (int)item1;\
mapName[#item2] = (int)item2;\
#define VIGRA_CREATE_ENUM_AND_STD_MAP3(mapName, item1, item2, item3) \
const int item1 = 1;\
const int item2 = 2;\
const int item3 = 3;\
std::map<std::string,int> mapName;\
mapName[#item1] = (int)item1;\
mapName[#item2] = (int)item2;\
mapName[#item3] = (int)item3;\
#define VIGRA_CREATE_ENUM_AND_STD_MAP4(mapName, item1, item2, item3, item4) \
const int item1 = 1;\
const int item2 = 2;\
const int item3 = 3;\
const int item4 = 4;\
std::map<std::string,int> mapName;\
mapName[#item1] = (int)item1;\
mapName[#item2] = (int)item2;\
mapName[#item3] = (int)item3;\
mapName[#item4] = (int)item4;\
#define VIGRA_CREATE_ENUM_AND_STD_MAP5(mapName, item1, item2, item3, item4, item5) \
const int item1 = 1;\
const int item2 = 2;\
const int item3 = 3;\
const int item4 = 4;\
const int item5 = 5;\
std::map<std::string, int> mapName;\
mapName[#item1] = (int)item1;\
mapName[#item2] = (int)item2;\
mapName[#item3] = (int)item3;\
mapName[#item4] = (int)item4;\
mapName[#item5] = (int)item5;\
#define VIGRA_CREATE_ENUM_AND_STD_MAP6(mapName, item1, item2, item3, item4, item5, item6) \
const int item1 = 1;\
const int item2 = 2;\
const int item3 = 3;\
const int item4 = 4;\
const int item5 = 5;\
const int item6 = 6;\
std::map<std::string,int> mapName;\
mapName[#item1] = (int)item1;\
mapName[#item2] = (int)item2;\
mapName[#item3] = (int)item3;\
mapName[#item4] = (int)item4;\
mapName[#item5] = (int)item5;\
mapName[#item6] = (int)item6;\
#endif // VIGRA_MATLAB_HXX
|