trpage_managers.cpp
来自「最新osg包」· C++ 代码 · 共 1,227 行 · 第 1/3 页
CPP
1,227 行
/* ************************ Copyright Terrain Experts Inc. Terrain Experts Inc (TERREX) reserves all rights to this source code unless otherwise specified in writing by the President of TERREX. This copyright may be updated in the future, in which case that version supercedes this one. ------------------- Terrex Experts Inc. 4400 East Broadway #314 Tucson, AZ 85711 info@terrex.com Tel: (520) 323-7990 ************************ */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <stdexcept>#include <trpage_sys.h>#include <trpage_geom.h>#include <trpage_read.h>#include <trpage_print.h>#include <trpage_managers.h>/* Managed Tile class. Check the header file for details. */trpgManagedTile::trpgManagedTile(){ isLoaded = false; location.x = location.y = -1; location.lod = -1; localData = NULL;}void trpgManagedTile::Reset(){ // Null out the local material data for (unsigned int i=0;i<localMatData.size();i++) localMatData[i] = NULL; groupIDs.resize(0); isLoaded = false; location.x = location.y = -1; location.lod = -1; localData = NULL; childLocationInfo.clear();}bool trpgManagedTile::ParseTileHeader(trpgReadBuffer &buf){ isLoaded = false; if (!tileHead.Read(buf)) return false; int numLoc; tileHead.GetNumLocalMaterial(numLoc); localMatData.resize(numLoc); // The tile considers itself loaded with just // the header. This isn't true as far as the paging // manager is concerned. isLoaded = true; return true;}bool trpgManagedTile::IsLoaded(){ return isLoaded;}bool trpgManagedTile::SetTileLoc(int inX,int inY,int inLod){ location.x = inX; location.y = inY; if (inLod < 0) return false; location.lod = inLod; return true;}bool trpgManagedTile::GetTileLoc(int &retx,int &rety,int &retLod) const{ retx = location.x; rety = location.y; retLod = location.lod; return true;}void trpgManagedTile::SetTileAddress(const trpgwAppAddress& gAddr){ location.addr = gAddr;}void trpgManagedTile::SetTileAddress(int32 file, int32 offset){ location.addr.file = file; location.addr.offset = offset;}const trpgwAppAddress& trpgManagedTile::GetTileAddress() const{ return location.addr;}bool trpgManagedTile::SetChildLocationInfo(int childIdx, int x, int y, const trpgwAppAddress& addr){ if(childIdx < 0) throw std::invalid_argument( "trpgManagedTile::SetChildLocationInfo(): index argument out of bound."); int size = childLocationInfo.size(); if(childIdx < size) childLocationInfo[childIdx] = TileLocationInfo(x, y, location.lod+1, addr); else if(childIdx == size) childLocationInfo.push_back(TileLocationInfo(x, y, location.lod+1, addr)); else { childLocationInfo.resize(childIdx+1); childLocationInfo[childIdx] = TileLocationInfo(x, y, location.lod+1, addr); } return true;}bool trpgManagedTile::SetChildLocationInfo(int childIdx, const TileLocationInfo& info){ if(childIdx < 0) throw std::invalid_argument( "trpgManagedTile::SetChildLocationInfo(): index argument out of bound."); int size = childLocationInfo.size(); if(childIdx < size) childLocationInfo[childIdx] = info; else if(childIdx == size) childLocationInfo.push_back(info); else { childLocationInfo.resize(childIdx+1); childLocationInfo[childIdx] = info; } return true;}const TileLocationInfo& trpgManagedTile::GetChildLocationInfo(int childIdx) const{ if(childIdx < 0 || childIdx >= (int)childLocationInfo.size()) throw std::invalid_argument( "trpgManagedTile::GetChildLocationInfo(): index argument out of bound."); return childLocationInfo[childIdx];}bool trpgManagedTile::GetChildTileLoc(int childIdx, int &x,int &y,int &lod) const{ if(childIdx < 0 || childIdx >= (int)childLocationInfo.size()) throw std::invalid_argument( "trpgManagedTile::GetChildTileLoc(): index argument out of bound."); TileLocationInfo const& info = childLocationInfo[childIdx]; x = info.x; y = info.y; lod = info.lod; return true;} const trpgwAppAddress& trpgManagedTile::GetChildTileAddress(int childIdx) const{ if(childIdx < 0 || childIdx >= (int)childLocationInfo.size()) throw std::invalid_argument( "trpgManagedTile::GetChildTileAddress(): index argument out of bound."); return childLocationInfo[childIdx].addr;}const trpgTileHeader *trpgManagedTile::GetTileHead(){ return &tileHead;}const std::vector<trpgLocalMaterial> *trpgManagedTile::GetLocMatList() const{ return tileHead.GetLocalMaterialList();}const trpgLocalMaterial *trpgManagedTile::GetLocMaterial(int id) const{ const std::vector<trpgLocalMaterial> *matList; matList = tileHead.GetLocalMaterialList(); if (id <0 || id >= (int)matList->size()) return NULL; return &(*matList)[id];}void trpgManagedTile::SetLocalData(void *inData){ localData = inData;}void *trpgManagedTile::GetLocalData() const{ return localData;}bool trpgManagedTile::SetMatData(int id,void *info){ if (id < 0 || id >= (int)localMatData.size()) return false; localMatData[id] = info; return true;}void *trpgManagedTile::GetMatData(int id) const{ if (id < 0 || id >= (int)localMatData.size()) return NULL; return localMatData[id];}void trpgManagedTile::AddGroupID(int id){ groupIDs.push_back(id);}const std::vector<int> *trpgManagedTile::GetGroupIDs() const{ return &groupIDs;}void trpgManagedTile::Print(trpgPrintBuffer &buf){ char line[1024]; sprintf(line,"x = %d, y = %d, lod = %d",location.x, location.y, location.lod); buf.prnLine(line); // Note: Should print the rest too, probably.} /* Page Manager LOD Page Info class. Used by the page manager to keep track of paging information for a single terrain LOD. See the header file for details.*/trpgPageManager::LodPageInfo::LodPageInfo(){ valid = false; pageDist = 0.0; cell.x = cell.y = -100;}trpgPageManager::LodPageInfo::~LodPageInfo(){ Clean();}void trpgPageManager::LodPageInfo::Clean(){ // Clean up managed tile structures unsigned int i; for (i=0;i<load.size();i++) if (load[i]) delete load[i]; load.resize(0); for (i=0;i<unload.size();i++) if (unload[i]) delete unload[i]; unload.resize(0); for (i=0;i<current.size();i++) if (current[i]) delete current[i]; current.resize(0); for (i=0;i<freeList.size();i++) delete freeList[i]; freeList.resize(0); activeLoad = false; activeUnload = false;}bool trpgPageManager::LodPageInfo::Init(trpgr_Archive *archive, int myLod, double scale, int freeListDivider){ Clean(); lod = myLod; // In theory we could have a negative scale, but I don't // really want to deal with that. if (scale < 0) scale = 0.0; tileTable = archive->GetTileTable(); // Need some size and shape info about our terrain LOD const trpgHeader *head = archive->GetHeader(); head->GetTileSize(lod,cellSize); head->GetLodRange(lod,pageDist); head->GetLodSize(lod,lodSize); pageDist *= scale; head->GetVersion(majorVersion, minorVersion); // Area of interest size (in cells) aoiSize.x = (int)(pageDist/cellSize.x); aoiSize.y = (int)(pageDist/cellSize.y); /* Make a guess as to how many tiles we might have loaded in at any given time. Give ourselves 15% extra room. From the area of interest in cells, we can guess the max number of tiles (aka cells) we'll have loaded in at once. Note that the AOI size is only ahead, so it must be doubled. Version 2.1 now support variable lods, it might be overkill to allocate a free list by supposing that the tiles exist. So only for version 2.1 an over we will use the divider to allocate less */ maxNumTiles = (int)(1.15*(2*aoiSize.x+1)*(2*aoiSize.y+1)); if(majorVersion == 2 && minorVersion >= 1) maxNumTiles = (int)(1.15*(2*aoiSize.x+1)*(2*aoiSize.y+1)/freeListDivider); else maxNumTiles = (int)(1.15*(2*aoiSize.x+1)*(2*aoiSize.y+1)); // Allocate 'em for (int i=0;i<maxNumTiles;i++) { trpgManagedTile *tile = new trpgManagedTile(); freeList.push_back(tile); } // We still don't have a position yet valid = true; return true;}bool trpgPageManager::LodPageInfo::SetLocation(trpg2dPoint &loc){ // Calculate the cell this falls into trpg2iPoint newCell; newCell.x = (int)(loc.x/cellSize.x); newCell.y = (int)(loc.y/cellSize.y); // Snap to the database border if (newCell.x < 0) newCell.x = 0; if (newCell.y < 0) newCell.y = 0; if (newCell.x >= lodSize.x) newCell.x = lodSize.x-1; if (newCell.y >= lodSize.y) newCell.y = lodSize.y-1; // Nothing to page. Done. if (newCell.x == cell.x && newCell.y == cell.y) return false; // Cell has changed. Update. cell = newCell; Update(); return true;}trpgManagedTile *trpgPageManager::LodPageInfo::GetNextLoad(){ // Can only load one tile at a time if (activeLoad) return NULL; // Clear any NULLs at the beginning while (load.size() && !load[0]) load.pop_front(); if (load.size() > 0) { activeLoad = true; return load[0]; } return NULL;}void trpgPageManager::LodPageInfo::AckLoad(){ if (activeLoad) { current.push_back(load[0]); load.pop_front(); } activeLoad = false;}trpgManagedTile *trpgPageManager::LodPageInfo::GetNextUnload(){ // Can only unload one tile at a time if (activeUnload) return NULL; // Clear any NULLs at the beginning while (unload.size() && !unload[0]) unload.pop_front(); if (unload.size() > 0) { activeUnload = true; return unload[0]; } return NULL;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?