root/OpenSceneGraph/trunk/src/osgUtil/IntersectionVisitor.cpp @ 13041

Revision 13041, 14.4 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14
15#include <osgUtil/IntersectionVisitor>
16#include <osgUtil/LineSegmentIntersector>
17
18#include <osg/PagedLOD>
19#include <osg/Transform>
20#include <osg/Projection>
21#include <osg/Camera>
22#include <osg/Geode>
23#include <osg/Billboard>
24#include <osg/Geometry>
25#include <osg/Notify>
26#include <osg/io_utils>
27
28using namespace osgUtil;
29
30
31///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
32//
33//  IntersectorGroup
34//
35
36IntersectorGroup::IntersectorGroup()
37{
38}
39
40void IntersectorGroup::addIntersector(Intersector* intersector)
41{
42    _intersectors.push_back(intersector);
43}
44
45void IntersectorGroup::clear()
46{
47    _intersectors.clear();
48}
49
50Intersector* IntersectorGroup::clone(osgUtil::IntersectionVisitor& iv)
51{
52    IntersectorGroup* ig = new IntersectorGroup;
53
54    // now copy across all intersectors that arn't disabled.
55    for(Intersectors::iterator itr = _intersectors.begin();
56        itr != _intersectors.end();
57        ++itr)
58    {
59        if (!(*itr)->disabled())
60        {
61            ig->addIntersector( (*itr)->clone(iv) );
62        }
63    }
64
65    return ig;
66}
67
68bool IntersectorGroup::enter(const osg::Node& node)
69{
70    if (disabled()) return false;
71
72    bool foundIntersections = false;
73
74    for(Intersectors::iterator itr = _intersectors.begin();
75        itr != _intersectors.end();
76        ++itr)
77    {
78        if ((*itr)->disabled()) (*itr)->incrementDisabledCount();
79        else if ((*itr)->enter(node)) foundIntersections = true;
80        else (*itr)->incrementDisabledCount();
81    }
82
83    if (!foundIntersections)
84    {
85        // need to call leave to clean up the DisabledCount's.
86        leave();
87        return false;
88    }
89
90    // we have found at least one suitable intersector, so return true
91    return true;
92}
93
94void IntersectorGroup::leave()
95{
96    for(Intersectors::iterator itr = _intersectors.begin();
97        itr != _intersectors.end();
98        ++itr)
99    {
100        if ((*itr)->disabled()) (*itr)->decrementDisabledCount();
101    }
102}
103
104void IntersectorGroup::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)
105{
106    if (disabled()) return;
107
108    unsigned int numTested = 0;
109    for(Intersectors::iterator itr = _intersectors.begin();
110        itr != _intersectors.end();
111        ++itr)
112    {
113        if (!(*itr)->disabled())
114        {
115            (*itr)->intersect(iv, drawable);
116
117            ++numTested;
118        }
119    }
120
121    // OSG_NOTICE<<"Number testing "<<numTested<<std::endl;
122
123}
124
125void IntersectorGroup::reset()
126{
127    Intersector::reset();
128
129    for(Intersectors::iterator itr = _intersectors.begin();
130        itr != _intersectors.end();
131        ++itr)
132    {
133        (*itr)->reset();
134    }
135}
136
137bool IntersectorGroup::containsIntersections()
138{
139    for(Intersectors::iterator itr = _intersectors.begin();
140        itr != _intersectors.end();
141        ++itr)
142    {
143        if ((*itr)->containsIntersections()) return true;
144    }
145    return false;
146}
147
148
149///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150//
151//  IntersectionVisitor
152//
153
154IntersectionVisitor::IntersectionVisitor(Intersector* intersector, ReadCallback* readCallback)
155{
156    // override the default node visitor mode.
157    setTraversalMode(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN);
158
159    _useKdTreesWhenAvailable = true;
160    _dummyTraversal = false;
161
162    _lodSelectionMode = USE_HIGHEST_LEVEL_OF_DETAIL;
163    _eyePointDirty = true;
164
165    LineSegmentIntersector* ls = dynamic_cast<LineSegmentIntersector*>(intersector);
166    if (ls)
167    {
168        setReferenceEyePoint(ls->getStart());
169        setReferenceEyePointCoordinateFrame(ls->getCoordinateFrame());
170    }
171    else
172    {
173        setReferenceEyePoint(osg::Vec3(0.0f,0.0f,0.0f));
174        setReferenceEyePointCoordinateFrame(Intersector::VIEW);
175    }
176
177    setIntersector(intersector);
178
179    setReadCallback(readCallback);
180}
181
182void IntersectionVisitor::setIntersector(Intersector* intersector)
183{
184    // keep reference around just in case intersector is already in the _intersectorStack, otherwise the clear could delete it.
185    osg::ref_ptr<Intersector> temp = intersector;
186
187    _intersectorStack.clear();
188
189    if (intersector) _intersectorStack.push_back(intersector);
190}
191
192void IntersectionVisitor::reset()
193{
194    if (!_intersectorStack.empty())
195    {
196        osg::ref_ptr<Intersector> intersector = _intersectorStack.front();
197        intersector->reset();
198
199        _intersectorStack.clear();
200        _intersectorStack.push_back(intersector);
201    }
202}
203
204void IntersectionVisitor::apply(osg::Node& node)
205{
206    // OSG_NOTICE<<"apply(Node&)"<<std::endl;
207
208    if (!enter(node)) return;
209
210    // OSG_NOTICE<<"inside apply(Node&)"<<std::endl;
211
212    traverse(node);
213
214    leave();
215}
216
217void IntersectionVisitor::apply(osg::Group& group)
218{
219    if (!enter(group)) return;
220
221    traverse(group);
222
223    leave();
224}
225
226void IntersectionVisitor::apply(osg::Geode& geode)
227{
228    // OSG_NOTICE<<"apply(Geode&)"<<std::endl;
229
230    if (!enter(geode)) return;
231
232    // OSG_NOTICE<<"inside apply(Geode&)"<<std::endl;
233
234    for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
235    {
236        intersect( geode.getDrawable(i) );
237    }
238
239    leave();
240}
241
242void IntersectionVisitor::apply(osg::Billboard& billboard)
243{
244    if (!enter(billboard)) return;
245
246#if 1
247    // IntersectVisitor doesn't have getEyeLocal(), can we use NodeVisitor::getEyePoint()?
248    osg::Vec3 eye_local = getEyePoint();
249
250    for(unsigned int i = 0; i < billboard.getNumDrawables(); i++ )
251    {
252        const osg::Vec3& pos = billboard.getPosition(i);
253        osg::ref_ptr<osg::RefMatrix> billboard_matrix = new osg::RefMatrix;
254        if (getViewMatrix())
255        {
256            if (getModelMatrix()) billboard_matrix->mult( *getModelMatrix(), *getViewMatrix() );
257            else billboard_matrix->set( *getViewMatrix() );
258        }
259        else if (getModelMatrix()) billboard_matrix->set( *getModelMatrix() );
260
261        billboard.computeMatrix(*billboard_matrix,eye_local,pos);
262
263        if (getViewMatrix()) billboard_matrix->postMult( osg::Matrix::inverse(*getViewMatrix()) );
264        pushModelMatrix(billboard_matrix.get());
265
266        // now push an new intersector clone transform to the new local coordinates
267        push_clone();
268
269        intersect( billboard.getDrawable(i) );
270
271        // now push an new intersector clone transform to the new local coordinates
272        pop_clone();
273
274        popModelMatrix();
275
276    }
277#else
278
279    for(unsigned int i=0; i<billboard.getNumDrawables(); ++i)
280    {
281        intersect( billboard.getDrawable(i) );
282    }
283#endif
284
285    leave();
286}
287
288void IntersectionVisitor::apply(osg::LOD& lod)
289{
290    if (!enter(lod)) return;
291
292    traverse(lod);
293
294    leave();
295}
296
297
298void IntersectionVisitor::apply(osg::PagedLOD& plod)
299{
300    if (!enter(plod)) return;
301
302    if (plod.getNumFileNames()>0)
303    {
304#if 1
305        // Identify the range value for the highest res child
306        float targetRangeValue;
307        if( plod.getRangeMode() == osg::LOD::DISTANCE_FROM_EYE_POINT )
308            targetRangeValue = 1e6; // Init high to find min value
309        else
310            targetRangeValue = 0; // Init low to find max value
311
312        const osg::LOD::RangeList rl = plod.getRangeList();
313        osg::LOD::RangeList::const_iterator rit;
314        for( rit = rl.begin();
315             rit != rl.end();
316             rit++ )
317        {
318            if( plod.getRangeMode() == osg::LOD::DISTANCE_FROM_EYE_POINT )
319            {
320                if( rit->first < targetRangeValue )
321                    targetRangeValue = rit->first;
322            }
323            else
324            {
325                if( rit->first > targetRangeValue )
326                    targetRangeValue = rit->first;
327            }
328        }
329
330        // Perform an intersection test only on children that display
331        // at the maximum resolution.
332        unsigned int childIndex;
333        for( rit = rl.begin(), childIndex = 0;
334             rit != rl.end();
335             rit++, childIndex++ )
336        {
337            if( rit->first != targetRangeValue )
338                // This is not one of the highest res children
339                continue;
340
341            osg::ref_ptr<osg::Node> child( NULL );
342            if( plod.getNumChildren() > childIndex )
343                child = plod.getChild( childIndex );
344
345            if( (!child.valid()) && (_readCallback.valid()) )
346            {
347                // Child is NULL; attempt to load it, if we have a readCallback...
348                unsigned int validIndex( childIndex );
349                if (plod.getNumFileNames() <= childIndex)
350                    validIndex = plod.getNumFileNames()-1;
351
352                child = _readCallback->readNodeFile( plod.getDatabasePath() + plod.getFileName( validIndex ) );
353            }
354
355            if ( !child.valid() && plod.getNumChildren()>0)
356            {
357                // Child is still NULL, so just use the one at the end of the list.
358                child = plod.getChild( plod.getNumChildren()-1 );
359            }
360
361            if (child.valid())
362            {
363                child->accept(*this);
364            }
365        }
366#else
367        // older code than above block, that assumes that the PagedLOD is ordered correctly
368        // i.e. low res children first, no duplicate ranges.
369
370        osg::ref_ptr<osg::Node> highestResChild;
371
372        if (plod.getNumFileNames() != plod.getNumChildren() && _readCallback.valid())
373        {
374            highestResChild = _readCallback->readNodeFile( plod.getDatabasePath() + plod.getFileName(plod.getNumFileNames()-1) );
375        }
376
377        if ( !highestResChild.valid() && plod.getNumChildren()>0)
378        {
379            highestResChild = plod.getChild( plod.getNumChildren()-1 );
380        }
381
382        if (highestResChild.valid())
383        {
384            highestResChild->accept(*this);
385        }
386#endif
387    }
388
389    leave();
390}
391
392
393void IntersectionVisitor::apply(osg::Transform& transform)
394{
395    if (!enter(transform)) return;
396
397    osg::ref_ptr<osg::RefMatrix> matrix = _modelStack.empty() ? new osg::RefMatrix() : new osg::RefMatrix(*_modelStack.back());
398    transform.computeLocalToWorldMatrix(*matrix,this);
399
400    pushModelMatrix(matrix.get());
401
402    // now push an new intersector clone transform to the new local coordinates
403    push_clone();
404
405    traverse(transform);
406
407    // pop the clone.
408    pop_clone();
409
410    popModelMatrix();
411
412    // tidy up an cached cull variables in the current intersector.
413    leave();
414}
415
416
417void IntersectionVisitor::apply(osg::Projection& projection)
418{
419    if (!enter(projection)) return;
420
421    pushProjectionMatrix(new osg::RefMatrix(projection.getMatrix()) );
422
423    // now push an new intersector clone transform to the new local coordinates
424    push_clone();
425
426    traverse(projection);
427
428    // pop the clone.
429    pop_clone();
430
431    popProjectionMatrix();
432
433    leave();
434}
435
436
437void IntersectionVisitor::apply(osg::Camera& camera)
438{
439    // OSG_NOTICE<<"apply(Camera&)"<<std::endl;
440
441    // note, commenting out right now because default Camera setup is with the culling active.  Should this be changed?
442    // if (!enter(camera)) return;
443
444    // OSG_NOTICE<<"inside apply(Camera&)"<<std::endl;
445
446    osg::RefMatrix* projection = NULL;
447    osg::RefMatrix* view = NULL;
448    osg::RefMatrix* model = NULL;
449
450    if (camera.getReferenceFrame()==osg::Transform::RELATIVE_RF && getProjectionMatrix() && getViewMatrix())
451    {
452        if (camera.getTransformOrder()==osg::Camera::POST_MULTIPLY)
453        {
454            projection = new osg::RefMatrix(*getProjectionMatrix()*camera.getProjectionMatrix());
455            view = new osg::RefMatrix(*getViewMatrix()*camera.getViewMatrix());
456            model = new osg::RefMatrix(*getModelMatrix());
457        }
458        else // pre multiply
459        {
460            projection = new osg::RefMatrix(camera.getProjectionMatrix()*(*getProjectionMatrix()));
461            view = new osg::RefMatrix(*getViewMatrix());
462            model = new osg::RefMatrix(camera.getViewMatrix()*(*getModelMatrix()));
463        }
464    }
465    else
466    {
467        // an absolute reference frame
468        projection = new osg::RefMatrix(camera.getProjectionMatrix());
469        view = new osg::RefMatrix(camera.getViewMatrix());
470        model =  new osg::RefMatrix();
471    }
472
473    if (camera.getViewport()) pushWindowMatrix( camera.getViewport() );
474    pushProjectionMatrix(projection);
475    pushViewMatrix(view);
476    pushModelMatrix(model);
477
478    // now push an new intersector clone transform to the new local coordinates
479    push_clone();
480
481    traverse(camera);
482
483    // pop the clone.
484    pop_clone();
485
486    popModelMatrix();
487    popViewMatrix();
488    popProjectionMatrix();
489    if (camera.getViewport()) popWindowMatrix();
490
491    // leave();
492}
493
494osg::Vec3 IntersectionVisitor::getEyePoint() const
495{
496    if (!_eyePointDirty) return _eyePoint;
497
498    osg::Matrix matrix;
499    switch (_referenceEyePointCoordinateFrame)
500    {
501        case(Intersector::WINDOW):
502            if (getWindowMatrix()) matrix.preMult( *getWindowMatrix() );
503            if (getProjectionMatrix()) matrix.preMult( *getProjectionMatrix() );
504            if (getViewMatrix()) matrix.preMult( *getViewMatrix() );
505            if (getModelMatrix()) matrix.preMult( *getModelMatrix() );
506            break;
507        case(Intersector::PROJECTION):
508            if (getProjectionMatrix()) matrix.preMult( *getProjectionMatrix() );
509            if (getViewMatrix()) matrix.preMult( *getViewMatrix() );
510            if (getModelMatrix()) matrix.preMult( *getModelMatrix() );
511            break;
512        case(Intersector::VIEW):
513            if (getViewMatrix()) matrix.preMult( *getViewMatrix() );
514            if (getModelMatrix()) matrix.preMult( *getModelMatrix() );
515            break;
516        case(Intersector::MODEL):
517            if (getModelMatrix()) matrix = *getModelMatrix();
518            break;
519    }
520
521    osg::Matrix inverse;
522    inverse.invert(matrix);
523
524    _eyePoint = _referenceEyePoint * inverse;
525    _eyePointDirty = false;
526
527    return _eyePoint;
528}
529
530float IntersectionVisitor::getDistanceToEyePoint(const osg::Vec3& pos, bool /*withLODScale*/) const
531{
532    if (_lodSelectionMode==USE_EYE_POINT_FOR_LOD_LEVEL_SELECTION)
533    {
534        return (pos-getEyePoint()).length();
535    }
536    else
537    {
538        return 0.0f;
539    }
540}
Note: See TracBrowser for help on using the browser.