2003-07-08 22:44:00 +08:00
|
|
|
#include <osg/PagedLOD>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2004-01-07 16:39:33 +08:00
|
|
|
PagedLOD::PerRangeData::PerRangeData():
|
|
|
|
_priorityOffset(0.0f),
|
|
|
|
_priorityScale(0.0f),
|
|
|
|
_timeStamp(0.0f) {}
|
|
|
|
|
|
|
|
PagedLOD::PerRangeData::PerRangeData(const PerRangeData& prd):
|
|
|
|
_filename(prd._filename),
|
|
|
|
_priorityOffset(prd._priorityOffset),
|
|
|
|
_priorityScale(prd._priorityScale),
|
|
|
|
_timeStamp(prd._timeStamp) {}
|
|
|
|
|
|
|
|
PagedLOD::PerRangeData& PagedLOD::PerRangeData::operator = (const PerRangeData& prd)
|
|
|
|
{
|
|
|
|
if (this==&prd) return *this;
|
|
|
|
_filename = prd._filename;
|
|
|
|
_priorityOffset = prd._priorityOffset;
|
|
|
|
_priorityScale = prd._priorityScale;
|
|
|
|
_timeStamp = prd._timeStamp;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-07-10 22:53:07 +08:00
|
|
|
PagedLOD::PagedLOD()
|
|
|
|
{
|
2004-09-22 01:26:08 +08:00
|
|
|
_frameNumberOfLastTraversal = 0;
|
2003-07-10 22:53:07 +08:00
|
|
|
_centerMode = USER_DEFINED_CENTER;
|
2003-12-09 20:08:27 +08:00
|
|
|
_radius = -1;
|
|
|
|
_numChildrenThatCannotBeExpired = 0;
|
2003-07-10 22:53:07 +08:00
|
|
|
}
|
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
PagedLOD::PagedLOD(const PagedLOD& plod,const CopyOp& copyop):
|
2003-12-09 20:08:27 +08:00
|
|
|
LOD(plod,copyop),
|
2004-09-22 01:26:08 +08:00
|
|
|
_frameNumberOfLastTraversal(plod._frameNumberOfLastTraversal),
|
2003-12-09 20:08:27 +08:00
|
|
|
_numChildrenThatCannotBeExpired(plod._numChildrenThatCannotBeExpired),
|
2004-01-06 04:45:28 +08:00
|
|
|
_perRangeDataList(plod._perRangeDataList)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-10-06 17:31:34 +08:00
|
|
|
void PagedLOD::setDatabasePath(const std::string& path)
|
|
|
|
{
|
|
|
|
_databasePath = path;
|
|
|
|
if (!_databasePath.empty())
|
|
|
|
{
|
|
|
|
char& lastCharacter = _databasePath[_databasePath.size()-1];
|
|
|
|
const char unixSlash = '/';
|
|
|
|
const char winSlash = '\\';
|
|
|
|
|
|
|
|
// make sure the last character is the appropriate slash
|
|
|
|
#ifdef WIN32
|
|
|
|
if (lastCharacter==unixSlash)
|
|
|
|
{
|
|
|
|
lastCharacter = winSlash;
|
|
|
|
}
|
|
|
|
else if (lastCharacter!=winSlash)
|
|
|
|
{
|
|
|
|
_databasePath += winSlash;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (lastCharacter==winSlash)
|
|
|
|
{
|
|
|
|
lastCharacter = unixSlash;
|
|
|
|
}
|
|
|
|
else if (lastCharacter!=unixSlash)
|
|
|
|
{
|
|
|
|
_databasePath += unixSlash;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-09 20:08:27 +08:00
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
void PagedLOD::traverse(NodeVisitor& nv)
|
|
|
|
{
|
2004-09-22 01:26:08 +08:00
|
|
|
// set the frame number of the traversal so that external nodes can find out how active this
|
|
|
|
// node is.
|
|
|
|
if (nv.getFrameStamp()) setFrameNumberOfLastTraversal(nv.getFrameStamp()->getFrameNumber());
|
2003-07-08 22:44:00 +08:00
|
|
|
|
|
|
|
double timeStamp = nv.getFrameStamp()?nv.getFrameStamp()->getReferenceTime():0.0;
|
|
|
|
bool updateTimeStamp = nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR;
|
|
|
|
|
|
|
|
switch(nv.getTraversalMode())
|
|
|
|
{
|
|
|
|
case(NodeVisitor::TRAVERSE_ALL_CHILDREN):
|
|
|
|
std::for_each(_children.begin(),_children.end(),NodeAcceptOp(nv));
|
|
|
|
break;
|
|
|
|
case(NodeVisitor::TRAVERSE_ACTIVE_CHILDREN):
|
|
|
|
{
|
|
|
|
float distance = nv.getDistanceToEyePoint(getCenter(),true);
|
2003-12-21 07:25:05 +08:00
|
|
|
|
|
|
|
int lastChildTraversed = -1;
|
|
|
|
bool needToLoadChild = false;
|
|
|
|
for(unsigned int i=0;i<_rangeList.size();++i)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
if (_rangeList[i].first<=distance && distance<_rangeList[i].second)
|
|
|
|
{
|
2003-12-21 07:25:05 +08:00
|
|
|
if (i<_children.size())
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
if (updateTimeStamp) _perRangeDataList[i]._timeStamp=timeStamp;
|
2003-07-08 22:44:00 +08:00
|
|
|
|
2003-12-21 07:25:05 +08:00
|
|
|
_children[i]->accept(nv);
|
|
|
|
lastChildTraversed = (int)i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
needToLoadChild = true;
|
|
|
|
}
|
2003-07-08 22:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-21 07:25:05 +08:00
|
|
|
if (needToLoadChild)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2003-12-21 07:25:05 +08:00
|
|
|
unsigned int numChildren = _children.size();
|
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
// select the last valid child.
|
2003-12-21 07:25:05 +08:00
|
|
|
if (numChildren>0 && ((int)numChildren-1)!=lastChildTraversed)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
if (updateTimeStamp) _perRangeDataList[numChildren-1]._timeStamp=timeStamp;
|
2003-07-08 22:44:00 +08:00
|
|
|
_children[numChildren-1]->accept(nv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now request the loading of the next unload child.
|
2004-01-06 04:45:28 +08:00
|
|
|
if (nv.getDatabaseRequestHandler() && numChildren<_perRangeDataList.size())
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2004-01-06 04:51:30 +08:00
|
|
|
// compute priority from where abouts in the required range the distance falls.
|
2003-10-11 03:25:14 +08:00
|
|
|
float priority = (_rangeList[numChildren].second-distance)/(_rangeList[numChildren].second-_rangeList[numChildren].first);
|
2004-01-06 04:51:30 +08:00
|
|
|
|
|
|
|
// modify the priority according to the child's priority offset and scale.
|
|
|
|
priority = _perRangeDataList[numChildren]._priorityOffset + priority * _perRangeDataList[numChildren]._priorityScale;
|
|
|
|
|
2004-10-06 17:31:34 +08:00
|
|
|
if (_databasePath.empty())
|
|
|
|
{
|
|
|
|
nv.getDatabaseRequestHandler()->requestNodeFile(_perRangeDataList[numChildren]._filename,this,priority,nv.getFrameStamp());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// prepend the databasePath to the childs filename.
|
|
|
|
nv.getDatabaseRequestHandler()->requestNodeFile(_databasePath+_perRangeDataList[numChildren]._filename,this,priority,nv.getFrameStamp());
|
|
|
|
}
|
2003-07-08 22:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-09 20:08:27 +08:00
|
|
|
|
2004-01-06 04:45:28 +08:00
|
|
|
void PagedLOD::childRemoved(unsigned int pos, unsigned int numChildrenToRemove)
|
|
|
|
{
|
|
|
|
LOD::childRemoved(pos, numChildrenToRemove);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::childInserted(unsigned int pos)
|
|
|
|
{
|
|
|
|
LOD::childInserted(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::rangeRemoved(unsigned int pos, unsigned int numChildrenToRemove)
|
|
|
|
{
|
|
|
|
LOD::rangeRemoved(pos, numChildrenToRemove);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::rangeInserted(unsigned int pos)
|
|
|
|
{
|
|
|
|
LOD::rangeInserted(pos);
|
|
|
|
expandPerRangeDataTo(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::expandPerRangeDataTo(unsigned int pos)
|
|
|
|
{
|
|
|
|
if (pos>=_perRangeDataList.size()) _perRangeDataList.resize(pos+1);
|
|
|
|
}
|
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
bool PagedLOD::addChild( Node *child )
|
|
|
|
{
|
|
|
|
if (LOD::addChild(child))
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
expandPerRangeDataTo(_children.size());
|
2003-07-08 22:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PagedLOD::addChild(Node *child, float min, float max)
|
|
|
|
{
|
|
|
|
if (LOD::addChild(child,min,max))
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
expandPerRangeDataTo(_children.size());
|
2003-07-08 22:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-06 04:45:28 +08:00
|
|
|
bool PagedLOD::addChild(Node *child, float min, float max,const std::string& filename, float priorityOffset, float priorityScale)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
if (LOD::addChild(child,min,max))
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
setFileName(_children.size()-1,filename);
|
|
|
|
setPriorityOffset(_children.size()-1,priorityOffset);
|
|
|
|
setPriorityScale(_children.size()-1,priorityScale);
|
2003-07-08 22:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PagedLOD::removeChild( Node *child )
|
|
|
|
{
|
|
|
|
// find the child's position.
|
|
|
|
unsigned int pos=getChildIndex(child);
|
|
|
|
if (pos==_children.size()) return false;
|
|
|
|
|
2004-01-06 04:45:28 +08:00
|
|
|
if (pos<_rangeList.size()) _rangeList.erase(_rangeList.begin()+pos);
|
|
|
|
if (pos<_perRangeDataList.size()) _perRangeDataList.erase(_perRangeDataList.begin()+pos);
|
2003-07-08 22:44:00 +08:00
|
|
|
|
|
|
|
return Group::removeChild(child);
|
|
|
|
}
|
|
|
|
|
2004-09-22 01:26:08 +08:00
|
|
|
bool PagedLOD::removeExpiredChildren(double expiryTime,NodeList& removedChildren)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2004-02-02 00:13:07 +08:00
|
|
|
if (_children.size()>_numChildrenThatCannotBeExpired)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2004-02-02 00:13:07 +08:00
|
|
|
if (!_perRangeDataList[_children.size()-1]._filename.empty() && _perRangeDataList[_children.size()-1]._timeStamp<expiryTime)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2004-08-02 21:57:47 +08:00
|
|
|
osg::Node* nodeToRemove = _children[_children.size()-1].get();
|
|
|
|
removedChildren.push_back(nodeToRemove);
|
2004-09-22 01:26:08 +08:00
|
|
|
return Group::removeChild(nodeToRemove);
|
2003-07-08 22:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2004-09-22 01:26:08 +08:00
|
|
|
return false;
|
2003-07-08 22:44:00 +08:00
|
|
|
}
|