Grip-Reloaded
Grip version visualizaiton interface for DART (DynamicAnimationandRoboticsToolkits)
/home/pete/myRepos/grip2/osgGolems/osgUtils.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 OSG_UTILS_H
00051 #define OSG_UTILS_H
00052 
00053 // OpenSceneGraph includes
00054 #include <osgViewer/View>
00055 #include <osgGA/OrbitManipulator>
00056 #include <osg/io_utils>
00057 #include <osg/StateSet>
00058 #include <osg/PolygonMode>
00059 #include <osg/Material>
00060 #include <osg/BlendFunc>
00061 
00062 // Eigen includes
00063 #include <eigen3/Eigen/Core>
00064 #include <eigen3/Eigen/Geometry>
00065 
00066 // C++ Standard Library includes
00067 #include <iostream>
00068 
00073 namespace osgGolems {
00074 
00085 osgViewer::View* createView(int x, int y, int w, int h, osg::Node* scene = NULL);
00086 
00093 inline osg::Matrix eigToOsgMatrix(const Eigen::Isometry3d& tf)
00094 {
00095     osg::Matrix output;
00096     for(ushort i=0; i<4; ++i)
00097         for(ushort j=0; j<4; ++j)
00098             output(i,j) = tf(j,i);
00099     return output;
00100 }
00101 
00108 inline osg::Matrix eigToOsgMatrix(const Eigen::Isometry3f& tf)
00109 {
00110     osg::Matrix output;
00111     for(ushort i=0; i<4; ++i)
00112         for(ushort j=0; j<4; ++j)
00113             output(i,j) = tf(j,i);
00114 
00115     return output;
00116 }
00117 
00123 inline osg::Vec3 eigToOsgVec3(const Eigen::Vector3d& vec)
00124 {
00125     osg::Vec3 output;
00126     for(ushort i=0; i<3; ++i) {
00127         output[i] = vec[i];
00128     }
00129     return output;
00130 }
00131 
00138 inline void addWireFrameMode(osg::Node* node)
00139 {
00140     if(!node) {
00141         std::cerr << "Invalid node. Line " << __LINE__ << " of " << __FILE__ << std::endl;
00142         return;
00143     }
00144 
00145     osg::PolygonMode* polyModeObj;
00146 
00147     polyModeObj = dynamic_cast<osg::PolygonMode*>(node->getOrCreateStateSet()->
00148                                                   getAttribute(osg::StateAttribute::POLYGONMODE));
00149 
00150     if(!polyModeObj) {
00151         polyModeObj = new osg::PolygonMode;
00152         node->getOrCreateStateSet()->setAttribute(polyModeObj);
00153     }
00154 }
00155 
00162 inline void setWireFrameOn(osg::Node* node)
00163 {
00164     if(!node) {
00165         std::cerr << "Invalid node. Line " << __LINE__ << " of " << __FILE__ << std::endl;
00166         return;
00167     }
00168 
00169     if(!node->getStateSet()) {
00170         addWireFrameMode(node);
00171     }
00172 
00173     osg::PolygonMode* polyModeObj;
00174     polyModeObj = dynamic_cast<osg::PolygonMode*>(node->getStateSet()->
00175                                                   getAttribute(osg::StateAttribute::POLYGONMODE));
00176 
00177     if(!polyModeObj) {
00178         polyModeObj = new osg::PolygonMode;
00179         node->getStateSet()->setAttribute(polyModeObj);
00180     }
00181 
00182     polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
00183 }
00184 
00191 inline void setWireFrameOff(osg::Node* node)
00192 {
00193     if(!node) {
00194         std::cerr << "Invalid node. Line " << __LINE__ << " of " << __FILE__ << std::endl;
00195         return;
00196     }
00197 
00198     if(!node->getStateSet()) {
00199         addWireFrameMode(node);
00200     }
00201 
00202     osg::PolygonMode* polyModeObj;
00203     polyModeObj = dynamic_cast<osg::PolygonMode*>(node->getStateSet()->
00204                                                   getAttribute(osg::StateAttribute::POLYGONMODE));
00205 
00206     if(!polyModeObj) {
00207         polyModeObj = new osg::PolygonMode;
00208         node->getStateSet()->setAttribute(polyModeObj);
00209     }
00210 
00211     polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL);
00212 }
00213 
00221 inline void setTransparency(osg::Node* node, float transparencyValue)
00222 {
00223     if(!node->getOrCreateStateSet()->getAttribute(osg::StateAttribute::MATERIAL)) {
00224         node->getOrCreateStateSet()->setAttribute(new osg::Material);
00225         std::cerr << "Created new material" << std::endl;
00226     }
00227     if(!node->getOrCreateStateSet()->getAttribute(osg::StateAttribute::BLENDFUNC)) {
00228         node->getOrCreateStateSet()->setAttributeAndModes(new osg::BlendFunc);
00229         std::cerr << "Created new blendfunc" << std::endl;
00230     }
00231 
00232     node->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
00233     osg::ref_ptr<osg::Material> mat = (osg::Material*)node->getOrCreateStateSet()->getAttribute(osg::StateAttribute::MATERIAL);
00234     osg::Vec4 diffuse = mat->getDiffuse(osg::Material::FRONT_AND_BACK);
00235     std::cerr << "Diffuse: " << diffuse << std::endl;
00236     std::cerr << "Ambient: " << mat->getAmbient(osg::Material::FRONT_AND_BACK) << std::endl;
00237     std::cerr << "Emissive:" << mat->getEmission(osg::Material::FRONT_AND_BACK) << std::endl;
00238     std::cerr << "Specular:" << mat->getSpecular(osg::Material::FRONT_AND_BACK) << std::endl;
00239 
00240     diffuse.set(diffuse.r(), diffuse.g(), diffuse.b(), transparencyValue);
00241     mat->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse);
00242 //    mat->setAlpha(osg::Material::FRONT_AND_BACK, transparencyValue);
00243     node->getOrCreateStateSet()->setAttributeAndModes(mat, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
00244 }
00245 
00246 } // end of osgGolems namespace
00247 
00248 #endif // OSG_UTILS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator