From Trajce Nicklov, "I went thru the TileMapper today very carefuly and I remove all the tileStack approach - it made everything simplier and I think more proper. Cleaned the code as well. It was tested on huge archive with many lods as well with variable lods. Fix attached."

This commit is contained in:
Robert Osfield
2010-12-19 13:25:45 +00:00
parent 362edaa1b9
commit 758bf7c3d9
3 changed files with 29 additions and 400 deletions

View File

@@ -134,8 +134,6 @@ void TXPNode::traverse(osg::NodeVisitor& nv)
//std::cout<<" found " << tileMapper._tileMap.size() << std::endl;
tileMapper->checkValidityOfAllVisibleTiles();
cv->setUserData(tileMapper.get());
#ifdef PRINT_TILEMAPP_TIMEINFO

View File

@@ -11,8 +11,11 @@
* OpenSceneGraph Public License for more details.
*/
/* Dec 2010 - TileMapper was fixed and simplified
Nick
*/
#include "TileMapper.h"
//#include "TXPTileNode.h"
#include "TXPPagedLOD.h"
#include <osg/Material>
@@ -22,20 +25,18 @@ using namespace txp;
float TileMapper::getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const
{
if (withLODScale)
return (pos-getEyeLocal()).length()*getLODScale();
return (pos-getEyeLocal()).length()*getLODScale();
else
return (pos-getEyeLocal()).length();
return (pos-getEyeLocal()).length();
}
inline TileMapper::value_type distance(const osg::Vec3& coord,const osg::Matrix& matrix)
{
//std::cout << "distance("<<coord<<", "<<matrix<<")"<<std::endl;
return -((TileMapper::value_type)coord[0]*(TileMapper::value_type)matrix(0,2)+
(TileMapper::value_type)coord[1]*(TileMapper::value_type)matrix(1,2)+
(TileMapper::value_type)coord[2]*(TileMapper::value_type)matrix(2,2)+
matrix(3,2));
(TileMapper::value_type)coord[1]*(TileMapper::value_type)matrix(1,2)+
(TileMapper::value_type)coord[2]*(TileMapper::value_type)matrix(2,2)+
matrix(3,2));
}
float TileMapper::getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScale) const
@@ -44,9 +45,9 @@ float TileMapper::getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScal
float dist = distance(pos,matrix);
if (withLODScale)
return dist*getLODScale();
return dist*getLODScale();
else
return dist;
return dist;
}
void TileMapper::apply(osg::Node& node)
@@ -58,7 +59,7 @@ void TileMapper::apply(osg::Node& node)
}
if (isCulled(node))
return;
return;
// push the culling mode.
pushCurrentMask();
@@ -78,7 +79,7 @@ void TileMapper::apply(osg::Group& node)
}
if (isCulled(node))
return;
return;
// push the culling mode.
pushCurrentMask();
@@ -87,8 +88,6 @@ void TileMapper::apply(osg::Group& node)
if (tid)
{
_tileStack.push_back(TileStack::value_type(*tid,&node));
_containsGeode = false;
}
@@ -102,44 +101,8 @@ void TileMapper::apply(osg::Group& node)
_containsGeode = false;
#if 0
std::cout<<"found Group="<<tid->lod
<<" X="<<tid->x
<<" Y="<<tid->y
<<" ptr="<<&node<<std::endl;
std::cout<<" inheritance list "<<_tileStack.size()<<std::endl;
for(TileStack::iterator itr=_tileStack.begin();
itr!=_tileStack.end();
++itr)
{
std::cout<<" LOD="<<itr->first.lod
<<" X="<<itr->first.x
<<" Y="<<itr->first.y
<<" className="<<itr->second->className()
<<" ptr="<<itr->second<<std::endl;
}
osg::StateSet* stateset = node.getOrCreateStateSet();
osg::Material* material = new osg::Material;
material->setColorMode(osg::Material::OFF);
stateset->setAttribute(material);
switch(tid->lod)
{
case(0): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f)); break;
case(1): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,0.0f,0.0f,1.0f)); break;
case(2): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(0.0f,1.0f,0.0f,1.0f)); break;
case(3): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(0.0f,0.0f,1.0f,1.0f)); break;
case(4): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,0.0f,1.0f,1.0f)); break;
}
#endif
}
_tileStack.pop_back();
}
// pop the culling mode.
@@ -154,7 +117,7 @@ void TileMapper::apply(osg::Geode&)
void TileMapper::apply(osg::PagedLOD& node)
{
if (isCulled(node))
return;
return;
// push the culling mode.
pushCurrentMask();
@@ -162,11 +125,7 @@ void TileMapper::apply(osg::PagedLOD& node)
TXPPagedLOD* txpPagedLOD = dynamic_cast<TXPPagedLOD*>(&node);
if (txpPagedLOD)
{
_tileStack.push_back(TileStack::value_type(txpPagedLOD->_tileIdentifier,&node));
_containsGeode = false;
}
traverse(node);
@@ -178,41 +137,7 @@ void TileMapper::apply(osg::PagedLOD& node)
insertTile(txpPagedLOD->_tileIdentifier);
_containsGeode = false;
#if 0
std::cout<<"found txpPagedLOD LOD="<<txpPagedLOD->_tileIdentifier.lod
<<" X="<<txpPagedLOD->_tileIdentifier.x
<<" Y="<<txpPagedLOD->_tileIdentifier.y
<<" ptr="<<txpPagedLOD<<std::endl;
std::cout<<" inheritance list "<<_tileStack.size()<<std::endl;
for(TileStack::iterator itr=_tileStack.begin();
itr!=_tileStack.end();
++itr)
{
std::cout<<" LOD="<<itr->first.lod
<<" X="<<itr->first.x
<<" Y="<<itr->first.y
<<" className="<<itr->second->className()
<<" ptr="<<itr->second<<std::endl;
}
osg::StateSet* stateset = txpPagedLOD->getOrCreateStateSet();
osg::Material* material = new osg::Material;
material->setColorMode(osg::Material::OFF);
stateset->setAttribute(material);
switch(txpPagedLOD->_tileIdentifier.lod)
{
case(0): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,1.0f,1.0f,1.0f)); break;
case(1): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,0.0f,0.0f,1.0f)); break;
case(2): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(0.0f,1.0f,0.0f,1.0f)); break;
case(3): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(0.0f,0.0f,1.0f,1.0f)); break;
case(4): material->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(1.0f,0.0f,1.0f,1.0f)); break;
}
#endif
}
_tileStack.pop_back();
}
// pop the culling mode.
@@ -221,212 +146,10 @@ void TileMapper::apply(osg::PagedLOD& node)
void TileMapper::insertTile(const TileIdentifier& tid)
{
_tileMap.insert(TileMap::value_type(tid,_tileStack));
_tileMap.insert(TileMap::value_type(tid,1));
}
bool TileMapper::canParentBeTraversed(const TileIdentifier& tid) const
{
// find the tiles parents.
TileMap::const_iterator itr = _tileMap.find(tid);
if (itr==_tileMap.end())
{
#if 0
// not found tile in _tileMap, what should we do??
// return true as a fallback right now.
std::cout<<"TileMapper::canDescend() Not found tile in map"<<std::endl;
#endif
return true;
}
const TileStack& ts = itr->second;
// note tile here, is tid's parent.
const TileStack::value_type* tile = (ts.size()>=2) ? &ts[ts.size()-2] : 0;
if (!tile)
{
// no self!!! so we can descend safely?!! shouldn't ever get here.
//std::cout<<"TileMapper::canDescend() tile==0"<<std::endl;
return true;
}
// note parent here, is tid's parents parent.
const TileStack::value_type* parent = (ts.size()>=3) ? &ts[ts.size()-3] : 0;
if (!parent)
{
// no parent so we can descend safely.
return true;
}
bool tileHasNorthNeighour = _tileMap.count(TileIdentifier(tid.x,tid.y+1,tid.lod))!=0;
bool tileHasEastNeighour = _tileMap.count(TileIdentifier(tid.x+1,tid.y,tid.lod))!=0;
bool tileHasSouthNeighour = _tileMap.count(TileIdentifier(tid.x,tid.y-1,tid.lod))!=0;
bool tileHasWestNeighour = _tileMap.count(TileIdentifier(tid.x-1,tid.y,tid.lod))!=0;
if (tileHasNorthNeighour && tileHasEastNeighour && tileHasSouthNeighour && tileHasWestNeighour)
{
// tile has neigbours on all sides at the same lod level, so its safe to descend.
//std::cout<<"TileMapper::canDescend() has neightbours on all sides"<<std::endl;
return true;
}
// FIXME: this guy gives us wrong parent
// Nick 2010/12/12
#if 0
const TileIdentifier& parent_tid = parent->first;
#else
TileIdentifier parent_tid(tid.x/2,tid.y/2,tid.lod-1);
#endif
bool parentHasNorthNeighour = _tileMap.count(TileIdentifier(parent_tid.x,parent_tid.y+1,parent_tid.lod))!=0;
bool parentHasEastNeighour = _tileMap.count(TileIdentifier(parent_tid.x+1,parent_tid.y,parent_tid.lod))!=0;
bool parentHasSouthNeighour = _tileMap.count(TileIdentifier(parent_tid.x,parent_tid.y-1,parent_tid.lod))!=0;
bool parentHasWestNeighour = _tileMap.count(TileIdentifier(parent_tid.x-1,parent_tid.y,parent_tid.lod))!=0;
// FIXME: this guy gives us wrong parent
// Nick 2010/12/12
#if 0
// identify whether the tile is a NE/SE/SW/NW tile relative to its parent.
osg::Vec3 delta = tile->second->getBound().center() - parent->second->getBound().center();
if (delta.y()>=0.0f) // noth side
{
if (delta.x()>=0.0f)
{
// NE, only traverse if our parent doesn't have any neighbours to the north or east.
return (!parentHasNorthNeighour && !parentHasEastNeighour);
}
else
{
// NW, only traverse if our parent doesn't have any neighbours to the north or west.
return (!parentHasNorthNeighour && !parentHasWestNeighour);
}
}
else // south side
{
if (delta.x()>=0.0f)
{
// SE, only traverse if our parent doesn't have any neighbours to the south or east.
return (!parentHasSouthNeighour && !parentHasEastNeighour);
}
else
{
// SW, only traverse if our parent doesn't have any neighbours to the south or west.
return (!parentHasSouthNeighour && !parentHasWestNeighour);
}
}
#else
// identify whether the tile is a NE/SE/SW/NW tile relative to its parent.
osg::Vec3 delta(tid.x%2,tid.y%2,0);
if (delta.y()>0.0f) // noth side
{
if (delta.x()>0.0f)
{
// NE, only traverse if our parent doesn't have any neighbours to the north or east.
return (!parentHasNorthNeighour && !parentHasEastNeighour);
}
else
{
// NW, only traverse if our parent doesn't have any neighbours to the north or west.
return (!parentHasNorthNeighour && !parentHasWestNeighour);
}
}
else // south side
{
if (delta.x()>0.0f)
{
// SE, only traverse if our parent doesn't have any neighbours to the south or east.
return (!parentHasSouthNeighour && !parentHasEastNeighour);
}
else
{
// SW, only traverse if our parent doesn't have any neighbours to the south or west.
return (!parentHasSouthNeighour && !parentHasWestNeighour);
}
}
#endif
}
void TileMapper::checkValidityOfAllVisibleTiles()
{
typedef std::vector<TileIdentifier> ToRemoveList;
typedef std::vector<TileStack> ToAddList;
ToRemoveList toRemoveList;
ToAddList toAddList;
do
{
// std::cout<<"doing checkAllVisibleTiles() loop with "<<_tileMap.size()<<std::endl;
toRemoveList.clear();
toAddList.clear();
for(TileMap::iterator itr=_tileMap.begin();
itr!=_tileMap.end();
++itr)
{
if (!canParentBeTraversed(itr->first))
{
// need to remove.
toRemoveList.push_back(itr->first);
// trim the end of itr's TileStack and add into toAddList
toAddList.push_back(itr->second);
// std::cout<<"Tile failed"
// <<" LOD="<<itr->first.lod
// <<" X="<<itr->first.x
// <<" Y="<<itr->first.y<<std::endl;
}
}
for(ToRemoveList::iterator ritr=toRemoveList.begin();
ritr!=toRemoveList.end();
++ritr)
{
//std::cout<<"Removing Tile"<<std::endl;
_tileMap.erase(*ritr);
}
for(ToAddList::iterator aitr=toAddList.begin();
aitr!=toAddList.end();
++aitr)
{
//std::cout<<"Adding Parents Tile back in"<<std::endl;
aitr->pop_back();
_blackListedNodeSet.insert(aitr->back().second);
_tileMap.insert(TileMap::value_type(aitr->back().first,*aitr));
}
}
while (!toRemoveList.empty());
#if 0
if ( !_blackListedNodeSet.empty() )
std::cout << "********** We have blacked list " << _blackListedNodeSet.size() << std::endl;
std::cout<<"TileMap contains "<<_tileMap.size()<<std::endl;
for(TileMap::iterator itr=_tileMap.begin();
itr!=_tileMap.end();
++itr)
{
std::cout<<" tile="<<itr->first.lod
<<" X="<<itr->first.x
<<" Y="<<itr->first.y<<std::endl;
}
#endif
}
bool TileMapper::isTileNeighbourALowerLODLevel(const TileIdentifier& tid, int dx, int dy) const
{
if (_tileMap.count(TileIdentifier(tid.x+dx,tid.y+dy,tid.lod))!=0)
@@ -442,43 +165,13 @@ bool TileMapper::isTileNeighbourALowerLODLevel(const TileIdentifier& tid, int dx
// not found tile in _tileMap, what should we do??
// return true as a fallback right now.
#if 0
std::cout << "TileMapper::isTileNeighbourALowerLODLevel() Not found tile in map," << std::endl;
std::cout << " LOD=" << tid.lod << " X=" << tid.x << " Y=" << tid.y << std::endl;
std::cout << "TileMapper::isTileNeighbourALowerLODLevel() Not found tile in map," << std::endl;
std::cout << " LOD=" << tid.lod << " X=" << tid.x << " Y=" << tid.y << std::endl;
#endif
return true;
}
const TileStack& ts = itr->second;
// note tile here, is tid's parent.
const TileStack::value_type* tile = (ts.size()>=1) ? &ts[ts.size()-1] : 0;
if (!tile)
{
// no tile, so must assume that neighbor is now at a lower level
return false;
}
// FIXME: this guy gives us wrong parent
// Nick 2010/12/12
#if 0
// note parent here, is tid's parents parent.
const TileStack::value_type* parent = (ts.size()>=2) ? &ts[ts.size()-2] : 0;
if (!parent)
{
// no parent so we must assume that is not at a lower level
return false;
}
const TileIdentifier& parent_tid = parent->first;
#else
TileIdentifier parent_tid(tid.x/2,tid.y/2,tid.lod-1);
#endif
bool parentHasNorthNeighour = _tileMap.count(TileIdentifier(parent_tid.x, parent_tid.y+1,parent_tid.lod))!=0;
bool parentHasEastNeighour = _tileMap.count(TileIdentifier(parent_tid.x+1,parent_tid.y, parent_tid.lod))!=0;
bool parentHasSouthNeighour = _tileMap.count(TileIdentifier(parent_tid.x, parent_tid.y-1,parent_tid.lod))!=0;
@@ -486,55 +179,10 @@ bool TileMapper::isTileNeighbourALowerLODLevel(const TileIdentifier& tid, int dx
// identify whether the tile is a NE/SE/SW/NW tile relative to its parent.
// FIXME: wrong parent wrong values
// Nick 2010/12/12
#if 0
osg::Vec3 delta = tile->second->getBound().center() - parent->second->getBound().center();
if (delta.y()>=0.0f) // noth side
{
if (delta.x()>=0.0f)
{
// NE
if (dy==1)
return parentHasNorthNeighour;
else if (dx==1)
return parentHasEastNeighour;
}
else
{
// NW
if (dy==1)
return parentHasNorthNeighour;
else if (dx==-1)
return parentHasWestNeighour;
}
}
else // south side
{
if (delta.x()>=0.0f)
{
// SE
if (dy==-1)
return parentHasSouthNeighour;
else if (dx==1)
return parentHasEastNeighour;
}
else
{
// SW
if (dy==-1)
return parentHasSouthNeighour;
else if (dx==-1)
return parentHasWestNeighour;
}
}
#else
osg::Vec3 delta(tid.x%2,tid.y%2,0);
if (delta.y()>0.0f) // noth side
{
{
if (delta.x()>0.0f)
{
// NE
@@ -571,7 +219,6 @@ bool TileMapper::isTileNeighbourALowerLODLevel(const TileIdentifier& tid, int dx
return parentHasWestNeighour;
}
}
#endif
return false;
}

View File

@@ -11,6 +11,10 @@
* OpenSceneGraph Public License for more details.
*/
/* Dec 2010 - TileMapper was fixed and simplified
Nick
*/
#ifndef __TILEMAPPER_H_
#define __TILEMAPPER_H_
@@ -96,7 +100,7 @@ public:
virtual osg::Vec3 getEyePoint() const
{
return getEyeLocal();
return getEyeLocal();
}
virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
virtual float getDistanceFromEyePoint(const osg::Vec3& pos, bool withLODScale) const;
@@ -108,33 +112,13 @@ public:
void insertTile(const TileIdentifier& tid);
bool canParentBeTraversed(const TileIdentifier& tid) const;
void checkValidityOfAllVisibleTiles();
bool containsBlackListedNodes() const
{
return !_blackListedNodeSet.empty();
}
inline bool isNodeBlackListed(const osg::Node* node) const
{
return _blackListedNodeSet.count(node)!=0;
}
bool isTileNeighbourALowerLODLevel(const TileIdentifier& tid, int dx, int dy) const;
protected:
typedef std::vector< std::pair<TileIdentifier,osg::Node*> > TileStack;
typedef std::map< TileIdentifier, TileStack > TileMap;
typedef std::set< const osg::Node* > BlacklistedNodeSet;
TileStack _tileStack;
TileMap _tileMap;
bool _containsGeode;
BlacklistedNodeSet _blackListedNodeSet;
typedef std::map< TileIdentifier, int> TileMap;
TileMap _tileMap;
bool _containsGeode;
};