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