// MEX-file gateway routine. Note that varbvsbinupdate.m checks the // inputs, so we do not have to do it here. void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { // GET INPUTS. const SingleMatrix X = getSingleMatrix(prhs[0]); const double sa = *mxGetPr(prhs[1]); const DoubleVector logodds = getDoubleVector(prhs[2]); const DoubleVector d = getDoubleVector(mxGetField(prhs[3],0,"d")); const DoubleVector xdx = getDoubleVector(mxGetField(prhs[3],0,"xdx")); const DoubleVector xy = getDoubleVector(mxGetField(prhs[3],0,"xy")); const DoubleVector xd = getDoubleVector(mxGetField(prhs[3],0,"xd")); const DoubleVector alpha0 = getDoubleVector(prhs[4]); const DoubleVector mu0 = getDoubleVector(prhs[5]); const DoubleVector Xr0 = getDoubleVector(prhs[6]); const DoubleVector I = getDoubleVector(prhs[7]); // Get the number of samples (n), the number of variables (p), and // the number of coordinate ascent updates (numiter). const Size n = X.nr; const Size p = X.nc; const Size numiter = I.n; // INITIALIZE OUTPUTS. DoubleVector alpha = createMatlabVector(p,&plhs[0]); DoubleVector mu = createMatlabVector(p,&plhs[1]); DoubleVector Xr = createMatlabVector(n,&plhs[2]); copyDoubleVector(alpha0,alpha); copyDoubleVector(mu0,mu); copyDoubleVector(Xr0,Xr); // This is storage for a column of matrix X. double* x = malloc(sizeof(double)*n); // RUN COORDINATE ASCENT UPDATES. // Repeat for each coordinate ascent update. for (Index iter = 0; iter < numiter; iter++) { Index k = (Index) I.elems[iter]; // Copy the kth column of matrix X. copyColumn(X.elems,x,k,n); // Perform the update. varbvsbinupdate(x,xy.elems[k],xd.elems[k],xdx.elems[k],d.elems,sa, logodds.elems[k],alpha.elems+k,mu.elems+k,Xr.elems,n); } // Free the dynamically allocated memory. free(x); }
ExtractedMesh FBXReader::extractMesh(const FBXNode& object, unsigned int& meshIndex, bool deduplicate) { MeshData data; data.extracted.mesh.meshIndex = meshIndex++; QVector<int> materials; QVector<int> textures; bool isMaterialPerPolygon = false; static const QVariant BY_VERTICE = QByteArray("ByVertice"); static const QVariant INDEX_TO_DIRECT = QByteArray("IndexToDirect"); bool isDracoMesh = false; foreach (const FBXNode& child, object.children) { if (child.name == "Vertices") { data.vertices = createVec3Vector(getDoubleVector(child)); } else if (child.name == "PolygonVertexIndex") { data.polygonIndices = getIntVector(child); } else if (child.name == "LayerElementNormal") { data.normalsByVertex = false; bool indexToDirect = false; foreach (const FBXNode& subdata, child.children) { if (subdata.name == "Normals") { data.normals = createVec3Vector(getDoubleVector(subdata)); } else if (subdata.name == "NormalsIndex") { data.normalIndices = getIntVector(subdata); } else if (subdata.name == "MappingInformationType" && subdata.properties.at(0) == BY_VERTICE) { data.normalsByVertex = true; } else if (subdata.name == "ReferenceInformationType" && subdata.properties.at(0) == INDEX_TO_DIRECT) { indexToDirect = true; } } if (indexToDirect && data.normalIndices.isEmpty()) { // hack to work around wacky Makehuman exports data.normalsByVertex = true; } } else if (child.name == "LayerElementColor") {
// MEX-file gateway routine. Note that varbvsupdate.m checks the // inputs, so we do not have to do it here. void mexFunction (int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { // Get inputs. const SparseMatrix SiRiS = getSparseMatrix(prhs[0]); const double sigma_beta = *mxGetPr(prhs[1]); const DoubleVector logodds = getDoubleVector(prhs[2]); const DoubleVector betahat = getDoubleVector(prhs[3]); const DoubleVector se = getDoubleVector(prhs[4]); const DoubleVector alpha0 = getDoubleVector(prhs[5]); const DoubleVector mu0 = getDoubleVector(prhs[6]); const DoubleVector SiRiSr0 = getDoubleVector(prhs[7]); const DoubleVector I = getDoubleVector(prhs[8]); // Get the number of SNPs (p) and coordinate ascent updates (m). const Size p = betahat.n; const Size m = I.n; Index* Ir = SiRiS.ir; Index* Jc = SiRiS.jc; // Initialize outputs. DoubleVector alpha = createMatlabVector(p,&plhs[0]); DoubleVector mu = createMatlabVector(p,&plhs[1]); DoubleVector SiRiSr = createMatlabVector(p,&plhs[2]); copyDoubleVector(alpha0,alpha); copyDoubleVector(mu0,mu); copyDoubleVector(SiRiSr0,SiRiSr); // Store a single column of matrix inv(S)*R*inv(S). double* SiRiS_snp = malloc(sizeof(double)*p); // Run coordinate ascent updates. // Repeat for each coordinate ascent update. for (Index j = 0; j < m; j++) { Index k = (Index) I.elems[j]; // Copy the kth column of matrix inv(S)*R*inv(S). copySparseColumn(SiRiS_snp, k, SiRiS.elems, Ir, Jc, p); // Copy the kth element of vector inv(S)*R*inv(S)*r. double SiRiSr_snp = SiRiSr.elems[k]; // Perform the mean-field variational update. rss_varbvsr_update(betahat.elems[k], se.elems[k], sigma_beta, SiRiS_snp, SiRiSr.elems, SiRiSr_snp, logodds.elems[k], alpha.elems+k, mu.elems+k, p); } // Free the dynamically allocated memory. free(SiRiS_snp); }
// Import a whole network or a parametric description of preprocessors or algorithms int csimMexImport(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nrhs == 3) { // Import preprocessor or filter // Get identifiers of preprocessors or algorithms uint32 *idx; int nIdx; if ( getUint32Vector(prhs[1],&idx,&nIdx) ) mexErrMsgTxt("CSIM-Usage: csim('set',idx[,field,value]*); idx is not a uint32 vector.\n"); // Get description data double *values; int n; if ( getDoubleVector(prhs[2],&values,&n) ) mexErrMsgTxt("CSIM-Usage: csim('import',idx,values; values is not a double vector.\n"); if ( n < 1 ) mexErrMsgTxt("CSIM-Usage: csim('import',idx,values; values is empty!"); // Import the data if ( TheNetwork->importObject(idx,nIdx,values,n) < 0 ) { TheCsimError.add("csim('import',idx,values; failed\n"); return -1; } return 0; } else if (nrhs == 2) { // Import a whole network if ( !TheNetwork ) TheNetwork = new MexNetwork(); return TheNetwork->importNetwork(prhs[1]); } else mexErrMsgTxt("CSIM-Usage: csim('import',net);\n"); }
data.normalsByVertex = true; } else if (subdata.name == "ReferenceInformationType" && subdata.properties.at(0) == "IndexToDirect") { indexToDirect = true; } } if (indexToDirect && data.normalIndices.isEmpty()) { // hack to work around wacky Makehuman exports data.normalsByVertex = true; } } else if (child.name == "LayerElementColor") { data.colorsByVertex = false; bool indexToDirect = false; foreach (const FBXNode& subdata, child.children) { if (subdata.name == "Colors") { data.colors = createVec4VectorRGBA(getDoubleVector(subdata), data.averageColor); } else if (subdata.name == "ColorsIndex") { data.colorIndices = getIntVector(subdata); } else if (subdata.name == "MappingInformationType" && subdata.properties.at(0) == "ByVertice") { data.colorsByVertex = true; } else if (subdata.name == "ReferenceInformationType" && subdata.properties.at(0) == "IndexToDirect") { indexToDirect = true; } } if (indexToDirect && data.normalIndices.isEmpty()) { // hack to work around wacky Makehuman exports data.colorsByVertex = true; }
// Create a column vector in MATLAB array. DoubleVector createMatlabVector (Size n, mxArray** ptr) { *ptr = mxCreateDoubleMatrix(n,1,mxREAL); return getDoubleVector(*ptr); }
int csimMexSimulate(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int i; int n, m, nInputs, nSigArg, nTeachers = 0; int i_spiking, i_dt, i_data, i_idx; if ( !TheNetwork ) mexErrMsgTxt("CSIM: No network initialized yet!\n"); if ( nrhs < 2 || nlhs > 1) mexErrMsgTxt("CSIM-Usage: [output = ]csim('simulate',Tsim,[,InputSignal]*);\n"); double Tsim; if ( getDouble(prhs[1],&Tsim) ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Tsim is not a single double.\n"); // prhs[2,...] should be something like a struct or an empty matrix nInputs=0; csimInputChannel *inputs=0; csimInputChannel *teachers=0; int k=2; while ( k < nrhs ) { n = mxGetN(prhs[k]); m = mxGetM(prhs[k]); nSigArg = max(n,m); if ( nSigArg > 0 && !mxIsStruct(prhs[k]) ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input is non empty but not a struct array.\n"); if ( nSigArg == 0 ) mexPrintf("CSIM-Warning: input is empty!\n"); if ( nSigArg > 0 && min(m,n) != 1 ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input is not a struct vector.\n"); double tmp=0.0; if ( nSigArg > 0 ) { i_spiking = mxGetFieldNumber(prhs[k],"spiking"); if ( i_spiking < 0 ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input has no field 'spiking' \n"); i_dt = mxGetFieldNumber(prhs[k],"dt"); if ( i_dt < 0 ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input has no field 'dt' \n"); i_data = mxGetFieldNumber(prhs[k],"data"); if ( i_data < 0 ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input has no field 'data' \n"); i_idx = mxGetFieldNumber(prhs[k],"idx"); if ( i_idx < 0 ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input has no field 'idx' \n"); inputs = (csimInputChannel *)mxRealloc(inputs,sizeof(csimInputChannel)*(nInputs+nSigArg)); // printf("after realloc %i\n",nSigArg); for(i=0; i<nSigArg; i++) { if ( getDouble(mxGetFieldByNumber(prhs[k], i, i_spiking),&tmp) < 0 ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input(i).spiking is not scalar \n"); inputs[nInputs+i].dt = ( tmp > 0.0 ) ? -1 : 0; if ( getDouble(mxGetFieldByNumber(prhs[k], i, i_dt),&tmp) < 0 ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input(i).dt is not scalar \n"); if ( inputs[nInputs+i].dt == 0 ) inputs[nInputs+i].dt = tmp; if ( getDoubleVector(mxGetFieldByNumber(prhs[k], i, i_data),&(inputs[nInputs+i].data),&(inputs[nInputs+i].length)) ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input(i).data is not a double vector \n"); if ( getUint32Vector(mxGetFieldByNumber(prhs[k], i, i_idx),&(inputs[nInputs+i].idx),&(inputs[nInputs+i].nIdx)) ) mexErrMsgTxt("CSIM-Usage: csim('simulate',Tsim[,InputSignal]*); Input(i).odx is not a uint32 scalar \n"); } nInputs += nSigArg; } k++; } if ( TheNetwork->simulate((unsigned long)(Tsim/DT+0.5),inputs,nInputs,teachers,nTeachers) < 0) { TheCsimError.add("CSIM: csim('simulate',Tsim[,InputSignal]*); failed!\n"); return -1; } //now we have to output the recorded stuff! if (nlhs > 0) plhs[0] = TheNetwork->getMexOutput(); return 0; }