HSQUIRRELVM sq_newthread(HSQUIRRELVM friendvm, SQInteger initialstacksize) { SQSharedState *ss; SQVM *v; ss=_ss(friendvm); v= (SQVM *)SQ_MALLOC(sizeof(SQVM)); new (v) SQVM(ss); if(v->Init(friendvm, initialstacksize)) { friendvm->Push(v); return v; } else { sq_delete(v, SQVM); return NULL; } }
static SQInteger array_slice(HSQUIRRELVM v) { SQInteger sidx,eidx; SQObjectPtr o; if(get_slice_params(v,sidx,eidx,o)==-1)return -1; SQInteger alen = _array(o)->Size(); if(sidx < 0)sidx = alen + sidx; if(eidx < 0)eidx = alen + eidx; if(eidx < sidx)return sq_throwerror(v,_SC("wrong indexes")); if(eidx > alen)return sq_throwerror(v,_SC("slice out of range")); SQArray *arr=SQArray::Create(_ss(v),eidx-sidx); SQObjectPtr t; SQInteger count=0; for(SQInteger i=sidx;i<eidx;i++){ _array(o)->Get(i,t); arr->Set(count++,t); } v->Push(arr); return 1; }
static SQInteger array_filter(HSQUIRRELVM v) { SQObject &o = stack_get(v,1); SQArray *a = _array(o); SQObjectPtr ret = SQArray::Create(_ss(v),0); SQInteger size = a->Size(); SQObjectPtr val; for(SQInteger n = 0; n < size; n++) { a->Get(n,val); v->Push(o); v->Push(n); v->Push(val); if(SQ_FAILED(sq_call(v,3,SQTrue,SQFalse))) { return SQ_ERROR; } if(!v->IsFalse(v->GetUp(-1))) { _array(ret)->Append(val); } v->Pop(); } v->Push(ret); return 1; }
bool Stats::LoadScore() { std::wfstream _file; _file.open(m_filename); // If file is not empty, read all so we can sort them std::pair<std::wstring, int> _inData[10] = { { L"", 0 } }; wchar_t _tempChars[100]; std::wstring _tempLine; int i = 0; while (getline(_file, _tempLine)) //Format: 1|Name|1000 Position|Name|Score { if (_tempLine != L"") { std::wistringstream _ss(_tempLine); _ss.get(_tempChars, 100, '|'); //Throw away position and delim _ss.ignore(); _ss.get(_tempChars, 100, '|'); //Read name _inData[i].first = _tempChars; _ss.ignore(); _ss.get(_tempChars, 100, '|'); //Get Score _inData[i].second = _wtoi(_tempChars); i++; } } for (i = 0; i < 10; i++) m_highScores[i] = _inData[i]; _file.close(); return true; }
void sq_newarray(HSQUIRRELVM v,SQInteger size) { v->Push(SQArray::Create(_ss(v), size)); }
void sq_newtable(HSQUIRRELVM v) { v->Push(SQTable::Create(_ss(v), 0)); }
void sq_pushstring(HSQUIRRELVM v,const SQChar *s,SQInteger len) { if(s) v->Push(SQObjectPtr(SQString::Create(_ss(v), s, len))); else v->PushNull(); }
void sq_enabledebuginfo(HSQUIRRELVM v, SQBool enable) { _ss(v)->_debuginfo = enable?true:false; }
static SQInteger closure_getinfos(HSQUIRRELVM v) { SQObject o = stack_get(v,1); SQTable *res = SQTable::Create(_ss(v),4); if(type(o) == OT_CLOSURE) { SQFunctionProto *f = _funcproto(_closure(o)->_function); SQInteger nparams = f->_nparameters + (f->_varparams?1:0); SQObjectPtr params = SQArray::Create(_ss(v),nparams); for(SQInteger n = 0; n<f->_nparameters; n++) { _array(params)->Set((SQInteger)n,f->_parameters[n]); } if(f->_varparams) { _array(params)->Set(nparams-1,SQString::Create(_ss(v),_SC("..."),-1)); } res->NewSlot(SQString::Create(_ss(v),_SC("native"),-1),false); res->NewSlot(SQString::Create(_ss(v),_SC("name"),-1),f->_name); res->NewSlot(SQString::Create(_ss(v),_SC("src"),-1),f->_sourcename); res->NewSlot(SQString::Create(_ss(v),_SC("parameters"),-1),params); res->NewSlot(SQString::Create(_ss(v),_SC("varargs"),-1),f->_varparams); } else { //OT_NATIVECLOSURE SQNativeClosure *nc = _nativeclosure(o); res->NewSlot(SQString::Create(_ss(v),_SC("native"),-1),true); res->NewSlot(SQString::Create(_ss(v),_SC("name"),-1),nc->_name); res->NewSlot(SQString::Create(_ss(v),_SC("paramscheck"),-1),nc->_nparamscheck); SQObjectPtr typecheck; if(nc->_typecheck.size() > 0) { typecheck = SQArray::Create(_ss(v), nc->_typecheck.size()); for(SQUnsignedInteger n = 0; n<nc->_typecheck.size(); n++) { _array(typecheck)->Set((SQInteger)n,nc->_typecheck[n]); } } res->NewSlot(SQString::Create(_ss(v),_SC("typecheck"),-1),typecheck); } v->Push(res); return 1; }
void sq_addref(HSQUIRRELVM v,HSQOBJECT *po) { if(!ISREFCOUNTED(type(*po))) return; _ss(v)->_refs_table.AddRef(*po); }
/** * Use the ZOO-Services messages translation function from the Ruby * environment (ZOO-API) * * @param argc the number of parameters * @param argv the parameter values given from the Ruby environment * @param obj the Ruby object on which we run the method * @return a new Ruby string containing the translated value * @see _ss */ VALUE RubyTranslate(int argc, VALUE *argv, VALUE obj) { return rb_str_new2(_ss(StringValueCStr(argv[0]))); }
SQClosure::~SQClosure() { __ObjRelease(_base); REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); }
bool SQDelegable::GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res) { if(_delegate) { return _delegate->Get((*_ss(v)->_metamethods)[mm],res); } return false; }
int tindex(maps*& conf, maps*& inputs,maps*& outputs) { char *index_filename = NULL; const char *tile_index = "location"; int i_arg, ti_field; OGRDataSourceH hTileIndexDS; OGRLayerH hLayer = NULL; OGRFeatureDefnH hFDefn; int write_absolute_path = FALSE; char* current_path = NULL; int i; int nExistingFiles; int skip_different_projection = FALSE; char** existingFilesTab = NULL; int alreadyExistingProjectionRefValid = FALSE; char* alreadyExistingProjectionRef = NULL; char* index_filename_mod; int bExists; VSIStatBuf sStatBuf; /* Check that we are running against at least GDAL 1.4 */ /* Note to developers : if we use newer API, please change the requirement */ if (atoi(GDALVersionInfo("VERSION_NUM")) < 1400) { char tmp[1024]; sprintf(tmp, "At least, GDAL >= 1.4.0 is required for this version of" "tindex, which was compiled against GDAL %s\n", GDAL_RELEASE_NAME); return SERVICE_FAILED; } GDALAllRegister(); OGRRegisterAll(); /* -------------------------------------------------------------------- */ /* Get the directory name to search for raster file to index. */ /* -------------------------------------------------------------------- */ char *tmpDataDir; char *pszDataDir; map* tmpMap=getMapFromMaps(conf,"main","isTrial"); map* tmpMap1=getMapFromMaps(conf,"main","dataPath"); map* tmpInputMap=getMapFromMaps(inputs,"dir","value"); if(tmpMap!=NULL && strcasecmp(tmpMap->value,"true")==0){ pszDataDir=(char*) malloc((strlen(tmpInputMap->value)+strlen(tmpMap1->value)+5+1)*sizeof(char)); sprintf(pszDataDir,"%s/ftp/%s",tmpMap1->value,tmpInputMap->value); }else{ pszDataDir=(char*) malloc(((strlen(tmpInputMap->value)+1)*sizeof(char))); sprintf(pszDataDir,"%s",tmpInputMap->value); } tmpMap=getMapFromMaps(inputs,"iname","value"); tmpMap1=getMapFromMaps(inputs,"idir","value"); map* tmpMap2=getMapFromMaps(conf,"main","dataPath"); if(tmpMap!=NULL && tmpMap1!=NULL && tmpMap2!=NULL){ index_filename = (char*) malloc((strlen(tmpMap->value)+strlen(tmpMap1->value)+strlen(tmpMap2->value)+12)*sizeof(char)); sprintf(index_filename,"%s/dirs/%s/%s.shp",tmpMap2->value,tmpMap1->value,tmpMap->value); } fprintf(stderr,"Filename %s\n",index_filename); /* -------------------------------------------------------------------- */ /* Open or create the target shapefile and DBF file. */ /* -------------------------------------------------------------------- */ index_filename_mod = CPLStrdup(CPLResetExtension(index_filename, "shp")); bExists = (VSIStat(index_filename_mod, &sStatBuf) == 0); if (!bExists) { CPLFree(index_filename_mod); index_filename_mod = CPLStrdup(CPLResetExtension(index_filename, "SHP")); bExists = (VSIStat(index_filename_mod, &sStatBuf) == 0); } CPLFree(index_filename_mod); if (bExists) { hTileIndexDS = OGROpen( index_filename, TRUE, NULL ); if (hTileIndexDS != NULL) { hLayer = OGR_DS_GetLayer(hTileIndexDS, 0); } } else { OGRSFDriverH hDriver; const char* pszDriverName = "ESRI Shapefile"; fprintf( stderr,"Creating new index file...\n" ); hDriver = OGRGetDriverByName( pszDriverName ); if( hDriver == NULL ) { char msg[1024]; sprintf( msg, "%s driver not available.", pszDriverName ); setMapInMaps(conf,"lenv","message",msg); return SERVICE_FAILED; } hTileIndexDS = OGR_Dr_CreateDataSource( hDriver, index_filename, NULL ); if (hTileIndexDS) { char* pszLayerName = CPLStrdup(CPLGetBasename(index_filename)); OGRSpatialReferenceH hSpatialRef = NULL; GDALDatasetH hDS = GDALOpen( index_filename, GA_ReadOnly ); if (hDS) { const char* pszWKT = GDALGetProjectionRef(hDS); if (pszWKT != NULL && pszWKT[0] != '\0') { hSpatialRef = OSRNewSpatialReference(pszWKT); } GDALClose(hDS); } DIR *dirp = opendir(pszDataDir); if(dirp==NULL){ char tmp1[1024]; sprintf(tmp1,_ss("The specified path %s doesn't exist."),pszDataDir); setMapInMaps(conf,"lenv","message",tmp1); return SERVICE_FAILED; } char *res=NULL; struct dirent *dp; tmpMap=getMapFromMaps(inputs,"ext","value"); char *ext; if(tmpMap!=NULL) ext=tmpMap->value; while ((dp = readdir(dirp)) != NULL){ if(strncmp(dp->d_name,".",1)!=0&&strncmp(dp->d_name,"..",2)!=0){ char* argv=(char*) malloc((strlen(dp->d_name)+strlen(pszDataDir)+2)*sizeof(char)); sprintf(argv,"%s/%s",pszDataDir,dp->d_name); GDALDatasetH hDS0 = GDALOpen( argv, GA_ReadOnly ); if (hDS0) { const char* pszWKT = GDALGetProjectionRef(hDS0); fprintf(stderr,"SRS %s \n",pszWKT); if (pszWKT != NULL && pszWKT[0] != '\0') { if (hSpatialRef) OSRRelease(hSpatialRef); hSpatialRef = OSRNewSpatialReference(pszWKT); } GDALClose(hDS); } break; } } closedir(dirp); hLayer = OGR_DS_CreateLayer( hTileIndexDS, pszLayerName, hSpatialRef, wkbPolygon, NULL ); CPLFree(pszLayerName); if (hSpatialRef) OSRRelease(hSpatialRef); if (hLayer) { OGRFieldDefnH hFieldDefn = OGR_Fld_Create( tile_index, OFTString ); OGR_Fld_SetWidth( hFieldDefn, 255); OGR_L_CreateField( hLayer, hFieldDefn, TRUE ); OGR_Fld_Destroy(hFieldDefn); } } } if( hTileIndexDS == NULL || hLayer == NULL ) { char msg[1024]; sprintf( msg, "Unable to open/create shapefile `%s'.", index_filename ); setMapInMaps(conf,"lenv","message",msg); } hFDefn = OGR_L_GetLayerDefn(hLayer); for( ti_field = 0; ti_field < OGR_FD_GetFieldCount(hFDefn); ti_field++ ) { OGRFieldDefnH hFieldDefn = OGR_FD_GetFieldDefn( hFDefn, ti_field ); if( strcmp(OGR_Fld_GetNameRef(hFieldDefn), tile_index) == 0 ) break; } if( ti_field == OGR_FD_GetFieldCount(hFDefn) ) { char msg[1024]; sprintf( msg, "Unable to find field `%s' in DBF file `%s'.\n", tile_index, index_filename ); setMapInMaps(conf,"lenv","message",msg); return SERVICE_FAILED; } /* Load in memory existing file names in SHP */ nExistingFiles = OGR_L_GetFeatureCount(hLayer, FALSE); if (nExistingFiles) { OGRFeatureH hFeature; existingFilesTab = (char**)CPLMalloc(nExistingFiles * sizeof(char*)); for(i=0;i<nExistingFiles;i++) { hFeature = OGR_L_GetNextFeature(hLayer); existingFilesTab[i] = CPLStrdup(OGR_F_GetFieldAsString( hFeature, ti_field )); if (i == 0) { GDALDatasetH hDS = GDALOpen(existingFilesTab[i], GA_ReadOnly ); if (hDS) { alreadyExistingProjectionRefValid = TRUE; alreadyExistingProjectionRef = CPLStrdup(GDALGetProjectionRef(hDS)); GDALClose(hDS); } } OGR_F_Destroy( hFeature ); } } if (write_absolute_path) { current_path = CPLGetCurrentDir(); if (current_path == NULL) { fprintf( stderr, "This system does not support the CPLGetCurrentDir call. " "The option -write_absolute_path will have no effect\n"); write_absolute_path = FALSE; } } /* -------------------------------------------------------------------- */ /* loop over GDAL files, processing. */ /* -------------------------------------------------------------------- */ DIR *dirp = opendir(pszDataDir); if(dirp==NULL){ char tmp1[1024]; sprintf(tmp1,_ss("The specified path %s doesn't exist."),pszDataDir); setMapInMaps(conf,"lenv","message",tmp1); return SERVICE_FAILED; } char *res=NULL; struct dirent *dp; tmpMap=getMapFromMaps(inputs,"ext","value"); char *ext; if(tmpMap!=NULL) ext=tmpMap->value; while ((dp = readdir(dirp)) != NULL){ if(strlen(dp->d_name)>2 && strstr(dp->d_name,".")!=0 && strstr(dp->d_name,ext)>0){ char* argv=(char*) malloc((strlen(dp->d_name)+strlen(pszDataDir)+2)*sizeof(char)); sprintf(argv,"%s/%s",pszDataDir,dp->d_name); GDALDatasetH hDS; double adfGeoTransform[6]; double adfX[5], adfY[5]; int nXSize, nYSize; char* fileNameToWrite; const char* projectionRef; VSIStatBuf sStatBuf; int k; OGRFeatureH hFeature; OGRGeometryH hPoly, hRing; /* Make sure it is a file before building absolute path name */ if (write_absolute_path && CPLIsFilenameRelative( argv ) && VSIStat( argv, &sStatBuf ) == 0) { fileNameToWrite = CPLStrdup(CPLProjectRelativeFilename(current_path, argv)); } else { fileNameToWrite = CPLStrdup(argv); } /* Checks that file is not already in tileindex */ for(i=0;i<nExistingFiles;i++) { if (EQUAL(fileNameToWrite, existingFilesTab[i])) { fprintf(stderr, "File %s is already in tileindex. Skipping it.\n", fileNameToWrite); break; } } if (i != nExistingFiles) { CPLFree(fileNameToWrite); continue; } hDS = GDALOpen( argv, GA_ReadOnly ); if( hDS == NULL ) { fprintf( stderr, "Unable to open %s, skipping.\n", argv ); CPLFree(fileNameToWrite); continue; } GDALGetGeoTransform( hDS, adfGeoTransform ); if( adfGeoTransform[0] == 0.0 && adfGeoTransform[1] == 1.0 && adfGeoTransform[3] == 0.0 && ABS(adfGeoTransform[5]) == 1.0 ) { fprintf( stderr, "It appears no georeferencing is available for\n" "`%s', skipping.\n", argv ); GDALClose( hDS ); CPLFree(fileNameToWrite); continue; } projectionRef = GDALGetProjectionRef(hDS); if (alreadyExistingProjectionRefValid) { int projectionRefNotNull, alreadyExistingProjectionRefNotNull; projectionRefNotNull = projectionRef && projectionRef[0]; alreadyExistingProjectionRefNotNull = alreadyExistingProjectionRef && alreadyExistingProjectionRef[0]; if ((projectionRefNotNull && alreadyExistingProjectionRefNotNull && EQUAL(projectionRef, alreadyExistingProjectionRef) == 0) || (projectionRefNotNull != alreadyExistingProjectionRefNotNull)) { fprintf(stderr, "Warning : %s is not using the same projection system as " "other files in the tileindex. This may cause problems when " "using it in MapServer for example.%s\n", argv, (skip_different_projection) ? " Skipping it" : ""); if (skip_different_projection) { CPLFree(fileNameToWrite); GDALClose( hDS ); continue; } } } else { alreadyExistingProjectionRefValid = TRUE; alreadyExistingProjectionRef = CPLStrdup(projectionRef); } nXSize = GDALGetRasterXSize( hDS ); nYSize = GDALGetRasterYSize( hDS ); adfX[0] = adfGeoTransform[0] + 0 * adfGeoTransform[1] + 0 * adfGeoTransform[2]; adfY[0] = adfGeoTransform[3] + 0 * adfGeoTransform[4] + 0 * adfGeoTransform[5]; adfX[1] = adfGeoTransform[0] + nXSize * adfGeoTransform[1] + 0 * adfGeoTransform[2]; adfY[1] = adfGeoTransform[3] + nXSize * adfGeoTransform[4] + 0 * adfGeoTransform[5]; adfX[2] = adfGeoTransform[0] + nXSize * adfGeoTransform[1] + nYSize * adfGeoTransform[2]; adfY[2] = adfGeoTransform[3] + nXSize * adfGeoTransform[4] + nYSize * adfGeoTransform[5]; adfX[3] = adfGeoTransform[0] + 0 * adfGeoTransform[1] + nYSize * adfGeoTransform[2]; adfY[3] = adfGeoTransform[3] + 0 * adfGeoTransform[4] + nYSize * adfGeoTransform[5]; adfX[4] = adfGeoTransform[0] + 0 * adfGeoTransform[1] + 0 * adfGeoTransform[2]; adfY[4] = adfGeoTransform[3] + 0 * adfGeoTransform[4] + 0 * adfGeoTransform[5]; hFeature = OGR_F_Create( OGR_L_GetLayerDefn( hLayer ) ); OGR_F_SetFieldString( hFeature, ti_field, fileNameToWrite ); hPoly = OGR_G_CreateGeometry(wkbPolygon); hRing = OGR_G_CreateGeometry(wkbLinearRing); for(k=0;k<5;k++) OGR_G_SetPoint_2D(hRing, k, adfX[k], adfY[k]); OGR_G_AddGeometryDirectly( hPoly, hRing ); OGR_F_SetGeometryDirectly( hFeature, hPoly ); if( OGR_L_CreateFeature( hLayer, hFeature ) != OGRERR_NONE ) { fprintf( stderr, "Failed to create feature in shapefile.\n" ); break; } OGR_F_Destroy( hFeature ); CPLFree(fileNameToWrite); GDALClose( hDS ); } } CPLFree(current_path); if (nExistingFiles) { for(i=0;i<nExistingFiles;i++) { CPLFree(existingFilesTab[i]); } CPLFree(existingFilesTab); } CPLFree(alreadyExistingProjectionRef); OGR_DS_Destroy( hTileIndexDS ); GDALDestroyDriverManager(); OGRCleanupAll(); setMapInMaps(outputs,"Result","value","Tile index successfully created."); //CSLDestroy(argv); return SERVICE_SUCCEEDED; }
static SQInteger base_type(HSQUIRRELVM v) { SQObjectPtr &o = stack_get(v,2); v->Push(SQString::Create(_ss(v),GetTypeName(o),-1)); return 1; }
static SQInteger base_getconsttable(HSQUIRRELVM v) { v->Push(_ss(v)->_consts); return 1; }
SQBool sq_release(HSQUIRRELVM v,HSQOBJECT *po) { if(!ISREFCOUNTED(type(*po))) return SQTrue; return _ss(v)->_refs_table.Release(*po); }
Scene* parseObjFile(std::string filename) { std::ifstream input(filename.c_str()); if (!input) { // file does not exist throw "file does not exist"; } std::vector<Object*> sceneObjects; Object* currObj = NULL; // std::vector<Vertex*> vertices; std::vector<Vec3> vertexCoords; std::vector<Vec3> vertexNormals; std::vector<Vec3> vertexTexCoords; // map from vertex coord index to vertex std::map<int, Vertex*> vertices; for (std::string line; getline(input, line); ) { std::stringstream ss(line); std::string item; getline(ss, item, ' '); if (item == "o") { // start of a new object if (currObj) { // if it's not the first object parsed sceneObjects.push_back(currObj); // add old object to list } currObj = new Object; } else if (item == "v") { // vertex float coords[3]; for (int i = 0; getline(ss, item, ' '); i++) { coords[i] = atof(item.c_str()); } vertexCoords.push_back(Vec3(coords)); } else if (item == "vn") { // vertex normal float normals[3]; for (int i = 0; getline(ss, item, ' '); i++) { normals[i] = atof(item.c_str()); } vertexNormals.push_back(Vec3(normals)); } else if (item == "vt") { // texture coordinate float texCoords[2]; for (int i = 0; getline(ss, item, ' '); i++) { texCoords[i] = atof(item.c_str()); } vertexTexCoords.push_back(Vec3(texCoords[0], texCoords[1], 0)); // texture coords are 2D } else if (item == "f") { // face Face* f = new Face; for (int i = 0; getline(ss, item, ' '); i++) { // item is in the format <P>/<T>/<N> std::stringstream _ss(item); std::string index; // index of the vectors parsed getline(_ss, index, '/'); int vertexIndex = atof(index.c_str()) - 1; getline(_ss, index, '/'); int texIndex = -1; if (index.length() != 0) { texIndex = atof(index.c_str()) - 1; } getline(_ss, index, '/'); int normalIndex = atof(index.c_str()) - 1; Vertex* v; auto result = vertices.find(vertexIndex); if (result == vertices.end()) { // cannot find vertex with index v = new Vertex(vertexCoords[vertexIndex], vertexNormals[normalIndex]); vertices.insert(std::pair<int, Vertex*>(vertexIndex, v)); if (texIndex != -1) { v->setTexCoords(vertexTexCoords[texIndex]); } } else { // found vertex with index v = result->second; } f->addVertex(v); } currObj->addFace(f); } else if (item == "usemtl") { // material getline(ss, item, ' '); currObj->materialName = item; } } /* for (Face* f : currObj->getFaces()) { std::cout << *f << std::endl; } */ sceneObjects.push_back(currObj); // add the last object to the list Scene* s = new Scene(sceneObjects); return s; }
void SQVM::Raise_Error(const SQChar *s) { _lasterror = SQString::Create(_ss(this),s); }
void sq_close(HSQUIRRELVM v) { SQSharedState *ss = _ss(v); _thread(ss->_root_vm)->Finalize(); sq_delete(ss, SQSharedState); }
void sq_setoplimit(HSQUIRRELVM v, SQUnsignedInteger limit) { _ss(v)->_oplimit = limit; }
void sq_notifyallexceptions(HSQUIRRELVM v, SQBool enable) { _ss(v)->_notifyallexceptions = enable?true:false; }
void sq_enableasserts(HSQUIRRELVM v, SQBool enable) { _ss(v)->_enableasserts = enable ? true : false; }
SQUserPointer sq_newuserdata(HSQUIRRELVM v,SQUnsignedInteger size) { SQUserData *ud = SQUserData::Create(_ss(v), size); v->Push(ud); return (SQUserPointer)sq_aligning(ud + 1); }
void sq_enablehelp(HSQUIRRELVM v, SQBool enable) { _ss(v)->_enablehelp = enable ? true : false; }
void sq_newtableex(HSQUIRRELVM v,SQInteger initialcapacity) { v->Push(SQTable::Create(_ss(v), initialcapacity)); }
SQUserPointer sq_newuserdata(HSQUIRRELVM v,SQUnsignedInteger size) { SQUserData *ud = SQUserData::Create(_ss(v), size); v->Push(ud); return ud->_val; }
SQInteger sq_aux_invalidtype(HSQUIRRELVM v,SQObjectType type) { scsprintf(_ss(v)->GetScratchPad(100), _SC("unexpected type %s"), IdType2Name(type)); return sq_throwerror(v, _ss(v)->GetScratchPad(-1)); }
CV_IMPL void cvCalcOpticalFlowBM( const void* srcarrA, const void* srcarrB, CvSize blockSize, CvSize shiftSize, CvSize maxRange, int usePrevious, void* velarrx, void* velarry ) { CvMat stubA, *srcA = cvGetMat( srcarrA, &stubA ); CvMat stubB, *srcB = cvGetMat( srcarrB, &stubB ); CvMat stubx, *velx = cvGetMat( velarrx, &stubx ); CvMat stuby, *vely = cvGetMat( velarry, &stuby ); if( !CV_ARE_TYPES_EQ( srcA, srcB )) CV_Error( CV_StsUnmatchedFormats, "Source images have different formats" ); if( !CV_ARE_TYPES_EQ( velx, vely )) CV_Error( CV_StsUnmatchedFormats, "Destination images have different formats" ); CvSize velSize = { (srcA->width - blockSize.width)/shiftSize.width, (srcA->height - blockSize.height)/shiftSize.height }; if( !CV_ARE_SIZES_EQ( srcA, srcB ) || !CV_ARE_SIZES_EQ( velx, vely ) || velx->width != velSize.width || vely->height != velSize.height ) CV_Error( CV_StsUnmatchedSizes, "" ); if( CV_MAT_TYPE( srcA->type ) != CV_8UC1 || CV_MAT_TYPE( velx->type ) != CV_32FC1 ) CV_Error( CV_StsUnsupportedFormat, "Source images must have 8uC1 type and " "destination images must have 32fC1 type" ); if( srcA->step != srcB->step || velx->step != vely->step ) CV_Error( CV_BadStep, "two source or two destination images have different steps" ); const int SMALL_DIFF=2; const int BIG_DIFF=128; // scanning scheme coordinates cv::vector<CvPoint> _ss((2 * maxRange.width + 1) * (2 * maxRange.height + 1)); CvPoint* ss = &_ss[0]; int ss_count = 0; int blWidth = blockSize.width, blHeight = blockSize.height; int blSize = blWidth*blHeight; int acceptLevel = blSize * SMALL_DIFF; int escapeLevel = blSize * BIG_DIFF; int i, j; cv::vector<uchar> _blockA(cvAlign(blSize + 16, 16)); uchar* blockA = (uchar*)cvAlignPtr(&_blockA[0], 16); // Calculate scanning scheme int min_count = MIN( maxRange.width, maxRange.height ); // use spiral search pattern // // 9 10 11 12 // 8 1 2 13 // 7 * 3 14 // 6 5 4 15 //... 20 19 18 17 // for( i = 0; i < min_count; i++ ) { // four cycles along sides int x = -i-1, y = x; // upper side for( j = -i; j <= i + 1; j++, ss_count++ ) { ss[ss_count].x = ++x; ss[ss_count].y = y; } // right side for( j = -i; j <= i + 1; j++, ss_count++ ) { ss[ss_count].x = x; ss[ss_count].y = ++y; } // bottom side for( j = -i; j <= i + 1; j++, ss_count++ ) { ss[ss_count].x = --x; ss[ss_count].y = y; } // left side for( j = -i; j <= i + 1; j++, ss_count++ ) { ss[ss_count].x = x; ss[ss_count].y = --y; } } // the rest part if( maxRange.width < maxRange.height ) { int xleft = -min_count; // cycle by neighbor rings for( i = min_count; i < maxRange.height; i++ ) { // two cycles by x int y = -(i + 1); int x = xleft; // upper side for( j = -maxRange.width; j <= maxRange.width; j++, ss_count++, x++ ) { ss[ss_count].x = x; ss[ss_count].y = y; } x = xleft; y = -y; // bottom side for( j = -maxRange.width; j <= maxRange.width; j++, ss_count++, x++ ) { ss[ss_count].x = x; ss[ss_count].y = y; } } } else if( maxRange.width > maxRange.height ) { int yupper = -min_count; // cycle by neighbor rings for( i = min_count; i < maxRange.width; i++ ) { // two cycles by y int x = -(i + 1); int y = yupper; // left side for( j = -maxRange.height; j <= maxRange.height; j++, ss_count++, y++ ) { ss[ss_count].x = x; ss[ss_count].y = y; } y = yupper; x = -x; // right side for( j = -maxRange.height; j <= maxRange.height; j++, ss_count++, y++ ) { ss[ss_count].x = x; ss[ss_count].y = y; } } } int maxX = srcB->cols - blockSize.width, maxY = srcB->rows - blockSize.height; const uchar* Adata = srcA->data.ptr; const uchar* Bdata = srcB->data.ptr; int Astep = srcA->step, Bstep = srcB->step; // compute the flow for( i = 0; i < velx->rows; i++ ) { float* vx = (float*)(velx->data.ptr + velx->step*i); float* vy = (float*)(vely->data.ptr + vely->step*i); for( j = 0; j < velx->cols; j++ ) { int X1 = j*shiftSize.width, Y1 = i*shiftSize.height, X2, Y2; int offX = 0, offY = 0; if( usePrevious ) { offX = cvRound(vx[j]); offY = cvRound(vy[j]); } int k; for( k = 0; k < blHeight; k++ ) memcpy( blockA + k*blWidth, Adata + Astep*(Y1 + k) + X1, blWidth ); X2 = X1 + offX; Y2 = Y1 + offY; int dist = INT_MAX; if( 0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY ) dist = cmpBlocks( blockA, Bdata + Bstep*Y2 + X2, Bstep, blockSize ); int countMin = 1; int sumx = offX, sumy = offY; if( dist > acceptLevel ) { // do brute-force search for( k = 0; k < ss_count; k++ ) { int dx = offX + ss[k].x; int dy = offY + ss[k].y; X2 = X1 + dx; Y2 = Y1 + dy; if( !(0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY) ) continue; int tmpDist = cmpBlocks( blockA, Bdata + Bstep*Y2 + X2, Bstep, blockSize ); if( tmpDist < acceptLevel ) { sumx = dx; sumy = dy; countMin = 1; break; } if( tmpDist < dist ) { dist = tmpDist; sumx = dx; sumy = dy; countMin = 1; } else if( tmpDist == dist ) { sumx += dx; sumy += dy; countMin++; } } if( dist > escapeLevel ) { sumx = offX; sumy = offY; countMin = 1; } } vx[j] = (float)sumx/countMin; vy[j] = (float)sumy/countMin; } } }