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 
00020 
00021 //Node tracker that updates vehicle trajectory visualization.
00022 class TrajectoryUpdateCallback : public osg::NodeTrackerCallback
00023 {
00024   virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
00025   {
00026     boost::shared_ptr<osg::Matrix> objectMat= getWorldCoords(node);
00027     osg::Matrixd  res=*objectMat * *LWMat;
00028     if (trajectory_initialized)
00029     {
00030       if ((trajectory_points->back() - res.getTrans()).length() > maxWaypointDistance)
00031       {
00032         trajectory_points->push_back(res.getTrans());
00033         trajectory->setVertexArray(trajectory_points);
00034         ((osg::DrawArrays*)prset)->setFirst(0);
00035         ((osg::DrawArrays*)prset)->setCount(trajectory_points->size());
00036         //std::cerr << "Trajectory_points size: " << trajectory_points->size() << std::endl;
00037       }
00038     }
00039     else
00040     {
00041       trajectory_points->clear();
00042       trajectory_points->push_back(res.getTrans());
00043       trajectory_initialized = true;
00044     }
00045     traverse(node, nv);
00046   }
00047 public:
00048   
00049   bool trajectory_initialized;
00050   osg::Vec3Array *trajectory_points;
00051   osg::ref_ptr<osg::Geometry> trajectory;
00052   osg::PrimitiveSet *prset;
00053   double maxWaypointDistance;
00054 
00055   osg::ref_ptr<osg::Geode> geode; //Geometry node that draws the beam
00056   boost::shared_ptr<osg::Matrix> LWMat; //LocalizedWorld Inverted Matrix ( to know distnace from it)
00057 
00058   TrajectoryUpdateCallback(double color[3], double maxWaypointDistance, int pattern, osg::Group *rootNode)
00059   {
00060     this->maxWaypointDistance=maxWaypointDistance;
00061     trajectory_initialized=false;
00062     trajectory_points = new osg::Vec3Array;
00063     trajectory_points->push_back(osg::Vec3(0, 0, 0));
00064     trajectory = osg::ref_ptr < osg::Geometry > (new osg::Geometry());
00065     trajectory->setVertexArray(trajectory_points);
00066 
00067     osg::Vec4Array* colors = new osg::Vec4Array;
00068     colors->push_back(osg::Vec4f(color[0], color[1], color[2], 1.0f));
00069     trajectory->setColorArray(colors);
00070     trajectory->setColorBinding(osg::Geometry::BIND_OVERALL);
00071     prset = new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP);
00072     trajectory->addPrimitiveSet(prset);
00073     trajectory->setUseDisplayList(false);
00074 
00075     geode = osg::ref_ptr < osg::Geode > (new osg::Geode());
00076     geode->addDrawable(trajectory);
00077     osg::LineWidth* linewidth = new osg::LineWidth();
00078     linewidth->setWidth(4.0f);
00079     geode->getOrCreateStateSet()->setAttributeAndModes(linewidth, osg::StateAttribute::ON);
00080 
00081     //stipple for dashed lines:
00082     if (pattern > 1)
00083     {
00084       osg::LineStipple* linestipple = new osg::LineStipple;
00085       linestipple->setFactor(1);
00086       if (pattern == 2)
00087         linestipple->setPattern(0xf0f0);
00088       if (pattern == 3)
00089         linestipple->setPattern(0xff00);
00090       if (pattern == 4)
00091         linestipple->setPattern(0xf000);
00092       geode->getOrCreateStateSet()->setAttributeAndModes(linestipple, osg::StateAttribute::ON);
00093     }
00094 
00095     //Attach the trajectory to a switch node son of the localizedWorld node
00096     findNodeVisitor finder("localizedWorld");
00097     rootNode->accept(finder);
00098     std::vector<osg::Node*> node_list = finder.getNodeList();
00099 
00100     osg::Switch *swNode = new osg::Switch();
00101     swNode->setNewChildDefaultValue(true);
00102     swNode->setName("switch_trajectory");
00103     node_list[0]->asGroup()->addChild(swNode);
00104 
00105 
00106 
00107     const std::string SIMULATOR_DATA_PATH = std::string(getenv("HOME")) + "/.uwsim/data";
00108     osgDB::Registry::instance()->getDataFilePathList().push_back(std::string(UWSIM_ROOT_PATH) + std::string("/data/shaders"));
00109     static const char model_vertex[] = "default_scene.vert";
00110     static const char model_fragment[] = "default_scene.frag";
00111 
00112     osg::ref_ptr < osg::Program > program = osgOcean::ShaderManager::instance().createProgram("robot_shader",
00113                                                                                               model_vertex,
00114                                                                                               model_fragment, "", "");
00115     program->addBindAttribLocation("aTangent", 6);
00116 
00117     geode->getOrCreateStateSet()->setAttributeAndModes(program, osg::StateAttribute::ON);
00118     geode->getStateSet()->addUniform(new osg::Uniform("uOverlayMap", 1));
00119     geode->getStateSet()->addUniform(new osg::Uniform("uNormalMap", 2));
00120     swNode->addChild(geode);
00121 
00122     //Save LocalizedWorld inverted matrix
00123     LWMat=getWorldCoords(findRN("localizedWorld",rootNode));
00124     LWMat->invert(*LWMat);
00125   }
00126 };
00127 
00128 #endif /* TRAJECTORYVISUALIZATION_H_ */


uwsim
Author(s): Mario Prats
autogenerated on Mon Oct 6 2014 08:24:07