Index: experimental/distortionNG/DistortionManipulator.cpp
===================================================================
--- experimental/distortionNG/DistortionManipulator.cpp	(revision 350)
+++ experimental/distortionNG/DistortionManipulator.cpp	(revision 353)
@@ -20,4 +20,5 @@
 
 #include<osgViewer/Viewer>
+#include<osg/Point>
 
 using namespace osg;
@@ -25,5 +26,6 @@
 
 
-DistortionManipulator::DistortionManipulator()
+DistortionManipulator::DistortionManipulator(DistortionSet* ds)
+ : _highlightColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) ), _distortionSet( ds )
 {
 	activeSetupMode = DISABLED;
@@ -32,5 +34,10 @@
 	activeVisualizationMode = NONE;
 
-	_distortionSet = NULL;
+	_highlighter = NULL;
+	
+	createVertexHighlighter();
+
+	_camera = 0;
+	//_distortionMesh(distortionMesh)
 }
 
@@ -52,11 +59,4 @@
 bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv)
 {
-	//osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
-	//if ( viewer && !_distortionSet.isValid())
-	//{
-	//	OSG_ALWAYS<<"Fetching valid DistortionSet!"<<std::endl;
-	//	_distortionSet = viewer->getDistortionSet();
-	//}
-
 	switch(ea.getEventType())
     {
@@ -64,12 +64,37 @@
         case(osgGA::GUIEventAdapter::DRAG):
 		{
-			OSG_ALWAYS<<"DRAG!"<<std::endl;
-			break;
+			osg::notify(osg::ALWAYS)<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl;
+			osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl;
+
+			return true;	// true means event handled: not forwarded to the camera manipulator;
+
 		}
         case(osgGA::GUIEventAdapter::PUSH):
 		{
-			if(ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
-			{
-				OSG_ALWAYS<<"mouse click left!"<<std::endl;
+			OSG_ALWAYS<<"mouse click!"<<std::endl;
+			if ( activeSetupMode == MANUAL && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
+			{
+				osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);
+				if ( viewer )
+				{
+					osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl;
+					osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl;
+					osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY());
+					osgUtil::IntersectionVisitor iv( intersector.get() );
+					_distortionSet->getDistortionCamera()->accept( iv );
+			        
+					if ( intersector->containsIntersections() )
+					{
+						osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin());
+						computeSelectedVertex( result );
+					}
+				}
 			}
 			break;
@@ -92,10 +117,27 @@
 				switch(activeSetupMode)
 				{
-					case DISABLED : activeSetupMode = MANUAL; break;
-					case MANUAL : activeSetupMode = DELEGATED; break;
-					case DELEGATED : activeSetupMode = DISABLED; break;
+					case DISABLED : 
+					{
+						activeSetupMode = MANUAL;
+						OSG_ALWAYS<<"SetupMode MANUAL activated"<<std::endl;
+						_distortionSet->getDistortionInternals()->setValue(1, true); // per definition #0 = mesh, #1 = highlighter
+						break;
+					}
+					case MANUAL :
+					{
+						activeSetupMode = DELEGATED;
+						_distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter
+						OSG_ALWAYS<<"SetupMode DELEGATED activated"<<std::endl;
+						break;
+					}
+					case DELEGATED :
+					{
+						activeSetupMode = DISABLED;
+						_distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter
+						OSG_ALWAYS<<"SetupMode DISABLED activated"<<std::endl;
+						break;
+					}
 				}
 
-				OSG_ALWAYS<<"KEY_KP_4 : Setup Mode now "<<activeSetupMode<<std::endl;
 				return(true);
 			}
@@ -229,2 +271,67 @@
 			_distortionSet->setShowIntensityMapOnly(show);
 }
+
+void DistortionManipulator::createVertexHighlighter()
+{
+	osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1);
+	(*colors)[0] = _highlightColor;
+
+	_highlighter = new osg::Geometry;
+	_highlighter->setDataVariance( osg::Object::DYNAMIC );
+	_highlighter->setUseDisplayList( false );
+	_highlighter->setUseVertexBufferObjects( true );
+	_highlighter->setVertexArray( new osg::Vec3Array(1) );	// The highlighter vertex is updated by computeSelectedVertex(..)
+	_highlighter->setColorArray( colors.get() );
+	_highlighter->setColorBinding( osg::Geometry::BIND_OVERALL );
+	_highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) );
+
+	
+	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
+	geode->addDrawable( _highlighter.get() );
+	geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) );
+	geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
+	geode->setCullingActive(false);	// disable the culling for the selector, otherwise the selector is culled away on the edge.
+
+	_distortionSet->getDistortionInternals()->addChild(geode, false);
+}
+
+void DistortionManipulator::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result )
+{
+	osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() );
+	if ( !geom || geom==_highlighter )
+		return;
+
+	osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
+	osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() );
+	if ( !vertices || !selVertices )
+		return;
+	
+	OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl;
+	OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl;
+
+	osg::Vec3 point = result.getWorldIntersectPoint();
+	osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath );	// To compute the intersection vertices in world coordinates not in model coordinates
+	OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl;
+	const std::vector<unsigned int>& selIndices = result.indexList;
+	{
+		double maxRatio = 0.0;
+		int closestVertexIndex = 0;
+		for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ )	//iterate through rations and search for maxRation=nearestVertex
+		{
+			if(result.ratioList[i] > maxRatio)
+			{
+				maxRatio = result.ratioList[i];
+				closestVertexIndex = result.indexList[i];
+			}
+			OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl;
+			OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl;
+		}
+		OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl;
+		osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix;
+
+		selVertices->front() = vertex;		
+		OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl;
+	}
+	selVertices->dirty();
+	_highlighter->dirtyBound(); 
+}
