/** Move the camera to the default position. If autoComputeHomePosition is on, home position is computed. The computation considers camera fov and model size and positions camera far enough to fit the model to the screen. StandardManipulator implementation only updates its internal data. If home position is expected to be supported by the descendant manipulator, it has to reimplement the method to update manipulator transformation.*/ void StandardManipulator::home( const GUIEventAdapter& ea, GUIActionAdapter& us ) { if( getAutoComputeHomePosition() ) { const Camera *camera = us.asView() ? us.asView()->getCamera() : NULL; computeHomePosition( camera, ( _flags & COMPUTE_HOME_USING_BBOX ) != 0 ); } _thrown = false; setTransformation( _homeEye, _homeCenter, _homeUp ); us.requestRedraw(); us.requestContinuousUpdate( false ); flushMouseEventStack(); }
// doc in parent bool SelectionManipulator::handleMouseMove( const GUIEventAdapter& ea, GUIActionAdapter& us ) { //return true; osgViewer::View* curView = (osgViewer::View*)us.asView(); float x = ea.getX(); float y = ea.getY(); float delta = 2; osgUtil::PolytopeIntersector::Intersections intersections; osgUtil::PolytopeIntersector::Intersections::iterator hitr; osg::ref_ptr< osgUtil::PolytopeIntersector > picker = new osgUtil::PolytopeIntersector(osgUtil::Intersector::WINDOW, x-delta, y-delta, x+delta, y+delta); //picker->setDimensionMask(osgUtil::PolytopeIntersector::AllDims); osgUtil::IntersectionVisitor iv(picker.get()); curView->getCamera()->accept(iv); /*osgUtil::LineSegmentIntersector::Intersections intersections; osgUtil::LineSegmentIntersector::Intersections::iterator hitr; osg::ref_ptr< osgUtil::LineSegmentIntersector > picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, x, y); osgUtil::IntersectionVisitor iv(picker.get()); curView->getCamera()->accept(iv);*/ if (picker->containsIntersections()) { intersections = picker->getIntersections(); osgUtil::PolytopeIntersector::Intersection firstRes = ComputerRightIntersection(intersections); /*osgUtil::LineSegmentIntersector::Intersection firstRes = *(intersections.begin());*/ osg::ref_ptr<osg::Node> lastNode = firstRes.nodePath.back(); string nodeName = lastNode->getName(); if (nodeName.find("VERTEX") == std::string::npos && nodeName.find("EDGE") == std::string::npos && nodeName.find("FACE") == std::string::npos) { ClearHighLightNode(); curView->requestRedraw(); return true; } if (!m_HighlightNodeList.empty() && lastNode == m_HighlightNodeList.front()) { return true; } ClearHighLightNode(); AddToHighLightNode(lastNode.get()); } else { ClearHighLightNode(); } // 9/8/2011 mwu : curView->requestRedraw(); return true; }
bool SelectionManipulator::handleMouseDrag( const GUIEventAdapter& ea, GUIActionAdapter& us ) { m_curView = (osgViewer::View*)us.asView(); addMouseEvent( ea ); if( performMovement() ) us.requestRedraw(); us.requestContinuousUpdate( false ); _thrown = false; return true; }
/** The method sends a ray into the scene and the point of the closest intersection is used to set a new center for the manipulator. For Orbit-style manipulators, the orbiting center is set. For FirstPerson-style manipulators, view is pointed towards the center.*/ bool StandardManipulator::setCenterByMousePointerIntersection( const GUIEventAdapter& ea, GUIActionAdapter& us ) { osg::View* view = us.asView(); if( !view ) return false; Camera *camera = view->getCamera(); if( !camera ) return false; // prepare variables float x = ( ea.getX() - ea.getXmin() ) / ( ea.getXmax() - ea.getXmin() ); float y = ( ea.getY() - ea.getYmin() ) / ( ea.getYmax() - ea.getYmin() ); LineSegmentIntersector::CoordinateFrame cf; Viewport *vp = camera->getViewport(); if( vp ) { cf = Intersector::WINDOW; x *= vp->width(); y *= vp->height(); } else cf = Intersector::PROJECTION; // perform intersection computation ref_ptr< LineSegmentIntersector > picker = new LineSegmentIntersector( cf, x, y ); IntersectionVisitor iv( picker.get() ); camera->accept( iv ); // return on no intersections if( !picker->containsIntersections() ) return false; // get all intersections LineSegmentIntersector::Intersections& intersections = picker->getIntersections(); // get current transformation osg::Vec3d eye, oldCenter, up; getTransformation( eye, oldCenter, up ); // new center osg::Vec3d newCenter = (*intersections.begin()).getWorldIntersectPoint(); // make vertical axis correction if( getVerticalAxisFixed() ) { CoordinateFrame coordinateFrame = getCoordinateFrame( newCenter ); Vec3d localUp = getUpVector( coordinateFrame ); fixVerticalAxis( newCenter - eye, up, up, localUp, true ); } // set the new center setTransformation( eye, newCenter, up ); // warp pointer // note: this works for me on standard camera on GraphicsWindowEmbedded and Qt, // while it was necessary to implement requestWarpPointer like follows: // // void QOSGWidget::requestWarpPointer( float x, float y ) // { // osgViewer::Viewer::requestWarpPointer( x, y ); // QCursor::setPos( this->mapToGlobal( QPoint( int( x+.5f ), int( y+.5f ) ) ) ); // } // // Additions of .5f are just for the purpose of rounding. centerMousePointer( ea, us ); return true; }
bool SelectionManipulator::handleMouseWheel( const GUIEventAdapter& ea, GUIActionAdapter& us ) { osgGA::GUIEventAdapter::ScrollingMotion sm = ea.getScrollingMotion(); osgViewer::View* curView = (osgViewer::View*)us.asView(); //float x = ea.getX(); //float y = ea.getY(); osg::Camera* masterCam = curView->getCamera(); double left,right,bottom,top,zNear,zFar; masterCam->getProjectionMatrixAsOrtho(left,right, bottom,top,zNear,zFar); // handle centering if( _flags & SET_CENTER_ON_WHEEL_FORWARD_MOVEMENT ) { if( ((sm == GUIEventAdapter::SCROLL_DOWN && _wheelZoomFactor > 0.)) || ((sm == GUIEventAdapter::SCROLL_UP && _wheelZoomFactor < 0.)) ) { if( getAnimationTime() <= 0. ) { // center by mouse intersection (no animation) setCenterByMousePointerIntersection( ea, us ); } else { // start new animation only if there is no animation in progress if( !isAnimating() ) startAnimationByMousePointerIntersection( ea, us ); } } } switch( sm ) { // mouse scroll up event case GUIEventAdapter::SCROLL_UP: { // perform zoom float scale = 1.0f + _wheelZoomFactor; masterCam->setProjectionMatrixAsOrtho(left*scale,right*scale, bottom*scale,top*scale,zNear,zFar); us.requestRedraw(); us.requestContinuousUpdate( isAnimating() || _thrown ); return true; } // mouse scroll down event case GUIEventAdapter::SCROLL_DOWN: { // perform zoom float scale = 1.0f - _wheelZoomFactor; masterCam->setProjectionMatrixAsOrtho(left*scale,right*scale, bottom*scale,top*scale,zNear,zFar); us.requestRedraw(); us.requestContinuousUpdate( false ); return true; } // unhandled mouse scrolling motion default: return false; } }
// doc in parent bool SelectionManipulator::OldhandleMouseMove( const GUIEventAdapter& ea, GUIActionAdapter& us ) { osgViewer::View* curView = (osgViewer::View*)us.asView(); float x = ea.getX(); float y = ea.getY(); // float delta = 2; /*osgUtil::PolytopeIntersector::Intersections intersections; osgUtil::PolytopeIntersector::Intersections::iterator hitr; osg::ref_ptr< osgUtil::PolytopeIntersector > picker = new osgUtil::PolytopeIntersector(osgUtil::Intersector::WINDOW, x-delta, y-delta, x+delta, y+delta); osgUtil::IntersectionVisitor iv(picker.get()); curView->getCamera()->accept(iv); if (picker->containsIntersections())*/ osgUtil::LineSegmentIntersector::Intersections intersections; if(curView->computeIntersections(x,y,intersections)) { //intersections = picker->getIntersections(); //osgUtil::PolytopeIntersector::Intersection firstRes = *(intersections.begin()); osgUtil::LineSegmentIntersector::Intersection firstRes = *(intersections.begin()); osg::ref_ptr<osg::Node> lastNode = firstRes.nodePath.back(); string nodeName = lastNode->getName(); string typeName; string idValueStr; string::size_type idx = nodeName.find("#"); if (idx != string::npos) { typeName = nodeName.substr(0, idx); idValueStr = nodeName.substr(idx+1); } int entID = (int)atoi(idValueStr.c_str()); //ENTITY* selEntity = NULL; //api_get_entity_from_id(entID, selEntity); // const char* type_name = selEntity->type_name(); osg::ref_ptr<osg::Geode> lastGeode = lastNode->asGeode(); /*osg::StateSet* curNodeState = lastGeode->getStateSet(); osg::Material* curMat = (osg::Material*)curNodeState->getAttribute(osg::StateAttribute::MATERIAL); if (curMat != NULL) { curMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(155/255.0f,0/255.0f,0/255.0f, 1.0)); curNodeState->setAttributeAndModes(curMat, osg::StateAttribute::ON); m_HighlightNodeList.push_back(lastNode); }*/ lastNode = lastNode->getParent(0)->getParent(0); int numDraw = lastGeode->getNumDrawables(); for (int i = 0; i < numDraw; ++i) { lastGeode->getDrawable(i); } if (!m_HighlightNodeList.empty() && lastNode == m_HighlightNodeList.front()) { return true; } vector<osg::ref_ptr<osg::Node>>::iterator iter; for (iter = m_HighlightNodeList.begin(); iter != m_HighlightNodeList.end(); ++iter) { osg::ref_ptr<osg::Node> curNode = *iter; if (m_highlightFaceGroup->containsNode(curNode)) { m_highlightFaceGroup->removeChild(curNode); m_faceGroup->addChild(curNode); } else if (m_highlightEdgeGroup->containsNode(curNode)) { m_highlightEdgeGroup->removeChild(curNode); m_edgeGroup->addChild(curNode); } else if (m_highlightVertexGroup->containsNode(curNode)) { m_highlightVertexGroup->removeChild(curNode); m_vertexGroup->addChild(curNode); } } m_HighlightNodeList.clear(); if (m_faceGroup->containsNode(lastNode)) { m_faceGroup->removeChild(lastNode); m_highlightFaceGroup->addChild(lastNode); m_HighlightNodeList.push_back(lastNode); } else if (m_edgeGroup->containsNode(lastNode)) { m_edgeGroup->removeChild(lastNode); m_highlightEdgeGroup->addChild(lastNode); m_HighlightNodeList.push_back(lastNode); } else if (m_vertexGroup->containsNode(lastNode)) { m_vertexGroup->removeChild(lastNode); m_highlightVertexGroup->addChild(lastNode); m_HighlightNodeList.push_back(lastNode); } } else { vector<osg::ref_ptr<osg::Node>>::iterator iter; for (iter = m_HighlightNodeList.begin(); iter != m_HighlightNodeList.end(); ++iter) { osg::ref_ptr<osg::Node> curNode = *iter; if (m_highlightFaceGroup->containsNode(curNode)) { m_highlightFaceGroup->removeChild(curNode); m_faceGroup->addChild(curNode); } else if (m_highlightEdgeGroup->containsNode(curNode)) { m_highlightEdgeGroup->removeChild(curNode); m_edgeGroup->addChild(curNode); } else if (m_highlightVertexGroup->containsNode(curNode)) { m_highlightVertexGroup->removeChild(curNode); m_vertexGroup->addChild(curNode); } } m_HighlightNodeList.clear(); } return true; }