Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
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
00043
00044
00045 mEnd = mStart;
00046 mEnd.preMultTranslate(osg::Vec3d(range, 0, 0));
00047
00048
00049
00050 intersector->reset();
00051 intersector->setStart(mStart.getTrans());
00052 intersector->setEnd(mEnd.getTrans());
00053
00054 root->accept(intersectVisitor);
00055
00056
00057
00058
00059 if (intersector->containsIntersections())
00060 {
00061 osgUtil::LineSegmentIntersector::Intersection intersection = intersector->getFirstIntersection();
00062 osg::Vec3d worldIntPoint = intersection.getWorldIntersectPoint();
00063
00064
00065
00066 distance_to_obstacle = (worldIntPoint - mStart.getTrans()).length();
00067
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;
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