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