Grip-Reloaded
Grip version visualizaiton interface for DART (DynamicAnimationandRoboticsToolkits)
/home/pete/myRepos/grip2/osgGolems/Line.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2014, Georgia Tech Research Corporation
00003  * All rights reserved.
00004  *
00005  * Author: Pete Vieira <pete.vieira@gatech.edu>
00006  * Date: Feb 2014
00007  *
00008  * Humanoid Robotics Lab      Georgia Institute of Technology
00009  * Director: Mike Stilman     http://www.golems.org
00010  *
00011  *
00012  * This file is provided under the following "BSD-style" License:
00013  *   Redistribution and use in source and binary forms, with or
00014  *   without modification, are permitted provided that the following
00015  *   conditions are met:
00016  *
00017  *   * Redistributions of source code must retain the above copyright
00018  *     notice, this list of conditions and the following disclaimer.
00019  *
00020  *   * Redistributions in binary form must reproduce the above
00021  *     copyright notice, this list of conditions and the following
00022  *     disclaimer in the documentation and/or other materials provided
00023  *     with the distribution.
00024  *
00025  *   * Neither the name of the Humanoid Robotics Lab nor the names of
00026  *     its contributors may be used to endorse or promote products
00027  *     derived from this software without specific prior written
00028  *     permission
00029  *
00030  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
00031  *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
00032  *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00033  *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00034  *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00035  *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00036  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00037  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00038  *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00039  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00040  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00041  *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00042  *   POSSIBILITY OF SUCH DAMAGE.
00043  */
00044 
00050 #ifndef LINE_H
00051 #define LINE_H
00052 
00053 // OpenSceneGraph includes
00054 #include <osg/Geometry>
00055 #include <osg/LineWidth>
00056 
00061 namespace osgGolems {
00062 
00067 typedef enum {
00068     LINE = 0,               
00069     LINE_ENDING_WITH_ARROW, 
00070     LINE_WITH_ARROWS,       
00071     NUM_LINE_TYPES          
00072 } lineType_t;
00073 
00078 class Line : public osg::Geometry
00079 {
00080 public:
00087     inline Line(lineType_t lineType, float length = 0.5f, float width = 3) :
00088         _lineType(lineType)
00089     {
00090         switch (lineType) {
00091             case LINE_ENDING_WITH_ARROW: _verts = new osg::Vec3Array(5); break;
00092             case LINE_WITH_ARROWS: _verts = new osg::Vec3Array(8); break;
00093             case LINE:
00094             default: _verts = new osg::Vec3Array(2); break;
00095         }
00096 
00097         _color = new osg::Vec4Array;
00098         _lineWidth = new osg::LineWidth(width);
00099 
00100         this->_createLine();
00101         this->setLength(length);
00102         this->setWidth(width);
00103     }
00104 
00110     inline std::string lineTypeToString(lineType_t lineType)
00111     {
00112         switch (lineType) {
00113             case LINE: return "LINE";
00114             case LINE_ENDING_WITH_ARROW: return "LINE_ENDING_WITH_ARROW";
00115             case LINE_WITH_ARROWS: return "LINE_WITH_ARROWS";
00116             case NUM_LINE_TYPES: return "NUM_LINE_TYPES";
00117             default: return "INVALID_LINE_TYPE";
00118         }
00119     }
00120 
00126     inline void setLength(float length)
00127     {
00128         // Arrow vertices layout
00129         //   / 5      2 \
00130         // 6---0------1---3
00131         //   \ 7      4 /
00132 
00133         // Set line vertices
00134         (*_verts)[0].set(0,0,0);
00135         (*_verts)[1].set(length,0,0);
00136 
00137         float arrowWidth = 0.003 * _lineWidth->getWidth();
00138         float arrowLength = 0.015 * _lineWidth->getWidth();
00139 
00140         switch (_lineType) {
00141             case LINE_ENDING_WITH_ARROW: {
00142                 // Set end arrow vertices
00143                 (*_verts)[2].set(length,arrowWidth, 0);
00144                 (*_verts)[3].set(length + arrowLength, 0, 0);
00145                 (*_verts)[4].set(length,-arrowWidth, 0);
00146                 break;
00147             }
00148             case LINE_WITH_ARROWS: {
00149                 // Set end arrow vertices
00150                 (*_verts)[2].set(length,arrowWidth, 0);
00151                 (*_verts)[3].set(length + arrowLength, 0, 0);
00152                 (*_verts)[4].set(length,-arrowWidth, 0);
00153                 // Set start arrow vertices
00154                 (*_verts)[5].set(0,arrowWidth, 0);
00155                 (*_verts)[6].set(-arrowLength, 0, 0);
00156                 (*_verts)[7].set(0,-arrowWidth, 0);
00157                 break;
00158             }
00159         }
00160 
00161         // Move vertices to VertexBuffer
00162         setVertexArray(_verts);
00163     }
00164 
00170     inline void setWidth(float width)
00171     {
00172         _lineWidth->setWidth(width);
00173         this->getOrCreateStateSet()->setAttribute(_lineWidth);
00174     }
00175 
00182     inline void setColor(const osg::Vec4& newColor)
00183     {
00184         (*_color)[0] = newColor;
00185         this->setColorArray(_color);
00186     }
00187 
00188 protected:
00189 
00194     inline void _createLine()
00195     {
00196         osg::DrawElementsUShort* elem =
00197                 new osg::DrawElementsUShort(osg::PrimitiveSet::LINES, 0);
00198         elem->push_back(0); elem->push_back(1);
00199         this->addPrimitiveSet(elem);
00200 
00201         switch (_lineType) {
00202             case LINE_ENDING_WITH_ARROW: {
00203                 osg::DrawElementsUShort* endArrow =
00204                         new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, 0);
00205                 endArrow->push_back(2); endArrow->push_back(3); endArrow->push_back(4);
00206                 this->addPrimitiveSet(endArrow);
00207                 break;
00208             }
00209             case LINE_WITH_ARROWS: {
00210                 osg::DrawElementsUShort* endArrow =
00211                         new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, 0);
00212                 endArrow->push_back(2); endArrow->push_back(3); endArrow->push_back(4);
00213                 this->addPrimitiveSet(endArrow);
00214 
00215                 osg::DrawElementsUShort* startArrow =
00216                         new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES, 0);
00217                 startArrow->push_back(5); startArrow->push_back(6); startArrow->push_back(7);
00218                 this->addPrimitiveSet(startArrow);
00219                 break;
00220             }
00221         }
00222 
00223         _color->push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
00224         this->setColorArray(_color);
00225         this->setColorBinding(osg::Geometry::BIND_OVERALL);
00226     }
00227 
00229     osg::Vec3Array* _verts;
00230 
00232     osg::Vec4Array* _color;
00233 
00235     osg::LineWidth* _lineWidth;
00236 
00238     const lineType_t _lineType;
00239 
00240 }; // end class Line
00241 
00242 } // end namepsace osgGolems
00243 
00244 #endif // LINE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator