TrajectoryVisualization.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 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 //Node tracker that updates vehicle trajectory visualization.
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         // remove points older than timeWindow [second]
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             //always removing the oldest one
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         //std::cerr << "Trajectory_points size: " << trajectory_points->size() << std::endl;
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; // vector to store time stamp of each trajectory point
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; //Geometry node that draws the beam
00082   boost::shared_ptr<osg::Matrix> LWMat; //LocalizedWorld Inverted Matrix ( to know distnace from it)
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     //stipple for dashed lines:
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     //Attach the trajectory to a switch node son of the localizedWorld node
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); //Unset shader
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     //Save LocalizedWorld inverted matrix
00144     LWMat=getWorldCoords(findRN("localizedWorld",rootNode));
00145     LWMat->invert(*LWMat);
00146 
00147     this->timeWindow=timeWindow;
00148   }
00149 };
00150 
00151 #endif /* TRAJECTORYVISUALIZATION_H_ */


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