int main(int argc, char *argv[]) { printf("\n"); if (argc < 2) { printf("not enough arguments\n\n"); return 1; } printf("STARTING MATLAB ENGINE\n\n"); Engine *ep = engOpen(""); if (ep == NULL) { printf("unable to start MATLAB engine\n\n"); return 1; } engSetVisible(ep, false); char out[BUFSIZE]; engOutputBuffer(ep, out, BUFSIZE); printf("SETTING PATH TO CNS\n\n"); Eval(ep, out, "run(fullfile('%s', 'cns_path'));", argv[1]); bool ok = RunDemo(ep, out); printf("PRESS RETURN TO CONTINUE: "); fgets(out, BUFSIZE, stdin); printf("\n"); printf("CLOSING MATLAB ENGINE\n\n"); Eval(ep, out, "close all;"); engClose(ep); return ok ? 0 : 1; }
void engvis(int v) { bool SUCCESS = true; //success flag bool vs = !(0 == v); if (NULL == Eng) //if MATLAB not opened { msg("eng::noMLB"); //message SUCCESS = false; } else if(engSetVisible(Eng, vs)) //if error occurs { msg("engVis::erchg"); SUCCESS = false; } if(SUCCESS) MLPutSymbol(stdlink, "Null"); else MLPutSymbol(stdlink, "$Failed"); }
int main(int argc, char** argv) { //OpenMesh::IO::Options r_options, w_options; //string tmeshfile = "C:\\Users\\duan_qi\\Desktop\\Reconstruction\\PhotoSynthToolkit11\\templeRing\\pmvs\\models\\pmvs_options.txt.ply"; //string fmeshfile = "C:\\Users\\duan_qi\\Desktop\\Reconstruction\\PhotoSynthToolkit11\\templeRing\\pmvs\\models\\pmvs_options.txt.filtered.off"; //r_options.set(OpenMesh::IO::Options::VertexColor); w_options.set(OpenMesh::IO::Options::VertexColor); //OpenMesh::IO::read_mesh(ObjTriMesh, tmeshfile, r_options); //if ( !r_options.check( OpenMesh::IO::Options::VertexColor ) ) { // cout << "Color is not loaded.." << endl; //} //int rcount = 0; //for (MyMesh::VertexIter v_it = ObjTriMesh.vertices_begin(); v_it != ObjTriMesh.vertices_end(); ++ v_it) { // OpenMesh::Vec3f tColor; // tColor[0] = ObjTriMesh.color(v_it).data()[0]; // tColor[1] = ObjTriMesh.color(v_it).data()[1]; // tColor[2] = ObjTriMesh.color(v_it).data()[2]; // if (tColor.norm() < 40) { // ObjTriMesh.delete_vertex(v_it, false); // rcount ++; // } //} //ObjTriMesh.garbage_collection(); //OpenMesh::IO::write_mesh(ObjTriMesh, fmeshfile, w_options); ParseParam(argc,argv, MOptions); ScaleDelta = MOptions.ScaleDelta; AnisotropicLaplace = MOptions.AnisotropicLaplace; RecordColor = MOptions.RecordColor; if (MOptions.UseMatlabSolver) { cout << "Use matlab solver for linear equations." << endl; if (!(m_ep = engOpen("\0"))) { std::cout << "Can not start Matlab engine" << std::endl; return false; } engSetVisible(m_ep, false); } ScaleDelta?cout<<"Scale delta P each time. ":cout<<" "; AnisotropicLaplace?cout<<"Using anisotropic laplacian term.":cout<<" "; RecordColor?cout<<"Record vertex color to mesh. ":cout<<" "; cout << endl; path = MOptions.DirName; printf("Number of threads %d\n",omp_get_num_procs()); omp_set_num_threads(omp_get_num_procs()); omp_set_num_threads(8); double timer_start = (double)cv::getTickCount(); if (!FileExisted( (MOptions.DirName + "InitialPoissonModel.ply").c_str() )) { if (!LoadMVSResult()) { readMiddleBuryData2(MOptions.DirName); chooseStereoPairs(); stereoMatching(); printf("\nTime = %lfs\n",((double)cv::getTickCount()-timer_start)/cv::getTickFrequency()); buildTracks(); printf("\nTime = %lfs\n",((double)cv::getTickCount()-timer_start)/cv::getTickFrequency()); //writeToOBJ(); //printf("\nTime = %lfs\n",((double)getTickCount()-timer_start)/getTickFrequency()); calNormals(); printf("\nTime = %lfs\n",((double)cv::getTickCount()-timer_start)/cv::getTickFrequency()); verifyTracks(); printf("\nTime = %lfs\n",((double)cv::getTickCount()-timer_start)/cv::getTickFrequency()); SaveMVSResult(); } writeToNPTS2(tracks,(MOptions.DirName + "PointInfo.npts")); //outputVerticesWithNormals(tracks,(MOptions.DirName + "PointModel.ply")); printf("\nTime = %lfs\n",((double)cv::getTickCount()-timer_start)/cv::getTickFrequency()); //printf("\nTime = %lfs\n",((double)cv::getTickCount()-timer_start)w/cv::getTickFrequency()); string PlyModelName = MOptions.DirName + "PoissonModel"; PoissonReconstruction((MOptions.DirName + "PointInfo.npts"), PlyModelName); MyCopyFile(PlyModelName, (MOptions.DirName + "InitialPoissonModel.ply")); } readMiddleBuryData2(MOptions.DirName); //read the initial generated Poisson object model OpenMesh::IO::Options read_options, write_options; string ObjName = MOptions.DirName; if (ObjName.find_last_of("\\") == ObjName.length()-1) { ObjName.erase(ObjName.end()-1); } ObjName = ObjName.substr(ObjName.find_last_of("\\")+1, ObjName.length()); MOptions.meshname = ObjName; string meshfile = (MOptions.DirName + ObjName +"-remeshed.off"); if (!FileExisted(meshfile.c_str())) { string cmd = "meshfix.exe "; cmd += MOptions.DirName + "InitialPoissonModel.ply"; WinExec(cmd.c_str(),0); ::Sleep(5000); MyMoveFile(MOptions.DirName + "InitialPoissonModel_fixed.off", MOptions.DirName + "temp.off"); for (int i = 0; i < 5; ++ i) { cmd = "meshfix.exe "; cmd += MOptions.DirName + "temp.off"; WinExec(cmd.c_str(),0); ::Sleep(5000); MyMoveFile(MOptions.DirName + "temp_fixed.off", MOptions.DirName + "temp.off"); } MyMoveFile(MOptions.DirName + "temp.off", meshfile); } fstream fin(meshfile,ios::in); string tag, temp_str; fin>>tag; fin>>temp_str; if (temp_str[0] == '#') { // need to load and rewrite the off file char buffer[100]; fin.getline(buffer, 100); fin.getline(buffer, 100); fstream fout(meshfile+"tmp",ios::out); int vnum, trinum, flag; double x, y, z; int a1, v0, v1, v2; fout<<tag<<endl; fin>>vnum>>trinum>>flag; fout<<vnum<<" "<<trinum<<" "<<flag<<endl; for (int i = 0; i < vnum; ++ i) { fin>>x>>y>>z; fout<<x<<" "<<y<<" "<<z<<endl; } for (int i = 0; i < trinum; ++i) { fin>>a1>>v0>>v1>>v2; fout<<a1<<" "<<v0<<" "<<v1<<" "<<v2<<endl; } fout.close(); MyMoveFile(meshfile+"tmp", meshfile); } fin.close();
EXPORT CLASS *init(CALLBACKS *fntable, MODULE *module, int argc, char *argv[]) { if (set_callback(fntable)==NULL) { errno = EINVAL; return NULL; } // open a connection to the Matlab engine int status=0; static char server[1024]; if (gl_global_getvar("matlab_server",server,sizeof(server))) matlab_server = server; if (strcmp(matlab_server,"standalone")==0) engine = engOpenSingleUse(NULL,NULL,&status); else engine = engOpen(matlab_server); if (engine==NULL) { gl_error("unable to start Matlab engine (code %d)",status); return NULL; } // prepare session char debug[8]; if (gl_global_getvar("debug",debug,sizeof(debug))) debugmode = (atoi(debug)==1); engSetVisible(engine,debugmode?1:0); engEvalString(engine,"clear all;"); char env[1024]; _snprintf(env,sizeof(env),"NEVER=%g;INVALID=%g;",TOSERIAL(TS_NEVER),TOSERIAL(TS_INVALID)); engEvalString(engine,env); // collect output from Matlab engOutputBuffer(engine,output,sizeof(output)); // setup the Matlab module and run the class constructor engEvalString(engine,"global passconfig;"); if (engEvalString(engine,argv[0])!=0) gl_error("unable to evaluate function '%s' in Matlab", argv[0]); else gl_matlab_output(); // read the pass configuration mxArray *pcfg= engGetVariable(engine,"passconfig"); if (pcfg && mxIsChar(pcfg)) { char passinfo[1024]; KEYWORD keys[] = { {"NOSYNC",PC_NOSYNC,keys+1}, {"PRETOPDOWN",PC_PRETOPDOWN,keys+2}, {"BOTTOMUP",PC_BOTTOMUP,keys+3}, {"POSTTOPDOWN",PC_POSTTOPDOWN,NULL}, }; PROPERTY pctype = {0,"passconfig",PT_set,1,PA_PUBLIC,NULL,&passconfig,NULL,keys,NULL}; set passdata; if (mxGetString(pcfg,passinfo,sizeof(passinfo))==0 && callback->convert.string_to_property(&pctype,&passdata,passinfo)>0) { passconfig = (PASSCONFIG)passdata; oclass=gl_register_class(module,argv[0],passconfig); if (oclass==NULL) gl_error("unable to register '%s' as a class",argv[0]); DELEGATEDTYPE *pDelegate = new DELEGATEDTYPE; pDelegate->oclass = oclass; strncpy(pDelegate->type,"matlab",sizeof(pDelegate->type)); pDelegate->from_string = object_from_string; pDelegate->to_string = object_to_string; if (gl_publish_variable(oclass,PT_delegated,pDelegate,"data",0,NULL)<1) GL_THROW("unable to publish properties in %s",__FILE__); } else gl_error("passconfig is invalid (expected set of NOSYNC, PRETOPDOWN, BOTTOMUP, and POSTTOPDOWN)", passinfo); } else gl_error("passconfig not specified"); // read the pass configuration mxArray *ans= engGetVariable(engine,"ans"); if (ans && mxIsStruct(ans)) { defaults = mxDuplicateArray(ans); // process the answer int nFields = mxGetNumberOfFields(ans), i; for (i=0; i<nFields; i++) { const char *name = mxGetFieldNameByNumber(ans,i); mxArray *data = mxGetFieldByNumber(ans,0,i); // @todo publish the structure } } else gl_error("result of call to matlab::%s did not return a structure", argv[0]); #ifdef OPTIONAL /* TODO: publish global variables (see class_define_map() for details) */ gl_global_create(char *name, ..., NULL); /* TODO: use gl_global_setvar, gl_global_getvar, and gl_global_find for access */ #endif /* always return the first class registered */ return oclass; }
EXPORT bool glx_init(glxlink *mod) { gl_verbose("initializing matlab link"); gl_verbose("PATH=%s", getenv("PATH")); // initialize matlab engine MATLABLINK *matlab = (MATLABLINK*)mod->get_data(); matlab->status = 0; #ifdef WIN32 if ( matlab->command ) matlab->engine = engOpen(matlab->command); else matlab->engine = engOpenSingleUse(NULL,NULL,&matlab->status); if ( matlab->engine==NULL ) { gl_error("matlab engine start failed, status code is '%d'", matlab->status); return false; } #else matlab->engine = engOpen(matlab->command); if ( matlab->engine==NULL ) { gl_error("matlab engine start failed"); return false; } #endif // set the output buffer if ( matlab->output_buffer!=NULL ) engOutputBuffer(matlab->engine,matlab->output_buffer,(int)matlab->output_size); // setup matlab engine engSetVisible(matlab->engine,window_show(matlab)); gl_debug("matlab link is open"); // special values needed by matlab mxArray *ts_never = mxCreateDoubleScalar((double)(TIMESTAMP)TS_NEVER); engPutVariable(matlab->engine,"TS_NEVER",ts_never); mxArray *ts_error = mxCreateDoubleScalar((double)(TIMESTAMP)TS_INVALID); engPutVariable(matlab->engine,"TS_ERROR",ts_error); mxArray *gld_ok = mxCreateDoubleScalar((double)(bool)true); engPutVariable(matlab->engine,"GLD_OK",gld_ok); mxArray *gld_err = mxCreateDoubleScalar((double)(bool)false); engPutVariable(matlab->engine,"GLD_ERROR",gld_err); // set the workdir if ( strcmp(matlab->workdir,"")!=0 ) { #ifdef WIN32 _mkdir(matlab->workdir); #else mkdir(matlab->workdir,0750); #endif if ( matlab->workdir[0]=='/' ) matlab_exec(matlab,"cd '%s'", matlab->workdir); else matlab_exec(matlab,"cd '%s/%s'", getcwd(NULL,0),matlab->workdir); } // run the initialization command(s) if ( matlab->init ) { mxArray *ans = matlab_exec(matlab,"%s",matlab->init); if ( ans && mxIsDouble(ans) && (bool)*mxGetPr(ans)==false ) { gl_error("matlab init failed"); return false; } else if ( ans && mxIsChar(ans) ) { int buflen = (mxGetM(ans) * mxGetN(ans)) + 1; char *string =(char*)malloc(buflen); int status_error = mxGetString(ans, string, buflen); if (status_error == 0) { gl_error("'%s'",string); return false; } else { gl_error("Did not catch Matlab error"); return false; } } } if ( matlab->rootname!=NULL ) { // build gridlabd data mwSize dims[] = {1,1}; mxArray *gridlabd_struct = mxCreateStructArray(2,dims,0,NULL); /////////////////////////////////////////////////////////////////////////// // build global data LINKLIST *item; mxArray *global_struct = mxCreateStructArray(2,dims,0,NULL); for ( item=mod->get_globals() ; item!=NULL ; item=mod->get_next(item) ) { char *name = mod->get_name(item); GLOBALVAR *var = mod->get_globalvar(item); mxArray *var_struct = NULL; mwIndex var_index; if ( var==NULL ) continue; // do not map module or structured globals if ( strchr(var->prop->name,':')!=NULL ) { // ignore module globals here } else if ( strchr(var->prop->name,'.')!=NULL ) { char struct_name[256]; if ( sscanf(var->prop->name,"%[^.]",struct_name)==0 ) { gld_property prop(var); var_index = mxAddField(global_struct,prop.get_name()); var_struct = matlab_create_value(&prop); if ( var_struct!=NULL ) { //mod->add_copyto(var->prop->addr,mxGetData(var_struct)); mxSetFieldByNumber(global_struct,0,var_index,var_struct); } } } else // simple data { gld_property prop(var); var_index = mxAddField(global_struct,prop.get_name()); var_struct = matlab_create_value(&prop); if ( var_struct!=NULL ) { //mod->add_copyto(var->prop->addr,mxGetData(var_struct)); mxSetFieldByNumber(global_struct,0,var_index,var_struct); } } // update export list if ( var_struct!=NULL ) { mod->set_addr(item,(void*)var_struct); mod->set_index(item,(size_t)var_index); } } // add globals structure to gridlabd structure mwIndex gridlabd_index = mxAddField(gridlabd_struct,"global"); mxSetFieldByNumber(gridlabd_struct,0,gridlabd_index,global_struct); /////////////////////////////////////////////////////////////////////////// // build module data dims[0] = dims[1] = 1; mxArray *module_struct = mxCreateStructArray(2,dims,0,NULL); // add modules for ( MODULE *module = callback->module.getfirst() ; module!=NULL ; module=module->next ) { // create module info struct mwIndex dims[] = {1,1}; mxArray *module_data = mxCreateStructArray(2,dims,0,NULL); mwIndex module_index = mxAddField(module_struct,module->name); mxSetFieldByNumber(module_struct,0,module_index,module_data); // create version info struct const char *version_fields[] = {"major","minor"}; mxArray *version_data = mxCreateStructArray(2,dims,sizeof(version_fields)/sizeof(version_fields[0]),version_fields); mxArray *major_data = mxCreateDoubleScalar((double)module->major); mxArray *minor_data = mxCreateDoubleScalar((double)module->minor); mxSetFieldByNumber(version_data,0,0,major_data); mxSetFieldByNumber(version_data,0,1,minor_data); // attach version info to module info mwIndex version_index = mxAddField(module_data,"version"); mxSetFieldByNumber(module_data,0,version_index,version_data); } gridlabd_index = mxAddField(gridlabd_struct,"module"); mxSetFieldByNumber(gridlabd_struct,0,gridlabd_index,module_struct); /////////////////////////////////////////////////////////////////////////// // build class data dims[0] = dims[1] = 1; mxArray *class_struct = mxCreateStructArray(2,dims,0,NULL); gridlabd_index = mxAddField(gridlabd_struct,"class"); mxSetFieldByNumber(gridlabd_struct,0,gridlabd_index,class_struct); mwIndex class_id[1024]; // index into class struct memset(class_id,0,sizeof(class_id)); // add classes for ( CLASS *oclass = callback->class_getfirst() ; oclass!=NULL ; oclass=oclass->next ) { // count objects in this class mwIndex dims[] = {0,1}; for ( item=mod->get_objects() ; item!=NULL ; item=mod->get_next(item) ) { OBJECT *obj = mod->get_object(item); if ( obj==NULL || obj->oclass!=oclass ) continue; dims[0]++; } if ( dims[0]==0 ) continue; mxArray *runtime_struct = mxCreateStructArray(2,dims,0,NULL); // add class mwIndex class_index = mxAddField(class_struct,oclass->name); mxSetFieldByNumber(class_struct,0,class_index,runtime_struct); // add properties to class for ( PROPERTY *prop=oclass->pmap ; prop!=NULL && prop->oclass==oclass ; prop=prop->next ) { mwIndex dims[] = {1,1}; mxArray *property_struct = mxCreateStructArray(2,dims,0,NULL); mwIndex runtime_index = mxAddField(runtime_struct,prop->name); mxSetFieldByNumber(runtime_struct,0,runtime_index,property_struct); } // add objects to class for ( item=mod->get_objects() ; item!=NULL ; item=mod->get_next(item) ) { OBJECT *obj = mod->get_object(item); if ( obj==NULL || obj->oclass!=oclass ) continue; mwIndex index = class_id[obj->oclass->id]++; // add properties to class for ( PROPERTY *prop=oclass->pmap ; prop!=NULL && prop->oclass==oclass ; prop=prop->next ) { gld_property p(obj,prop); mxArray *data = matlab_create_value(&p); mxSetField(runtime_struct,index,prop->name,data); } // update export list mod->set_addr(item,(void*)runtime_struct); mod->set_index(item,(size_t)index); } } /////////////////////////////////////////////////////////////////////////// // build the object data dims[0] = 0; for ( item=mod->get_objects() ; item!=NULL ; item=mod->get_next(item) ) { if ( mod->get_object(item)!=NULL ) dims[0]++; } dims[1] = 1; memset(class_id,0,sizeof(class_id)); const char *objfields[] = {"name","class","id","parent","rank","clock","valid_to","schedule_skew", "latitude","longitude","in","out","rng_state","heartbeat","lock","flags"}; mxArray *object_struct = mxCreateStructArray(2,dims,sizeof(objfields)/sizeof(objfields[0]),objfields); mwIndex n=0; for ( item=mod->get_objects() ; item!=NULL ; item=mod->get_next(item) ) { OBJECT *obj = mod->get_object(item); if ( obj==NULL ) continue; class_id[obj->oclass->id]++; // index into class struct const char *objname[] = {obj->name&&isdigit(obj->name[0])?NULL:obj->name}; const char *oclassname[] = {obj->oclass->name}; if (obj->name) mxSetFieldByNumber(object_struct,n,0,mxCreateCharMatrixFromStrings(mwSize(1),objname)); mxSetFieldByNumber(object_struct,n,1,mxCreateCharMatrixFromStrings(mwSize(1),oclassname)); mxSetFieldByNumber(object_struct,n,2,mxCreateDoubleScalar((double)class_id[obj->oclass->id])); if (obj->parent) mxSetFieldByNumber(object_struct,n,3,mxCreateDoubleScalar((double)obj->parent->id+1)); mxSetFieldByNumber(object_struct,n,4,mxCreateDoubleScalar((double)obj->rank)); mxSetFieldByNumber(object_struct,n,5,mxCreateDoubleScalar((double)obj->clock)); mxSetFieldByNumber(object_struct,n,6,mxCreateDoubleScalar((double)obj->valid_to)); mxSetFieldByNumber(object_struct,n,7,mxCreateDoubleScalar((double)obj->schedule_skew)); if ( isfinite(obj->latitude) ) mxSetFieldByNumber(object_struct,n,8,mxCreateDoubleScalar((double)obj->latitude)); if ( isfinite(obj->longitude) ) mxSetFieldByNumber(object_struct,n,9,mxCreateDoubleScalar((double)obj->longitude)); mxSetFieldByNumber(object_struct,n,10,mxCreateDoubleScalar((double)obj->in_svc)); mxSetFieldByNumber(object_struct,n,11,mxCreateDoubleScalar((double)obj->out_svc)); mxSetFieldByNumber(object_struct,n,12,mxCreateDoubleScalar((double)obj->rng_state)); mxSetFieldByNumber(object_struct,n,13,mxCreateDoubleScalar((double)obj->heartbeat)); mxSetFieldByNumber(object_struct,n,14,mxCreateDoubleScalar((double)obj->lock)); mxSetFieldByNumber(object_struct,n,15,mxCreateDoubleScalar((double)obj->flags)); n++; } gridlabd_index = mxAddField(gridlabd_struct,"object"); mxSetFieldByNumber(gridlabd_struct,0,gridlabd_index,object_struct); /////////////////////////////////////////////////////////////////////////// // post the gridlabd structure matlab->root = gridlabd_struct; engPutVariable(matlab->engine,matlab->rootname,matlab->root); } /////////////////////////////////////////////////////////////////////////// // build the import/export data for ( LINKLIST *item=mod->get_exports() ; item!=NULL ; item=mod->get_next(item) ) { OBJECTPROPERTY *objprop = mod->get_export(item); if ( objprop==NULL ) continue; // add to published items gld_property prop(objprop->obj,objprop->prop); item->addr = (mxArray*)matlab_create_value(&prop); engPutVariable(matlab->engine,item->name,(mxArray*)item->addr); } for ( LINKLIST *item=mod->get_imports() ; item!=NULL ; item=mod->get_next(item) ) { OBJECTPROPERTY *objprop = mod->get_import(item); if ( objprop==NULL ) continue; // check that not already in export list LINKLIST *export_item; bool found=false; for ( export_item=mod->get_exports() ; export_item!=NULL ; export_item=mod->get_next(export_item) ) { OBJECTPROPERTY *other = mod->get_export(item); if ( memcmp(objprop,other,sizeof(OBJECTPROPERTY)) ) found=true; } if ( !found ) { gld_property prop(objprop->obj,objprop->prop); item->addr = (mxArray*)matlab_create_value(&prop); engPutVariable(matlab->engine,item->name,(mxArray*)item->addr); } } static int32 matlab_flag = 1; gl_global_create("MATLAB",PT_int32,&matlab_flag,PT_ACCESS,PA_REFERENCE,PT_DESCRIPTION,"indicates that MATLAB is available",NULL); mod->last_t = gl_globalclock; return true; }