Files
OpenSceneGraph/src/osgSim/LineOfSight.cpp
Robert Osfield dd996a3289 Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods,
forcing users to use osgDB::readRef*File() methods.  The later is preferable as it closes a potential threading bug when using paging databases in conjunction
with the osgDB::Registry Object Cache.  This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only
a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another
thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero.  Using osgDB::readREf*File() makes sure the a ref_ptr<> is
passed back and the referenceCount never goes to zero.

To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File()
usage.  The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of
templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group:

    bool addChild(Node* child); // old method which can only be used with a Node*

    tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method

These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache
and multi-threaded loaded more robust.



git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 13:42:19 +00:00

165 lines
5.4 KiB
C++

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgSim/LineOfSight>
#include <osg/Notify>
#include <osgDB/ReadFile>
#include <osgUtil/LineSegmentIntersector>
using namespace osgSim;
DatabaseCacheReadCallback::DatabaseCacheReadCallback()
{
_maxNumFilesToCache = 2000;
}
void DatabaseCacheReadCallback::clearDatabaseCache()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_filenameSceneMap.clear();
}
void DatabaseCacheReadCallback::pruneUnusedDatabaseCache()
{
}
osg::ref_ptr<osg::Node> DatabaseCacheReadCallback::readNodeFile(const std::string& filename)
{
// first check to see if file is already loaded.
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
FileNameSceneMap::iterator itr = _filenameSceneMap.find(filename);
if (itr != _filenameSceneMap.end())
{
OSG_INFO<<"Getting from cache "<<filename<<std::endl;
return itr->second.get();
}
}
// now load the file.
osg::ref_ptr<osg::Node> node = osgDB::readRefNodeFile(filename);
// insert into the cache.
if (node.valid())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (_filenameSceneMap.size() < _maxNumFilesToCache)
{
OSG_INFO<<"Inserting into cache "<<filename<<std::endl;
_filenameSceneMap[filename] = node;
}
else
{
// for time being implement a crude search for a candidate to chuck out from the cache.
for(FileNameSceneMap::iterator itr = _filenameSceneMap.begin();
itr != _filenameSceneMap.end();
++itr)
{
if (itr->second->referenceCount()==1)
{
OSG_NOTICE<<"Erasing "<<itr->first<<std::endl;
// found a node which is only referenced in the cache so we can discard it
// and know that the actual memory will be released.
_filenameSceneMap.erase(itr);
break;
}
}
OSG_INFO<<"And the replacing with "<<filename<<std::endl;
_filenameSceneMap[filename] = node;
}
}
return node;
}
LineOfSight::LineOfSight()
{
setDatabaseCacheReadCallback(new DatabaseCacheReadCallback);
}
void LineOfSight::clear()
{
_LOSList.clear();
}
unsigned int LineOfSight::addLOS(const osg::Vec3d& start, const osg::Vec3d& end)
{
unsigned int index = _LOSList.size();
_LOSList.push_back(LOS(start,end));
return index;
}
void LineOfSight::computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask)
{
osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
for(LOSList::iterator itr = _LOSList.begin();
itr != _LOSList.end();
++itr)
{
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(itr->_start, itr->_end);
intersectorGroup->addIntersector( intersector.get() );
}
_intersectionVisitor.reset();
_intersectionVisitor.setTraversalMask(traversalMask);
_intersectionVisitor.setIntersector( intersectorGroup.get() );
scene->accept(_intersectionVisitor);
unsigned int index = 0;
osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
intersector_itr != intersectors.end();
++intersector_itr, ++index)
{
osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get());
if (lsi)
{
Intersections& intersectionsLOS = _LOSList[index]._intersections;
_LOSList[index]._intersections.clear();
osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections();
for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
itr != intersections.end();
++itr)
{
const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
if (intersection.matrix.valid()) intersectionsLOS.push_back( intersection.localIntersectionPoint * (*intersection.matrix) );
else intersectionsLOS.push_back( intersection.localIntersectionPoint );
}
}
}
}
LineOfSight::Intersections LineOfSight::computeIntersections(osg::Node* scene, const osg::Vec3d& start, const osg::Vec3d& end, osg::Node::NodeMask traversalMask)
{
LineOfSight los;
unsigned int index = los.addLOS(start,end);
los.computeIntersections(scene, traversalMask);
return los.getIntersections(index);
}
void LineOfSight::setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc)
{
_dcrc = dcrc;
_intersectionVisitor.setReadCallback(dcrc);
}