SPIN Framework

include/osgUtil.h

00001 // -----------------------------------------------------------------------------
00002 // |    ___  ___  _  _ _     ___                                        _      |
00003 // |   / __>| . \| || \ |   | __>_ _  ___ ._ _ _  ___  _ _ _  ___  _ _ | |__   |
00004 // |   \__ \|  _/| ||   |   | _>| '_><_> || ' ' |/ ._>| | | |/ . \| '_>| / /   |
00005 // |   <___/|_|  |_||_\_|   |_| |_|  <___||_|_|_|\___.|__/_/ \___/|_|  |_\_\   |
00006 // |                                                                           |
00007 // |---------------------------------------------------------------------------|
00008 //
00009 // http://spinframework.sourceforge.net
00010 // Copyright (C) 2009 Mike Wozniewski, Zack Settel
00011 //
00012 // Developed/Maintained by:
00013 //    Mike Wozniewski (http://www.mikewoz.com)
00014 //    Zack Settel (http://www.sheefa.net/zack)
00015 // 
00016 // Principle Partners:
00017 //    Shared Reality Lab, McGill University (http://www.cim.mcgill.ca/sre)
00018 //    La Societe des Arts Technologiques (http://www.sat.qc.ca)
00019 //
00020 // Funding by:
00021 //    NSERC/Canada Council for the Arts - New Media Initiative
00022 //    Heritage Canada
00023 //    Ministere du Developpement economique, de l'Innovation et de l'Exportation
00024 //
00025 // -----------------------------------------------------------------------------
00026 //  This file is part of the SPIN Framework.
00027 //
00028 //  SPIN Framework is free software: you can redistribute it and/or modify
00029 //  it under the terms of the GNU Lesser General Public License as published by
00030 //  the Free Software Foundation, either version 3 of the License, or
00031 //  (at your option) any later version.
00032 //
00033 //  SPIN Framework is distributed in the hope that it will be useful,
00034 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00036 //  GNU Lesser General Public License for more details.
00037 //
00038 //  You should have received a copy of the GNU Lesser General Public License
00039 //  along with SPIN Framework. If not, see <http://www.gnu.org/licenses/>.
00040 // -----------------------------------------------------------------------------
00041 
00042 #ifndef __OSGUTIL_H
00043 #define __OSGUTIL_H
00044 
00045 #include <iostream>
00046 
00047 #include <osg/Vec3>
00048 #include <osg/Quat>
00049 #include <osg/Matrix>
00050 #include <osg/PositionAttitudeTransform>
00051 #include <osg/Group>
00052 #include <osg/Geometry>
00053 #include <osg/Geode>
00054 
00055 
00056 
00057 #ifdef __APPLE__
00058     #if !defined(isnan)
00059         #include <math.h> // dont_vxl_filter: this is *not* supposed to be <cmath>
00060         #define isnan(x) __inline_isnand((double)x)
00061     #endif
00062 #endif
00063 
00064 
00065 #include "ReferencedNode.h"
00066 
00067 
00068 namespace spin
00069 {
00070 
00071 #define Vec3inDegrees(v) (osg::Vec3( osg::RadiansToDegrees(v.x()), osg::RadiansToDegrees(v.y()), osg::RadiansToDegrees(v.z()) ))
00072 
00073 #define GENERIC_SHAPE_RESOLUTION 10.0f
00074 
00075 #define AS_UNIT_SCALE  1.0f // 1m
00076 #define AS_DEBUG_SCALE 1.0f // size of debug views (radiation/sensitivity/etc)
00077 
00078 double random(double min,double max);
00079 float random(float min,float max);
00080 int random(int min,int max);
00081 osg::Vec3 randomVec3();
00082 
00083 
00088 double AngleBetweenVectors(osg::Vec3 v1, osg::Vec3 v2);
00089 
00094 double AngleBetweenVectors(osg::Vec3 v1, osg::Vec3 v2, int nullAxis);
00095 
00099 osg::Quat RotationBetweenVectors(osg::Vec3 v1, osg::Vec3 v2);
00100 
00101 
00107 osg::Vec3 cartesianToSpherical(osg::Vec3 v);
00108     
00109 osg::Vec3 rotateAroundAxis(osg::Vec3 v, osg::Vec3 axis, float angle);
00110 osg::Quat EulerToQuat(float roll, float pitch, float yaw);
00111 osg::Vec3 QuatToEuler(osg::Quat q);
00112 osg::Vec3 QuatToEuler2(osg::Quat q);
00113 
00114 //osg::Geode*     createGrid(int radius, osg::Vec4 color);
00115 osg::Geometry*    createPlane(float halfLength, osg::Vec4 color);
00116 osg::Geode*        createHollowSphere(float radius, osg::Vec4 color);
00117 osg::Geode*        createWireframeRolloff(int rolloff, float distortion, float scale, osg::Vec4 color);
00118 osg::Geode*        createHollowCone(float length, float radius, osg::Vec4 color);
00119 
00120 
00121 // can't make this work (argh!):
00122 /*
00123 class worldMatrixUpdater : public osg::NodeVisitor
00124 {
00125     public:
00126         
00127         worldMatrixUpdater() : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) {}
00128         
00129         virtual void apply(osg::PositionAttitudeTransform &node)
00130       {
00131 
00132             ss_soundNode *ourNode = dynamic_cast<ss_soundNode*>(node.getUserData());
00133             osg::Matrix mat = osg::computeWorldToLocal( getNodePath() );
00134             osg::Vec3 trans = mat.getTrans();
00135             osg::notify(osg::NOTICE) << "global position of '" << ourNode->getID() << "' is (" << trans.x() << "," << trans.y() << "," << trans.z() << ")" << std::endl;
00136             //ourNode->worldMatrix = osg::computeWorldToLocal( getNodePath() );
00137             traverse(node);
00138 
00139             
00140             //for (unsigned int i = 0; i< node.getNumChildren(); i++)
00141             //{
00142             //    osg::ref_ptr<osg::PositionAttitudeTransform> nd = dynamic_cast<osg::PositionAttitudeTransform*>( node.getChild(i) );
00143             //    if ( nd.valid() ) traverse(*(nd.get()));
00144             //}
00145             
00146         }
00147         
00148         virtual void apply(osg::Node &node) { traverse(node); }         
00149         virtual void apply(osg::Geode &node) { traverse(node); }
00150         virtual void apply(osg::Group &node) { traverse(node); }
00151         
00152 };
00153 */
00154 
00155 
00156 } // end of namespace spin
00157 
00158 #endif