diff --git a/OpenRoadEd/Osg/OSGCameraControls2.cpp b/OpenRoadEd/Osg/OSGCameraControls2.cpp index 0f56fb6a55939914dcc3851526a025f19d18881f..4ccd4a438511c7cfe979714128e26f463778b4df 100644 --- a/OpenRoadEd/Osg/OSGCameraControls2.cpp +++ b/OpenRoadEd/Osg/OSGCameraControls2.cpp @@ -1,4 +1,5 @@ #include "OSGCameraControls2.h" +#include <osg/ComputeBoundsVisitor> // Constructor and Destructor OSGCameraControls2::OSGCameraControls2() @@ -47,16 +48,25 @@ void OSGCameraControls2::home(const osgGA::GUIEventAdapter& ea ,osgGA::GUIAction } void OSGCameraControls2::computeHomePosition() { + double x,y,z,width,height; + if(getNode()) { - const osg::BoundingSphere& boundingSphere=getNode()->getBound(); - - double radius = boundingSphere._radius; - if(radius < 1) radius = 100; - - // Gets the home position as a top-down view on the entire road network (using it's bounding box) - setHomePosition(boundingSphere._center+osg::Vec3( 0.0,0.0,3.5f * radius), - boundingSphere._center, + osg::ref_ptr<osg::ComputeBoundsVisitor> cbv = new osg::ComputeBoundsVisitor(); + //attach the compute bounds visitor to the node + getNode()->accept(*cbv); + osg::BoundingBox bb(cbv->getBoundingBox()); + x=(bb.xMax()+bb.xMin())/2.0; + y=(bb.yMax()+bb.yMin())/2.0; + z=(bb.zMax()+bb.zMin())/2.0; + width=bb.xMax()-bb.xMin(); + height=bb.yMax()-bb.yMin(); + + if(width < 1.0) width = 100; + if(height < 1.0) height = 100; + + setHomePosition(osg::Vec3(x,y,z)+osg::Vec3(0.0,0.0,std::max(width,height)/(2.0*tan(0.2618))), + osg::Vec3(x,y,z), osg::Vec3(0.0f,0.0f,1.0f), _autoComputeHomePosition); } diff --git a/OpenRoadEd/Osg/OSGMain.cpp b/OpenRoadEd/Osg/OSGMain.cpp index 924412916d432e6c5e89a45158bd9f94c74ee0f0..360c0523433470782f13402a4ba6fffc0e27dbce 100644 --- a/OpenRoadEd/Osg/OSGMain.cpp +++ b/OpenRoadEd/Osg/OSGMain.cpp @@ -12,6 +12,7 @@ #include <osg/MatrixTransform> #include <osg/Notify> #include <osg/Camera> +#include <osg/ComputeBoundsVisitor> #include <osgQt/GraphicsWindowQt> @@ -646,6 +647,11 @@ void OSGMain::DrawRecordHelpers() */ void OSGMain::TakeScreenshot (const std::string &filename) { + double x,y,width,height; + std::string::size_type pos; + std::string yaml_filename; + std::ofstream yaml_file; + //backup the viewer osg::Matrixd matrix = mViewer->getCameraManipulator()->getMatrix(); @@ -658,6 +664,18 @@ void OSGMain::TakeScreenshot (const std::string &filename) //get the screen size and take the screenshot mViewer->TakeScreenshot(filename); + // get the bounding sphere + this->GetBoundingBox(x,y,width,height); + yaml_filename=filename; + pos=yaml_filename.rfind('.',yaml_filename.length()); + if(pos!=std::string::npos) + yaml_filename.replace(pos+1,4,"yaml"); + yaml_file.open(yaml_filename.c_str()); + yaml_file << "road_x: " << x << std::endl; + yaml_file << "road_y: " << y << std::endl; + yaml_file << "road_yaw: 0.0" << std::endl; + yaml_file << "road_image_size: " << std::max(width,height) << std::endl; + yaml_file.close(); mViewer->frame(); @@ -918,6 +936,20 @@ void OSGMain::ShowRoad(unsigned int index) mRoadsGroup->setValue(index,true); } +void OSGMain::GetBoundingBox(double &x,double &y,double &width,double &height) +{ + //initialize the visitor, matrixtransform and geode + osg::ref_ptr<osg::ComputeBoundsVisitor> cbv = new osg::ComputeBoundsVisitor(); + //attach the compute bounds visitor to the node + this->mRoadsGroup->accept(*cbv); + osg::BoundingBox bb(cbv->getBoundingBox()); + + x=(bb.xMax()+bb.xMin())/2.0; + y=(bb.yMax()+bb.yMin())/2.0; + width=bb.xMax()-bb.xMin(); + height=bb.yMax()-bb.yMin(); +} + /** * Depending on the node type and the hierarchycal index array, find it in the structure * @param objToSelect Object to be selected diff --git a/OpenRoadEd/Osg/OSGMain.h b/OpenRoadEd/Osg/OSGMain.h index b56d06baf18e083b2cd80e1ac2e7c6d8fbfedeee..ee7f3fc6ab97b48607ce02462fe829e9540215d3 100644 --- a/OpenRoadEd/Osg/OSGMain.h +++ b/OpenRoadEd/Osg/OSGMain.h @@ -234,6 +234,7 @@ public: void HideRoad(unsigned int index); void ShowRoad(unsigned int index); + void GetBoundingBox(double &x,double &y,double &width,double &height); /** * Export all the useful geometry to a file diff --git a/OpenRoadEd/Qt/QOSGWidget.cpp b/OpenRoadEd/Qt/QOSGWidget.cpp index fd2afb018738e1675d9b43d9919cb1eb02816e98..d72ed227ce82608668efc51841d1d7706cb26ce2 100644 --- a/OpenRoadEd/Qt/QOSGWidget.cpp +++ b/OpenRoadEd/Qt/QOSGWidget.cpp @@ -47,7 +47,7 @@ QOSGWidget::QOSGWidget(OSGMain *osgMain, QWidget * parent, const char * name, Qt traits->setUndefinedScreenDetailsToDefaultScreen(); osg::GraphicsContext *gc=osg::GraphicsContext::createGraphicsContext(traits.get()); camera->setGraphicsContext(gc); - camera->setClearColor(osg::Vec4(0.0,0.0,0.0,1.0) ); + camera->setClearColor(osg::Vec4(0.0,0.0,0.0,0.0) ); camera->setViewport(new osg::Viewport(0,0,traits->width,traits->height)); GLenum buffer=gc->getTraits()->doubleBuffer?GL_BACK:GL_FRONT; camera->setDrawBuffer(buffer); diff --git a/OpenRoadEd/Qt/QOSGWidget.h b/OpenRoadEd/Qt/QOSGWidget.h index d3273653cec871dee60768badd5187e85b322250..5c19574534a0a31a637d40cbdbbbd7c4bbba889f 100644 --- a/OpenRoadEd/Qt/QOSGWidget.h +++ b/OpenRoadEd/Qt/QOSGWidget.h @@ -208,7 +208,6 @@ public: // save render buffer to file osg::ref_ptr<osg::Image> scrImage = new osg::Image; scrImage->readPixels( 0, 0, mScreenshotRequest->GetWidth(), mScreenshotRequest->GetHeight(), GL_RGB, GL_UNSIGNED_BYTE ); - mkdir("ScreenShots", 0777); osgDB::writeImageFile( *scrImage, mScreenshotRequest->GetFileName() ); // done, reset request