VirtualRangeSensor.h
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2013 University of Jaume-I.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Mario Prats
00010  *     Javier Perez
00011  */
00012 
00013 #ifndef VirtualRangeSensor_H_
00014 #define VirtualRangeSensor_H_
00015 
00016 #include "SimulatorConfig.h"
00017 #include "CustomWidget.h"
00018 #include "ConfigXMLParser.h"
00019 
00020 #include <osgViewer/Viewer>
00021 #include <osgGA/NodeTrackerManipulator>
00022 #include <osg/Camera>
00023 #include <osg/Texture2D>
00024 #include <osgGA/GUIEventHandler>
00025 #include <osg/Geometry>
00026 #include <osg/Geode>
00027 #include <osg/NodeTrackerCallback>
00028 #include <osgUtil/IntersectionVisitor>
00029 #include <osgUtil/LineSegmentIntersector>
00030 
00031 #include <tf/transform_datatypes.h>
00032 
00033 //Node tracker that updates the ray coordinates from the tracked node position and computes intersections
00034 class IntersectorUpdateCallback : public osg::NodeTrackerCallback
00035 {
00036   virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
00037   {
00038     osg::Matrixd mStart, mEnd;
00039     mStart = osg::computeLocalToWorld(nv->getNodePath());
00040     traverse(node, nv);
00041 
00042     //osg::Timer_t startTick = osg::Timer::instance()->tick();
00043 
00044     //update ray and compute intersections. Checks intersections along X axis of the local frame
00045     mEnd = mStart;
00046     mEnd.preMultTranslate(osg::Vec3d(range, 0, 0));
00047 
00048     //std::cerr << "mStart: " << mStart.getTrans().x() << " " << mStart.getTrans().y() << " " << mStart.getTrans().z() << std::endl;
00049     //std::cerr << "mEnd: " << mEnd.getTrans().x() << " " << mEnd.getTrans().y() << " " << mEnd.getTrans().z() << std::endl;
00050     intersector->reset();
00051     intersector->setStart(mStart.getTrans());
00052     intersector->setEnd(mEnd.getTrans());
00053 
00054     root->accept(intersectVisitor);
00055 
00056     //osg::Timer_t endTick = osg::Timer::instance()->tick();
00057     //std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
00058 
00059     if (intersector->containsIntersections())
00060     {
00061       osgUtil::LineSegmentIntersector::Intersection intersection = intersector->getFirstIntersection();
00062       osg::Vec3d worldIntPoint = intersection.getWorldIntersectPoint();
00063       //osg::Vec3d localIntPoint=intersection.getLocalIntersectPoint();
00064       //std::cerr << "Intersection point(world): " << worldIntPoint.x() << " " << worldIntPoint.y() << " " << worldIntPoint.z() << std::endl;
00065       //std::cerr << "Intersection point(local): " << localIntPoint.x() << " " << localIntPoint.y() << " " << worldIntPoint.z() << std::endl;
00066       distance_to_obstacle = (worldIntPoint - mStart.getTrans()).length();
00067       //std::cerr << "Distance to obstacle: " << distance_to_obstacle << std::endl;
00068     }
00069     else
00070       distance_to_obstacle = range;
00071   }
00072 public:
00073   double range, distance_to_obstacle;
00074   osg::ref_ptr<osg::Node> root;
00075   osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector;
00076   osgUtil::IntersectionVisitor intersectVisitor;
00077 
00078   osg::ref_ptr<osg::Geode> geode; //Geometry node that draws the beam
00079   osg::ref_ptr<osg::Geometry> beam;
00080 
00081   IntersectorUpdateCallback(double range, bool visible, osg::Node *root)
00082   {
00083     this->range = range;
00084     this->distance_to_obstacle = range;
00085     this->root = root;
00086     intersector = new osgUtil::LineSegmentIntersector(osg::Vec3d(0, 0, 0), osg::Vec3d(0, 0, 0));
00087     intersectVisitor.setIntersector(intersector.get());
00088 
00089     if (visible)
00090     {
00091       beam = osg::ref_ptr<osg::Geometry>(new osg::Geometry);
00092       osg::ref_ptr<osg::Vec3Array> points = new osg::Vec3Array;
00093       osg::Vec3d start(0, 0, 0);
00094       osg::Vec3d end(range, 0, 0);
00095       points->push_back(start);
00096       points->push_back(end);
00097       osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
00098       color->push_back(osg::Vec4(0.0, 1.0, 0.0, 0.6));
00099       beam->setVertexArray(points.get());
00100       beam->setColorArray(color.get());
00101       beam->setColorBinding(osg::Geometry::BIND_OVERALL);
00102       beam->addPrimitiveSet(new osg::DrawArrays(GL_LINES, 0, 2));
00103       geode = osg::ref_ptr<osg::Geode>(new osg::Geode());
00104       geode->addDrawable(beam.get());
00105     }
00106   }
00107 };
00108 
00110 class VirtualRangeSensor
00111 {
00112 public:
00113   std::string name, parentLinkName;
00114   osg::ref_ptr<osg::Node> trackNode;
00115   osg::ref_ptr<osg::Node> root;
00116   double range; 
00117   bool visible; 
00118   osg::ref_ptr<IntersectorUpdateCallback> node_tracker;
00119 
00120   VirtualRangeSensor(std::string name, std::string parentName, osg::Node *root, osg::Node *trackNode, double range, bool visible,unsigned int mask);
00121   VirtualRangeSensor();
00122   int getTFTransform(tf::Pose & pose, std::string & parent);
00123 
00124   virtual void init(std::string name, std::string parentName, osg::Node *root, osg::Node *trackNode, double range, bool visible, unsigned int mask);
00125 
00126 };
00127 
00128 #endif /* VirtualRangeSensor_H_ */


uwsim
Author(s): Mario Prats , Javier Perez
autogenerated on Fri Aug 28 2015 13:28:58