From 503568a303692e99c5f0fd5b5434a2cfb9ee915b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 22 Dec 2003 07:26:45 +0000 Subject: [PATCH] Moved TileMapper code out into its own file and made it a signleton --- VisualStudio/osgPlugins/txp/txp.dsp | 86 ++++++++++++++++++----------- src/osgPlugins/txp/GNUmakefile | 1 + src/osgPlugins/txp/TXPArchive.cpp | 33 ----------- src/osgPlugins/txp/TXPArchive.h | 31 ----------- src/osgPlugins/txp/TXPNode.cpp | 5 +- src/osgPlugins/txp/TXPSeamLOD.cpp | 3 +- src/osgPlugins/txp/TXPTileNode.cpp | 3 +- src/osgPlugins/txp/TileMapper.cpp | 48 ++++++++++++++++ src/osgPlugins/txp/TileMapper.h | 64 +++++++++++++++++++++ 9 files changed, 175 insertions(+), 99 deletions(-) create mode 100644 src/osgPlugins/txp/TileMapper.cpp create mode 100644 src/osgPlugins/txp/TileMapper.h diff --git a/VisualStudio/osgPlugins/txp/txp.dsp b/VisualStudio/osgPlugins/txp/txp.dsp index d6df39335..b472e7130 100644 --- a/VisualStudio/osgPlugins/txp/txp.dsp +++ b/VisualStudio/osgPlugins/txp/txp.dsp @@ -98,11 +98,35 @@ SOURCE=..\..\..\src\osgPlugins\txp\ReaderWriterTXP.cpp # End Source File # Begin Source File -SOURCE=..\..\..\src\osgPlugins\txp\TerrapageNode.cpp +SOURCE=..\..\..\src\osgPlugins\txp\TXPArchive.cpp # End Source File # Begin Source File -SOURCE=..\..\..\src\osgPlugins\txp\IO_TerrapageNode.cpp +SOURCE=..\..\..\src\osgPlugins\txp\TXPIO.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPNode.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPPageManager.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPParser.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPSeamLOD.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPTileNode.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TileMap.cpp # End Source File # Begin Source File @@ -192,18 +216,6 @@ SOURCE=..\..\..\src\osgPlugins\txp\trpage_warchive.cpp SOURCE=..\..\..\src\osgPlugins\txp\trpage_writebuf.cpp # End Source File -# Begin Source File - -SOURCE=..\..\..\src\osgPlugins\txp\TrPageArchive.cpp -# End Source File -# Begin Source File - -SOURCE=..\..\..\src\osgPlugins\txp\TrPageParser.cpp -# End Source File -# Begin Source File - -SOURCE=..\..\..\src\osgPlugins\txp\TrPagePageManager.cpp -# End Source File # End Group # Begin Group "Header Files" @@ -214,7 +226,35 @@ SOURCE=..\..\..\src\osgPlugins\txp\ReaderWriterTXP.h # End Source File # Begin Source File -SOURCE=..\..\..\src\osgPlugins\txp\TerrapageNode.h +SOURCE=..\..\..\src\osgPlugins\txp\TXPArchive.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPIO.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPNode.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPPageManager.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPParser.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPSeamLOD.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TXPTileNode.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\txp\TileMap.h # End Source File # Begin Source File @@ -222,10 +262,6 @@ SOURCE=..\..\..\src\osgPlugins\txp\trdll.h # End Source File # Begin Source File -SOURCE=..\..\..\src\osgPlugins\txp\trPagePageManager.h -# End Source File -# Begin Source File - SOURCE=..\..\..\src\osgPlugins\txp\trpage_compat.h # End Source File # Begin Source File @@ -272,18 +308,6 @@ SOURCE=..\..\..\src\osgPlugins\txp\trpage_util.h SOURCE=..\..\..\src\osgPlugins\txp\trpage_write.h # End Source File -# Begin Source File - -SOURCE=..\..\..\src\osgPlugins\txp\TrPageArchive.h -# End Source File -# Begin Source File - -SOURCE=..\..\..\src\osgPlugins\txp\TrPageParser.h -# End Source File -# Begin Source File - -SOURCE=..\..\..\src\osgPlugins\txp\WaitBlock.h -# End Source File # End Group # Begin Group "Resource Files" diff --git a/src/osgPlugins/txp/GNUmakefile b/src/osgPlugins/txp/GNUmakefile index daf221b68..da5ab703a 100644 --- a/src/osgPlugins/txp/GNUmakefile +++ b/src/osgPlugins/txp/GNUmakefile @@ -10,6 +10,7 @@ CXXFILES =\ TXPTileNode.cpp\ TXPParser.cpp\ TXPSeamLOD.cpp\ + TileMapper.cpp\ trpage_basic.cpp\ trpage_compat.cpp\ trpage_geom.cpp\ diff --git a/src/osgPlugins/txp/TXPArchive.cpp b/src/osgPlugins/txp/TXPArchive.cpp index a5bd49962..d4a26defc 100644 --- a/src/osgPlugins/txp/TXPArchive.cpp +++ b/src/osgPlugins/txp/TXPArchive.cpp @@ -563,36 +563,3 @@ osg::Group* TXPArchive::getTileContent(int x, int y, int lod) osg::Group *tileGroup = _parser->parseScene(buf,_gstates,_models); return tileGroup; } - -osg::PagedLOD* TXPArchive::getPagedLOD(int x, int y, int lod) -{ - TileMap::iterator itr = _tileMap.find(TileTriple(x,y,lod)); - if (itr!=_tileMap.end()) return itr->second.get(); - else return 0; -} - -void TXPArchive::insertPagedLOD(int x, int y, int lod, osg::PagedLOD* pagedLod) -{ - _tileMap[TileTriple(x,y,lod)]=pagedLod; -} - -void TXPArchive::removePagedLOD(int x, int y, int lod) -{ - TileMap::iterator itr = _tileMap.find(TileTriple(x,y,lod)); - if (itr!=_tileMap.end()) return _tileMap.erase(itr); -} - -void TXPArchive::prunePagedLOD() -{ - for(TileMap::iterator itr = _tileMap.begin(); - itr!=_tileMap.end(); - ++itr) - { - if (itr->second.valid() && itr->second->referenceCount()==1) - { - TileMap::iterator eitr = itr; - --itr; - _tileMap.erase(eitr); - } - } -} diff --git a/src/osgPlugins/txp/TXPArchive.h b/src/osgPlugins/txp/TXPArchive.h index 493f95154..4ebca3e0c 100644 --- a/src/osgPlugins/txp/TXPArchive.h +++ b/src/osgPlugins/txp/TXPArchive.h @@ -79,14 +79,6 @@ namespace txp osg::Group* getTileContent(int x, int y, int lod); - - osg::PagedLOD* getPagedLOD(int x, int y, int lod); - - void insertPagedLOD(int x, int y, int lod, osg::PagedLOD* pagedLod); - - void removePagedLOD(int x, int y, int lod); - - void prunePagedLOD(); protected: @@ -119,29 +111,6 @@ namespace txp // Light attributes vector std::vector _lights; - - struct TileTriple - { - TileTriple(int ax, int ay, int alod): - x(ax),y(ay),lod(alod) {} - - int x,y,lod; - - bool operator < (const TileTriple& rhs) const - { - if (xrhs.x) return false; - if (yrhs.y) return false; - if (lodrhs.lod) return false; - return false; - } - }; - - typedef std::map< TileTriple, osg::ref_ptr > TileMap; - TileMap _tileMap; - }; } // namespace diff --git a/src/osgPlugins/txp/TXPNode.cpp b/src/osgPlugins/txp/TXPNode.cpp index 355324c4d..1252cfb50 100644 --- a/src/osgPlugins/txp/TXPNode.cpp +++ b/src/osgPlugins/txp/TXPNode.cpp @@ -6,6 +6,7 @@ #include #include +#include "TileMapper.h" #include "TXPNode.h" using namespace txp; @@ -193,7 +194,7 @@ osg::Node* TXPNode::addPagedLODTile(int x, int y, int lod) _nodesToAdd.push_back(pagedLOD); - _archive->insertPagedLOD(x,y,lod,pagedLOD); + TileMapper::instance()->insertPagedLOD(x,y,lod,pagedLOD); return pagedLOD; } @@ -219,7 +220,7 @@ void TXPNode::updateSceneGraph() } - _archive->prunePagedLOD(); + TileMapper::instance()->prunePagedLOD(); } diff --git a/src/osgPlugins/txp/TXPSeamLOD.cpp b/src/osgPlugins/txp/TXPSeamLOD.cpp index d426b0106..6ec74ce0b 100644 --- a/src/osgPlugins/txp/TXPSeamLOD.cpp +++ b/src/osgPlugins/txp/TXPSeamLOD.cpp @@ -1,6 +1,7 @@ #include "TXPSeamLOD.h" #include "TXPArchive.h" #include "TXPTileNode.h" +#include "TileMapper.h" using namespace txp; @@ -52,7 +53,7 @@ void TXPSeamLOD::traverse(osg::NodeVisitor& nv) if (distance<_mid) { // cap the lod's that can be used to what is available in the adjacent PagedLOD. - osg::PagedLOD* pagedLOD = _archive->getPagedLOD(_neighbourTileX,_neighbourTileY, _neighbourTileLOD); + osg::PagedLOD* pagedLOD = TileMapper::instance()->getPagedLOD(_neighbourTileX,_neighbourTileY, _neighbourTileLOD); if (pagedLOD && pagedLOD->getNumChildren()>1) getChild(1)->accept(nv); else getChild(0)->accept(nv); } diff --git a/src/osgPlugins/txp/TXPTileNode.cpp b/src/osgPlugins/txp/TXPTileNode.cpp index d9808f032..5ff51f7d0 100644 --- a/src/osgPlugins/txp/TXPTileNode.cpp +++ b/src/osgPlugins/txp/TXPTileNode.cpp @@ -1,5 +1,6 @@ #include +#include "TileMapper.h" #include "TXPTileNode.h" #include "TXPArchive.h" #include "TXPSeamLOD.h" @@ -198,7 +199,7 @@ bool TXPTileNode::loadTile(int x, int y, int lod) pagedLOD->setRadius(info.radius); pagedLOD->setNumChildrenThatCannotBeExpired(1); - _archive->insertPagedLOD(x,y,lod,pagedLOD); + TileMapper::instance()->insertPagedLOD(x,y,lod,pagedLOD); addChild(pagedLOD); } diff --git a/src/osgPlugins/txp/TileMapper.cpp b/src/osgPlugins/txp/TileMapper.cpp new file mode 100644 index 000000000..eb2a6cc55 --- /dev/null +++ b/src/osgPlugins/txp/TileMapper.cpp @@ -0,0 +1,48 @@ +#include "TileMapper.h" +#include + +using namespace txp; + +TileMapper* TileMapper::instance() +{ + static osg::ref_ptr s_tilemapper = new TileMapper; + return s_tilemapper.get(); +} + +osg::PagedLOD* TileMapper::getPagedLOD(int x, int y, int lod) +{ + OpenThreads::ScopedLock lock(_mutex); + TileMap::iterator itr = _tileMap.find(TileTriple(x,y,lod)); + if (itr!=_tileMap.end()) return itr->second.get(); + else return 0; +} + +void TileMapper::insertPagedLOD(int x, int y, int lod, osg::PagedLOD* pagedLod) +{ + OpenThreads::ScopedLock lock(_mutex); + _tileMap[TileTriple(x,y,lod)]=pagedLod; +} + +void TileMapper::removePagedLOD(int x, int y, int lod) +{ + OpenThreads::ScopedLock lock(_mutex); + TileMap::iterator itr = _tileMap.find(TileTriple(x,y,lod)); + if (itr!=_tileMap.end()) return _tileMap.erase(itr); +} + +void TileMapper::prunePagedLOD() +{ + OpenThreads::ScopedLock lock(_mutex); + for(TileMap::iterator itr = _tileMap.begin(); + itr!=_tileMap.end(); + ++itr) + { + if (itr->second.valid() && itr->second->referenceCount()==1) + { + TileMap::iterator eitr = itr; + --itr; + _tileMap.erase(eitr); + } + } + //std::cout<<"_tileMap.size()="<<_tileMap.size()< +#include + +#include + +namespace txp +{ + class TileMapper : public osg::Referenced + { + public: + + static TileMapper* instance(); + + osg::PagedLOD* getPagedLOD(int x, int y, int lod); + + void insertPagedLOD(int x, int y, int lod, osg::PagedLOD* pagedLod); + + void removePagedLOD(int x, int y, int lod); + + void prunePagedLOD(); + + protected: + + // Constructor + TileMapper() {} + + // Destructor + virtual ~TileMapper() {} + + struct TileTriple + { + TileTriple(int ax, int ay, int alod): + x(ax),y(ay),lod(alod) {} + + int x,y,lod; + + bool operator < (const TileTriple& rhs) const + { + if (xrhs.x) return false; + if (yrhs.y) return false; + if (lodrhs.lod) return false; + return false; + } + }; + + typedef std::map< TileTriple, osg::ref_ptr > TileMap; + + OpenThreads::Mutex _mutex; + TileMap _tileMap; + + }; + +} // namespace + +#endif // __TXPARCHIVE_H_