[346] | 1 | /* osgVisual test. distortionNG, experimental. |
---|
| 2 | * |
---|
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
| 4 | * of this software and associated documentation files (the "Software"), to deal |
---|
| 5 | * in the Software without restriction, including without limitation the rights |
---|
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
| 7 | * copies of the Software, and to permit persons to whom the Software is |
---|
| 8 | * furnished to do so, subject to the following conditions: |
---|
| 9 | * |
---|
| 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 11 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 12 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
| 13 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
| 14 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
| 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
| 16 | * THE SOFTWARE. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "DistortionManipulator.h" |
---|
| 20 | |
---|
[347] | 21 | #include<osgViewer/Viewer> |
---|
[353] | 22 | #include<osg/Point> |
---|
[347] | 23 | |
---|
[346] | 24 | using namespace osg; |
---|
| 25 | using namespace osgViewer; |
---|
| 26 | |
---|
| 27 | |
---|
[353] | 28 | DistortionManipulator::DistortionManipulator(DistortionSet* ds) |
---|
| 29 | : _highlightColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) ), _distortionSet( ds ) |
---|
[346] | 30 | { |
---|
[348] | 31 | activeSetupMode = DISABLED; |
---|
| 32 | activeDistortionMode = MESH; |
---|
| 33 | activeManualSetupMode = DISTORTION; |
---|
| 34 | activeVisualizationMode = NONE; |
---|
[349] | 35 | |
---|
[353] | 36 | _highlighter = NULL; |
---|
| 37 | |
---|
| 38 | createVertexHighlighter(); |
---|
| 39 | |
---|
| 40 | _camera = 0; |
---|
| 41 | //_distortionMesh(distortionMesh) |
---|
[346] | 42 | } |
---|
| 43 | |
---|
| 44 | DistortionManipulator::~DistortionManipulator() |
---|
| 45 | { |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void DistortionManipulator::getUsage(osg::ApplicationUsage& usage) const |
---|
| 49 | { |
---|
| 50 | usage.addKeyboardMouseBinding("Keypad 7","Show distortion mesh / intensity map / none."); |
---|
| 51 | usage.addKeyboardMouseBinding("Keypad 8","Save distortion set."); // via plugin |
---|
| 52 | usage.addKeyboardMouseBinding("Keypad 4","Toggles Setup Mode between DISABLED, MANUAL & DELEGATED."); |
---|
| 53 | usage.addKeyboardMouseBinding("Keypad 5","MANUAL Mode: Toggle between blending & distortion setup."); |
---|
| 54 | usage.addKeyboardMouseBinding("Keypad 6","MANUAL Mode: Toggle if distortion drags affect mesh or rtt texture coordinates."); // Defaults to Mesh |
---|
| 55 | usage.addKeyboardMouseBinding("Keypad 1","Reset distortion."); |
---|
| 56 | usage.addKeyboardMouseBinding("Keypad 2","Reset intensity blending."); |
---|
| 57 | } |
---|
| 58 | |
---|
[348] | 59 | bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv) |
---|
[346] | 60 | { |
---|
[347] | 61 | switch(ea.getEventType()) |
---|
[346] | 62 | { |
---|
[348] | 63 | case(osgGA::GUIEventAdapter::MOVE): break; |
---|
[346] | 64 | case(osgGA::GUIEventAdapter::DRAG): |
---|
| 65 | { |
---|
[353] | 66 | osg::notify(osg::ALWAYS)<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl; |
---|
| 67 | osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl; |
---|
| 68 | osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl; |
---|
| 69 | osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
| 70 | osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl; |
---|
| 71 | osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl; |
---|
| 72 | osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
| 73 | osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl; |
---|
| 74 | osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl; |
---|
| 75 | |
---|
| 76 | return true; // true means event handled: not forwarded to the camera manipulator; |
---|
| 77 | |
---|
[346] | 78 | } |
---|
| 79 | case(osgGA::GUIEventAdapter::PUSH): |
---|
[348] | 80 | { |
---|
[353] | 81 | OSG_ALWAYS<<"mouse click!"<<std::endl; |
---|
| 82 | if ( activeSetupMode == MANUAL && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) |
---|
[348] | 83 | { |
---|
[353] | 84 | osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa); |
---|
| 85 | if ( viewer ) |
---|
| 86 | { |
---|
| 87 | osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
| 88 | osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
| 89 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY()); |
---|
| 90 | osgUtil::IntersectionVisitor iv( intersector.get() ); |
---|
| 91 | _distortionSet->getDistortionCamera()->accept( iv ); |
---|
| 92 | |
---|
| 93 | if ( intersector->containsIntersections() ) |
---|
| 94 | { |
---|
| 95 | osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); |
---|
| 96 | computeSelectedVertex( result ); |
---|
| 97 | } |
---|
| 98 | } |
---|
[348] | 99 | } |
---|
| 100 | break; |
---|
| 101 | } |
---|
| 102 | case(osgGA::GUIEventAdapter::RELEASE): break; |
---|
[346] | 103 | case(osgGA::GUIEventAdapter::KEYDOWN): |
---|
[347] | 104 | { |
---|
| 105 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_End) // KP 1: reset distortion |
---|
| 106 | { |
---|
| 107 | resetDistortion(); |
---|
| 108 | return(true); |
---|
| 109 | } |
---|
| 110 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down) // KP 2: reset intensity map |
---|
| 111 | { |
---|
| 112 | resetIntensityMap(); |
---|
| 113 | return(true); |
---|
| 114 | } |
---|
| 115 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left) // KP 4: Toggles Setup Mode between DISABLED, MANUAL & DELEGATED. |
---|
| 116 | { |
---|
[348] | 117 | switch(activeSetupMode) |
---|
| 118 | { |
---|
[353] | 119 | case DISABLED : |
---|
| 120 | { |
---|
| 121 | activeSetupMode = MANUAL; |
---|
| 122 | OSG_ALWAYS<<"SetupMode MANUAL activated"<<std::endl; |
---|
| 123 | _distortionSet->getDistortionInternals()->setValue(1, true); // per definition #0 = mesh, #1 = highlighter |
---|
| 124 | break; |
---|
| 125 | } |
---|
| 126 | case MANUAL : |
---|
| 127 | { |
---|
| 128 | activeSetupMode = DELEGATED; |
---|
| 129 | _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter |
---|
| 130 | OSG_ALWAYS<<"SetupMode DELEGATED activated"<<std::endl; |
---|
| 131 | break; |
---|
| 132 | } |
---|
| 133 | case DELEGATED : |
---|
| 134 | { |
---|
| 135 | activeSetupMode = DISABLED; |
---|
| 136 | _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter |
---|
| 137 | OSG_ALWAYS<<"SetupMode DISABLED activated"<<std::endl; |
---|
| 138 | break; |
---|
| 139 | } |
---|
[348] | 140 | } |
---|
| 141 | |
---|
[347] | 142 | return(true); |
---|
| 143 | } |
---|
| 144 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Begin) // KP 5: MANUAL Mode: Toggle between blending & distortion setup. |
---|
| 145 | { |
---|
[348] | 146 | |
---|
| 147 | activeManualSetupMode = (activeManualSetupMode==DISTORTION?BLENDING:DISTORTION); |
---|
[349] | 148 | OSG_ALWAYS<<"KEY_KP_5 : activeManualSetupMode is now "<<activeManualSetupMode<<std::endl; |
---|
[347] | 149 | return(true); |
---|
| 150 | } |
---|
| 151 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right) // KP 6: MANUAL Mode: Toggle if distortion drags affect mesh or rtt texture coordinates -> defaults to Mesh |
---|
| 152 | { |
---|
[348] | 153 | activeDistortionMode = (activeDistortionMode==MESH?TEXCOORDINATES:MESH); |
---|
[349] | 154 | OSG_ALWAYS<<"KEY_KP_6 : activeDistortionMode is now "<<activeDistortionMode<<std::endl; |
---|
[347] | 155 | return(true); |
---|
| 156 | } |
---|
| 157 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home) // KP 7: Show distortion mesh / intensity map / none. |
---|
| 158 | { |
---|
[348] | 159 | switch(activeVisualizationMode) |
---|
| 160 | { |
---|
[349] | 161 | case DISTORTION_MESH : |
---|
| 162 | { |
---|
| 163 | activeVisualizationMode = INTENSITY_MAP; |
---|
| 164 | showDistortionMesh(true); |
---|
| 165 | showIntensityMap(false); |
---|
| 166 | break; |
---|
| 167 | } |
---|
| 168 | case INTENSITY_MAP : |
---|
| 169 | { |
---|
| 170 | activeVisualizationMode = NONE; |
---|
| 171 | showDistortionMesh(false); |
---|
| 172 | showIntensityMap(true); |
---|
| 173 | break; |
---|
| 174 | } |
---|
| 175 | case NONE : |
---|
| 176 | { |
---|
| 177 | activeVisualizationMode = DISTORTION_MESH; |
---|
| 178 | showDistortionMesh(false); |
---|
| 179 | showIntensityMap(false); |
---|
| 180 | break; |
---|
| 181 | } |
---|
[348] | 182 | } |
---|
[349] | 183 | |
---|
| 184 | |
---|
[350] | 185 | OSG_ALWAYS<<"KEY_KP_7 : activeVisualizationMode is now "<<activeVisualizationMode<<std::endl<<std::endl; |
---|
[347] | 186 | return(true); |
---|
| 187 | } |
---|
| 188 | if (ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up) // KP 8: Save distortion set via plugin |
---|
| 189 | { |
---|
[349] | 190 | OSG_ALWAYS<<"KEY_KP_8 : todo: Save DistortionContainer"<<std::endl; |
---|
[347] | 191 | return(true); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | return false; // Event ignored |
---|
| 196 | } |
---|
[348] | 197 | case(osgGA::GUIEventAdapter::KEYUP): break; |
---|
[346] | 198 | case(osgGA::GUIEventAdapter::FRAME): |
---|
| 199 | { |
---|
[349] | 200 | //OSG_ALWAYS<<"FRAME!"<<std::endl; |
---|
[346] | 201 | break; |
---|
| 202 | } |
---|
| 203 | case (osgGA::GUIEventAdapter::RESIZE): // todo: adapt distortion mesh to new screen size |
---|
| 204 | { |
---|
[349] | 205 | OSG_ALWAYS<<"RESIZE!"<<std::endl; |
---|
[346] | 206 | break; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | default: |
---|
| 210 | return false; |
---|
| 211 | } |
---|
| 212 | return false; |
---|
[347] | 213 | } |
---|
| 214 | |
---|
| 215 | void DistortionManipulator::resetIntensityMap() |
---|
| 216 | { |
---|
[349] | 217 | if(!_distortionSet.valid()) |
---|
| 218 | return; |
---|
| 219 | |
---|
| 220 | osg::ref_ptr<osg::Image> image = _distortionSet->getIntensityMap(); |
---|
| 221 | |
---|
| 222 | OSG_ALWAYS<<"Reseting IntensityMap Blending."<<std::endl; |
---|
| 223 | |
---|
| 224 | if (!image->isDataContiguous()) |
---|
| 225 | { |
---|
| 226 | OSG_WARN<<"Warning: DistortionManipulator does not support working with non contiguous imagery as blendmaps!"<<std::endl; |
---|
| 227 | return; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | // Fill intensity map with 255 |
---|
| 231 | unsigned char* dataPtr = image->data(); |
---|
| 232 | for(unsigned int i=0;i<image->getTotalSizeInBytes();i++) |
---|
| 233 | { |
---|
| 234 | *dataPtr++ = 255; |
---|
| 235 | } |
---|
| 236 | image->dirty(); |
---|
[347] | 237 | } |
---|
| 238 | |
---|
| 239 | void DistortionManipulator::resetDistortion() |
---|
| 240 | { |
---|
[349] | 241 | if(!_distortionSet.valid()) |
---|
| 242 | return; |
---|
| 243 | |
---|
| 244 | OSG_ALWAYS<<"ToDo: resetDistortion()"<<std::endl; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | void DistortionManipulator::showDistortionMesh(bool show) |
---|
| 248 | { |
---|
| 249 | OSG_ALWAYS<<"ToDo: showDistortionMesh(bool) is now "<<show<<std::endl; |
---|
| 250 | |
---|
| 251 | // Todo: Stateset muss das von der MeshGeode sein. |
---|
| 252 | |
---|
| 253 | /* osg::PolygonMode* polyModeObj = dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); |
---|
| 254 | if (!polyModeObj) |
---|
| 255 | { |
---|
| 256 | polyModeObj = new osg::PolygonMode; |
---|
| 257 | _stateset->setAttribute(polyModeObj); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | if(show) |
---|
| 261 | polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); |
---|
| 262 | else |
---|
| 263 | polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::FILL);*/ |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | void DistortionManipulator::showIntensityMap(bool show) |
---|
| 267 | { |
---|
| 268 | OSG_ALWAYS<<"ToDo: showIntensityMap(bool) is now "<<show<<std::endl; |
---|
[350] | 269 | |
---|
| 270 | if(_distortionSet.valid()) |
---|
| 271 | _distortionSet->setShowIntensityMapOnly(show); |
---|
[349] | 272 | } |
---|
[353] | 273 | |
---|
| 274 | void DistortionManipulator::createVertexHighlighter() |
---|
| 275 | { |
---|
| 276 | osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1); |
---|
| 277 | (*colors)[0] = _highlightColor; |
---|
| 278 | |
---|
| 279 | _highlighter = new osg::Geometry; |
---|
| 280 | _highlighter->setDataVariance( osg::Object::DYNAMIC ); |
---|
| 281 | _highlighter->setUseDisplayList( false ); |
---|
| 282 | _highlighter->setUseVertexBufferObjects( true ); |
---|
| 283 | _highlighter->setVertexArray( new osg::Vec3Array(1) ); // The highlighter vertex is updated by computeSelectedVertex(..) |
---|
| 284 | _highlighter->setColorArray( colors.get() ); |
---|
| 285 | _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); |
---|
| 286 | _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
---|
| 290 | geode->addDrawable( _highlighter.get() ); |
---|
| 291 | geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) ); |
---|
| 292 | geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
| 293 | geode->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge. |
---|
| 294 | |
---|
| 295 | _distortionSet->getDistortionInternals()->addChild(geode, false); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | void DistortionManipulator::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) |
---|
| 299 | { |
---|
| 300 | osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); |
---|
| 301 | if ( !geom || geom==_highlighter ) |
---|
| 302 | return; |
---|
| 303 | |
---|
| 304 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); |
---|
| 305 | osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); |
---|
| 306 | if ( !vertices || !selVertices ) |
---|
| 307 | return; |
---|
| 308 | |
---|
| 309 | OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl; |
---|
| 310 | OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl; |
---|
| 311 | |
---|
| 312 | osg::Vec3 point = result.getWorldIntersectPoint(); |
---|
| 313 | osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates |
---|
| 314 | OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl; |
---|
| 315 | const std::vector<unsigned int>& selIndices = result.indexList; |
---|
| 316 | { |
---|
| 317 | double maxRatio = 0.0; |
---|
| 318 | int closestVertexIndex = 0; |
---|
| 319 | for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex |
---|
| 320 | { |
---|
| 321 | if(result.ratioList[i] > maxRatio) |
---|
| 322 | { |
---|
| 323 | maxRatio = result.ratioList[i]; |
---|
| 324 | closestVertexIndex = result.indexList[i]; |
---|
| 325 | } |
---|
| 326 | OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl; |
---|
| 327 | OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl; |
---|
| 328 | } |
---|
| 329 | OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl; |
---|
| 330 | osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; |
---|
| 331 | |
---|
| 332 | selVertices->front() = vertex; |
---|
| 333 | OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl; |
---|
| 334 | } |
---|
| 335 | selVertices->dirty(); |
---|
| 336 | _highlighter->dirtyBound(); |
---|
| 337 | } |
---|