source: experimental/distortionNG/distortionNG.cpp @ 347

Last change on this file since 347 was 347, checked in by Torben Dannhauer, 12 years ago
File size: 5.1 KB
Line 
1#include "distortionNG.h"
2
3#include<osg/Point>
4
5
6#include<osgViewer/Viewer>
7
8
9
10distortionNG::distortionNG()
11{
12}
13
14distortionNG::~distortionNG()
15{
16}
17
18
19
20osg::Geode* distortionHandler::createVertexHighlighter()
21{
22        osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
23        (*colors)[0] = _highlightColor;
24
25        _highlighter = new osg::Geometry;
26        _highlighter->setDataVariance( osg::Object::DYNAMIC );
27        _highlighter->setUseDisplayList( false );
28        _highlighter->setUseVertexBufferObjects( true );
29        _highlighter->setVertexArray( new osg::Vec3Array(1) );  // The highlighter vertex is updated by computeSelectedVertex(..)
30        _highlighter->setColorArray( colors.get() );
31        _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL );
32        _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) );
33
34        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
35        geode->addDrawable( _highlighter.get() );
36        geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) );
37        geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
38
39        return geode.release();
40}
41
42bool distortionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
43{
44        // Only pressing CTRL activates distortion editing
45        if ( !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) )
46                return false;   // False means not handled.
47
48
49    if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
50        {
51                osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
52                if ( viewer )
53                {
54                        osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl;
55                        osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
56                        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY());
57                        osgUtil::IntersectionVisitor iv( intersector.get() );
58                        //viewer->getCamera()->accept( iv );
59                        _camera->accept( iv );
60               
61                        if ( intersector->containsIntersections() )
62                        {
63                                osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
64                                computeSelectedVertex( result );
65                        }
66                }
67        }
68
69        if ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG  )
70        {
71                osg::notify(osg::ALWAYS)<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl;
72        osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl;
73        osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl;
74        osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl;
75        osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl;
76        osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl;
77        osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
78        osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl;
79        osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl;
80
81                return true;    // true means event handeld: not forwarded to the camera manipulator;
82        }
83
84
85    return false;       // False means not handled.
86}
87
88void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result )
89{
90        osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() );
91        if ( !geom || !_highlighter || geom==_highlighter )
92                return;
93
94        osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
95        osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() );
96        if ( !vertices || !selVertices )
97                return;
98       
99        OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl;
100        OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl;
101
102        osg::Vec3 point = result.getWorldIntersectPoint();
103        osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath );       // To compute the intersection vertices in world coordinates not in model coordinates
104        OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl;
105        const std::vector<unsigned int>& selIndices = result.indexList;
106        {
107                double maxRatio = 0.0;
108                int closestVertexIndex = 0;
109                for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex
110                {
111                        if(result.ratioList[i] > maxRatio)
112                        {
113                                maxRatio = result.ratioList[i];
114                                closestVertexIndex = result.indexList[i];
115                        }
116                        OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl;
117                        OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl;
118                }
119                OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl;
120                osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix;
121
122                selVertices->front() = vertex;         
123                OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl;
124        }
125        selVertices->dirty();
126        _highlighter->dirtyBound(); 
127}
Note: See TracBrowser for help on using the repository browser.