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"
62 int cantorPair(
int x,
int y){
63 return (
int)(((x+y)*(x+y+1))/2+y);
66 int cantorPair(
int x,
int y,
int z){
67 return cantorPair(cantorPair(x,y),z);
70 template <
int x,
int y>
72 enum { value = (int)(((x+y)*(x+y+1))/2+y)};
75 template <
int x,
int y,
int z>
77 enum { value = cP<cP<x, y>::value, z>::value};
81 inline bool is_in_range(T in, T min, T max)
83 return (in >= min && in <= max);
86 inline bool is_in_range(T in, std::string min, T max)
92 inline bool is_in_range(T in, T min, std::string max)
100 #define VIGRA_MATLAB_VALUETYPE_UTIL(type, functionName, typeID, matTypeName) \
102 struct ValueType<type> \
104 static bool check(mxArray const * t) \
106 return mxIs##functionName(t); \
109 static mxClassID const classID = typeID; \
111 static std::string typeName() \
113 return #matTypeName; \
117 VIGRA_MATLAB_VALUETYPE_UTIL(
double, Double, mxDOUBLE_CLASS,
double)
118 VIGRA_MATLAB_VALUETYPE_UTIL(
float, Single, mxSINGLE_CLASS, single)
119 VIGRA_MATLAB_VALUETYPE_UTIL(
Int8, Int8, mxINT8_CLASS, int8)
120 VIGRA_MATLAB_VALUETYPE_UTIL(
UInt8, Uint8, mxUINT8_CLASS, uint8)
121 VIGRA_MATLAB_VALUETYPE_UTIL(
Int16, Int16, mxINT16_CLASS, int16)
122 VIGRA_MATLAB_VALUETYPE_UTIL(
UInt16, Uint16, mxUINT16_CLASS, uint16)
124 #if VIGRA_BITSOF_INT == 32
125 VIGRA_MATLAB_VALUETYPE_UTIL(
int,
Int32, mxINT32_CLASS, int32)
126 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned int, Uint32, mxUINT32_CLASS, uint32)
127 #elif VIGRA_BITSOF_INT == 64
128 VIGRA_MATLAB_VALUETYPE_UTIL(
int,
Int64, mxINT64_CLASS, int64)
129 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned int, Uint64, mxUINT64_CLASS, uint64)
132 #if VIGRA_BITSOF_LONG == 32
133 VIGRA_MATLAB_VALUETYPE_UTIL(
long,
Int32, mxINT32_CLASS, int32)
134 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long, Uint32, mxUINT32_CLASS, uint32)
135 #elif VIGRA_BITSOF_LONG == 64
136 VIGRA_MATLAB_VALUETYPE_UTIL(
long,
Int64, mxINT64_CLASS, int64)
137 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long, Uint64, mxUINT64_CLASS, uint64)
140 #if VIGRA_BITSOF_LONG_LONG == 32
141 VIGRA_MATLAB_VALUETYPE_UTIL(
long long,
Int32, mxINT32_CLASS, int32)
142 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long long, Uint32, mxUINT32_CLASS, uint32)
143 #elif VIGRA_BITSOF_LONG_LONG == 64
144 VIGRA_MATLAB_VALUETYPE_UTIL(
long long,
Int64, mxINT64_CLASS, int64)
145 VIGRA_MATLAB_VALUETYPE_UTIL(
unsigned long long, Uint64, mxUINT64_CLASS, uint64)
148 #undef VIGRA_MATLAB_VALUETYPE_UTIL
150 class ConstStructArray
153 mxArray * matPointer_;
159 mxArray * matPointer_;
162 Proxy(mxArray * matPointer,
int index)
163 : matPointer_(matPointer),
167 operator const mxArray *()
const
169 return mxGetFieldByNumber(matPointer_, 0, index_);
173 ConstStructArray(
const mxArray * matPointer = 0)
174 : matPointer_(const_cast<mxArray *>(matPointer))
176 if(matPointer != 0 && !mxIsStruct(matPointer))
177 mexErrMsgTxt(
"StructArray(mxArray *): Argument must be a Matlab struct array.");
180 Proxy operator[](
const char * field_name)
const
183 mexErrMsgTxt(
"StructArray::operator[]: Cannot access uninitialized struct array.");
185 int i = mxGetFieldNumber(matPointer_, field_name);
187 mexErrMsgTxt(
"StructArray::operator[]: Unknown field name.");
189 return Proxy(matPointer_, i);
192 Proxy operator[](std::string field_name)
const
194 return operator[](field_name.c_str());
199 return matPointer_ != 0;
202 bool isValid(
const char * field_name)
const
204 return isValid() && mxGetFieldNumber(matPointer_, field_name) != -1;
207 bool isValid(std::string field_name)
const
209 return isValid(field_name.c_str());
216 mxArray * matPointer_;
223 mxArray * matPointer_;
226 Proxy(mxArray * matPointer,
int index)
227 : matPointer_(matPointer),
231 operator const mxArray *()
const
233 return mxGetCell(matPointer_, index_);
237 ConstCellArray(
const mxArray * matPointer = 0)
238 : matPointer_(const_cast<mxArray *>(matPointer)),
241 if(matPointer != 0 && !mxIsCell(matPointer))
242 mexErrMsgTxt(
"CellArray(mxArray *): Argument must be a Matlab cell array.");
244 size_ =
static_cast<int>(mxGetNumberOfElements(matPointer));
249 Proxy operator[](
int i)
const
252 mexErrMsgTxt(
"CellArray::operator[]: Index out of range.");
253 return Proxy(matPointer_, i);
261 bool isValid(
int i )
const
263 return i >= 0 && i < size_;
269 :
public ConstCellArray
274 :
public ConstCellArray::Proxy
276 Proxy(mxArray * matPointer,
int index)
277 : ConstCellArray::Proxy(matPointer, index)
280 void operator=(mxArray * v)
282 mxDestroyArray(mxGetCell(matPointer_, index_));
283 mxSetCell(matPointer_, index_, v);
287 CellArray(
const mxArray * matPointer)
288 : ConstCellArray(matPointer)
291 Proxy operator[](
int i)
294 mexErrMsgTxt(
"CellArray::operator[]: Index out of range.");
295 return Proxy(matPointer_, i);
298 ConstCellArray::Proxy operator[](
int i)
const
301 mexErrMsgTxt(
"CellArray::operator[]: Index out of range.");
302 return ConstCellArray::Proxy(matPointer_, i);
310 template <
class T,
unsigned int SIZE>
311 TinyVectorView<T, SIZE>
312 getTinyVector(mxArray
const * t)
314 if(!ValueType<T>::check(t))
316 std::string msg = std::string(
"Input array must have type ") +
317 ValueType<T>::typeName() +
".";
318 mexErrMsgTxt(msg.c_str());
320 if(SIZE != mxGetNumberOfElements(t))
322 mexErrMsgTxt(
"getTinyVector(): Input array has wrong number of elements.");
325 return TinyVectorView<T, SIZE>((T *)mxGetData(t));
328 template <
unsigned int SIZE>
329 typename MultiArrayShape<SIZE>::type
330 getShape(mxArray
const * t)
332 if(!ValueType<Int32>::check(t))
334 std::string msg = std::string(
"Input array must have type 'int32'.");
335 mexErrMsgTxt(msg.c_str());
337 if(SIZE != mxGetNumberOfElements(t))
339 mexErrMsgTxt(
"getShape(): Input array has wrong number of elements.");
345 template <
int DIM,
class T>
346 MultiArrayView<DIM, T>
347 getMultiArray(mxArray
const * t)
349 typedef typename MultiArrayView<DIM, T>::difference_type Shape;
351 if(!ValueType<T>::check(t))
353 std::string msg = std::string(
"getMultiArray(): Input array must have type ") +
354 ValueType<T>::typeName() +
".";
355 mexErrMsgTxt(msg.c_str());
361 int mdim = mxGetNumberOfDimensions(t);
364 mexErrMsgTxt(
"getMultiArray(): Input array has too many dimensions.");
366 const mwSize * matlabShape = mxGetDimensions(t);
367 for(
int k=0; k<mdim; ++k)
369 shape[k] =
static_cast<typename Shape::value_type
>(matlabShape[k]);
371 for(
int k=mdim; k<DIM; ++k)
378 shape[0] =
static_cast<typename Shape::value_type
>(mxGetNumberOfElements(t));
380 return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
383 template <
int DIM,
class T>
384 MultiArrayView<DIM, T>
385 createMultiArray(
typename MultiArrayShape<DIM>::type
const & shape, mxArray * & t)
387 mwSize matlabShape[DIM];
388 for(
int k=0; k<DIM; ++k)
389 matlabShape[k] = static_cast<mwSize>(shape[k]);
390 t = mxCreateNumericArray(DIM, matlabShape, ValueType<T>::classID, mxREAL);
392 return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
395 template <
int DIM,
class T>
396 MultiArrayView<DIM, T>
397 createMultiArray(
typename MultiArrayShape<DIM>::type
const & shape, CellArray::Proxy t)
399 mwSize matlabShape[DIM];
400 for(
int k=0; k<DIM; ++k)
401 matlabShape[k] = static_cast<mwSize>(shape[k]);
402 t = mxCreateNumericArray(DIM, matlabShape, ValueType<T>::classID, mxREAL);
404 return MultiArrayView<DIM, T>(shape, (T *)mxGetData(t));
408 inline MultiArrayView<1, T>
409 getArray(mxArray
const * t)
411 return getMultiArray<1, T>(t);
415 inline MultiArrayView<1, T>
422 inline MultiArrayView<1, T>
430 getMatrix(mxArray
const * t)
432 typedef typename MultiArrayView<2, T>::difference_type Shape;
434 if(!ValueType<T>::check(t))
436 std::string msg = std::string(
"getMatrix(): Input matrix must have type ") +
437 ValueType<T>::typeName() +
".";
438 mexErrMsgTxt(msg.c_str());
441 if(2 != mxGetNumberOfDimensions(t))
442 mexErrMsgTxt(
"getMatrix(): Input matrix must have 2 dimensions.");
444 const mwSize * matlabShape = mxGetDimensions(t);
445 Shape shape(static_cast<MultiArrayIndex>(matlabShape[0]),
446 static_cast<MultiArrayIndex>(matlabShape[1]));
448 return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
455 typedef typename MultiArrayView<2, T>::difference_type Shape;
457 Shape shape(rowCount, columnCount);
458 t = mxCreateNumericMatrix(rowCount, columnCount, ValueType<T>::classID, mxREAL);
460 return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
465 createMatrix(mwSize rowCount, mwSize columnCount, CellArray::Proxy t)
467 typedef typename MultiArrayView<2, T>::difference_type Shape;
469 Shape shape(rowCount, columnCount);
470 t = mxCreateNumericMatrix(rowCount, columnCount, ValueType<T>::classID, mxREAL);
472 return MultiArrayView<2, T>(shape, (T *)mxGetData(t));
477 getImage(mxArray
const * t)
479 if(!ValueType<T>::check(t))
481 std::string msg = std::string(
"getImage(): Input matrix must have type ") +
482 ValueType<T>::typeName() +
".";
483 mexErrMsgTxt(msg.c_str());
486 if(2 != mxGetNumberOfDimensions(t))
487 mexErrMsgTxt(
"getImage(): Input matrix must have 2 dimensions.");
489 const mwSize * matlabShape = mxGetDimensions(t);
490 return BasicImageView<T>((T *)mxGetData(t),
static_cast<int>(matlabShape[0]),
491 static_cast<int>(matlabShape[1]));
496 createImage(mwSize width, mwSize height, mxArray * & t)
498 t = mxCreateNumericMatrix(width, height, ValueType<T>::classID, mxREAL);
500 return BasicImageView<T>((T *)mxGetData(t), width, height);
505 createImage(mwSize width, mwSize height, CellArray::Proxy t)
507 t = mxCreateNumericMatrix(width, height, ValueType<T>::classID, mxREAL);
509 return BasicImageView<T>((T *)mxGetData(t), width, height);
512 inline ConstCellArray
513 getCellArray(mxArray
const * t)
515 return ConstCellArray(t);
519 createCellArray(mwSize size, mxArray * & t)
521 mwSize matSize[] = { size };
522 t = mxCreateCellArray(1, matSize);
528 createCellArray(mwSize size, CellArray::Proxy t)
530 mwSize matSize[] = { size };
531 t = mxCreateCellArray(1, matSize);
536 inline ConstStructArray
537 getStructArray(mxArray
const * t)
539 return ConstStructArray(t);
544 getScalar(mxArray
const * t)
547 mexErrMsgTxt(
"getScalar() on empty input.");
548 if(!mxIsNumeric(t) && !mxIsLogical(t))
549 mexErrMsgTxt(
"getScalar(): argument is not numeric.");
550 return static_cast<T
>(mxGetScalar(t));
558 createMatrix<double>(1, 1, m)(0,0) =
static_cast<double>(v);
563 getString(mxArray
const * t)
566 mexErrMsgTxt(
"getString() on empty input.");
568 mexErrMsgTxt(
"getString(): argument is not a string.");
569 int size =
static_cast<int>(mxGetNumberOfElements(t) + 1);
570 ArrayVector<char> buf(size);
571 mxGetString(t, buf.begin(), size);
572 return std::string(buf.begin());
577 class CompileTimeError;
584 void argumentWasProvided()
const { }
594 mutable bool * argumentWasProvided_;
596 DefaultImpl(T v,
bool * argFlag = 0)
598 argumentWasProvided_(argFlag)
600 if(argumentWasProvided_ != 0)
601 *argumentWasProvided_ =
false;
604 void argumentWasProvided()
const
606 if(argumentWasProvided_ != 0)
607 *argumentWasProvided_ =
true;
614 mutable bool * argumentWasProvided_;
616 OptionalImpl(
bool * argFlag = 0)
617 : argumentWasProvided_(argFlag)
619 if(argumentWasProvided_ != 0)
620 *argumentWasProvided_ =
false;
623 void argumentWasProvided()
const
625 if(argumentWasProvided_ != 0)
626 *argumentWasProvided_ =
true;
632 inline detail::Required v_required()
634 return detail::Required();
638 inline detail::DefaultImpl<T> v_default(T in)
640 return detail::DefaultImpl<T>(in);
644 inline detail::DefaultImpl<T> v_default(T in,
bool & argFlag)
646 return detail::DefaultImpl<T>(in, &argFlag);
649 inline detail::OptionalImpl v_optional()
651 return detail::OptionalImpl();
654 inline detail::OptionalImpl v_optional(
bool& argFlag)
656 return detail::OptionalImpl(&argFlag);
667 const mxArray ** data_;
669 std::string createErrMsg(std::string name)
672 s1 =
"Required input '" + name +
"' not found in option struct!";
675 std::string createErrMsg(
int pos)
677 char tmp[10] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'};
678 std::string oi(1, tmp[pos%10]);
679 std::string s1 =
"Required input in signature of function at position: '"+ oi+
"' has not been supplied";
685 ConstStructArray options_;
688 typedef const mxArray * value_type;
689 typedef value_type & reference;
690 typedef value_type
const & const_reference;
691 typedef value_type * pointer;
692 typedef value_type
const * const_pointer;
693 typedef int size_type;
694 typedef int difference_type;
697 InputArray(size_type size, pointer data)
700 options_(isValid(size-1) && mxIsStruct(data_[size-1])
706 const_reference operator[]( difference_type i )
const
709 mexErrMsgTxt(
"Too few input arguments.");
713 value_type operator[]( std::string name)
const
715 std::string errMsg =
"Not Found " + name +
" in OptionStruct or OptionStruct not set";
717 mexErrMsgTxt(errMsg.c_str());
718 return options_[name];
723 size_type size()
const
728 bool isValid( difference_type i )
const
730 return i >= 0 && i < size_;
733 bool isValid(std::string name)
const
735 return options_.isValid(name);
738 bool isEmpty(difference_type i)
const
740 return mxIsEmpty(data_[i]);
743 bool isEmpty(std::string name)
const
745 return mxIsEmpty(options_[name]);
748 bool hasData(difference_type i)
const
750 return isValid(i) && !isEmpty(i);
753 bool hasData(std::string name)
const
755 return isValid(name) && !isEmpty(name);
758 template<
class Place>
759 mxClassID typeOf(Place posOrName)
761 return mxGetClassID((*
this)[posOrName]);
765 template <
class T,
class U,
class Place>
766 T errorOrDefault(detail::DefaultImpl<U>
const & o, Place posOrName)
768 return o.defaultValue_;
771 template <
class T,
class Place>
772 T errorOrDefault(detail::OptionalImpl, Place posOrName)
777 template <
class T,
class Place>
778 T errorOrDefault(detail::Required r, Place posOrName)
780 std::string a = createErrMsg(posOrName);
781 mexErrMsgTxt( a.c_str());
786 template <
class Place,
class ReqType>
787 int getEnum(Place posOrName, ReqType req, std::map<std::string, int>
const & converter)
789 if(!hasData(posOrName))
791 return errorOrDefault<int>(req, posOrName);
793 std::string enumAsString = matlab::getString((*
this)[posOrName]);
794 typename std::map<std::string, int>::const_iterator m = converter.find(enumAsString);
795 if(m == converter.end())
797 std::string msg = std::string(
"Unknown option: ") + enumAsString +
".";
798 mexErrMsgTxt(msg.c_str());
801 req.argumentWasProvided();
807 template <
class Place,
class ReqType>
808 std::string getString(Place posOrName, ReqType req)
810 if(!hasData(posOrName))
812 return errorOrDefault<std::string>(req, posOrName);
816 req.argumentWasProvided();
817 return matlab::getString((*
this)[posOrName]);
822 template <
class T,
class Place,
class ReqType>
823 T getScalar(Place posOrName, ReqType req)
825 if(!hasData(posOrName))
827 return errorOrDefault<T>(req, posOrName);
831 req.argumentWasProvided();
832 return matlab::getScalar<T>((*this)[posOrName]);
837 template <
class T,
class Place,
class ReqType,
class minClass,
class maxClass>
838 T getScalarMinMax(Place posOrName, ReqType req, minClass min_, maxClass max_)
840 T temp = this->getScalar<T>(posOrName, req);
841 if (!is_in_range(temp, min_, max_))
842 mexErrMsgTxt(
"Value out of bounds.");
846 template <
class T,
class Place,
class ReqType,
class iteratorType>
847 T getScalarVals(Place posOrName, ReqType req, iteratorType begin_, iteratorType end_)
849 T temp = this->getScalar<T>(posOrName, req);
850 for(iteratorType iter = begin_; iter != end_; ++iter)
852 if((*iter) == temp)
return temp;
854 mexErrMsgTxt(
"Value not allowed");
859 template <
class T,
class Place,
class ReqType,
class iteratorType>
860 T getScalarVals2D3D(Place posOrName, ReqType req, iteratorType begin2D_, iteratorType end2D_,
861 iteratorType begin3D_, iteratorType end3D_,
864 T temp = this->getScalar<T>(posOrName, req);
868 for(iteratorType iter = begin2D_; iter != end2D_; ++iter)
870 if((*iter) == temp)
return temp;
874 for(iteratorType iter = begin3D_; iter != end3D_; ++iter)
876 if((*iter) == temp)
return temp;
880 mexErrMsgTxt(
"dimVar specified must be 2 or 3");
882 mexErrMsgTxt(
"Value not allowed");
885 template <
class Place,
class ReqType>
886 bool getBool(Place posOrName, ReqType req)
888 return this->getScalarMinMax<int>(posOrName, req, 0, 1) != 0;
892 template <
unsigned int N,
class T,
class Place,
class ReqType>
893 MultiArrayView<N,T> getMultiArray(Place posOrName, ReqType req)
895 if(!hasData(posOrName))
897 return errorOrDefault< MultiArrayView<N,T> >(req, posOrName);
901 req.argumentWasProvided();
902 value_type temp = (*this)[posOrName];
903 return matlab::getMultiArray<N,T>(temp);
907 template <
class T,
class Place,
class ReqType>
908 BasicImageView<T> getImage(Place posOrName, ReqType req)
910 if(!hasData(posOrName))
912 return errorOrDefault<BasicImageView<T> >(req, posOrName);
916 req.argumentWasProvided();
917 value_type temp = (*this)[posOrName];
918 return matlab::getImage<T>(temp);
922 template<
class T,
unsigned int sze,
class Place,
class ReqType>
923 TinyVectorView< T, sze> getTinyVector(Place posOrName, ReqType req)
925 if(!hasData(posOrName))
927 return errorOrDefault<TinyVectorView< T, sze> >(req, posOrName);
931 req.argumentWasProvided();
932 value_type temp = (*this)[posOrName];
933 return matlab::getTinyVector< T, sze>(temp);
937 template<
unsigned int sze,
class Place,
class ReqType>
938 TinyVectorView<MultiArrayIndex, sze> getShape(Place posOrName, ReqType req)
940 if(!hasData(posOrName))
942 return errorOrDefault<TinyVectorView<MultiArrayIndex, sze> >(req, posOrName);
946 req.argumentWasProvided();
947 value_type temp = (*this)[posOrName];
948 return matlab::getShape<sze>(temp);
953 template<
class Place,
class ReqType>
954 int getDimOfInput(Place posOrName, ReqType req)
956 if(!hasData(posOrName))
958 return errorOrDefault<int>(req, posOrName);
962 req.argumentWasProvided();
963 return mxGetNumberOfDimensions((*
this)[posOrName]);
967 template<
class ReqType>
968 ConstCellArray getCellArray(
int posOrName, ReqType req)
970 if(!hasData(posOrName))
972 return errorOrDefault<ConstCellArray>(req, posOrName);
976 req.argumentWasProvided();
977 value_type temp = (*this)[posOrName];
978 return matlab::getCellArray(temp);
982 template<
class ReqType>
983 ConstCellArray getCellArray(std::string posOrName, ReqType req)
985 CompileTimeError ERROR__Const_Cell_Array_May_Not_Be_In_Option_Struct;
986 return ConstCellArray();
995 std::string createErrMsgOut(
int pos)
997 char tmp[10] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'};
998 std::string oi(1, tmp[pos%10]);
999 std::string s1 =
"Required Output at position: '" + oi +
"' has not been supplied";
1004 typedef mxArray * value_type;
1005 typedef value_type & reference;
1006 typedef value_type
const & const_reference;
1007 typedef value_type * pointer;
1008 typedef value_type
const * const_pointer;
1009 typedef int size_type;
1010 typedef int difference_type;
1012 OutputArray(size_type size, pointer data)
1017 reference operator[]( difference_type i )
1020 mexErrMsgTxt(
"Too few output arguments.");
1024 const_reference operator[]( difference_type i )
const
1027 mexErrMsgTxt(
"Too few output arguments.");
1031 size_type size()
const
1036 bool isValid( difference_type i )
const
1038 return i >= 0 && i < size_;
1041 bool isEmpty(difference_type i){
1042 return mxIsEmpty(data_[i]);
1046 T errorOrDefault(detail::OptionalImpl
const & o,
int Pos)
1052 T errorOrDefault(detail::Required r,
int Pos)
1054 mexErrMsgTxt(createErrMsgOut(Pos).c_str());
1059 template <
unsigned int DIM,
class T,
class ReqType>
1060 MultiArrayView<DIM, T> createMultiArray(
int pos,ReqType req,
1061 const TinyVector<int, DIM> & shape)
1064 return errorOrDefault<MultiArrayView<DIM, T> >(req, pos);
1065 req.argumentWasProvided();
1066 return matlab::createMultiArray<DIM, T>(shape, (*this)[pos]);
1069 template <
class T,
class ReqType>
1070 BasicImageView<T> createImage(
int pos, ReqType req,
1071 mwSize width, mwSize height)
1074 return errorOrDefault<BasicImageView<T> >(req, pos);
1075 req.argumentWasProvided();
1076 return matlab::createImage<T>(width, height, (*this)[pos]);
1079 template <
class T,
class ReqType>
1080 BasicImageView<T> createImage(
int pos, ReqType req,
1083 return createImage<T>(pos, req, shape[1], shape[0]);
1086 template <
class T,
class ReqType>
1087 T* createScalar(
int pos, ReqType req)
1090 return errorOrDefault<T*>(req, pos);
1091 req.argumentWasProvided();
1092 BasicImageView<T> temp = matlab::createImage<T>(1, 1, (*this)[pos]);
1096 template <
class T,
class ReqType>
1097 void createScalar(
int pos, ReqType req, T val)
1101 errorOrDefault<T>(req, pos);
1104 req.argumentWasProvided();
1105 BasicImageView<T> temp = matlab::createImage<T>(1, 1, (*this)[pos]);
1109 template <
class ReqType>
1110 ConstCellArray createCellArray(
int pos, ReqType req, mwSize sze)
1113 return errorOrDefault<ConstCellArray>(req, pos);
1114 return matlab::createCellArray(sze, (*
this)[pos]);
1123 using namespace vigra;
1132 return (s1[0] < s2[0]);
1134 return s1[1] < s2[1];
1143 std::map<TinyVector<int,2>, T,ShapeCmp> data;
1147 void assign(
int i = 1,
int j = 1){
1151 SparseArray(
int i = 1 ,
int j = 1){
1158 template<
class indexType>
1159 T& operator()(indexType i_, indexType j_){
1163 typename std::map<TinyVector<int,2>, T, ShapeCmp>::iterator iter;
1165 return data[newShapew];
1168 template<
class indexType>
1169 const T
get(indexType i_, indexType j_){
1173 if(data.find(newShape) == data.end())
return 0;
1174 else return data.find(newShape)->second;
1178 void mapToMxArray(mxArray * & in){
1180 int len = data.size();
1181 in = mxCreateSparse(width, length, len, mxREAL);
1182 int* jc = mxGetJc(in);
1183 int* ir = mxGetIr(in);
1184 double* pr = mxGetPr(in);
1189 typename std::map<TinyVector<int,2>, T, ShapeCmp>::iterator iter;
1194 for( iter = data.begin(); iter != data.end(); ++iter ) {
1195 newShape = iter->first;
1196 ir[ii] = newShape[1];
1197 pr[ii] = iter->second;
1198 if(newShape[0] != curjc){
1199 curjc = newShape[0] ;
1211 enum DataDimension {IMAGE = 2, VOLUME = 3};
1217 void vigraMexFunction(vigra::matlab::OutputArray, vigra::matlab::InputArray);
1219 #ifndef VIGRA_CUSTOM_MEXFUNCTION
1226 void mexFunction(
int nlhs, mxArray *plhs[],
1227 int nrhs,
const mxArray *prhs[])
1231 vigra::matlab::InputArray inputs(nrhs, prhs);
1232 vigra::matlab::OutputArray outputs(nlhs, plhs);
1234 vigraMexFunction(outputs, inputs);
1236 catch(std::exception & e)
1238 mexErrMsgTxt(e.what());
1245 #define VIGRA_CREATE_ENUM_AND_STD_MAP2(mapName, item1, item2) \
1246 const int item1 = 1;\
1247 const int item2 = 2;\
1248 std::map<std::string,int> mapName;\
1249 mapName[#item1] = (int)item1;\
1250 mapName[#item2] = (int)item2;\
1253 #define VIGRA_CREATE_ENUM_AND_STD_MAP3(mapName, item1, item2, item3) \
1254 const int item1 = 1;\
1255 const int item2 = 2;\
1256 const int item3 = 3;\
1257 std::map<std::string,int> mapName;\
1258 mapName[#item1] = (int)item1;\
1259 mapName[#item2] = (int)item2;\
1260 mapName[#item3] = (int)item3;\
1263 #define VIGRA_CREATE_ENUM_AND_STD_MAP4(mapName, item1, item2, item3, item4) \
1264 const int item1 = 1;\
1265 const int item2 = 2;\
1266 const int item3 = 3;\
1267 const int item4 = 4;\
1268 std::map<std::string,int> mapName;\
1269 mapName[#item1] = (int)item1;\
1270 mapName[#item2] = (int)item2;\
1271 mapName[#item3] = (int)item3;\
1272 mapName[#item4] = (int)item4;\
1274 #define VIGRA_CREATE_ENUM_AND_STD_MAP5(mapName, item1, item2, item3, item4, item5) \
1275 const int item1 = 1;\
1276 const int item2 = 2;\
1277 const int item3 = 3;\
1278 const int item4 = 4;\
1279 const int item5 = 5;\
1280 std::map<std::string, int> mapName;\
1281 mapName[#item1] = (int)item1;\
1282 mapName[#item2] = (int)item2;\
1283 mapName[#item3] = (int)item3;\
1284 mapName[#item4] = (int)item4;\
1285 mapName[#item5] = (int)item5;\
1287 #define VIGRA_CREATE_ENUM_AND_STD_MAP6(mapName, item1, item2, item3, item4, item5, item6) \
1288 const int item1 = 1;\
1289 const int item2 = 2;\
1290 const int item3 = 3;\
1291 const int item4 = 4;\
1292 const int item5 = 5;\
1293 const int item6 = 6;\
1294 std::map<std::string,int> mapName;\
1295 mapName[#item1] = (int)item1;\
1296 mapName[#item2] = (int)item2;\
1297 mapName[#item3] = (int)item3;\
1298 mapName[#item4] = (int)item4;\
1299 mapName[#item5] = (int)item5;\
1300 mapName[#item6] = (int)item6;\
1302 #endif // VIGRA_MATLAB_HXX
MultiArrayIndex rowCount(const MultiArrayView< 2, T, C > &x)
Definition: matrix.hxx:671
detail::SelectIntegerType< 8, detail::UnsignedIntTypes >::type UInt8
8-bit unsigned int
Definition: sized_int.hxx:179
std::ptrdiff_t MultiArrayIndex
Definition: multi_fwd.hxx:60
detail::SelectIntegerType< 16, detail::UnsignedIntTypes >::type UInt16
16-bit unsigned int
Definition: sized_int.hxx:181
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:272
MultiArrayIndex columnCount(const MultiArrayView< 2, T, C > &x)
Definition: matrix.hxx:684
detail::SelectIntegerType< 8, detail::SignedIntTypes >::type Int8
8-bit signed int
Definition: sized_int.hxx:171