Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef TRAJECTORYVISUALIZATION_H_
00014 #define TRAJECTORYVISUALIZATION_H_
00015
00016 #include "UWSimUtils.h"
00017 #include <osg/NodeTrackerCallback>
00018 #include <osg/LineStipple>
00019 #include <time.h>
00020
00021
00022
00023 class TrajectoryUpdateCallback : public osg::NodeTrackerCallback
00024 {
00025 virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
00026 {
00027 boost::shared_ptr<osg::Matrix> objectMat= getWorldCoords(node);
00028 osg::Matrixd res=*objectMat * *LWMat;
00029 if (trajectory_initialized)
00030 {
00031 time_t now=time(NULL);
00032 if(timeWindow > 0)
00033 {
00034
00035 while (points_stamps.size() > 0)
00036 {
00037 std::vector<time_t>::iterator it=points_stamps.begin();
00038 if (difftime(now,(*it)) > timeWindow)
00039 {
00040
00041 it = points_stamps.erase(it);
00042 trajectory_points->erase(trajectory_points->begin());
00043 }
00044 else
00045 {
00046 break;
00047 }
00048 }
00049 }
00050 if ((trajectory_points->back() - res.getTrans()).length() > maxWaypointDistance)
00051 {
00052 trajectory_points->push_back(res.getTrans());
00053 trajectory->setVertexArray(trajectory_points);
00054 ((osg::DrawArrays*)prset)->setFirst(0);
00055 ((osg::DrawArrays*)prset)->setCount(trajectory_points->size());
00056
00057
00058 points_stamps.push_back(now);
00059 }
00060 }
00061 else
00062 {
00063 trajectory_points->clear();
00064 trajectory_points->push_back(res.getTrans());
00065 trajectory_initialized = true;
00066 points_stamps.clear();
00067 points_stamps.push_back(std::clock());
00068 }
00069 traverse(node, nv);
00070 }
00071 public:
00072
00073 bool trajectory_initialized;
00074 osg::Vec3Array *trajectory_points;
00075 std::vector<time_t> points_stamps;
00076 osg::ref_ptr<osg::Geometry> trajectory;
00077 osg::PrimitiveSet *prset;
00078 double maxWaypointDistance;
00079 double timeWindow;
00080
00081 osg::ref_ptr<osg::Geode> geode;
00082 boost::shared_ptr<osg::Matrix> LWMat;
00083
00084 void reset()
00085 {
00086 trajectory_initialized=false;
00087
00088 }
00089
00090 TrajectoryUpdateCallback(double color[3], double maxWaypointDistance, int pattern, double timeWindow, osg::Group *rootNode, unsigned int mask)
00091 {
00092 this->maxWaypointDistance=maxWaypointDistance;
00093 trajectory_initialized=false;
00094 trajectory_points = new osg::Vec3Array;
00095 trajectory_points->push_back(osg::Vec3(0, 0, 0));
00096 trajectory = osg::ref_ptr < osg::Geometry > (new osg::Geometry());
00097 trajectory->setVertexArray(trajectory_points);
00098
00099 osg::Vec4Array* colors = new osg::Vec4Array;
00100 colors->push_back(osg::Vec4f(color[0], color[1], color[2], 1.0f));
00101 trajectory->setColorArray(colors);
00102 trajectory->setColorBinding(osg::Geometry::BIND_OVERALL);
00103 prset = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP);
00104 trajectory->addPrimitiveSet(prset);
00105 trajectory->setUseDisplayList(false);
00106
00107 geode = osg::ref_ptr < osg::Geode > (new osg::Geode());
00108 geode->addDrawable(trajectory);
00109 osg::LineWidth* linewidth = new osg::LineWidth();
00110 linewidth->setWidth(4.0f);
00111 geode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
00112
00113
00114 if (pattern > 1)
00115 {
00116 osg::LineStipple* linestipple = new osg::LineStipple;
00117 linestipple->setFactor(1);
00118 if (pattern == 2)
00119 linestipple->setPattern(0xf0f0);
00120 if (pattern == 3)
00121 linestipple->setPattern(0xff00);
00122 if (pattern == 4)
00123 linestipple->setPattern(0xf000);
00124 geode->getOrCreateStateSet()->setAttributeAndModes(linestipple, osg::StateAttribute::ON);
00125 }
00126
00127
00128 findNodeVisitor finder("localizedWorld");
00129 rootNode->accept(finder);
00130 std::vector<osg::Node*> node_list = finder.getNodeList();
00131
00132 osg::Switch *swNode = new osg::Switch();
00133 swNode->setNewChildDefaultValue(true);
00134 swNode->setName("switch_trajectory");
00135 node_list[0]->asGroup()->addChild(swNode);
00136
00137 geode->getOrCreateStateSet()->setAttributeAndModes(new osg::Program(), osg::StateAttribute::ON);
00138 geode->getStateSet()->addUniform(new osg::Uniform("uOverlayMap", 1));
00139 geode->getStateSet()->addUniform(new osg::Uniform("uNormalMap", 2));
00140 geode->setNodeMask(mask);
00141 swNode->addChild(geode);
00142
00143
00144 LWMat=getWorldCoords(findRN("localizedWorld",rootNode));
00145 LWMat->invert(*LWMat);
00146
00147 this->timeWindow=timeWindow;
00148 }
00149 };
00150
00151 #endif