37 #ifndef VIGRA_MATLAB_HXX
38 #define VIGRA_MATLAB_HXX
42 #include "array_vector.hxx"
43 #include "sized_int.hxx"
52 #include "matlab_FLEXTYPE.hxx"
61 #define VIGRA_MATLAB_VALUETYPE_UTIL(type, functionName, typeID, matTypeName) \
63 struct ValueType<type> \
65 static bool check(mxArray const * t) \
67 return mxIs##functionName(t); \
70 static mxClassID const classID = typeID; \
72 static std::string typeName() \
74 return #matTypeName; \
78 VIGRA_MATLAB_VALUETYPE_UTIL(
double, Double, mxDOUBLE_CLASS,
double)
79 VIGRA_MATLAB_VALUETYPE_UTIL(
float, Single, mxSINGLE_CLASS, single)
80 VIGRA_MATLAB_VALUETYPE_UTIL(
Int8, Int8, mxINT8_CLASS, int8)
81 VIGRA_MATLAB_VALUETYPE_UTIL(
UInt8, Uint8, mxUINT8_CLASS, uint8)
82 VIGRA_MATLAB_VALUETYPE_UTIL(
Int16, Int16, mxINT16_CLASS, int16)
83 VIGRA_MATLAB_VALUETYPE_UTIL(
UInt16, Uint16, mxUINT16_CLASS, uint16)
85 #if VIGRA_BITSOF_INT == 32
86 VIGRA_MATLAB_VALUETYPE_UTIL(
int,
Int32, mxINT32_CLASS, int32)
87 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned int, Uint32, mxUINT32_CLASS, uint32)
88 #elif VIGRA_BITSOF_INT == 64
89 VIGRA_MATLAB_VALUETYPE_UTIL(
int,
Int64, mxINT64_CLASS, int64)
90 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned int, Uint64, mxUINT64_CLASS, uint64)
93 #if VIGRA_BITSOF_LONG == 32
94 VIGRA_MATLAB_VALUETYPE_UTIL(
long,
Int32, mxINT32_CLASS, int32)
95 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long, Uint32, mxUINT32_CLASS, uint32)
96 #elif VIGRA_BITSOF_LONG == 64
97 VIGRA_MATLAB_VALUETYPE_UTIL(
long,
Int64, mxINT64_CLASS, int64)
98 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long, Uint64, mxUINT64_CLASS, uint64)
101 #if VIGRA_BITSOF_LONG_LONG == 32
102 VIGRA_MATLAB_VALUETYPE_UTIL(
long long,
Int32, mxINT32_CLASS, int32)
103 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long long, Uint32, mxUINT32_CLASS, uint32)
104 #elif VIGRA_BITSOF_LONG_LONG == 64
105 VIGRA_MATLAB_VALUETYPE_UTIL(
long long,
Int64, mxINT64_CLASS, int64)
106 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long long, Uint64, mxUINT64_CLASS, uint64)
109 #undef VIGRA_MATLAB_VALUETYPE_UTIL
111 class ConstStructArray
114 mxArray * matPointer_;
120 mxArray * matPointer_;
123 Proxy(mxArray * matPointer,
int index)
124 : matPointer_(matPointer),
128 operator const mxArray *()
const
130 return mxGetFieldByNumber(matPointer_, 0, index_);
134 ConstStructArray(
const mxArray * matPointer = 0)
135 : matPointer_(const_cast<mxArray *>(matPointer))
137 if(matPointer != 0 && !mxIsStruct(matPointer))
138 mexErrMsgTxt(
"StructArray(mxArray *): Argument must be a Matlab struct array.");
141 Proxy operator[](
const char * field_name)
const
144 mexErrMsgTxt(
"StructArray::operator[]: Cannot access uninitialized struct array.");
146 int i = mxGetFieldNumber(matPointer_, field_name);
148 mexErrMsgTxt(
"StructArray::operator[]: Unknown field name.");
150 return Proxy(matPointer_, i);
153 Proxy operator[](std::string field_name)
const
155 return operator[](field_name.c_str());
160 return matPointer_ != 0;
163 bool isValid(
const char * field_name)
const
165 return isValid() && mxGetFieldNumber(matPointer_, field_name) != -1;
168 bool isValid(std::string field_name)
const
170 return isValid(field_name.c_str());
177 mxArray * matPointer_;
184 mxArray * matPointer_;
187 Proxy(mxArray * matPointer,
int index)
188 : matPointer_(matPointer),
192 operator const mxArray *()
const
194 return mxGetCell(matPointer_, index_);
198 ConstCellArray(
const mxArray * matPointer = 0)
199 : matPointer_(const_cast<mxArray *>(matPointer)),
202 if(matPointer != 0 && !mxIsCell(matPointer))
203 mexErrMsgTxt(
"CellArray(mxArray *): Argument must be a Matlab cell array.");
205 size_ =
static_cast<int>(mxGetNumberOfElements(matPointer));
210 Proxy operator[](
int i)
const
213 mexErrMsgTxt(
"CellArray::operator[]: Index out of range.");
214 return Proxy(matPointer_, i);
222 bool isValid(
int i )
const
224 return i >= 0 && i < size_;
230 :
public ConstCellArray
235 :
public ConstCellArray::Proxy
237 Proxy(mxArray * matPointer,
int index)
238 : ConstCellArray::Proxy(matPointer, index)
241 void operator=(mxArray * v)
243 mxDestroyArray(mxGetCell(matPointer_, index_));
244 mxSetCell(matPointer_, index_, v);
248 CellArray(
const mxArray * matPointer)
249 : ConstCellArray(matPointer)
252 Proxy operator[](
int i)
255 mexErrMsgTxt(
"CellArray::operator[]: Index out of range.");
256 return Proxy(matPointer_, i);
259 ConstCellArray::Proxy operator[](
int i)
const
262 mexErrMsgTxt(
"CellArray::operator[]: Index out of range.");
263 return ConstCellArray::Proxy(matPointer_, i);
271 template <
class T,
unsigned int SIZE>
272 TinyVectorView<T, SIZE>
273 getTinyVector(mxArray
const * t)
275 if(!ValueType<T>::check(t))
277 std::string msg = std::string(
"Input array must have type ") +
278 ValueType<T>::typeName() +
".";
279 mexErrMsgTxt(msg.c_str());
281 if(SIZE != mxGetNumberOfElements(t))
283 mexErrMsgTxt(
"getTinyVector(): Input array has wrong number of elements.");
286 return TinyVectorView<T, SIZE>((T *)mxGetData(t));
289 template <
unsigned int SIZE>
290 typename MultiArrayShape<SIZE>::type
291 getShape(mxArray
const * t)
293 if(!ValueType<Int32>::check(t))
295 std::string msg = std::string(
"Input array must have type 'int32'.");
296 mexErrMsgTxt(msg.c_str());
298 if(SIZE != mxGetNumberOfElements(t))
300 mexErrMsgTxt(
"getShape(): Input array has wrong number of elements.");
306 template <
int DIM,
class T>
307 MultiArrayView<DIM, T>
308 getMultiArray(mxArray
const * t)
310 typedef typename MultiArrayView<DIM, T>::difference_type Shape;
312 if(!ValueType<T>::check(t))
314 std::string msg = std::string(
"getMultiArray(): Input array must have type ") +
315 ValueType<T>::typeName() +
".";
316 mexErrMsgTxt(msg.c_str());
322 int mdim = mxGetNumberOfDimensions(t);
325 mexErrMsgTxt(
"getMultiArray(): Input array has too many dimensions.");
327 const mwSize * matlabShape = mxGetDimensions(t);
328 for(
int k=0; k<mdim; ++k)
330 shape[k] =
static_cast<typename Shape::value_type
>(matlabShape[k]);
332 for(
int k=mdim; k<DIM; ++k)
339 shape[0] =
static_cast<typename Shape::value_type
>(mxGetNumberOfElements(t));
341 return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
344 template <
int DIM,
class T>
345 MultiArrayView<DIM, T>
346 createMultiArray(
typename MultiArrayShape<DIM>::type
const & shape, mxArray * & t)
348 mwSize matlabShape[DIM];
349 for(
int k=0; k<DIM; ++k)
350 matlabShape[k] = static_cast<mwSize>(shape[k]);
351 t = mxCreateNumericArray(DIM, matlabShape, ValueType<T>::classID, mxREAL);
353 return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
356 template <
int DIM,
class T>
357 MultiArrayView<DIM, T>
358 createMultiArray(
typename MultiArrayShape<DIM>::type
const & shape, CellArray::Proxy t)
360 mwSize matlabShape[DIM];
361 for(
int k=0; k<DIM; ++k)
362 matlabShape[k] = static_cast<mwSize>(shape[k]);
363 t = mxCreateNumericArray(DIM, matlabShape, ValueType<T>::classID, mxREAL);
365 return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
369 inline MultiArrayView<1, T>
370 getArray(mxArray
const * t)
372 return getMultiArray<1, T>(t);
376 inline MultiArrayView<1, T>
383 inline MultiArrayView<1, T>
391 getMatrix(mxArray
const * t)
393 typedef typename MultiArrayView<2, T>::difference_type Shape;
395 if(!ValueType<T>::check(t))
397 std::string msg = std::string(
"getMatrix(): Input matrix must have type ") +
398 ValueType<T>::typeName() +
".";
399 mexErrMsgTxt(msg.c_str());
402 if(2 != mxGetNumberOfDimensions(t))
403 mexErrMsgTxt(
"getMatrix(): Input matrix must have 2 dimensions.");
405 const mwSize * matlabShape = mxGetDimensions(t);
406 Shape shape(static_cast<MultiArrayIndex>(matlabShape[0]),
407 static_cast<MultiArrayIndex>(matlabShape[1]));
409 return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
416 typedef typename MultiArrayView<2, T>::difference_type Shape;
418 Shape shape(rowCount, columnCount);
419 t = mxCreateNumericMatrix(rowCount, columnCount, ValueType<T>::classID, mxREAL);
421 return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
426 createMatrix(mwSize rowCount, mwSize columnCount, CellArray::Proxy t)
428 typedef typename MultiArrayView<2, T>::difference_type Shape;
430 Shape shape(rowCount, columnCount);
431 t = mxCreateNumericMatrix(rowCount, columnCount, ValueType<T>::classID, mxREAL);
433 return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
438 getImage(mxArray
const * t)
440 if(!ValueType<T>::check(t))
442 std::string msg = std::string(
"getImage(): Input matrix must have type ") +
443 ValueType<T>::typeName() +
".";
444 mexErrMsgTxt(msg.c_str());
447 if(2 != mxGetNumberOfDimensions(t))
448 mexErrMsgTxt(
"getImage(): Input matrix must have 2 dimensions.");
450 const mwSize * matlabShape = mxGetDimensions(t);
451 return BasicImageView<T>((T *)mxGetData(t),
static_cast<int>(matlabShape[0]),
452 static_cast<int>(matlabShape[1]));
457 createImage(mwSize width, mwSize height, mxArray * & t)
459 t = mxCreateNumericMatrix(width, height, ValueType<T>::classID, mxREAL);
461 return BasicImageView<T>((T *)mxGetData(t), width, height);
466 createImage(mwSize width, mwSize height, CellArray::Proxy t)
468 t = mxCreateNumericMatrix(width, height, ValueType<T>::classID, mxREAL);
470 return BasicImageView<T>((T *)mxGetData(t), width, height);
473 inline ConstCellArray
474 getCellArray(mxArray
const * t)
476 return ConstCellArray(t);
480 createCellArray(mwSize size, mxArray * & t)
482 mwSize matSize[] = { size };
483 t = mxCreateCellArray(1, matSize);
489 createCellArray(mwSize size, CellArray::Proxy t)
491 mwSize matSize[] = { size };
492 t = mxCreateCellArray(1, matSize);
497 inline ConstStructArray
498 getStructArray(mxArray
const * t)
500 return ConstStructArray(t);
505 getScalar(mxArray
const * t)
508 mexErrMsgTxt(
"getScalar() on empty input.");
509 if(!mxIsNumeric(t) && !mxIsLogical(t))
510 mexErrMsgTxt(
"getScalar(): argument is not numeric.");
511 return static_cast<T
>(mxGetScalar(t));
519 createMatrix<double>(1, 1, m)(0,0) =
static_cast<double>(v);
524 getString(mxArray
const * t)
527 mexErrMsgTxt(
"getString() on empty input.");
529 mexErrMsgTxt(
"getString(): argument is not a string.");
530 int size =
static_cast<int>(mxGetNumberOfElements(t) + 1);
531 ArrayVector<char> buf(size);
532 mxGetString(t, buf.begin(), size);
533 return std::string(buf.begin());
538 class CompileTimeError;
545 void argumentWasProvided()
const { }
555 mutable bool * argumentWasProvided_;
557 DefaultImpl(T v,
bool * argFlag = 0)
559 argumentWasProvided_(argFlag)
561 if(argumentWasProvided_ != 0)
562 *argumentWasProvided_ =
false;
565 void argumentWasProvided()
const
567 if(argumentWasProvided_ != 0)
568 *argumentWasProvided_ =
true;
575 mutable bool * argumentWasProvided_;
577 OptionalImpl(
bool * argFlag = 0)
578 : argumentWasProvided_(argFlag)
580 if(argumentWasProvided_ != 0)
581 *argumentWasProvided_ =
false;
584 void argumentWasProvided()
const
586 if(argumentWasProvided_ != 0)
587 *argumentWasProvided_ =
true;
593 inline detail::Required v_required()
595 return detail::Required();
599 inline detail::DefaultImpl<T> v_default(T in)
601 return detail::DefaultImpl<T>(in);
605 inline detail::DefaultImpl<T> v_default(T in,
bool & argFlag)
607 return detail::DefaultImpl<T>(in, &argFlag);
610 inline detail::OptionalImpl v_optional()
612 return detail::OptionalImpl();
615 inline detail::OptionalImpl v_optional(
bool& argFlag)
617 return detail::OptionalImpl(&argFlag);
628 const mxArray ** data_;
630 std::string createErrMsg(std::string name)
633 s1 =
"Required input '" + name +
"' not found in option struct!";
636 std::string createErrMsg(
int pos)
638 char tmp[10] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'};
639 std::string oi(1, tmp[pos%10]);
640 std::string s1 =
"Required input in signature of function at position: '"+ oi+
"' has not been supplied";
646 ConstStructArray options_;
649 typedef const mxArray * value_type;
650 typedef value_type & reference;
651 typedef value_type
const & const_reference;
652 typedef value_type * pointer;
653 typedef value_type
const * const_pointer;
654 typedef int size_type;
655 typedef int difference_type;
658 InputArray(size_type size, pointer data)
661 options_(isValid(size-1) && mxIsStruct(data_[size-1])
667 const_reference operator[]( difference_type i )
const
670 mexErrMsgTxt(
"Too few input arguments.");
674 value_type operator[]( std::string name)
const
676 std::string errMsg =
"Not Found " + name +
" in OptionStruct or OptionStruct not set";
678 mexErrMsgTxt(errMsg.c_str());
679 return options_[name];
684 size_type size()
const
689 bool isValid( difference_type i )
const
691 return i >= 0 && i < size_;
694 bool isValid(std::string name)
const
696 return options_.isValid(name);
699 bool isEmpty(difference_type i)
const
701 return mxIsEmpty(data_[i]);
704 bool isEmpty(std::string name)
const
706 return mxIsEmpty(options_[name]);
709 bool hasData(difference_type i)
const
711 return isValid(i) && !isEmpty(i);
714 bool hasData(std::string name)
const
716 return isValid(name) && !isEmpty(name);
719 template<
class Place>
720 mxClassID typeOf(Place posOrName)
722 return mxGetClassID((*
this)[posOrName]);
726 template <
class T,
class U,
class Place>
727 T errorOrDefault(detail::DefaultImpl<U>
const & o, Place posOrName)
729 return o.defaultValue_;
732 template <
class T,
class Place>
733 T errorOrDefault(detail::OptionalImpl, Place posOrName)
738 template <
class T,
class Place>
739 T errorOrDefault(detail::Required r, Place posOrName)
741 std::string a = createErrMsg(posOrName);
742 mexErrMsgTxt( a.c_str());
747 template <
class Place,
class ReqType>
748 int getEnum(Place posOrName, ReqType req, std::map<std::string, int>
const & converter)
750 if(!hasData(posOrName))
752 return errorOrDefault<int>(req, posOrName);
754 std::string enumAsString = matlab::getString((*
this)[posOrName]);
755 typename std::map<std::string, int>::const_iterator m = converter.find(enumAsString);
756 if(m == converter.end())
758 std::string msg = std::string(
"Unknown option: ") + enumAsString +
".";
759 mexErrMsgTxt(msg.c_str());
762 req.argumentWasProvided();
768 template <
class Place,
class ReqType>
769 std::string getString(Place posOrName, ReqType req)
771 if(!hasData(posOrName))
773 return errorOrDefault<std::string>(req, posOrName);
777 req.argumentWasProvided();
778 return matlab::getString((*
this)[posOrName]);
783 template <
class T,
class Place,
class ReqType>
784 T getScalar(Place posOrName, ReqType req)
786 if(!hasData(posOrName))
788 return errorOrDefault<T>(req, posOrName);
792 req.argumentWasProvided();
793 return matlab::getScalar<T>((*this)[posOrName]);
798 template <
class T,
class Place,
class ReqType,
class minClass,
class maxClass>
799 T getScalarMinMax(Place posOrName, ReqType req, minClass min_, maxClass max_)
801 T temp = this->getScalar<T>(posOrName, req);
802 if (!is_in_range(temp, min_, max_))
803 mexErrMsgTxt(
"Value out of bounds.");
807 template <
class T,
class Place,
class ReqType,
class iteratorType>
808 T getScalarVals(Place posOrName, ReqType req, iteratorType begin_, iteratorType end_)
810 T temp = this->getScalar<T>(posOrName, req);
811 for(iteratorType iter = begin_; iter != end_; ++iter)
813 if((*iter) == temp)
return temp;
815 mexErrMsgTxt(
"Value not allowed");
820 template <
class T,
class Place,
class ReqType,
class iteratorType>
821 T getScalarVals2D3D(Place posOrName, ReqType req, iteratorType begin2D_, iteratorType end2D_,
822 iteratorType begin3D_, iteratorType end3D_,
825 T temp = this->getScalar<T>(posOrName, req);
829 for(iteratorType iter = begin2D_; iter != end2D_; ++iter)
831 if((*iter) == temp)
return temp;
835 for(iteratorType iter = begin3D_; iter != end3D_; ++iter)
837 if((*iter) == temp)
return temp;
841 mexErrMsgTxt(
"dimVar specified must be 2 or 3");
843 mexErrMsgTxt(
"Value not allowed");
846 template <
class Place,
class ReqType>
847 bool getBool(Place posOrName, ReqType req)
849 return this->getScalarMinMax<int>(posOrName, req, 0, 1) != 0;
853 template <
unsigned int N,
class T,
class Place,
class ReqType>
854 MultiArrayView<N,T> getMultiArray(Place posOrName, ReqType req)
856 if(!hasData(posOrName))
858 return errorOrDefault< MultiArrayView<N,T> >(req, posOrName);
862 req.argumentWasProvided();
863 value_type temp = (*this)[posOrName];
864 return matlab::getMultiArray<N,T>(temp);
868 template <
class T,
class Place,
class ReqType>
869 BasicImageView<T> getImage(Place posOrName, ReqType req)
871 if(!hasData(posOrName))
873 return errorOrDefault<BasicImageView<T> >(req, posOrName);
877 req.argumentWasProvided();
878 value_type temp = (*this)[posOrName];
879 return matlab::getImage<T>(temp);
883 template<
class T,
unsigned int sze,
class Place,
class ReqType>
884 TinyVectorView< T, sze> getTinyVector(Place posOrName, ReqType req)
886 if(!hasData(posOrName))
888 return errorOrDefault<TinyVectorView< T, sze> >(req, posOrName);
892 req.argumentWasProvided();
893 value_type temp = (*this)[posOrName];
894 return matlab::getTinyVector< T, sze>(temp);
898 template<
unsigned int sze,
class Place,
class ReqType>
899 TinyVectorView<MultiArrayIndex, sze> getShape(Place posOrName, ReqType req)
901 if(!hasData(posOrName))
903 return errorOrDefault<TinyVectorView<MultiArrayIndex, sze> >(req, posOrName);
907 req.argumentWasProvided();
908 value_type temp = (*this)[posOrName];
909 return matlab::getShape<sze>(temp);
914 template<
class Place,
class ReqType>
915 int getDimOfInput(Place posOrName, ReqType req)
917 if(!hasData(posOrName))
919 return errorOrDefault<int>(req, posOrName);
923 req.argumentWasProvided();
924 return mxGetNumberOfDimensions((*
this)[posOrName]);
928 template<
class ReqType>
929 ConstCellArray getCellArray(
int posOrName, ReqType req)
931 if(!hasData(posOrName))
933 return errorOrDefault<ConstCellArray>(req, posOrName);
937 req.argumentWasProvided();
938 value_type temp = (*this)[posOrName];
939 return matlab::getCellArray(temp);
943 template<
class ReqType>
944 ConstCellArray getCellArray(std::string posOrName, ReqType req)
946 CompileTimeError ERROR__Const_Cell_Array_May_Not_Be_In_Option_Struct;
947 return ConstCellArray();
956 std::string createErrMsgOut(
int pos)
958 char tmp[10] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'};
959 std::string oi(1, tmp[pos%10]);
960 std::string s1 =
"Required Output at position: '" + oi +
"' has not been supplied";
965 typedef mxArray * value_type;
966 typedef value_type & reference;
967 typedef value_type
const & const_reference;
968 typedef value_type * pointer;
969 typedef value_type
const * const_pointer;
970 typedef int size_type;
971 typedef int difference_type;
973 OutputArray(size_type size, pointer data)
978 reference operator[]( difference_type i )
981 mexErrMsgTxt(
"Too few output arguments.");
985 const_reference operator[]( difference_type i )
const
988 mexErrMsgTxt(
"Too few output arguments.");
992 size_type size()
const
997 bool isValid( difference_type i )
const
999 return i >= 0 && i < size_;
1002 bool isEmpty(difference_type i){
1003 return mxIsEmpty(data_[i]);
1007 T errorOrDefault(detail::OptionalImpl
const & o,
int Pos)
1013 T errorOrDefault(detail::Required r,
int Pos)
1015 mexErrMsgTxt(createErrMsgOut(Pos).c_str());
1020 template <
unsigned int DIM,
class T,
class ReqType>
1021 MultiArrayView<DIM, T> createMultiArray(
int pos,ReqType req,
1022 const TinyVector<int, DIM> & shape)
1025 return errorOrDefault<MultiArrayView<DIM, T> >(req, pos);
1026 req.argumentWasProvided();
1027 return matlab::createMultiArray<DIM, T>(shape, (*this)[pos]);
1030 template <
class T,
class ReqType>
1031 BasicImageView<T> createImage(
int pos, ReqType req,
1032 mwSize width, mwSize height)
1035 return errorOrDefault<BasicImageView<T> >(req, pos);
1036 req.argumentWasProvided();
1037 return matlab::createImage<T>(width, height, (*this)[pos]);
1040 template <
class T,
class ReqType>
1041 BasicImageView<T> createImage(
int pos, ReqType req,
1042 typename MultiArrayShape<2>::type
const & shape)
1044 return createImage<T>(pos, req, shape[1], shape[0]);
1047 template <
class T,
class ReqType>
1048 T* createScalar(
int pos, ReqType req)
1051 return errorOrDefault<T*>(req, pos);
1052 req.argumentWasProvided();
1053 BasicImageView<T> temp = matlab::createImage<T>(1, 1, (*this)[pos]);
1057 template <
class T,
class ReqType>
1058 void createScalar(
int pos, ReqType req, T val)
1062 errorOrDefault<T>(req, pos);
1065 req.argumentWasProvided();
1066 BasicImageView<T> temp = matlab::createImage<T>(1, 1, (*this)[pos]);
1070 template <
class ReqType>
1071 ConstCellArray createCellArray(
int pos, ReqType req, mwSize sze)
1074 return errorOrDefault<ConstCellArray>(req, pos);
1075 return matlab::createCellArray(sze, (*
this)[pos]);
1084 using namespace vigra;
1091 int cantorPair(
int x,
int y){
1092 return (
int)(((x+y)*(x+y+1))/2+y);
1095 int cantorPair(
int x,
int y,
int z){
1096 return cantorPair(cantorPair(x,y),z);
1099 template <
int x,
int y>
1101 enum { value = (int)(((x+y)*(x+y+1))/2+y)};
1104 template <
int x,
int y,
int z>
1106 enum { value = cP<cP<x, y>::value, z>::value};
1110 inline bool is_in_range(T in, T min, T max)
1112 return (in >= min && in <= max);
1115 inline bool is_in_range(T in, std::string min, T max)
1121 inline bool is_in_range(T in, T min, std::string max)
1134 return (s1[0] < s2[0]);
1136 return s1[1] < s2[1];
1145 std::map<TinyVector<int,2>, T,ShapeCmp> data;
1149 void assign(
int i = 1,
int j = 1){
1153 SparseArray(
int i = 1 ,
int j = 1){
1160 template<
class indexType>
1161 T& operator()(indexType i_, indexType j_){
1165 typename std::map<TinyVector<int,2>, T, ShapeCmp>::iterator iter;
1167 return data[newShapew];
1170 template<
class indexType>
1171 const T
get(indexType i_, indexType j_){
1175 if(data.find(newShape) == data.end())
return 0;
1176 else return data.find(newShape)->second;
1180 void mapToMxArray(mxArray * & in){
1182 int len = data.size();
1183 in = mxCreateSparse(width, length, len, mxREAL);
1184 int* jc = mxGetJc(in);
1185 int* ir = mxGetIr(in);
1186 double* pr = mxGetPr(in);
1191 typename std::map<TinyVector<int,2>, T, ShapeCmp>::iterator iter;
1196 for( iter = data.begin(); iter != data.end(); ++iter ) {
1197 newShape = iter->first;
1198 ir[ii] = newShape[1];
1199 pr[ii] = iter->second;
1200 if(newShape[0] != curjc){
1201 curjc = newShape[0] ;
1213 enum DataDimension {IMAGE = 2, VOLUME = 3};
1219 void vigraMexFunction(vigra::matlab::OutputArray, vigra::matlab::InputArray);
1221 #ifndef VIGRA_CUSTOM_MEXFUNCTION
1228 void mexFunction(
int nlhs, mxArray *plhs[],
1229 int nrhs,
const mxArray *prhs[])
1233 vigra::matlab::InputArray inputs(nrhs, prhs);
1234 vigra::matlab::OutputArray outputs(nlhs, plhs);
1236 vigraMexFunction(outputs, inputs);
1238 catch(std::exception & e)
1240 mexErrMsgTxt(e.what());
1247 #define VIGRA_CREATE_ENUM_AND_STD_MAP2(mapName, item1, item2) \
1248 const int item1 = 1;\
1249 const int item2 = 2;\
1250 std::map<std::string,int> mapName;\
1251 mapName[#item1] = (int)item1;\
1252 mapName[#item2] = (int)item2;\
1255 #define VIGRA_CREATE_ENUM_AND_STD_MAP3(mapName, item1, item2, item3) \
1256 const int item1 = 1;\
1257 const int item2 = 2;\
1258 const int item3 = 3;\
1259 std::map<std::string,int> mapName;\
1260 mapName[#item1] = (int)item1;\
1261 mapName[#item2] = (int)item2;\
1262 mapName[#item3] = (int)item3;\
1265 #define VIGRA_CREATE_ENUM_AND_STD_MAP4(mapName, item1, item2, item3, item4) \
1266 const int item1 = 1;\
1267 const int item2 = 2;\
1268 const int item3 = 3;\
1269 const int item4 = 4;\
1270 std::map<std::string,int> mapName;\
1271 mapName[#item1] = (int)item1;\
1272 mapName[#item2] = (int)item2;\
1273 mapName[#item3] = (int)item3;\
1274 mapName[#item4] = (int)item4;\
1276 #define VIGRA_CREATE_ENUM_AND_STD_MAP5(mapName, item1, item2, item3, item4, item5) \
1277 const int item1 = 1;\
1278 const int item2 = 2;\
1279 const int item3 = 3;\
1280 const int item4 = 4;\
1281 const int item5 = 5;\
1282 std::map<std::string, int> mapName;\
1283 mapName[#item1] = (int)item1;\
1284 mapName[#item2] = (int)item2;\
1285 mapName[#item3] = (int)item3;\
1286 mapName[#item4] = (int)item4;\
1287 mapName[#item5] = (int)item5;\
1289 #define VIGRA_CREATE_ENUM_AND_STD_MAP6(mapName, item1, item2, item3, item4, item5, item6) \
1290 const int item1 = 1;\
1291 const int item2 = 2;\
1292 const int item3 = 3;\
1293 const int item4 = 4;\
1294 const int item5 = 5;\
1295 const int item6 = 6;\
1296 std::map<std::string,int> mapName;\
1297 mapName[#item1] = (int)item1;\
1298 mapName[#item2] = (int)item2;\
1299 mapName[#item3] = (int)item3;\
1300 mapName[#item4] = (int)item4;\
1301 mapName[#item5] = (int)item5;\
1302 mapName[#item6] = (int)item6;\
1304 #endif // VIGRA_MATLAB_HXX
MultiArrayIndex rowCount(const MultiArrayView< 2, T, C > &x)
Definition: matrix.hxx:669
detail::SelectIntegerType< 8, detail::UnsignedIntTypes >::type UInt8
8-bit unsigned int
Definition: sized_int.hxx:179
std::ptrdiff_t MultiArrayIndex
Definition: multi_shape.hxx:55
detail::SelectIntegerType< 64, detail::SignedIntTypes >::type Int64
64-bit signed int
Definition: sized_int.hxx:177
detail::SelectIntegerType< 16, detail::SignedIntTypes >::type Int16
16-bit signed int
Definition: sized_int.hxx:173
detail::SelectIntegerType< 32, detail::SignedIntTypes >::type Int32
32-bit signed int
Definition: sized_int.hxx:175
TinyVector< MultiArrayIndex, N > type
Definition: multi_shape.hxx:237
Class for fixed size vectors.This class contains an array of size SIZE of the specified VALUETYPE...
Definition: accessor.hxx:940
MultiArrayIndex columnCount(const MultiArrayView< 2, T, C > &x)
Definition: matrix.hxx:682
detail::SelectIntegerType< 16, detail::UnsignedIntTypes >::type UInt16
16-bit unsigned int
Definition: sized_int.hxx:181
detail::SelectIntegerType< 8, detail::SignedIntTypes >::type Int8
8-bit signed int
Definition: sized_int.hxx:171