| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgSim/LineOfSight> |
|---|
| 15 | |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <osgUtil/LineSegmentIntersector> |
|---|
| 19 | |
|---|
| 20 | using namespace osgSim; |
|---|
| 21 | |
|---|
| 22 | DatabaseCacheReadCallback::DatabaseCacheReadCallback() |
|---|
| 23 | { |
|---|
| 24 | _maxNumFilesToCache = 2000; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | void DatabaseCacheReadCallback::clearDatabaseCache() |
|---|
| 28 | { |
|---|
| 29 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 30 | _filenameSceneMap.clear(); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | void DatabaseCacheReadCallback::pruneUnusedDatabaseCache() |
|---|
| 34 | { |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | osg::Node* DatabaseCacheReadCallback::readNodeFile(const std::string& filename) |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | { |
|---|
| 41 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 42 | |
|---|
| 43 | FileNameSceneMap::iterator itr = _filenameSceneMap.find(filename); |
|---|
| 44 | if (itr != _filenameSceneMap.end()) |
|---|
| 45 | { |
|---|
| 46 | OSG_INFO<<"Getting from cache "<<filename<<std::endl; |
|---|
| 47 | |
|---|
| 48 | return itr->second.get(); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(filename); |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | if (node.valid()) |
|---|
| 57 | { |
|---|
| 58 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 59 | |
|---|
| 60 | if (_filenameSceneMap.size() < _maxNumFilesToCache) |
|---|
| 61 | { |
|---|
| 62 | OSG_INFO<<"Inserting into cache "<<filename<<std::endl; |
|---|
| 63 | |
|---|
| 64 | _filenameSceneMap[filename] = node; |
|---|
| 65 | } |
|---|
| 66 | else |
|---|
| 67 | { |
|---|
| 68 | |
|---|
| 69 | for(FileNameSceneMap::iterator itr = _filenameSceneMap.begin(); |
|---|
| 70 | itr != _filenameSceneMap.end(); |
|---|
| 71 | ++itr) |
|---|
| 72 | { |
|---|
| 73 | if (itr->second->referenceCount()==1) |
|---|
| 74 | { |
|---|
| 75 | OSG_NOTICE<<"Erasing "<<itr->first<<std::endl; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | _filenameSceneMap.erase(itr); |
|---|
| 79 | break; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | OSG_INFO<<"And the replacing with "<<filename<<std::endl; |
|---|
| 83 | _filenameSceneMap[filename] = node; |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | return node.release(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | LineOfSight::LineOfSight() |
|---|
| 91 | { |
|---|
| 92 | setDatabaseCacheReadCallback(new DatabaseCacheReadCallback); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void LineOfSight::clear() |
|---|
| 96 | { |
|---|
| 97 | _LOSList.clear(); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | unsigned int LineOfSight::addLOS(const osg::Vec3d& start, const osg::Vec3d& end) |
|---|
| 101 | { |
|---|
| 102 | unsigned int index = _LOSList.size(); |
|---|
| 103 | _LOSList.push_back(LOS(start,end)); |
|---|
| 104 | return index; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | void LineOfSight::computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask) |
|---|
| 108 | { |
|---|
| 109 | osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup(); |
|---|
| 110 | |
|---|
| 111 | for(LOSList::iterator itr = _LOSList.begin(); |
|---|
| 112 | itr != _LOSList.end(); |
|---|
| 113 | ++itr) |
|---|
| 114 | { |
|---|
| 115 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(itr->_start, itr->_end); |
|---|
| 116 | intersectorGroup->addIntersector( intersector.get() ); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | _intersectionVisitor.reset(); |
|---|
| 120 | _intersectionVisitor.setTraversalMask(traversalMask); |
|---|
| 121 | _intersectionVisitor.setIntersector( intersectorGroup.get() ); |
|---|
| 122 | |
|---|
| 123 | scene->accept(_intersectionVisitor); |
|---|
| 124 | |
|---|
| 125 | unsigned int index = 0; |
|---|
| 126 | osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors(); |
|---|
| 127 | for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin(); |
|---|
| 128 | intersector_itr != intersectors.end(); |
|---|
| 129 | ++intersector_itr, ++index) |
|---|
| 130 | { |
|---|
| 131 | osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get()); |
|---|
| 132 | if (lsi) |
|---|
| 133 | { |
|---|
| 134 | Intersections& intersectionsLOS = _LOSList[index]._intersections; |
|---|
| 135 | _LOSList[index]._intersections.clear(); |
|---|
| 136 | |
|---|
| 137 | osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections(); |
|---|
| 138 | |
|---|
| 139 | for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin(); |
|---|
| 140 | itr != intersections.end(); |
|---|
| 141 | ++itr) |
|---|
| 142 | { |
|---|
| 143 | const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr; |
|---|
| 144 | if (intersection.matrix.valid()) intersectionsLOS.push_back( intersection.localIntersectionPoint * (*intersection.matrix) ); |
|---|
| 145 | else intersectionsLOS.push_back( intersection.localIntersectionPoint ); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | LineOfSight::Intersections LineOfSight::computeIntersections(osg::Node* scene, const osg::Vec3d& start, const osg::Vec3d& end, osg::Node::NodeMask traversalMask) |
|---|
| 153 | { |
|---|
| 154 | LineOfSight los; |
|---|
| 155 | unsigned int index = los.addLOS(start,end); |
|---|
| 156 | los.computeIntersections(scene, traversalMask); |
|---|
| 157 | return los.getIntersections(index); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void LineOfSight::setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc) |
|---|
| 161 | { |
|---|
| 162 | _dcrc = dcrc; |
|---|
| 163 | _intersectionVisitor.setReadCallback(dcrc); |
|---|
| 164 | } |
|---|