SPIN Framework

include/spinUtil.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 __spinUtil_H
00043 #define __spinUtil_H
00044 
00045 #include <vector>
00046 #include <string>
00047 #include <sstream>
00048 
00049 // forward declaration
00050 namespace osg {
00051     class Object;
00052     class Quat;
00053     class Vec2f;
00054     class Vec3f;
00055 }
00056 
00057 // *****************************************************************************
00058 // GLOBAL DEFINITIONS:
00059 
00060 #define OSG_NUM_LIGHTS 8
00061 
00062 // set the following DEBUG flag to 1 for more verbose print statements:
00063 //#define DEBUG 0
00064 
00065 #define NULL_SYMBOL gensym("NULL")
00066 #define WORLD_SYMBOL gensym("world")
00067 
00068 #define SPIN_DIRECTORY spin::getAbsolutePath("~/.spinFramework")
00069 
00070 // *****************************************************************************
00071 // NODE MASKS:
00072 
00073 // We define several nodemasks that describe how scene graph traversal should be
00074 // done in different modes of operation. A nodeVisitor can be created to visit
00075 // any subset of these nodes
00076 // GEOMETRIC nodes are those that always have physical presence in the scene,
00077 // such as models or shapes. These items will be processed for collisions,
00078 // intersections, etc.
00079 #define GEOMETRIC_NODE_MASK 0x00000001
00080 
00081 // INTERACTIVE nodes are those that can be picked and drawn upon.
00082 #define INTERACTIVE_NODE_MASK 0x00000010
00083 
00084 
00085 // DEBUGVIEW nodes are those that should be visible in a viewing window, but do
00086 // not count when doing collision detection or intersection testing.
00087 #define DEBUGVIEW_NODE_MASK 0x00000100
00088 
00089 // STATSDATA nodes are those which do not need a visual representation, and so
00090 // they are culled in camera traversals. These nodes are typically used to hold
00091 // information for interaction.
00092 #define STATSDATA_NODE_MASK 0x10000000
00093 
00094 
00095 
00096 namespace spin
00097 {
00098 
00099 
00100 // *****************************************************************************
00101 // networking functions
00102 
00103 std::string getHostname();
00104 std::string getMyIPaddress();
00105 std::string getMyBroadcastAddress();
00106 bool isMulticastAddress(const std::string &s);
00107 bool isBroadcastAddress(const std::string &s);
00108 
00109 // *****************************************************************************
00110 // string handling functions
00111 
00112 template <class T> bool fromString(T& t, const std::string& s)
00113 {
00114     std::istringstream iss(s);
00115     return !(iss >> t).fail();
00116 }
00117 
00118 /*
00119 template <typename T> bool fromString(T &aValue, const std::string &aStr)
00120 {
00121     std::stringstream ss(aStr);
00122     return ss >> aValue;
00123 }
00124 */
00125 
00126 std::string stringify(float x);
00127 std::string stringify(int x);
00128 std::string stringify(osg::Vec2f v);
00129 std::string stringify(osg::Vec3f v);
00130 std::string stringify(osg::Quat q);
00131 std::string leadingSpaces(int n);
00132 
00133 
00134 std::vector<std::string> tokenize(const std::string& str, const std::string& delimiters = " ");
00135 std::vector<float> floatsFromString (std::string theString);
00136 
00137 bool wildcardMatch(const char *pat, const char *str);
00138 
00139 
00140 // *****************************************************************************
00141 // file helpers
00142 
00143 bool fileExists(const std::string& fileName);
00144 std::string getRelativePath(const std::string &path);
00145 std::string getAbsolutePath(const std::string &path);
00146 
00147 bool isVideoPath(const std::string &path);
00148 bool isImagePath(const std::string &path);
00149 bool isShaderPath(const std::string &path);
00150 
00151 std::string getSpinPath(const std::string &path);
00152 std::vector<char*> getUserArgs();
00153 
00154 // *****************************************************************************
00155 // gensym stuff (from m_pd.h)
00156 
00157 #include <stddef.h>     // just for size_t -- how lame!
00158 
00159 enum ReferencedType { REFERENCED_NODE, REFERENCED_STATESET };
00160 
00161 
00162 #define EXTERN extern
00163 #define EXTERN_STRUCT extern struct
00164 
00165 typedef struct _symbol
00166 {
00167     char *s_name;
00168     osg::Object *s_thing;
00169     struct _symbol *s_next;
00170     ReferencedType s_type;
00171 } t_symbol;
00172 
00173 typedef float t_float;
00174 typedef float t_floatarg;  
00175 
00176 #define t_class struct _class
00177 typedef t_class *t_pd; 
00178 
00179 EXTERN t_symbol *gensym(const char *s);
00180 
00181 EXTERN void *getbytes(size_t nbytes);
00182 EXTERN void *copybytes(void *src, size_t nbytes);
00183 EXTERN void freebytes(void *x, size_t nbytes);
00184 
00185 } // end of namespace spin
00186 
00187 
00188 
00189 
00190 #endif