ColUtils.h

00001 
00002 //---------------------------------------------------------------------------
00003 //                              ColUtils
00004 //---------------------------------------------------------------------------
00005 //  Copyright (C): Gabriel Zachmann
00006 //---------------------------------------------------------------------------
00007 
00008 
00009 #ifndef ColUtils_H
00010 #define ColUtils_H
00011 #if defined(__sgi) || defined(_WIN32)
00012 #pragma once
00013 #endif
00014 
00015 //---------------------------------------------------------------------------
00016 //  Includes
00017 //---------------------------------------------------------------------------
00018 
00019 #include <math.h>
00020 
00021 #include <OpenSG/OSGConfig.h>
00022 #include <OpenSG/OSGBaseTypes.h>
00023 #include <OpenSG/OSGBaseFunctions.h>
00024 #include <OpenSG/OSGVector.h>
00025 #include <OpenSG/OSGMatrix.h>
00026 #include <OpenSG/OSGGeoProperty.h>
00027 #include <OpenSG/OSGQuaternion.h>
00028 
00029 #include <Collision.h>
00030 #include <col_import_export.h>
00031 
00032 
00033 using osg::Vec3f;
00034 using osg::Vec4f;
00035 using osg::Pnt3f;
00036 using osg::MFPnt3f;
00037 using osg::NodePtr;
00038 using osg::GeometryPtr;
00039 
00040 using namespace std;
00041 
00042 using namespace std;
00043 namespace col {
00044 
00045 //---------------------------------------------------------------------------
00046 //  Constants
00047 //---------------------------------------------------------------------------
00048 
00050 const float NearZero = 1E-6;
00051 
00052 //---------------------------------------------------------------------------
00053 //  Forward celarations
00054 //---------------------------------------------------------------------------
00055 
00056 struct TopoFace;
00057 
00058 //---------------------------------------------------------------------------
00059 //  Functions
00060 //---------------------------------------------------------------------------
00061 
00062 COL_EXPORTIMPORT
00063 float operator * ( const Vec3f& vec3, const Vec4f& vec4 );
00064 COL_EXPORTIMPORT
00065 float operator * ( const Pnt3f& pnt, const float vec[3] );
00066 COL_EXPORTIMPORT
00067 float operator * ( const Vec4f& vec4, const Pnt3f& pnt3 );
00068 COL_EXPORTIMPORT
00069 float operator * ( const Pnt3f& pnt3, const Vec3f& vec3 );
00070 COL_EXPORTIMPORT
00071 void operator += ( Vec4f &vec4, const Vec3f &vec3 );
00072 
00073 COL_EXPORTIMPORT
00074 Pnt3f lincomb( float c1, const Pnt3f& pnt1, float c2, const Pnt3f& pnt2 );
00075 
00076 COL_EXPORTIMPORT
00077 float dist  ( const Pnt3f& pnt1, const Pnt3f& pnt2 );
00078 
00079 COL_EXPORTIMPORT
00080 float dist2 ( const Pnt3f& pnt1, const Pnt3f& pnt2 );
00081 
00082 
00083 COL_EXPORTIMPORT
00084 Pnt3f barycenter    ( const Pnt3f* points, const unsigned int npoints );
00085 COL_EXPORTIMPORT
00086 Pnt3f barycenter    ( const vector<Pnt3f>& points );
00087 COL_EXPORTIMPORT
00088 Pnt3f barycenter    ( const Pnt3f* points,
00089                       const unsigned int index[], const unsigned int nindices );
00090 COL_EXPORTIMPORT
00091 Pnt3f barycenter    ( const MFPnt3f* points,
00092                       const unsigned int index[], const unsigned int nindices );
00093 COL_EXPORTIMPORT
00094 Pnt3f barycenter    ( const vector<Pnt3f> &points, const TopoFace &face );
00095 
00096 COL_EXPORTIMPORT
00097 bool  collinear     ( const Vec3f &a, const Vec3f &b );
00098 
00099 COL_EXPORTIMPORT
00100 bool  coplanar      ( const Pnt3f& p0, const Pnt3f& p1, const Pnt3f& p2,
00101                       const Pnt3f& q0, const Pnt3f& q1, const Pnt3f& q2 );
00102 
00103 COL_EXPORTIMPORT
00104 Vec3f triangleNormal( const Pnt3f& p0, const Pnt3f& p1, const Pnt3f& p2 );
00105 
00106 COL_EXPORTIMPORT
00107 void dominantIndices( const Vec3f& v, unsigned int* x, unsigned int* y );
00108 
00109 COL_EXPORTIMPORT
00110 void dominantIndices( const Vec3f& v,
00111                       unsigned int* x, unsigned int* y, unsigned int * z );
00112 
00113 COL_EXPORTIMPORT
00114 unsigned int dominantIndex( const Vec3f &v );
00115 
00116 COL_EXPORTIMPORT
00117 Vec3f operator *	( const osg::Matrix &m, const Vec3f &v );
00118 COL_EXPORTIMPORT
00119 Pnt3f operator *	( const osg::Matrix &m, const Pnt3f &p );
00120 COL_EXPORTIMPORT
00121 Vec3f mulMTVec      ( const osg::Matrix &m, const Vec3f &v );
00122 COL_EXPORTIMPORT
00123 Pnt3f mulM3Pnt      ( const osg::Matrix &m, const Pnt3f &p );
00124 
00125 COL_EXPORTIMPORT
00126 osg::Matrix operator * ( const osg::Matrix & m1, const osg::Matrix & m2 );
00127 
00128 COL_EXPORTIMPORT
00129 osg::Matrix axisToMat( const Vec3f & a, float d );
00130 
00131 COL_EXPORTIMPORT
00132 unsigned int discretizeOri( osg::Quaternion q, unsigned int r );
00133 
00134 COL_EXPORTIMPORT
00135 void printMat( const osg::Matrix &m, FILE *file = stdout );
00136 
00137 COL_EXPORTIMPORT
00138 void printPnt( const osg::Pnt3f  &p, FILE *file = stdout );
00139 
00140 COL_EXPORTIMPORT
00141 void sortVerticesCounterClockwise( const vector<Pnt3f> &vertex,
00142                                    const Vec3f &normal,
00143                                    TopoFace &face );
00144 
00145 COL_EXPORTIMPORT
00146 osg::NodePtr geomFromPoints( const vector<Pnt3f> &vertex,
00147                              vector<TopoFace> &face,
00148                              int gl_type,
00149                              bool skip_redundant,
00150                              const Vec3f normals[] );
00151 
00152 COL_EXPORTIMPORT
00153 osg::NodePtr geomFromPoints( const Pnt3f vertex[], unsigned int nvertices,
00154                              unsigned int face[],
00155                              const unsigned int face_nv[],
00156                              unsigned int nfaces,
00157                              int gl_type,
00158                              bool skip_redundant,
00159                              const Vec3f normals[] );
00160 
00161 COL_EXPORTIMPORT
00162 osg::NodePtr makeCube( float radius, int gl_type );
00163 
00164 COL_EXPORTIMPORT
00165 void getNodeBBox( NodePtr node, float min[3], float max[3] );
00166 
00167 COL_EXPORTIMPORT
00168 GeometryPtr getGeom( const NodePtr node );
00169 
00170 COL_EXPORTIMPORT
00171 MFPnt3f*  getPoints( const NodePtr node );
00172 
00173 COL_EXPORTIMPORT
00174 MFPnt3f*  getPoints( const GeometryPtr geo );
00175 
00176 COL_EXPORTIMPORT
00177 osg::GeoPositions3fPtr getPositions( const osg::NodePtr node );
00178 
00179 COL_EXPORTIMPORT
00180 void calcVertexNormals( const osg::NodePtr node, const float creaseAngle = 90.0 );
00181 
00182 COL_EXPORTIMPORT
00183 osg::NodePtr findGeomNode( const osg::NodePtr node );
00184 COL_EXPORTIMPORT
00185 osg::MaterialPtr findMaterial( const osg::NodePtr node );
00186 
00187 COL_EXPORTIMPORT
00188 void mergeGeom( const NodePtr &subtree, NodePtr *geonode );
00189 
00190 COL_EXPORTIMPORT
00191 void mlerp( osg::Matrix* intermat, 
00192             const osg::Matrix& m1, const osg::Matrix& m2,
00193             float t );
00194 
00195 COL_EXPORTIMPORT
00196 bool lockToProcessor( unsigned int processor );
00197 
00198 COL_EXPORTIMPORT
00199 void sleep( unsigned int microseconds );
00200 
00201 COL_EXPORTIMPORT
00202 float time( void );
00203 
00204 COL_EXPORTIMPORT
00205 double my_drand48( void );
00206 
00207 COL_EXPORTIMPORT
00208 float pseudo_randomf( void );
00209 
00210 COL_EXPORTIMPORT
00211 unsigned int pseudo_random( void );
00212 
00213 
00214 class COL_EXPORTIMPORT NanoTimer
00215 {
00216 public:
00217 
00218     NanoTimer();
00219 
00220     void start( void );
00221     double elapsed( void ) const;
00222 
00223     static bool usesHighFrequ( void );
00224     static double frequ( void );
00225 
00226 private:
00227 
00228     unsigned long long int m_time_stamp;
00229 
00230     static bool     M_Use_High_Frequ;
00231     static double   M_GHz;
00232     static bool     M_FrequencyChecked;
00233 
00234     static long long unsigned int getTimeStamp( void );
00235     static void checkFrequency( void );
00236 };
00237 
00238 
00239 COL_EXPORTIMPORT
00240 unsigned int sign( float & x );
00241 COL_EXPORTIMPORT
00242 unsigned int sign( double & x );
00243 COL_EXPORTIMPORT
00244 unsigned int sign( int x );
00245 
00246 
00247 COL_EXPORTIMPORT
00248 bool isectCoplanarTriangles( const Vec3f& normalV,
00249                              const Pnt3f& polyVv0, const Pnt3f& polyVv1,
00250                              const Pnt3f& polyVv2,
00251                              const Pnt3f& polyUv0, const Pnt3f& polyUv1,
00252                              const Pnt3f& polyUv2 );
00253 
00254 COL_EXPORTIMPORT
00255 bool isectCoplanarEdges( const Pnt3f& v0V, const Pnt3f& v1V,
00256                          const Pnt3f& u0V, const Pnt3f& u1V,
00257                          unsigned int x, unsigned int y );
00258 
00259 COL_EXPORTIMPORT
00260 bool pointInPolygon( const Pnt3f& pt,
00261                      const Pnt3f* poly, unsigned int plSize,
00262                      unsigned int x, unsigned int y );
00263 
00264 COL_EXPORTIMPORT
00265 bool pointInTriangle( const Pnt3f& pt,
00266                       const Pnt3f& v0, const Pnt3f& v1, const Pnt3f& v2,
00267                       unsigned int x, unsigned int y ); 
00268 
00269 COL_EXPORTIMPORT
00270 void isectEdgePolygon( const Pnt3f& v1, const Pnt3f& v2,
00271                        const Pnt3f* poly, unsigned int plSize,
00272                        const Vec3f& normalV,
00273                        unsigned int x, unsigned int y,
00274                        bool* isect, bool* oneside );
00275 
00276 COL_EXPORTIMPORT
00277 void isectEdgeTriangle( const Pnt3f& v1, const Pnt3f& v2,
00278                         const Pnt3f* poly, const Vec3f& normalV,
00279                         unsigned int x, unsigned int y,
00280                         bool* isect, bool* oneside );
00281 
00282 
00283 COL_EXPORTIMPORT
00284 void getTransfomUpto( const osg::NodePtr &cur, const osg::NodePtr &upto, osg::Matrix &result );
00285 
00286 COL_EXPORTIMPORT
00287 void iterFaces( const osg::NodePtr &node, void (*Callback)(const osg::NodePtr &, const osg::GeometryPtr &, const osg::FaceIterator &, void *), void *data );
00288 
00289 COL_EXPORTIMPORT
00290 void countFaces( const osg::NodePtr &, const osg::GeometryPtr &, const osg::FaceIterator &, void *data );
00291 
00292 
00293 //---------------------------------------------------------------------------
00294 //  Classes
00295 //---------------------------------------------------------------------------
00296 
00297 
00298 class COL_EXPORTIMPORT FibRand
00299 {
00300 public:
00301 
00302     FibRand( int seed );
00303     unsigned int rand( void );
00304     unsigned int mrand( unsigned int m );
00305     float frand( void );
00306 
00308     static const int M_MaxRand = (1 << 30);
00309 
00310 private:
00311 
00312     void refresh(void);
00313 
00314     static const int M_HashConst = 618033988;
00315     static const int M_KK = 100;
00316     static const int M_LL = 37;
00317     static const int M_MM = M_MaxRand;
00318     static const int M_TT = 70;
00319     static const int M_BufSize = 100;
00320 
00321     int m_buf[M_BufSize];
00322     int m_current_idx;
00323 
00324 };
00325 
00326 
00327 //---------------------------------------------------------------------------
00328 //  Macros
00329 //---------------------------------------------------------------------------
00330 
00331 
00333 #define col_min(col_X1,col_X2) (((col_X1) < (col_X2)) ? (col_X1) : (col_X2))
00334 
00336 #define col_max(col_X1,col_X2) (((col_X1) > (col_X2)) ? (col_X1) : (col_X2))
00337 
00339 #define col_max3( col_X1, col_X2, col_X3 )      \
00340     col_max( col_max(col_X1,col_X2), col_X3 )
00341 
00343 #define col_abs_max3( col_X1, col_X2, col_X3 )      \
00344     col_max3( fabsf(col_X1), fabsf(col_X2), fabsf(col_X3) )
00345 
00347 #define col_max_v( col_V )                      \
00348     col_max3( (col_V)[0], (col_V)[1], (col_V)[2] )
00349 
00351 #define col_min_max(col_MIN,col_X,col_MAX)        \
00352     if ( (col_MIN) > (col_X) )                    \
00353         col_MIN = (col_X);                        \
00354     else if ( (col_MAX) < (col_X) )               \
00355         col_MAX = (col_X);
00356 
00358 #define col_clamp(col_XX, col_LOW, col_HIGH)            \
00359     (((col_XX) < (col_LOW)) ?                   \
00360          (col_LOW)                      \
00361        : (col_XX) > (col_HIGH) ?                \
00362             (col_HIGH)              \
00363           : (col_XX))
00364 
00365 //is a single value almost zero?
00366 #define col_near_zero(col_X)        \
00367     ( fabsf( col_X ) < col::NearZero )
00368 
00370 #define col_near_null( col_V )      \
00371     ( fabsf( col_V[0] ) < col::NearZero &&  \
00372       fabsf( col_V[1] ) < col::NearZero &&  \
00373       fabsf( col_V[2] ) < col::NearZero       )
00374 
00376 #define col_almost_equal( col_X, col_Y ) \
00377     ( fabsf( (col_X) - (col_Y) ) < col::NearZero )
00378 
00379 } // namespace col
00380 
00381 #endif /* ColUtils_H */
00382 
00383 

Generated on Tue Oct 16 18:12:21 2007 for CollDet by  doxygen 1.5.2