| 1 | #include <osg/ArgumentParser> |
|---|
| 2 | #include <osg/ApplicationUsage> |
|---|
| 3 | #include <osg/Timer> |
|---|
| 4 | #include <osg/CoordinateSystemNode> |
|---|
| 5 | #include <osg/Notify> |
|---|
| 6 | #include <osg/io_utils> |
|---|
| 7 | |
|---|
| 8 | #include <osgDB/ReadFile> |
|---|
| 9 | |
|---|
| 10 | #include <osgUtil/IntersectionVisitor> |
|---|
| 11 | #include <osgUtil/LineSegmentIntersector> |
|---|
| 12 | |
|---|
| 13 | #include <osgSim/LineOfSight> |
|---|
| 14 | #include <osgSim/HeightAboveTerrain> |
|---|
| 15 | #include <osgSim/ElevationSlice> |
|---|
| 16 | |
|---|
| 17 | #include <iostream> |
|---|
| 18 | |
|---|
| 19 | struct MyReadCallback : public osgUtil::IntersectionVisitor::ReadCallback |
|---|
| 20 | { |
|---|
| 21 | virtual osg::Node* readNodeFile(const std::string& filename) |
|---|
| 22 | { |
|---|
| 23 | return osgDB::readNodeFile(filename); |
|---|
| 24 | } |
|---|
| 25 | }; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | int main(int argc, char **argv) |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker."); |
|---|
| 35 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()); |
|---|
| 36 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 37 | |
|---|
| 38 | osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments); |
|---|
| 39 | |
|---|
| 40 | if (!scene) |
|---|
| 41 | { |
|---|
| 42 | std::cout<<"No model loaded, please specify a valid model on the command line."<<std::endl; |
|---|
| 43 | return 0; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | std::cout<<"Intersection "<<std::endl; |
|---|
| 47 | |
|---|
| 48 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(scene.get()); |
|---|
| 49 | osg::EllipsoidModel* em = csn ? csn->getEllipsoidModel() : 0; |
|---|
| 50 | |
|---|
| 51 | osg::BoundingSphere bs = scene->getBound(); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | bool useIntersectorGroup = true; |
|---|
| 55 | bool useLineOfSight = true; |
|---|
| 56 | |
|---|
| 57 | if (useLineOfSight) |
|---|
| 58 | { |
|---|
| 59 | |
|---|
| 60 | osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0); |
|---|
| 61 | osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0); |
|---|
| 62 | osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01); |
|---|
| 63 | osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0); |
|---|
| 64 | unsigned int numRows = 20; |
|---|
| 65 | unsigned int numColumns = 20; |
|---|
| 66 | |
|---|
| 67 | osgSim::LineOfSight los; |
|---|
| 68 | |
|---|
| 69 | #if 0 |
|---|
| 70 | osgSim::HeightAboveTerrain hat; |
|---|
| 71 | hat.setDatabaseCacheReadCallback(los.getDatabaseCacheReadCallback()); |
|---|
| 72 | |
|---|
| 73 | for(unsigned int r=0; r<numRows; ++r) |
|---|
| 74 | { |
|---|
| 75 | for(unsigned int c=0; c<numColumns; ++c) |
|---|
| 76 | { |
|---|
| 77 | osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r); |
|---|
| 78 | osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r); |
|---|
| 79 | los.addLOS(s,e); |
|---|
| 80 | hat.addPoint(s); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | { |
|---|
| 86 | std::cout<<"Computing LineOfSight"<<std::endl; |
|---|
| 87 | |
|---|
| 88 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 89 | |
|---|
| 90 | los.computeIntersections(scene.get()); |
|---|
| 91 | |
|---|
| 92 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 93 | |
|---|
| 94 | std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl; |
|---|
| 95 | |
|---|
| 96 | for(unsigned int i=0; i<los.getNumLOS(); i++) |
|---|
| 97 | { |
|---|
| 98 | const osgSim::LineOfSight::Intersections& intersections = los.getIntersections(i); |
|---|
| 99 | for(osgSim::LineOfSight::Intersections::const_iterator itr = intersections.begin(); |
|---|
| 100 | itr != intersections.end(); |
|---|
| 101 | ++itr) |
|---|
| 102 | { |
|---|
| 103 | std::cout<<" point "<<*itr<<std::endl; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | { |
|---|
| 109 | |
|---|
| 110 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 111 | |
|---|
| 112 | std::cout<<"Computing HeightAboveTerrain"<<std::endl; |
|---|
| 113 | |
|---|
| 114 | hat.computeIntersections(scene.get()); |
|---|
| 115 | |
|---|
| 116 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 117 | |
|---|
| 118 | for(unsigned int i=0; i<hat.getNumPoints(); i++) |
|---|
| 119 | { |
|---|
| 120 | std::cout<<" point = "<<hat.getPoint(i)<<" hat = "<<hat.getHeightAboveTerrain(i)<<std::endl; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl; |
|---|
| 125 | } |
|---|
| 126 | #endif |
|---|
| 127 | |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 131 | |
|---|
| 132 | std::cout<<"Computing ElevationSlice"<<std::endl; |
|---|
| 133 | osgSim::ElevationSlice es; |
|---|
| 134 | es.setDatabaseCacheReadCallback(los.getDatabaseCacheReadCallback()); |
|---|
| 135 | |
|---|
| 136 | es.setStartPoint(bs.center()+osg::Vec3d(bs.radius(),0.0,0.0) ); |
|---|
| 137 | es.setEndPoint(bs.center()+osg::Vec3d(bs.radius(),bs.radius(),bs.radius()) ); |
|---|
| 138 | |
|---|
| 139 | es.computeIntersections(scene.get()); |
|---|
| 140 | |
|---|
| 141 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 142 | |
|---|
| 143 | std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl; |
|---|
| 144 | |
|---|
| 145 | typedef osgSim::ElevationSlice::DistanceHeightList DistanceHeightList; |
|---|
| 146 | const DistanceHeightList& dhl = es.getDistanceHeightIntersections(); |
|---|
| 147 | std::cout<<"Number of intersections ="<<dhl.size()<<std::endl; |
|---|
| 148 | for(DistanceHeightList::const_iterator dhitr = dhl.begin(); |
|---|
| 149 | dhitr != dhl.end(); |
|---|
| 150 | ++dhitr) |
|---|
| 151 | { |
|---|
| 152 | std::cout.precision(10); |
|---|
| 153 | std::cout<<" "<<dhitr->first<<" "<<dhitr->second<<std::endl; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | else if (useIntersectorGroup) |
|---|
| 160 | { |
|---|
| 161 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 162 | |
|---|
| 163 | osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0); |
|---|
| 164 | osg::Vec3d end = bs.center(); |
|---|
| 165 | osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01); |
|---|
| 166 | osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0); |
|---|
| 167 | unsigned int numRows = 20; |
|---|
| 168 | unsigned int numColumns = 20; |
|---|
| 169 | |
|---|
| 170 | osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup(); |
|---|
| 171 | |
|---|
| 172 | for(unsigned int r=0; r<numRows; ++r) |
|---|
| 173 | { |
|---|
| 174 | for(unsigned int c=0; c<numColumns; ++c) |
|---|
| 175 | { |
|---|
| 176 | osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r); |
|---|
| 177 | osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r); |
|---|
| 178 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(s, e); |
|---|
| 179 | intersectorGroup->addIntersector( intersector.get() ); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | osgUtil::IntersectionVisitor intersectVisitor( intersectorGroup.get(), new MyReadCallback ); |
|---|
| 185 | scene->accept(intersectVisitor); |
|---|
| 186 | |
|---|
| 187 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 188 | |
|---|
| 189 | std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl; |
|---|
| 190 | |
|---|
| 191 | if ( intersectorGroup->containsIntersections() ) |
|---|
| 192 | { |
|---|
| 193 | std::cout<<"Found intersections "<<std::endl; |
|---|
| 194 | |
|---|
| 195 | osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors(); |
|---|
| 196 | for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin(); |
|---|
| 197 | intersector_itr != intersectors.end(); |
|---|
| 198 | ++intersector_itr) |
|---|
| 199 | { |
|---|
| 200 | osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get()); |
|---|
| 201 | if (lsi) |
|---|
| 202 | { |
|---|
| 203 | osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections(); |
|---|
| 204 | for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin(); |
|---|
| 205 | itr != intersections.end(); |
|---|
| 206 | ++itr) |
|---|
| 207 | { |
|---|
| 208 | const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr; |
|---|
| 209 | std::cout<<" ratio "<<intersection.ratio<<std::endl; |
|---|
| 210 | std::cout<<" point "<<intersection.localIntersectionPoint<<std::endl; |
|---|
| 211 | std::cout<<" normal "<<intersection.localIntersectionNormal<<std::endl; |
|---|
| 212 | std::cout<<" indices "<<intersection.indexList.size()<<std::endl; |
|---|
| 213 | std::cout<<" primitiveIndex "<<intersection.primitiveIndex<<std::endl; |
|---|
| 214 | std::cout<<std::endl; |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | } |
|---|
| 222 | else |
|---|
| 223 | { |
|---|
| 224 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 225 | |
|---|
| 226 | #if 1 |
|---|
| 227 | osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0); |
|---|
| 228 | osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0); |
|---|
| 229 | #else |
|---|
| 230 | osg::Vec3d start = bs.center() + osg::Vec3d(0.0,0.0, bs.radius()); |
|---|
| 231 | osg::Vec3d end = bs.center() - osg::Vec3d(0.0, 0.0, bs.radius()); |
|---|
| 232 | #endif |
|---|
| 233 | |
|---|
| 234 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end); |
|---|
| 235 | |
|---|
| 236 | osgUtil::IntersectionVisitor intersectVisitor( intersector.get(), new MyReadCallback ); |
|---|
| 237 | |
|---|
| 238 | scene->accept(intersectVisitor); |
|---|
| 239 | |
|---|
| 240 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 241 | |
|---|
| 242 | std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl; |
|---|
| 243 | |
|---|
| 244 | if ( intersector->containsIntersections() ) |
|---|
| 245 | { |
|---|
| 246 | osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections(); |
|---|
| 247 | for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin(); |
|---|
| 248 | itr != intersections.end(); |
|---|
| 249 | ++itr) |
|---|
| 250 | { |
|---|
| 251 | const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr; |
|---|
| 252 | std::cout<<" ratio "<<intersection.ratio<<std::endl; |
|---|
| 253 | std::cout<<" point "<<intersection.localIntersectionPoint<<std::endl; |
|---|
| 254 | std::cout<<" normal "<<intersection.localIntersectionNormal<<std::endl; |
|---|
| 255 | std::cout<<" indices "<<intersection.indexList.size()<<std::endl; |
|---|
| 256 | std::cout<<" primitiveIndex "<<intersection.primitiveIndex<<std::endl; |
|---|
| 257 | std::cout<<std::endl; |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | return 0; |
|---|
| 263 | } |
|---|