void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { int structNum = sizeof(friends)/sizeof(struct phonebook); // 結構陣列的長度 int fieldNum = sizeof(fieldNames)/sizeof(*fieldNames); // 欄位的個數 int dims[2] = {1, structNum}; // 結構陣列的維度 int i, nameFieldIndex, phoneFieldIndex; // 檢查輸入和輸出參數的個數 if (nrhs>0) mexErrMsgTxt("No input argument required."); if (nlhs>1) mexErrMsgTxt("Too many output arguments."); // 產生輸出結構陣列 OUT = mxCreateStructArray(2, dims, fieldNum, fieldNames); // 取得欄位名稱對應的索引值,以便使用 mxSetFieldByNumber() 對欄位值進行設定 nameFieldIndex = mxGetFieldNumber(OUT, "name"); phoneFieldIndex = mxGetFieldNumber(OUT, "phone"); // 填入 MATLAB 結構陣列的欄位值 for (i=0; i<structNum; i++) { mxArray *fieldValue; // 填入欄位名稱 name 的值(有兩種方法) // mxSetField(OUT, i, "name", mxCreateString(friends[i].name)); // 方法一:效率較低 mxSetFieldByNumber(OUT, i, nameFieldIndex, mxCreateString(friends[i].name)); // 方法二:效率較高 // 填入欄位名稱 phone 的值(有兩種方法) fieldValue = mxCreateDoubleMatrix(1, 1, mxREAL); *mxGetPr(fieldValue) = friends[i].phone; // mxSetField(OUT, i, "phone", fieldValue); // 方法一:效率較低 mxSetFieldByNumber(OUT, i, phoneFieldIndex, fieldValue); // 方法二:效率較高 } }
DLL_EXPORT_SYM void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { if (nrhs < 1) { mexPrintf("Usage: publishLCMLog(lcm_log)\n"); return; } if (!lcm) lcm = lcm_create(NULL); if (!lcm) mexErrMsgTxt("failed to create lcm node"); char* channel; mxArray* data; int channel_field_number = mxGetFieldNumber(prhs[0], "channel"), data_field_number = mxGetFieldNumber(prhs[0], "data"); if (channel_field_number < 0 || data_field_number < 0) mexErrMsgTxt( "publishLCMLog failed: input must be a structure with fields 'channel' " "and 'data'"); for (size_t i = 0; i < mxGetNumberOfElements(prhs[0]); i++) { channel = mxArrayToString(mxGetFieldByNumber(prhs[0], i, channel_field_number)); data = mxGetFieldByNumber(prhs[0], i, data_field_number); lcm_publish(lcm, channel, mxGetData(data), mxGetNumberOfElements(data)); mxFree(channel); } }
/* Create a default freeman pyramid cache control structure, and then customize it with the values from a matlab structure */ int mexFreemanCacheCntrlStruct( const mxArray *mxStruct, FreemanCacheCntrlStruct **fpcc ) { mxArray *field; double *ptr, ftmp; int fn, m, n, k, itmp; int nOrient, nScale; /* make sure mxStruct is a structure */ if( !mxIsStruct(mxStruct) ) mexErrMsgTxt( "Freeman cache control structure expected" ); /* start with a default control structure */ *fpcc = defaultFreemanCacheCntrlStruct(); /* get the cacheFiles flags if they are specified */ fn = mxGetFieldNumber( mxStruct, "cacheFiles" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); m = mxGetM(field); n = mxGetN(field); if( min(m,n) != 1 ) mexErrMsgTxt( "cacheFiles - must be a vector" ); n = max(m,n); if( n > NUMCODE ) mexErrMsgTxt( "cacheFiles - too many values" ); ptr = mxGetPr(field); for( k = 0; k < n; k++ ) { itmp = (int)ptr[k]; if( itmp != 0 && itmp != 1 ) mexErrMsgTxt( "cacheFiles - elements must be 0 or 1" ); (*fpcc)->cacheFiles[k] = itmp; } } /* get the pathNameCache field */ fn = mxGetFieldNumber( mxStruct, "pathNameCache" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); if( !mxIsChar( field ) ) mexErrMsgTxt( "pathNameCache - must be a string" ); if( mxGetM(field)*mxGetN(field) > MAXLEN - 1 ) mexErrMsgTxt( "pathNameCache - string too long" ); mxGetString( field, (*fpcc)->pathNameCache, MAXLEN - 1 ); } /* get the pathNameCache field */ fn = mxGetFieldNumber( mxStruct, "rootNameCache" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); if( !mxIsChar( field ) ) mexErrMsgTxt( "rootNameCache - must be a string" ); if( mxGetM(field)*mxGetN(field) > MAXLEN - 1 ) mexErrMsgTxt( "rootNameCache - string too long" ); mxGetString( field, (*fpcc)->rootNameCache, MAXLEN - 1 ); } }
int PopulateParams(const mxArray *params, PogsData<T, T*> *pogs_data) { // Check if parameter exists in params, then make sure that it has // dimension 1x1 and finally set the corresponding value in pogs_data. int rel_tol_idx = mxGetFieldNumber(params, "rel_tol"); if (rel_tol_idx != -1) { mxArray *arr = mxGetFieldByNumber(params, 0, rel_tol_idx); if (mxGetM(arr) != 1 || mxGetN(arr) != 1) { mexErrMsgIdAndTxt("MATLAB:pogs:dimensionMismatch", "Parameter rel_tol must have dimension (1,1)"); return 1; } pogs_data->rel_tol = GetVal<T>(mxGetPr(arr), 0, mxGetClassID(arr)); } int abs_tol_idx = mxGetFieldNumber(params, "abs_tol"); if (abs_tol_idx != -1) { mxArray *arr = mxGetFieldByNumber(params, 0, abs_tol_idx); if (mxGetM(arr) != 1 || mxGetN(arr) != 1) { mexErrMsgIdAndTxt("MATLAB:pogs:dimensionMismatch", "Parameter abs_tol must have dimension (1,1)"); return 1; } pogs_data->abs_tol = GetVal<T>(mxGetPr(arr), 0, mxGetClassID(arr)); } int rho_idx = mxGetFieldNumber(params, "rho"); if (rho_idx != -1) { mxArray *arr = mxGetFieldByNumber(params, 0, rho_idx); if (mxGetM(arr) != 1 || mxGetN(arr) != 1) { mexErrMsgIdAndTxt("MATLAB:pogs:dimensionMismatch", "Parameter rho must have dimension (1,1)"); return 1; } pogs_data->rho = GetVal<T>(mxGetPr(arr), 0, mxGetClassID(arr)); } int max_iter_idx = mxGetFieldNumber(params, "max_iter"); if (max_iter_idx != -1) { mxArray *arr = mxGetFieldByNumber(params, 0, max_iter_idx); if (mxGetM(arr) != 1 || mxGetN(arr) != 1) { mexErrMsgIdAndTxt("MATLAB:pogs:dimensionMismatch", "Parameter max_iter must have dimension (1,1)"); return 1; } pogs_data->max_iter = GetVal<unsigned int>(mxGetPr(arr), 0, mxGetClassID(arr)); } int quiet_idx = mxGetFieldNumber(params, "quiet"); if (quiet_idx != -1) { mxArray *arr = mxGetFieldByNumber(params, 0, quiet_idx); if (mxGetM(arr) != 1 || mxGetN(arr) != 1) { mexErrMsgIdAndTxt("MATLAB:pogs:dimensionMismatch", "Parameter quiet must have dimension (1,1)"); return 1; } pogs_data->quiet = GetVal<bool>(mxGetPr(arr), 0, mxGetClassID(arr)); } return 0; }
/* PsychSetStructArrayDoubleElement() Note: The variable "index" is zero-indexed. */ void PsychSetStructArrayDoubleElement( char *fieldName, int index, double value, PsychGenericScriptType *pStruct) { int fieldNumber; size_t numElements; psych_bool isStruct; mxArray *mxFieldValue; char errmsg[256]; //check for bogus arguments numElements = mxGetM(pStruct) * mxGetN(pStruct); if((size_t) index >= numElements) PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index"); fieldNumber=mxGetFieldNumber(pStruct, fieldName); if(fieldNumber==-1) { sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName); PsychErrorExitMsg(PsychError_internal, errmsg); } isStruct= mxIsStruct(pStruct); if(!isStruct) PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure."); //do stuff mxFieldValue= mxCreateDoubleMatrix(1, 1, mxREAL); mxGetPr(mxFieldValue)[0] = value; mxSetField(pStruct, (mwIndex) index, fieldName, mxFieldValue); if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(mxFieldValue); }
/* PsychSetStructArrayNativeElement() */ void PsychSetStructArrayNativeElement( char *fieldName, int index, PsychGenericScriptType *pNativeElement, PsychGenericScriptType *pStructArray) { int fieldNumber, numElements; boolean isStruct; char errmsg[256]; //check for bogus arguments numElements=mxGetM(pStructArray) *mxGetN(pStructArray); if(index>=numElements) PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index"); fieldNumber=mxGetFieldNumber(pStructArray, fieldName); if(fieldNumber==-1) { sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName); PsychErrorExitMsg(PsychError_internal, errmsg); } isStruct= mxIsStruct(pStructArray); if(!isStruct) PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure."); //do stuff mxSetField(pStructArray, index, fieldName, pNativeElement); }
bool addGroupTimestampsField(mxArray* mxTrial, const GroupInfo* pg, unsigned trialIdx, timestamp_t timeTrialStart, bool useGroupPrefix, unsigned index, unsigned nSamples) { // build groupName_time field name char fieldName[MAX_SIGNAL_NAME]; if(useGroupPrefix) snprintf(fieldName, MAX_SIGNAL_NAME, "%s_time", pg->name); else strcpy(fieldName, "time"); timestamp_t *pTimestampsBase = pg->tsBuffers[trialIdx].timestamps; uint32_t nTimestampsBase = pg->tsBuffers[trialIdx].nSamples; // create the matlab array to hold the timestamps, use double as the type mxArray* mxTimestamps = mxCreateNumericMatrix(nTimestampsBase, 1, mxDOUBLE_CLASS, mxREAL); // enter the timestamps minus the trial start into the array double_t* buffer = (double_t*)mxGetData(mxTimestamps); // no offsets, subtract trial start and round to ms (should be integer anyway) for(unsigned i = 0; i < nTimestampsBase; i++) buffer[i] = pTimestampsBase[i] - timeTrialStart; // add to trial struct int fieldNum; fieldNum = mxGetFieldNumber(mxTrial, fieldName); if(fieldNum == -1) fieldNum = mxAddField(mxTrial, fieldName); mxSetFieldByNumber(mxTrial, index, fieldNum, mxTimestamps); return true; }
/* PsychSetStructArrayStructElement() */ void PsychSetStructArrayStructElement( char *fieldName, int index, PsychGenericScriptType *pStructInner, PsychGenericScriptType *pStructOuter) { int fieldNumber, numElements; boolean isStruct; char errmsg[256]; //check for bogus arguments numElements=mxGetM(pStructOuter) *mxGetN(pStructOuter); if(index>=numElements) PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index"); fieldNumber=mxGetFieldNumber(pStructOuter, fieldName); if(fieldNumber==-1) { sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName); PsychErrorExitMsg(PsychError_internal, errmsg); } isStruct= mxIsStruct(pStructInner); if(!isStruct) PsychErrorExitMsg(PsychError_internal, "Attempt to set a struct field to a non-existent structure."); isStruct= mxIsStruct(pStructOuter); if(!isStruct) PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure."); //do stuff mxSetField(pStructOuter, index, fieldName, pStructInner); if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(pStructInner); }
/* PsychSetStructArrayBooleanElement() Note: The variable "index" is zero-indexed. */ void PsychSetStructArrayBooleanElement( char *fieldName, int index, boolean state, PsychGenericScriptType *pStruct) { int fieldNumber, numElements; boolean isStruct; mxArray *mxFieldValue; char errmsg[256]; //check for bogus arguments numElements=mxGetM(pStruct) *mxGetN(pStruct); if(index>=numElements) PsychErrorExitMsg(PsychError_internal, "Attempt to set a structure field at an out-of-bounds index"); fieldNumber=mxGetFieldNumber(pStruct, fieldName); if(fieldNumber==-1) { sprintf(errmsg, "Attempt to set a non-existent structure name field: %s", fieldName); PsychErrorExitMsg(PsychError_internal, errmsg); } isStruct= mxIsStruct(pStruct); if(!isStruct) PsychErrorExitMsg(PsychError_internal, "Attempt to set a field within a non-existent structure."); //do stuff mxFieldValue=mxCreateLogicalMatrix(1, 1); mxGetLogicals(mxFieldValue)[0]= state; mxSetField(pStruct, index, fieldName, mxFieldValue); if (PSYCH_LANGUAGE == PSYCH_OCTAVE) mxDestroyArray(mxFieldValue); }
//Extracts values form prhs, which are right hand side values in matlab. Problem::Problem(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { //If field_number != 1 checks are necessary to allow rearanging and skipping fields in input structure int field_number = mxGetFieldNumber(prhs[1],"res"); if(field_number != -1) res = mxGetScalar(mxGetFieldByNumber(prhs[1], 0, field_number)); field_number = mxGetFieldNumber(prhs[1],"prec"); if(field_number != -1) prec = int(mxGetScalar(mxGetFieldByNumber(prhs[1], 0, field_number))); field_number = mxGetFieldNumber(prhs[1],"vars"); if(field_number != -1) extract_vars(mxGetFieldByNumber(prhs[1], 0, field_number), var_names, vars); field_number = mxGetFieldNumber(prhs[1],"cnstrs"); if(field_number != -1) extract_strings(mxGetFieldByNumber(prhs[1], 0, field_number), cnstrs); this->plhs = plhs; this->nlhs = nlhs; };
void mxSetField(mxArray *array_ptr, int lindex, const char *field_name, mxArray *value) { int field_num = mxGetFieldNumber(array_ptr, field_name); if (field_num >= 0) { mxSetFieldByNumber(array_ptr, lindex, field_num, value); } }
mxArray *mxGetField(const mxArray *ptr, int lindex, const char *string) { int field_num = mxGetFieldNumber(ptr, string); if (field_num < 0) { return NULL; } return mxGetFieldByNumber(ptr, lindex, field_num); }
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray*prhs[] ) { /* Check for proper number of arguments */ if (!(nrhs == 2) || !(nlhs==0)) { mexErrMsgTxt("You have to input camera handle"); } if (!mxIsStruct(prhs[1])) { mexErrMsgTxt("That second input has to be an image structure, you know."); } int error = 0; HCAM hCam = *(HCAM *)mxGetPr(prhs[0]); int pointer_field = mxGetFieldNumber(prhs[1],"pointer"); int ID_field = mxGetFieldNumber(prhs[1],"id"); mxArray *ppointer_field = mxGetFieldByNumber(prhs[1],0,pointer_field); char *ppImgMem = (char *)*(int *)mxGetPr(ppointer_field); mxArray *pID_field = mxGetFieldByNumber(prhs[1],0,ID_field); int id = *(int *)mxGetPr(pID_field); if (hCam!= NULL) { error = is_FreeImageMem(hCam, ppImgMem, id); if (error != IS_SUCCESS) { mexErrMsgTxt("Error freeing image memory"); } //Close and exit camera error = is_ExitCamera(hCam ); if (error != IS_SUCCESS) { mexErrMsgTxt("Error exiting camera"); } hCam = NULL; ppImgMem = NULL; // mexPrintf("Memory freed. \n"); } return; }
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]) { const char *field_names[] = {"name", "phone"}; struct phonebook friends[] = {{"Jordan Robert", 3386},{"Mary Smith",3912}, {"Stacy Flora", 3238},{"Harry Alpert",3077}}; mwSize dims[2] = {1, NUMBER_OF_STRUCTS }; int name_field, phone_field; mwIndex i; (void) prhs; /* Check for proper number of input and output arguments */ if (nrhs !=0) { mexErrMsgTxt("No input argument required."); } if(nlhs > 1){ mexErrMsgTxt("Too many output arguments."); } /* Create a 1-by-n array of structs. */ plhs[0] = mxCreateStructArray(2, dims, NUMBER_OF_FIELDS, field_names); /* This is redundant, but here for illustration. Since we just created the structure and the field number indices are zero based, name_field will always be 0 and phone_field will always be 1 */ name_field = mxGetFieldNumber(plhs[0],"name"); phone_field = mxGetFieldNumber(plhs[0],"phone"); /* Populate the name and phone fields of the phonebook structure. */ for (i=0; i<NUMBER_OF_STRUCTS; i++) { mxArray *field_value; /* Use mxSetFieldByNumber instead of mxSetField for efficiency mxSetField(plhs[0],i,"name",mxCreateString(friends[i].name); */ mxSetFieldByNumber(plhs[0],i,name_field,mxCreateString(friends[i].name)); field_value = mxCreateDoubleMatrix(1,1,mxREAL); *mxGetPr(field_value) = friends[i].phone; /* Use mxSetFieldByNumber instead of mxSetField for efficiency mxSetField(plhs[0],i,"name",mxCreateString(friends[i].name); */ mxSetFieldByNumber(plhs[0],i,phone_field,field_value); } }
static int mstruct_getfield_number(mxArray *m, char *field) { int f; if ((f = mxGetFieldNumber(m, field)) < 0) error("no element %s in link structure"); return f; }
/* * This function will get the number of the field if the field doesn't exist * it will add the field to the struct. */ static int getFieldNumber(mxArray *pStruct, const char *fieldname){ int fieldNumber = mxGetFieldNumber(pStruct, fieldname); if(-1 == fieldNumber) { fieldNumber = mxAddField(pStruct, fieldname); abv_assert(-1 != fieldNumber, "bbci_acquire_bv: Could not create field for struct."); } return fieldNumber; }
int mxAddField(mxArray *ptr, const char *fieldname) { if (!mxIsStruct(ptr)) { return -1; } Struct *pa = (Struct *)ptr; wchar_t *wfieldname = to_wide_string(fieldname); pa->addField(wfieldname); return mxGetFieldNumber(ptr, fieldname); }
/* return field number on success, or -1 if already existing or invalid */ int addIfNew(mxArray *S, const char *name) { int field; if (mxGetFieldNumber(S, name) >= 0) { printf("Chunk '%s' already defined. Skipping.\n", name); return -1; } field = mxAddField(S, name); if (field<0) { printf("Can not add '%s' to output struct. Skipping.\n", name); } return field; }
void ConstMatlabStructure::Configure(const MatlabProto& proto, const mxArray* arr) { CHECK_NOT_NULL(arr); CHECK(mxIsStruct(arr)) << "Matlab array was not a struct"; m = arr; fields.clear(); BOOST_FOREACH(const string& f, proto.fields) { fields[f] = mxGetFieldNumber(arr, f.c_str()); CHECK_NE(fields[f], -1) << "Matlab array does not conform to protocol: missing field '" << f << "'"; } }
/* ---------------------------------------------------------------------- */ static int args_ok(int nlhs, int nrhs, const mxArray *prhs[]) /* ---------------------------------------------------------------------- */ { int ok; ok = (nlhs == 1) && ((nrhs == 1) || (nrhs == 2)); ok = ok && !mxIsEmpty(prhs[0]); ok = ok && mxIsStruct(prhs[0]); ok = ok && (mxGetFieldNumber(prhs[0], "cartDims") >= 0); ok = ok && (mxGetFieldNumber(prhs[0], "COORD" ) >= 0); ok = ok && (mxGetFieldNumber(prhs[0], "ZCORN" ) >= 0); if (ok && (nrhs == 2)) { ok = mxIsDouble(prhs[1]) && (mxGetNumberOfElements(prhs[1]) == 1); } return ok; }
/** * storeString( ) - store the given string value in the * mxStruct structure in field named fieldName */ void storeString(mxArray *mxStruct, char *fieldName, char *value) { char msg[1025]; mxArray *fieldValue; int fieldNum = mxGetFieldNumber(mxStruct, fieldName); if (fieldNum < 0) { sprintf(msg,"Error: unknown field: (%s)\n", fieldName); mexErrMsgTxt(msg); } fieldValue = mxCreateString(value); mxSetFieldByNumber(mxStruct, 0, fieldNum, fieldValue); }
/** * storeValue( ) - store the given double value in the mxStruct * structure at field fieldName */ void storeValue(mxArray *mxStruct, char *fieldName, double value) { char msg[1025]; mxArray *fieldValue; int fieldNum = mxGetFieldNumber(mxStruct, fieldName); if (fieldNum < 0) { sprintf(msg,"Error: unknown field: (%s)\n", fieldName); mexErrMsgTxt(msg); } fieldValue = mxCreateDoubleMatrix(1,1,mxREAL); *mxGetPr(fieldValue) = value; mxSetFieldByNumber(mxStruct, 0, fieldNum, fieldValue); }
ExportMode int* passFunction(const mxArray *ElemData,int *FieldNumbers, double *r_in, int num_particles, int mode) #define NUM_FIELDS_2_REMEMBER 1 { double *M; int *returnptr; int fnum,*NewFieldNumbers; switch(mode) { case NO_LOCAL_COPY: /* Not used in AT1.3 Get fields by names from MATLAB workspace */ { } break; case MAKE_LOCAL_COPY: /* Find field numbers first Save a list of field number in an array and make returnptr point to that array */ { NewFieldNumbers = (int*)mxCalloc(NUM_FIELDS_2_REMEMBER,sizeof(int)); fnum = mxGetFieldNumber(ElemData,"M66"); if(fnum<0) mexErrMsgTxt("Required field 'M66' was not found in the element data structure"); else { NewFieldNumbers[0] = fnum; M = mxGetPr(mxGetFieldByNumber(ElemData,0,fnum)); returnptr = NewFieldNumbers; } } break; case USE_LOCAL_COPY: /* Get fields from MATLAB using field numbers The second argument ponter to the array of field numbers is previously created with QuadLinPass( ..., MAKE_LOCAL_COPY) */ { M = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[0])); returnptr = FieldNumbers; } break; } Matrix66Pass(r_in, M, num_particles); return(returnptr); }
void mexFunction(int nOut, mxArray *pOut[], int nIn, const mxArray *pIn[]) { mxArray *address, *data; char *field_name; int field_number; // assert(nOut == 2) // assert(nIn == 2) address = GetPointerData(pIn[0]); data = GetPointerData(address); if (!data) { //mexErrMsgTxt("Pointer is NULL"); pOut[0] = mxCreateScalarDouble(0); pOut[1] = mxCreateScalarDouble(0); } else { field_name = AllocAndGetString(pIn[1]); field_number = mxGetFieldNumber(data, field_name); if (field_number == -1) { //mexErrMsgTxt("Reference to non-existent field"); pOut[0] = mxCreateScalarDouble(-1); pOut[1] = mxCreateScalarDouble(-1); } else { pOut[0] = mxGetFieldByNumber(data, 0, field_number); pOut[1] = mxCreateScalarDouble(1); } mxFree(field_name); } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { /* Input image and ouput image */ double *u, *u_new; /* Options structure variables */ mxArray *TempField; double *OptionsField; int field_num; struct options Options; /* Size input image */ int ndimsu; const mwSize *dimsu_const; int dimsu[3]; int npixels2; int npixels3; double *usigma; /* Gaussian filtered image */ double *ux, *uy; /* Gradients of smoothed image */ double *Jxx, *Jxy, *Jyy, *J[3]; /* Structure tensor */ double *Dxx, *Dxy, *Dyy; /* Diffusion tensor */ /* Check number of inputs/outputs */ if(nrhs<1) { mexErrMsgTxt("1 input variable is required, the other one is optional."); } if(nlhs<1) { mexErrMsgTxt("1 output variable is required"); } /* Check type of inputs */ if(!mxIsDouble(prhs[0])) { mexErrMsgTxt("Input Image must be of datatype Double");} if(nrhs==2){ if(!mxIsStruct(prhs[1])){ mexErrMsgTxt("Options must be of type structure"); } } /* Set Options struct */ setdefaultoptions(&Options); if(nrhs==2) { field_num = mxGetFieldNumber(prhs[1], "T"); if(field_num>=0) { TempField=mxGetFieldByNumber(prhs[1], 0, field_num); if(!mxIsDouble(TempField)) { mexErrMsgTxt("aValues in options structure must be of datatype double"); } OptionsField=mxGetPr(TempField); Options.T=OptionsField[0]; } field_num = mxGetFieldNumber(prhs[1], "dt"); if(field_num>=0) { TempField=mxGetFieldByNumber(prhs[1], 0, field_num); if(!mxIsDouble(TempField)) { mexErrMsgTxt("Values in options structure must be of datatype double"); } OptionsField=mxGetPr(TempField); Options.dt=OptionsField[0]; } field_num = mxGetFieldNumber(prhs[1], "sigma"); if(field_num>=0) { TempField=mxGetFieldByNumber(prhs[1], 0, field_num); if(!mxIsDouble(TempField)) { mexErrMsgTxt("Values in options structure must be of datatype double"); } OptionsField=mxGetPr(TempField); Options.sigma=OptionsField[0]; } field_num = mxGetFieldNumber(prhs[1], "rho"); if(field_num>=0) { TempField=mxGetFieldByNumber(prhs[1], 0, field_num); if(!mxIsDouble(TempField)) { mexErrMsgTxt("Values in options structure must be of datatype double"); } OptionsField=mxGetPr(TempField); Options.rho=OptionsField[0]; } field_num = mxGetFieldNumber(prhs[1], "C"); if(field_num>=0) { TempField=mxGetFieldByNumber(prhs[1], 0, field_num); if(!mxIsDouble(TempField)) { mexErrMsgTxt("Values in options structure must be of datatype double"); } OptionsField=mxGetPr(TempField); Options.C=OptionsField[0]; } field_num = mxGetFieldNumber(prhs[1], "m"); if(field_num>=0) { TempField=mxGetFieldByNumber(prhs[1], 0, field_num); if(!mxIsDouble(TempField)) { mexErrMsgTxt("Values in options structure must be of datatype double"); } OptionsField=mxGetPr(TempField); Options.m=OptionsField[0]; } field_num = mxGetFieldNumber(prhs[1], "alpha"); if(field_num>=0) { TempField=mxGetFieldByNumber(prhs[1], 0, field_num); if(!mxIsDouble(TempField)) { mexErrMsgTxt("Values in options structure must be of datatype double"); } OptionsField=mxGetPr(TempField); Options.alpha=OptionsField[0]; } } /* Check and get input image dimensions */ ndimsu=mxGetNumberOfDimensions(prhs[0]); if((ndimsu<2)||(ndimsu>3)) { mexErrMsgTxt("Input Image must be 2D"); } dimsu_const = mxGetDimensions(prhs[0]); dimsu[0]=dimsu_const[0]; dimsu[1]=dimsu_const[1]; if(ndimsu==3) { dimsu[2]=dimsu_const[2]; } else { dimsu[2]=1; } npixels2=dimsu[0]*dimsu[1]; npixels3=dimsu[0]*dimsu[1]*dimsu[2]; /* Connect input */ u =(double *)mxGetData(prhs[0]); /* Gaussian Filtering of input image */ usigma = mallocd(npixels3); GaussianFiltering2Dcolor_double(u, usigma, dimsu, Options.sigma, 4*Options.sigma); /* Calculate the image gradients of the smoothed image */ ux = mallocd(npixels3); uy = mallocd(npixels3); gradient2Dx_color(usigma, dimsu, ux); gradient2Dy_color(usigma, dimsu, uy); /* remove usigma from memory */ free(usigma); /* Compute the 2D structure tensors J of the image */ StructureTensor2D(ux,uy, J, dimsu, Options.rho); Jxx=J[0]; Jyy=J[1] ;Jxy=J[2]; /* remove gradients from memory */ free(ux); free(uy); /* Structure to Diffusion Tensor Weickert */ Dxx = mallocd(npixels2); Dyy = mallocd(npixels2); Dxy = mallocd(npixels2); StructureTensor2DiffusionTensor(Jxx,Jxy,Jyy,Dxx,Dxy,Dyy,dimsu,Options.C,Options.m,Options.alpha); /* remove structure tensor from memory */ free(Jxx); free(Jyy); free(Jxy); /* Create output array */ if(dimsu[2]==1) { plhs[0] = mxCreateNumericArray(2, dimsu, mxDOUBLE_CLASS, mxREAL); } else { plhs[0] = mxCreateNumericArray(3, dimsu, mxDOUBLE_CLASS, mxREAL); } /* Assign pointer to output. */ u_new = (double *)mxGetData(plhs[0]); /* Perform the image diffusion */ diffusion_scheme_2D_rotation_invariance(u,u_new,dimsu,Dxx,Dxy,Dyy,Options.dt); /* remove diffusion tensor from memory */ free(Dxx); free(Dyy); free(Dxy); }
ExportMode int* passFunction(const mxArray *ElemData, int *FieldNumbers, double *r_in, int num_particles, int mode) #define NUM_FIELDS_2_REMEMBER 3 { double *A , *B; int max_order; int *returnptr; int *NewFieldNumbers,fnum; mxArray *tmpmxptr; switch(mode) { case NO_LOCAL_COPY: /* Get fields by names from MATLAB workspace */ { tmpmxptr =mxGetField(ElemData,0,"PolynomA"); if(tmpmxptr) A = mxGetPr(tmpmxptr); else mexErrMsgTxt("Required field 'PolynomA' was not found in the element data structure"); tmpmxptr =mxGetField(ElemData,0,"PolynomB"); if(tmpmxptr) B = mxGetPr(tmpmxptr); else mexErrMsgTxt("Required field 'PolynomB' was not found in the element data structure"); tmpmxptr = mxGetField(ElemData,0,"MaxOrder"); if(tmpmxptr) max_order = (int)mxGetScalar(tmpmxptr); else mexErrMsgTxt("Required field 'MaxOrder' was not found in the element data structure"); returnptr = NULL; } break; case MAKE_LOCAL_COPY: /* Find field numbers first Save a list of field number in an array and make returnptr point to that array */ { /* Allocate memory for integer array of field numbers for faster futurereference */ NewFieldNumbers = (int*)mxCalloc(NUM_FIELDS_2_REMEMBER,sizeof(int)); /* Populate */ fnum = mxGetFieldNumber(ElemData,"PolynomA"); if(fnum<0) mexErrMsgTxt("Required field 'PolynomA' was not found in the element data structure"); NewFieldNumbers[0] = fnum; fnum = mxGetFieldNumber(ElemData,"PolynomB"); if(fnum<0) mexErrMsgTxt("Required field 'PolynomB' was not found in the element data structure"); NewFieldNumbers[1] = fnum; fnum = mxGetFieldNumber(ElemData,"MaxOrder"); if(fnum<0) mexErrMsgTxt("Required field 'MaxOrder' was not found in the element data structure"); NewFieldNumbers[2] = fnum; A = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[0])); B = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[1])); max_order = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[2])); returnptr = NewFieldNumbers; } break; case USE_LOCAL_COPY: /* Get fields from MATLAB using field numbers The second argument ponter to the array of field numbers is previously created with QuadLinPass( ..., MAKE_LOCAL_COPY) */ { A = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[0])); B = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[1])); max_order = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[2])); returnptr = FieldNumbers; } break; default: { mexErrMsgTxt("No match for calling mode in function ThinMPolePass\n"); } } ThinMPolePass(r_in,A, B, max_order,num_particles); return(returnptr); }
ExportMode int* passFunction(const mxArray *ElemData,int *FieldNumbers, double *r_in, int num_particles, int mode) #define NUM_FIELDS_2_REMEMBER 12 { int *returnptr; int fnum, *NewFieldNumbers; mxArray *tmpmxptr; double le; double *pr1, *pr2, *pt1, *pt2; int num_int_steps; int nz, nx; double *b, *lambdaz, *phasez, *kx; switch(mode) { case NO_LOCAL_COPY: { /* Get fields by names from MATLAB workspace */ tmpmxptr=mxGetField(ElemData,0,"Length"); if(tmpmxptr) { le = mxGetScalar(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'Length' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"NumOfLongHarm"); if(tmpmxptr) { nz = (int)mxGetScalar(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'NumOfLongHarm' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"NumOfTranHarm"); if(tmpmxptr) { nx = (int)mxGetScalar(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'NumOfTranHarm' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"Bfield"); if(tmpmxptr) { b = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'Bfield' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"Lambdaz"); if(tmpmxptr) { lambdaz = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'Lambdaz' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"Phasez"); if(tmpmxptr) { phasez = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'Phasez' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"Kx"); if(tmpmxptr) { kx = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'Kx' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"NumIntSteps"); if(tmpmxptr) { num_int_steps = (int)mxGetScalar(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'NumIntSteps' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"T1"); if(tmpmxptr) { pt1 = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'T1' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"T2"); if(tmpmxptr) { pt2 = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'T2' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"R1"); if(tmpmxptr) { pr1 = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'R1' was not found in the element data structure"); tmpmxptr=mxGetField(ElemData,0,"R2"); if(tmpmxptr) { pr2 = mxGetPr(tmpmxptr); returnptr = NULL; } else mexErrMsgTxt("Required field 'R2' was not found in the element data structure"); } break; case MAKE_LOCAL_COPY: { /* Find field numbers first Save a list of field number in an array and make returnptr point to that array */ NewFieldNumbers = (int*)mxCalloc(NUM_FIELDS_2_REMEMBER,sizeof(int)); fnum = mxGetFieldNumber(ElemData,"Length"); if(fnum<0) mexErrMsgTxt("Required field 'Length' was not found in the element data structure"); NewFieldNumbers[0] = fnum; fnum = mxGetFieldNumber(ElemData,"NumOfLongHarm"); if(fnum<0) mexErrMsgTxt("Required field 'NumOfLongHarm' was not found in the element data structure"); NewFieldNumbers[1] = fnum; fnum = mxGetFieldNumber(ElemData,"NumOfTranHarm"); if(fnum<0) mexErrMsgTxt("Required field 'NumOfTranHarm' was not found in the element data structure"); NewFieldNumbers[2] = fnum; fnum = mxGetFieldNumber(ElemData,"Bfield"); if(fnum<0) mexErrMsgTxt("Required field 'Bfield' was not found in the element data structure"); NewFieldNumbers[3] = fnum; fnum = mxGetFieldNumber(ElemData,"Lambdaz"); if(fnum<0) mexErrMsgTxt("Required field 'Lambdaz' was not found in the element data structure"); NewFieldNumbers[4] = fnum; fnum = mxGetFieldNumber(ElemData,"Phasez"); if(fnum<0) mexErrMsgTxt("Required field 'Phasez' was not found in the element data structure"); NewFieldNumbers[5] = fnum; fnum = mxGetFieldNumber(ElemData,"Kx"); if(fnum<0) mexErrMsgTxt("Required field 'Kx' was not found in the element data structure"); NewFieldNumbers[6] = fnum; fnum = mxGetFieldNumber(ElemData,"NumIntSteps"); if(fnum<0) mexErrMsgTxt("Required field 'NumIntSteps' was not found in the element data structure"); NewFieldNumbers[7] = fnum; fnum = mxGetFieldNumber(ElemData,"T1"); if(fnum<0) mexErrMsgTxt("Required field 'T1' was not found in the element data structure"); NewFieldNumbers[8] = fnum; fnum = mxGetFieldNumber(ElemData,"T2"); if(fnum<0) mexErrMsgTxt("Required field 'T2' was not found in the element data structure"); NewFieldNumbers[9] = fnum; fnum = mxGetFieldNumber(ElemData,"R1"); if(fnum<0) mexErrMsgTxt("Required field 'R1' was not found in the element data structure"); NewFieldNumbers[10] = fnum; fnum = mxGetFieldNumber(ElemData,"R2"); if(fnum<0) mexErrMsgTxt("Required field 'R2' was not found in the element data structure"); NewFieldNumbers[11] = fnum; le = mxGetScalar(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[0])); nz = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[1])); nx = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[2])); b = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[3])); lambdaz = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[4])); phasez = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[5])); kx = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[6])); num_int_steps = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[7])); pt1 = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[8])); pt2 = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[9])); pr1 = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[10])); pr2 = mxGetPr(mxGetFieldByNumber(ElemData,0,NewFieldNumbers[11])); returnptr = NewFieldNumbers; } break; case USE_LOCAL_COPY: { /* Get fields from MATLAB using field numbers The second argument ponter to the array of field numbers is previously created with QuadLinPass( ..., MAKE_LOCAL_COPY) */ le = mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[0])); nz = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[1])); nx = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[2])); b = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[3])); lambdaz = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[4])); phasez = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[5])); kx = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[6])); num_int_steps = (int)mxGetScalar(mxGetFieldByNumber(ElemData,0,FieldNumbers[7])); pt1 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[8])); pt2 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[9])); pr1 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[10])); pr2 = mxGetPr(mxGetFieldByNumber(ElemData,0,FieldNumbers[11])); returnptr = FieldNumbers; } break; default: { mexErrMsgTxt("No match for calling mode in function WigSymplectic4Pass\n"); } } WigSymplectic4Pass(r_in, le, nz, nx, b, lambdaz, phasez, kx, num_int_steps, pt1, pt2, pr1, pr2, num_particles); return(returnptr); }
/* Create a default freeman pyramid control structure, and then customize it with the values from a matlab structure */ int mexFreemanPyramidCntrlStruct( const mxArray *mxStruct, FreemanPyramidCntrlStruct **cntrl, float *epsAmp, float *epsContrast ) { mxArray *field; double *ptr, ftmp; int fn, m, n, k, itmp; int nOrient, nScale; /* make sure mxStruct is a structure */ if( !mxIsStruct(mxStruct) ) mexErrMsgTxt( "Freeman pyramid control structure expected" ); /* start with a default control structure */ *cntrl = defaultFreemanPyramidCntrlStruct(); /* get the number of orientations if specified */ nOrient = -1; fn = mxGetFieldNumber( mxStruct, "nOrient" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); nOrient = (int)mxGetScalar( field ); if( nOrient < 1 || nOrient > NUMORIENT ) mexErrMsgTxt( "nOrient - invalid number of orientations" ); (*cntrl)->nOrient = nOrient; } /* get the orientations if they are specified */ fn = mxGetFieldNumber( mxStruct, "thetaDeg" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); m = mxGetM(field); n = mxGetN(field); if( min(m,n) != 1 ) mexErrMsgTxt( "thetaDeg - must be a vector" ); if( max(m,n) > NUMORIENT ) mexErrMsgTxt( "thetaDeg - too many orientations" ); (*cntrl)->nOrient = max(m,n); if( nOrient > 0 && (*cntrl)->nOrient != nOrient ) mexErrMsgTxt( "nOrient - must match length of vector thetaDeg" ); ptr = mxGetPr(field); for( k = 0; k < (*cntrl)->nOrient; k++ ) (*cntrl)->thetaDeg[k] = (float)ptr[k]; } /* get the number of orientations if specified */ nScale = -1; fn = mxGetFieldNumber( mxStruct, "nScale" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); nScale = (int)mxGetScalar( field ); if( nScale < 1 || nScale > NUMORIENT ) mexErrMsgTxt( "nScale - invalid number of scales" ); (*cntrl)->nScale = nScale; } /* get the scales if they are specified */ fn = mxGetFieldNumber( mxStruct, "lambda" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); m = mxGetM(field); n = mxGetN(field); if( min(m,n) != 1 ) mexErrMsgTxt( "lambda - must be a vector" ); if( max(m,n) > NUMORIENT ) mexErrMsgTxt( "lambda - too many scales" ); (*cntrl)->nScale = max(m,n); ptr = mxGetPr(field); for( k = 0; k < (*cntrl)->nScale; k++ ) (*cntrl)->lambda[k] = (float)ptr[k]; } /* get the size of the pyramid if specified */ fn = mxGetFieldNumber( mxStruct, "nPyr" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); itmp = (int)mxGetScalar( field ); if( itmp < 0 ) mexErrMsgTxt( "nPyr - cannot be negative" ); (*cntrl)->nPyr = itmp; } /* get the storeMap flags if they are specified */ fn = mxGetFieldNumber( mxStruct, "storeMap" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); m = mxGetM(field); n = mxGetN(field); if( min(m,n) != 1 ) mexErrMsgTxt( "storeMap - must be a vector" ); n = max(m,n); if( n > NUMCODE ) mexErrMsgTxt( "storeMap - too many values" ); ptr = mxGetPr(field); for( k = 0; k < n; k++ ) { itmp = (int)ptr[k]; if( itmp != 0 && itmp != 1 ) mexErrMsgTxt( "storeMap - elements must be 0 or 1" ); (*cntrl)->storeMap[k] = itmp; } } /* get the size of the border if specified */ fn = mxGetFieldNumber( mxStruct, "border" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); itmp = (int)mxGetScalar( field ); if( itmp < 0 ) mexErrMsgTxt( "border - cannot be negative" ); (*cntrl)->border = itmp; } /* get preSigma if specified */ fn = mxGetFieldNumber( mxStruct, "preSigma" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); ftmp = (float)mxGetScalar( field ); if( ftmp < 0.0 ) mexErrMsgTxt( "perSigma - cannot be negative" ); (*cntrl)->preSigma = ftmp; } /* get rockBottom if specified */ fn = mxGetFieldNumber( mxStruct, "rockBottom" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); ftmp = (float)mxGetScalar( field ); if( ftmp < 0.0 ) mexErrMsgTxt( "rockBottom - cannot be negative" ); (*cntrl)->rockBottom = ftmp; } /* get tauThres if specified */ fn = mxGetFieldNumber( mxStruct, "tauThres" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); ftmp = (float)mxGetScalar( field ); if( ftmp < 0.0 ) mexErrMsgTxt( "tauThres - cannot be negative" ); (*cntrl)->tauThres = ftmp; } /* get filterDiam if specified */ fn = mxGetFieldNumber( mxStruct, "filterDiam" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); ftmp = (float)mxGetScalar( field ); if( ftmp < 0.0 ) mexErrMsgTxt( "filterDiam - cannot be negative" ); (*cntrl)->filterDiam = ftmp; } /* get thetaTol if specified */ fn = mxGetFieldNumber( mxStruct, "thetaTol" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); ftmp = (float)mxGetScalar( field ); if( ftmp < 0.0 ) mexErrMsgTxt( "thetaTol - cannot be negative" ); (*cntrl)->thetaTol = ftmp; } /* get epsAmp if specified */ fn = mxGetFieldNumber( mxStruct, "epsAmp" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); ftmp = (float)mxGetScalar( field ); if( ftmp < 0.0 ) mexErrMsgTxt( "epsAmp - cannot be negative" ); *epsAmp = ftmp; } /* get epsC if specified */ fn = mxGetFieldNumber( mxStruct, "epsContrast" ); if( fn >= 0 ) { field = mxGetFieldByNumber( mxStruct, 0, fn ); ftmp = (float)mxGetScalar( field ); if( ftmp < 0.0 ) mexErrMsgTxt( "epsContrast - cannot be negative" ); *epsContrast = ftmp; } }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { float *Dxx, *Dxy, *Dxz, *Dyy, *Dyz, *Dzz; float *Jxx, *Jxy, *Jxz, *Jyy, *Jyz, *Jzz; /* Matrices of Eigenvector calculation */ double Ma[3][3]; double Davec[3][3]; double Daeig[3]; /* Eigenvector and eigenvalues as scalars */ double mu1, mu2, mu3, v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z; /* Amplitudes of diffustion tensor */ double lambda1, lambda2, lambda3; /* Constants used in diffusion tensor calculation */ double alpha, C, m; /* Denominator */ double di; /* Eps for finite values */ float eps=(float)1e-20; /* Used to read Options structure */ double *OptionsField; int field_num; /* Loop variable */ int i; /* Size of input */ const mwSize *idims; int nsubs=0; /* Number of pixels */ int npixels=1; /* Check for proper number of arguments. */ if(nrhs!=7) { mexErrMsgTxt("Seven inputs are required."); } else if(nlhs!=6) { mexErrMsgTxt("Six outputs are required"); } for(i=0; i<6; i++) { if(!mxIsSingle(prhs[i])){ mexErrMsgTxt("Inputs must be single"); } } /* Get constants from Options structure */ if(!mxIsStruct(prhs[6])){ mexErrMsgTxt("Options must be structure"); } field_num = mxGetFieldNumber(prhs[6], "alpha"); if(field_num>=0) { OptionsField=mxGetPr(mxGetFieldByNumber(prhs[6], 0, field_num)); alpha=(double)OptionsField[0]; } field_num = mxGetFieldNumber(prhs[6], "C"); if(field_num>=0) { OptionsField=mxGetPr(mxGetFieldByNumber(prhs[6], 0, field_num)); C=(double)OptionsField[0]; } field_num = mxGetFieldNumber(prhs[6], "m"); if(field_num>=0) { OptionsField=mxGetPr(mxGetFieldByNumber(prhs[6], 0, field_num)); m=(double)OptionsField[0]; } /* Get the number of dimensions */ nsubs = mxGetNumberOfDimensions(prhs[0]); /* Get the sizes of the inputs */ idims = mxGetDimensions(prhs[0]); for (i=0; i<nsubs; i++) { npixels=npixels*idims[i]; } /* Create output Tensor Volumes */ plhs[0] = mxCreateNumericArray(nsubs, idims, mxSINGLE_CLASS, mxREAL); plhs[1] = mxCreateNumericArray(nsubs, idims, mxSINGLE_CLASS, mxREAL); plhs[2] = mxCreateNumericArray(nsubs, idims, mxSINGLE_CLASS, mxREAL); plhs[3] = mxCreateNumericArray(nsubs, idims, mxSINGLE_CLASS, mxREAL); plhs[4] = mxCreateNumericArray(nsubs, idims, mxSINGLE_CLASS, mxREAL); plhs[5] = mxCreateNumericArray(nsubs, idims, mxSINGLE_CLASS, mxREAL); /* Assign pointers to each input. */ Jxx = (float *)mxGetPr(prhs[0]); Jxy = (float *)mxGetPr(prhs[1]); Jxz = (float *)mxGetPr(prhs[2]); Jyy = (float *)mxGetPr(prhs[3]); Jyz = (float *)mxGetPr(prhs[4]); Jzz = (float *)mxGetPr(prhs[5]); /* Assign pointers to each output. */ Dxx = (float *)mxGetPr(plhs[0]); Dxy = (float *)mxGetPr(plhs[1]); Dxz = (float *)mxGetPr(plhs[2]); Dyy = (float *)mxGetPr(plhs[3]); Dyz = (float *)mxGetPr(plhs[4]); Dzz = (float *)mxGetPr(plhs[5]); for(i=0; i<npixels; i++) { /* Calculate eigenvectors and values of local Hessian */ Ma[0][0]=(double)Jxx[i]+eps; Ma[0][1]=(double)Jxy[i]; Ma[0][2]=(double)Jxz[i]; Ma[1][0]=(double)Jxy[i]; Ma[1][1]=(double)Jyy[i]+eps; Ma[1][2]=(double)Jyz[i]; Ma[2][0]=(double)Jxz[i]; Ma[2][1]=(double)Jyz[i]; Ma[2][2]=(double)Jzz[i]+eps; eigen_decomposition(Ma, Davec, Daeig); /* Convert eigenvector and eigenvalue matrices back to scalar variables */ mu1=Daeig[2]; mu2=Daeig[1]; mu3=Daeig[0]; v1x=Davec[0][0]; v1y=Davec[1][0]; v1z=Davec[2][0]; v2x=Davec[0][1]; v2y=Davec[1][1]; v2z=Davec[2][1]; v3x=Davec[0][2]; v3y=Davec[1][2]; v3z=Davec[2][2]; /* Scaling of diffusion tensor */ di=mu1-mu3; if((di<eps)&&(di>-eps)) { lambda1 = alpha; } else { lambda1 = alpha + (1.0- alpha)*exp(-C/pow(di,(2.0*m))); } lambda2 = alpha; lambda3 = alpha; /* Construct the diffusion tensor */ Dxx[i] = (float)(lambda1*v1x*v1x + lambda2*v2x*v2x + lambda3*v3x*v3x); Dyy[i] = (float)(lambda1*v1y*v1y + lambda2*v2y*v2y + lambda3*v3y*v3y); Dzz[i] = (float)(lambda1*v1z*v1z + lambda2*v2z*v2z + lambda3*v3z*v3z); Dxy[i] = (float)(lambda1*v1x*v1y + lambda2*v2x*v2y + lambda3*v3x*v3y); Dxz[i] = (float)(lambda1*v1x*v1z + lambda2*v2x*v2z + lambda3*v3x*v3z); Dyz[i] = (float)(lambda1*v1y*v1z + lambda2*v2y*v2z + lambda3*v3y*v3z); } }
mxArray * mp_create_mxDict_from_dict(MP_Dict_c *dict) { const char *func = "mp_create_mxDict_from_dict"; map<string,string,mp_ltstring> *paramMap; mxArray *mxBlockCell; mxArray *mxBlock; mxArray *mxDict; mxArray *mxTable; MP_Block_c *block; int numDictFieldNames; int iFieldNumber; const char *dictFieldNames[] = {"block"}; // Case of a NULL input if(NULL==dict) { mp_error_msg(func,"the input is NULL\n"); return(NULL); } // Create the block cell mxBlockCell = mxCreateCellMatrix((mwSize)dict->numBlocks,(mwSize)1); if(mxBlockCell == NULL) { mp_error_msg(func,"could not create block cell\n"); return(NULL); } // Loop to create each block for (unsigned int i=0; i < dict->numBlocks; i++) { block = dict->block[i]; if(block == NULL) { mp_error_msg(func,"block 0<=i=%d<%d is NULL\n",i,dict->numBlocks); mxDestroyArray(mxBlockCell); return(NULL); } paramMap = block->get_block_parameters_map(); if (NULL==paramMap) { mp_error_msg(func,"empty paramMap for block 0<=i=%d<%d\n", i,dict->numBlocks); mxDestroyArray(mxBlockCell); return(NULL); } // Create mxBlock mxBlock = mxCreateStructMatrix((mwSize)1,(mwSize)1,0,NULL); if(mxBlock == NULL) { mp_error_msg(func,"could not create block 0<=i=%d<%d\n",i,dict->numBlocks); mxDestroyArray(mxBlockCell); return(NULL); } // Add all fields map<string, string, mp_ltstring>::const_iterator iter; for ( iter = (*paramMap).begin(); iter != (*paramMap).end(); iter++ ) { // TODO: we shouldn't need to know what fields are possible in the first place if(!strcmp(iter->first.c_str(),"data")) { // Adding the table Field mxAddField(mxBlock,"data"); if((mxTable = anywaveTableRead(paramMap, NULL)) == NULL) { mp_error_msg(func,"could not load the anywaveTable %s\n",iter->second.c_str()); mxDestroyArray(mxBlockCell); return(NULL); } iFieldNumber = mxGetFieldNumber(mxBlock, "data"); mxSetCell(mxBlock,iFieldNumber,mxTable); } else { // Add the field mxAddField(mxBlock,iter->first.c_str()); // TODO: Here should query the type of the field to set it appropriately! // Set the field value mxArray *mxTmp = mxCreateString(iter->second.c_str()); if(NULL==mxTmp) { mp_error_msg(func,"could not create field %s in block 0<=i=%d<%d\n",iter->second.c_str(),i,dict->numBlocks); mxDestroyArray(mxBlockCell); return(NULL); } mxSetField(mxBlock,0,iter->first.c_str(),mxTmp); // If the paramMap contains a file link to xml, then go search it ! // TODO: we shouldn't need to know what fields are possible in the first place if(!strcmp(iter->first.c_str(),"tableFileName")) { // Adding the table Field mxAddField(mxBlock,"data"); if((mxTable = anywaveTableRead(paramMap, (char *)iter->second.c_str())) == NULL) { mp_error_msg(func,"could not load the anywaveTable %s\n",iter->second.c_str()); mxDestroyArray(mxBlockCell); return(NULL); } iFieldNumber = mxGetFieldNumber(mxBlock, "data"); mxSetCell(mxBlock,iFieldNumber,mxTable); } } } // Put the mxBlock in the mxBlockCell mxSetCell(mxBlockCell,i,mxBlock); // Delete tmp variables delete paramMap; } // Create the output information structure // dict.block{numBlocks} numDictFieldNames = 1; mxDict = mxCreateStructMatrix((mwSize)1,(mwSize)1,numDictFieldNames,dictFieldNames); if(NULL==mxDict) { mp_error_msg(func,"could not create dict\n"); mxDestroyArray(mxBlockCell); return(NULL); } mxSetField(mxDict,0,"block",mxBlockCell); return(mxDict); }