📄 signdata.mex.c
字号:
/* file: signdata.mex.c** description: MEX weighted signdata function.** author: Andrea Vedaldi**//* AUTORIGHTSCopyright (C) 2006 Regents of the University of CaliforniaAll rights reservedWritten by Andrea Vedaldi (UCLA VisionLab).Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of California, Berkeley nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANYEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED ANDON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include<mexutils.c>#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<assert.h>intpowi(int x, int i){ int j,z ; if(i == 0) return 1 ; if(i == 1) return x ; j = i >> 1 ; z = powi(x,j) ; return (i - j - j) ? z*z*x : z*z ; }/* pairs are used to generate random permutations of data */typedef struct {int w ; int j ;} pair_t ; int cmp_pair(void const* a, void const* b) { pair_t* pa = (pair_t*) a; pair_t* pb = (pair_t*) b; return pa->w - pb->w ;}/* driver */voidmexFunction(int nout, mxArray *out[], int nin, const mxArray *in[]){ typedef unsigned char data_t ; typedef int acc_t ; typedef unsigned int idx_t ; idx_t* asgn_pt ; idx_t* signature_pt ; /* mxClassID data_class = mxINT8_CLASS ;*/ enum {IN_K=0,IN_DEPTH,IN_ASGN} ; enum {OUT_SIGNATURE=0} ; int M,N,L,K=0 ; int depth=0 ; /** ----------------------------------------------------------------- ** Check the arguments ** -------------------------------------------------------------- */ if (nin != 3) { mexErrMsgTxt("Three arguments required.") ; } else if (nout > 2) { mexErrMsgTxt("Too many output arguments."); } if(mxGetClassID(in[IN_ASGN]) != mxUINT32_CLASS) { mexErrMsgTxt("ASGN must be of class uint32") ; } if(uIsRealScalar(in[IN_K])) { K = (int) *mxGetPr(in[IN_K]) ; if(K > 0) goto K_is_ok ; } mexErrMsgTxt("K must be a positive integer.") ; K_is_ok: if(uIsRealScalar(in[IN_DEPTH])) { depth = (int) *mxGetPr(in[IN_DEPTH]) ; if(depth > 0) goto depth_is_ok ; } mexErrMsgTxt("DEPTH must be a positive integer.") ; depth_is_ok: M = mxGetM(in[IN_ASGN]) ; /* n of components */ N = mxGetN(in[IN_ASGN]) ; /* n of elements */ L = (powi(K,depth+1) - 1) / (K - 1) ; { int dims [2] ; dims[0] = L ; dims[1] = 1 ; out[OUT_SIGNATURE] = mxCreateNumericArray(2,dims,mxUINT32_CLASS, mxREAL) ; } asgn_pt = (idx_t*) mxGetPr(in[IN_ASGN]) ; signature_pt = (idx_t*) mxGetPr(out[OUT_SIGNATURE]) ; /** ----------------------------------------------------------------- ** Go ** -------------------------------------------------------------- */ { int d,j ; /* branch_offset | level_offset --------------------------------+----------------- s(0) K^0 | 1 s(0) K^1 + s(1)*K^0 | 1 + K s(0) K^2 + s(1)*K^1 + s(2)*K^0 | 1 + K + K^2 ... | ... */ for(j = 0 ; j < N ; ++j) { int level_offset = 0 ; int branch_offset = 0 ; for(d = 0 ; d < depth ; ++d) { int s = asgn_pt[depth*j + d] - 1 ; level_offset = K * level_offset + 1 ; branch_offset = K * branch_offset + s ; /* -1 because we do not have the root */ ++ signature_pt[ level_offset + branch_offset - 1] ; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -