Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGBaseFunctions.inl

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  *                                                                           *
00016  * This library is free software; you can redistribute it and/or modify it   *
00017  * under the terms of the GNU Library General Public License as published    *
00018  * by the Free Software Foundation, version 2.                               *
00019  *                                                                           *
00020  * This library is distributed in the hope that it will be useful, but       *
00021  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00023  * Library General Public License for more details.                          *
00024  *                                                                           *
00025  * You should have received a copy of the GNU Library General Public         *
00026  * License along with this library; if not, write to the Free Software       *
00027  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00028  *                                                                           *
00029  *                                                                           *
00030 \*---------------------------------------------------------------------------*/
00031 /*---------------------------------------------------------------------------*\
00032  *                                Changes                                    *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037  *                                                                           *
00038  *                                                                           *
00039 \*---------------------------------------------------------------------------*/
00040 
00041 #ifndef WIN32
00042 #include <unistd.h>
00043 #include <time.h>
00044 #endif
00045 
00046 OSG_BEGIN_NAMESPACE
00047 
00048 //---------------------------------------------------------------------------
00049 //   Base Math Functions
00050 //---------------------------------------------------------------------------
00051 
00052 /*---------------------------------------------------------------------*/
00059 template <class FloatTypeT> inline
00060 typename TypeTraits<FloatTypeT>::RealReturnType 
00061     osgsqrt(const FloatTypeT rValue)
00062 {
00063     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00064 
00065     return  osgsqrt(RealType(rValue));
00066 }
00067 
00071 inline
00072 Real32 osgsqrt(const Real32 rValue)
00073 {
00074 #ifdef OSG_USE_STDMATH
00075     return std::sqrt(rValue);
00076 #elif defined(OSG_HAS_FLOATMATH)
00077     return sqrtf(rValue);
00078 #else
00079     return (Real32) sqrt((Real32) rValue);
00080 #endif
00081 }
00082 
00086 inline
00087 Real64 osgsqrt(const Real64 rValue)
00088 {
00089 #ifdef OSG_USE_STDMATH
00090     return std::sqrt(rValue);
00091 #else
00092     return sqrt(rValue);
00093 #endif
00094 }
00095 
00099 inline
00100 Real128 osgsqrt(const Real128 rValue)
00101 {
00102 #ifdef OSG_USE_STDMATH
00103     return std::sqrt(rValue);
00104 #else
00105     return sqrtl(rValue);
00106 #endif
00107 }
00108 
00110 /*---------------------------------------------------------------------*/
00117 template <class FloatTypeT> inline
00118 typename TypeTraits<FloatTypeT>::RealReturnType 
00119     osgcos(const FloatTypeT rValue)
00120 {
00121     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00122 
00123     return  osgcos(RealType(rValue));
00124 }
00125 
00129 inline
00130 Real32 osgcos(const Real32 rValue)
00131 {
00132 #ifdef OSG_USE_STDMATH
00133     return std::cos(rValue);
00134 #elif defined(OSG_HAS_FLOATMATH)
00135     return cosf(rValue);
00136 #else
00137     return (Real32) cos((Real32) rValue);
00138 #endif
00139 }
00140 
00144 inline
00145 Real64 osgcos(const Real64 rValue)
00146 {
00147 #ifdef OSG_USE_STDMATH
00148     return std::cos(rValue);
00149 #else
00150     return cos(rValue);
00151 #endif
00152 }
00153 
00157 inline
00158 Real128 osgcos(const Real128 rValue)
00159 {
00160 #ifdef OSG_USE_STDMATH
00161     return std::cos(rValue);
00162 #else
00163     return cosl(rValue);
00164 #endif
00165 }
00166 
00168 /*---------------------------------------------------------------------*/
00175 template <class FloatTypeT> inline
00176 typename TypeTraits<FloatTypeT>::RealReturnType 
00177     osgsin(const FloatTypeT rValue)
00178 {
00179     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00180 
00181     return  osgsin(RealType(rValue));
00182 }
00183 
00187 inline
00188 Real32 osgsin(const Real32 rValue)
00189 {
00190 #ifdef OSG_USE_STDMATH
00191     return std::sin(rValue);
00192 #elif defined(OSG_HAS_FLOATMATH)
00193     return sinf(rValue);
00194 #else
00195     return (Real32) sin((Real32) rValue);
00196 #endif
00197 }
00198 
00202 inline
00203 Real64 osgsin(const Real64 rValue)
00204 {
00205 #ifdef OSG_USE_STDMATH
00206     return std::sin(rValue);
00207 #else
00208     return sin(rValue);
00209 #endif
00210 }
00211 
00215 inline
00216 Real128 osgsin(const Real128 rValue)
00217 {
00218 #ifdef OSG_USE_STDMATH
00219     return std::sin(rValue);
00220 #else
00221     return sinl(rValue);
00222 #endif
00223 }
00224 
00226 /*---------------------------------------------------------------------*/
00233 template <class FloatTypeT> inline
00234 typename TypeTraits<FloatTypeT>::RealReturnType
00235     osgtan(const FloatTypeT rValue)
00236 {
00237     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00238 
00239     return  osgtan(RealType(rValue));
00240 }
00241 
00245 inline
00246 Real32 osgtan(const Real32 rValue)
00247 {
00248 #ifdef OSG_USE_STDMATH
00249     return std::tan(rValue);
00250 #elif defined(OSG_HAS_FLOATMATH)
00251     return tanf(rValue);
00252 #else
00253     return (Real32) tan((Real32) rValue);
00254 #endif
00255 }
00256 
00260 inline
00261 Real64 osgtan(const Real64 rValue)
00262 {
00263 #ifdef OSG_USE_STDMATH
00264     return std::tan(rValue);
00265 #else
00266     return tan(rValue);
00267 #endif
00268 }
00269 
00273 inline
00274 Real128 osgtan(const Real128 rValue)
00275 {
00276 #ifdef OSG_USE_STDMATH
00277     return std::tan(rValue);
00278 #else
00279     return tanl(rValue);
00280 #endif
00281 }
00282 
00284 /*---------------------------------------------------------------------*/
00291 template <class FloatTypeT> inline
00292 typename TypeTraits<FloatTypeT>::RealReturnType 
00293     osgacos(const FloatTypeT rValue)
00294 {
00295     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00296 
00297     return  osgacos(RealType(rValue));
00298 }
00299 
00303 inline
00304 Real32 osgacos(const Real32 rValue)
00305 {
00306 #ifdef OSG_USE_STDMATH
00307     return std::acos(rValue);
00308 #elif defined(OSG_HAS_FLOATMATH)
00309     return acosf(rValue);
00310 #else
00311     return (Real32) acos((Real32) rValue);
00312 #endif
00313 }
00314 
00318 inline
00319 Real64 osgacos(const Real64 rValue)
00320 {
00321 #ifdef OSG_USE_STDMATH
00322     return std::acos(rValue);
00323 #else
00324     return acos(rValue);
00325 #endif
00326 }
00327 
00331 inline
00332 Real128 osgacos(const Real128 rValue)
00333 {
00334 #ifdef OSG_USE_STDMATH
00335     return std::acos(rValue);
00336 #else
00337     return acosl(rValue);
00338 #endif
00339 }
00340 
00342 /*---------------------------------------------------------------------*/
00349 template <class FloatTypeT> inline
00350 typename TypeTraits<FloatTypeT>::RealReturnType
00351     osgasin(const FloatTypeT rValue)
00352 {
00353     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00354 
00355     return  osgasin(RealType(rValue));
00356 }
00357 
00361 inline
00362 Real32 osgasin(const Real32 rValue)
00363 {
00364 #ifdef OSG_USE_STDMATH
00365     return std::asin(rValue);
00366 #elif defined(OSG_HAS_FLOATMATH)
00367     return asinf(rValue);
00368 #else
00369     return (Real32) asin((Real32) rValue);
00370 #endif
00371 }
00372 
00373 
00377 inline
00378 Real64 osgasin(const Real64 rValue)
00379 {
00380 #ifdef OSG_USE_STDMATH
00381     return std::asin(rValue);
00382 #else
00383     return asin(rValue);
00384 #endif
00385 }
00386 
00390 inline
00391 Real128 osgasin(const Real128 rValue)
00392 {
00393 #ifdef OSG_USE_STDMATH
00394     return std::asin(rValue);
00395 #else
00396     return asinl(rValue);
00397 #endif
00398 }
00399 
00401 /*---------------------------------------------------------------------*/
00408 template <class FloatTypeT> inline
00409 typename TypeTraits<FloatTypeT>::RealReturnType 
00410     osgatan(const FloatTypeT rValue)
00411 {
00412     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00413 
00414     return  osgatan(RealType(rValue));
00415 }
00416 
00420 inline
00421 Real32 osgatan(const Real32 rValue)
00422 {
00423 #ifdef OSG_USE_STDMATH
00424     return std::atan(rValue);
00425 #elif defined(OSG_HAS_FLOATMATH)
00426     return atanf(rValue);
00427 #else
00428     return (Real32) atan((Real32) rValue);
00429 #endif
00430 }
00431 
00435 inline
00436 Real64 osgatan(const Real64 rValue)
00437 {
00438 #ifdef OSG_USE_STDMATH
00439     return std::atan(rValue);
00440 #else
00441     return atan(rValue);
00442 #endif
00443 }
00444 
00448 inline
00449 Real128 osgatan(const Real128 rValue)
00450 {
00451 #ifdef OSG_USE_STDMATH
00452     return std::atan(rValue);
00453 #else
00454     return atanl(rValue);
00455 #endif
00456 }
00457 
00459 /*---------------------------------------------------------------------*/
00466 template <class FloatTypeT> inline
00467 typename TypeTraits<FloatTypeT>::RealReturnType 
00468     osgatan2(const FloatTypeT rValue1,
00469              const FloatTypeT rValue2)
00470 {
00471     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00472 
00473     return  osgatan2(RealType(rValue1), RealType(rValue2));
00474 }
00475 
00479 inline
00480 Real32 osgatan2(const Real32 rValue1, const Real32 rValue2)
00481 {
00482 #ifdef OSG_USE_STDMATH
00483     return std::atan2(rValue1, rValue2);
00484 #elif defined(OSG_HAS_FLOATMATH)
00485 #ifndef _OSG_HAS_ATANF2__
00486     return atan2f(rValue1, rValue2);
00487 #else
00488     return (Real32) atan2((Real32) rValue1, (Real32) rValue2);
00489 #endif
00490 #else
00491     return (Real32) atan2((Real32) rValue1, (Real32) rValue2);
00492 #endif
00493 }
00494 
00498 inline
00499 Real64 osgatan2(const Real64 rValue1, const Real64 rValue2)
00500 {
00501 #ifdef OSG_USE_STDMATH
00502     return std::atan2(rValue1, rValue2);
00503 #else
00504     return atan2(rValue1, rValue2);
00505 #endif
00506 }
00507 
00511 inline
00512 Real128 osgatan2(const Real128 rValue1, const Real128 rValue2)
00513 {
00514 #ifdef OSG_USE_STDMATH
00515     return std::atan2(rValue1, rValue2);
00516 #else
00517     return atan2l(rValue1, rValue2);
00518 #endif
00519 }
00520 
00522 /*---------------------------------------------------------------------*/
00529 template <class TypeT> inline
00530 TypeT osgabs(const TypeT rValue)
00531 {
00532     return (rValue > 0) ? rValue : -rValue;
00533 }
00534 
00538 inline
00539 Real32 osgabs(const Real32 rValue)
00540 {
00541 #ifdef OSG_USE_STDMATH
00542     return std::abs(rValue);
00543 #elif defined(OSG_HAS_FLOATMATH) && !defined(OSG_NO_FABSF)
00544     return fabsf(rValue);
00545 #else
00546     return (Real32) fabs((Real32) rValue);
00547 #endif
00548 }
00549 
00553 inline
00554 Real64 osgabs(const Real64 rValue)
00555 {
00556 #ifdef OSG_USE_STDMATH
00557     return std::abs(rValue);
00558 #else
00559     return fabs(rValue);
00560 #endif
00561 }
00562 
00566 inline
00567 Real128 osgabs(const Real128 rValue)
00568 {
00569 #ifdef OSG_USE_STDMATH
00570     return std::abs(rValue);
00571 #else
00572     return fabsl(rValue);
00573 #endif
00574 }
00575 
00577 /*---------------------------------------------------------------------*/
00584 template <class FloatTypeT> inline
00585 typename TypeTraits<FloatTypeT>::RealReturnType
00586     osgpow(const FloatTypeT rValue,
00587            const FloatTypeT rExp)
00588 {
00589     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00590 
00591     return  osgpow(RealType(rValue), RealType(rExp));
00592 }
00593 
00597 inline
00598 Real32 osgpow(const Real32 rValue, const Real32 rExp)
00599 {
00600 #ifdef OSG_USE_STDMATH
00601     return std::pow(rValue, rExp);
00602 #elif defined(OSG_HAS_FLOATMATH)
00603     return powf(rValue, rExp);
00604 #else
00605     return (Real32) pow((Real32) rValue, (Real32) rExp);
00606 #endif
00607 }
00608 
00612 inline
00613 Real64 osgpow(const Real64 rValue, const Real64 rExp)
00614 {
00615 #ifdef OSG_USE_STDMATH
00616     return std::pow(rValue, rExp);
00617 #else
00618     return pow(rValue, rExp);
00619 #endif
00620 }
00621 
00625 inline
00626 Real128 osgpow(const Real128 rValue, const Real128 rExp)
00627 {
00628 #ifdef OSG_USE_STDMATH
00629     return std::pow(rValue, rExp);
00630 #else
00631     return powl(rValue, rExp);
00632 #endif
00633 }
00634 
00636 /*---------------------------------------------------------------------*/
00643 template <class FloatTypeT> inline
00644 typename TypeTraits<FloatTypeT>::RealReturnType
00645     osglog(const FloatTypeT rValue)
00646 {
00647     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00648 
00649     return  osglog(RealType(rValue));
00650 }
00651 
00655 inline
00656 Real32 osglog(const Real32 rValue)
00657 {
00658 #ifdef OSG_USE_STDMATH
00659     return std::log(rValue);
00660 #elif defined(OSG_HAS_FLOATMATH)
00661     return logf(rValue);
00662 #else
00663     return (Real32) log((Real32) rValue);
00664 #endif
00665 }
00666 
00670 inline
00671 Real64 osglog(const Real64 rValue)
00672 {
00673 #ifdef OSG_USE_STDMATH
00674     return std::log(rValue);
00675 #else
00676     return log(rValue);
00677 #endif
00678 }
00679 
00683 inline
00684 Real128 osglog(const Real128 rValue)
00685 {
00686 #ifdef OSG_USE_STDMATH
00687     return std::log(rValue);
00688 #else
00689     return logl(rValue);
00690 #endif
00691 }
00692 
00694 /*---------------------------------------------------------------------*/
00701 template <class FloatTypeT> inline
00702 typename TypeTraits<FloatTypeT>::RealReturnType
00703     osgexp(const FloatTypeT rValue)
00704 {
00705     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00706 
00707     return  osgexp(RealType(rValue));
00708 }
00709 
00713 inline
00714 Real32 osgexp(const Real32 rValue)
00715 {
00716 #ifdef OSG_USE_STDMATH
00717     return std::exp(rValue);
00718 #elif defined(OSG_HAS_FLOATMATH)
00719     return expf(rValue);
00720 #else
00721     return (Real32) exp((Real32) rValue);
00722 #endif
00723 }
00724 
00728 inline
00729 Real64 osgexp(const Real64 rValue)
00730 {
00731 #ifdef OSG_USE_STDMATH
00732     return std::exp(rValue);
00733 #else
00734     return exp(rValue);
00735 #endif
00736 }
00737 
00741 inline
00742 Real128 osgexp(const Real128 rValue)
00743 {
00744 #ifdef OSG_USE_STDMATH
00745     return std::exp(rValue);
00746 #else
00747     return expl(rValue);
00748 #endif
00749 }
00750 
00752 /*---------------------------------------------------------------------*/
00759 template <class FloatTypeT> inline
00760 typename TypeTraits<FloatTypeT>::RealReturnType
00761     osgfloor(const FloatTypeT rValue)
00762 {
00763     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00764 
00765     return  osgfloor(RealType(rValue));
00766 }
00767 
00771 inline
00772 Real32 osgfloor(const Real32 rValue)
00773 {
00774 #ifdef OSG_USE_STDMATH
00775     return std::floor(rValue);
00776 #elif defined(OSG_HAS_FLOATMATH)
00777 # ifdef OSG_HPUX_ACC
00778     return (Real32) floor((Real32) rValue);
00779 # else
00780     return floorf(rValue);
00781 # endif
00782 #else
00783     return (Real32) floor((Real32) rValue);
00784 #endif
00785 }
00786 
00790 inline
00791 Real64 osgfloor(const Real64 rValue)
00792 {
00793 #ifdef OSG_USE_STDMATH
00794     return std::floor(rValue);
00795 #else
00796     return floor(rValue);
00797 #endif
00798 }
00799 
00803 inline
00804 Real128 osgfloor(const Real128 rValue)
00805 {
00806 #ifdef OSG_USE_STDMATH
00807     return std::floor(rValue);
00808 #else
00809     return floorl(rValue);
00810 #endif
00811 }
00812 
00814 /*---------------------------------------------------------------------*/
00821 template <class FloatTypeT> inline
00822 typename TypeTraits<FloatTypeT>::RealReturnType
00823     osgceil(const FloatTypeT rValue)
00824 {
00825     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00826 
00827     return  osgceil(RealType(rValue));
00828 }
00829 
00833 inline
00834 Real32 osgceil(const Real32 rValue)
00835 {
00836 #ifdef OSG_USE_STDMATH
00837     return std::ceil(rValue);
00838 #elif defined(OSG_HAS_FLOATMATH)
00839 # ifdef OSG_HPUX_ACC
00840     return (Real32) ceil((Real32) rValue);
00841 # else
00842     return ceilf(rValue);
00843 # endif
00844 #else
00845     return (Real32) ceil((Real32) rValue);
00846 #endif
00847 }
00848 
00852 inline
00853 Real64 osgceil(const Real64 rValue)
00854 {
00855 #ifdef OSG_USE_STDMATH
00856     return std::ceil(rValue);
00857 #else
00858     return ceil(rValue);
00859 #endif
00860 }
00861 
00865 inline
00866 Real128 osgceil(const Real128 rValue)
00867 {
00868 #ifdef OSG_USE_STDMATH
00869     return std::ceil(rValue);
00870 #else
00871     return ceill(rValue);
00872 #endif
00873 }
00874 
00876 /*---------------------------------------------------------------------*/
00883 template <class FloatTypeT> inline
00884 UInt32 osgfinite(const FloatTypeT rValue)
00885 {
00886 #ifdef WIN32
00887     return _finite(rValue);
00888 #else
00889 #ifdef __hpux
00890     return  isfinite(rValue);
00891 #else
00892     return  finite(rValue);
00893 #endif
00894 #endif
00895 }
00896 
00898 /*---------------------------------------------------------------------*/
00905 template <class TypeT> inline
00906 TypeT osgMin(const TypeT lVal, const TypeT rValue)
00907 {
00908     return ((lVal < rValue) ? lVal : rValue);
00909 }
00910 
00914 template <class TypeT> inline
00915 TypeT osgMax(const TypeT lVal, const TypeT rValue)
00916 {
00917     return ((lVal > rValue) ? lVal : rValue);
00918 }
00919 
00924 template <class TypeT> inline
00925 TypeT osgClamp(const TypeT minVal, const TypeT val, const TypeT maxVal)
00926 {
00927     return ((val > minVal) ? ((val < maxVal) ? val : maxVal) : minVal);
00928 }
00929 
00931 /*---------------------------------------------------------------------*/
00938 template <class TypeT> inline
00939 Int8 osgSgn(TypeT val)
00940 {
00941     if(val < 0)
00942         return -1;
00943         
00944     if(val > 0)
00945         return  1;
00946     
00947     return 0;
00948 }
00949 
00951 /*---------------------------------------------------------------------*/
00958 template <class TypeT> inline
00959 void osgSwap(TypeT & lVal, TypeT & rValue)
00960 {
00961     TypeT val(lVal);
00962 
00963     lVal = rValue;
00964     rValue = val;
00965 }
00966 
00968 /*---------------------------------------------------------------------*/
00975 inline
00976 Real32 osgdegree2rad(const Real32 rValue)
00977 {
00978    return (rValue/360.f) * 2.f * 3.1415926535f;
00979 }
00980 
00984 inline
00985 Real64 osgdegree2rad(const Real64 rValue)
00986 {
00987    return (rValue/360) * 2 * 3.1415926535;
00988 }
00989 
00993 template <class FloatTypeT> inline
00994 typename TypeTraits<FloatTypeT>::RealReturnType 
00995     osgdegree2rad(const FloatTypeT rValue)
00996 {
00997     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
00998 
00999     return  OSG::osgdegree2rad(RealType(rValue));
01000 }
01001 
01003 /*---------------------------------------------------------------------*/
01010 inline
01011 Real32 osgrad2degree(const Real32 rValue)
01012 {
01013     return (rValue/(2.f * 3.1415926535f)) * 360.f;
01014 }
01015 
01019 inline
01020 Real64 osgrad2degree(const Real64 rValue)
01021 {
01022     return (rValue/(2 * 3.1415926535)) * 360;
01023 }
01024 
01028 template <class FloatTypeT> inline
01029 typename TypeTraits<FloatTypeT>::RealReturnType
01030     osgrad2degree(const FloatTypeT rValue)
01031 {
01032     typedef typename TypeTraits<FloatTypeT>::RealReturnType RealType;
01033 
01034     return  OSG::osgrad2degree(RealType(rValue));
01035 }
01036 
01038 /*---------------------------------------------------------------------*/
01045 template <class FloatTypeT> inline
01046 typename TypeTraits<FloatTypeT>::RealReturnType
01047     deg2rad(const FloatTypeT rValue)
01048 {
01049     return osgdegree2rad(rValue);
01050 }
01051 
01055 template <class FloatTypeT> inline
01056 typename TypeTraits<FloatTypeT>::RealReturnType 
01057     rad2deg(const FloatTypeT rValue)
01058 {
01059     return osgrad2degree(rValue);
01060 }
01061 
01063 /*---------------------------------------------------------------------*/
01070 inline
01071 bool osgispower2(UInt32 rValue)
01072 {
01073     // find the lowest 1 bit
01074     while(rValue && ! (rValue & 1))
01075     {
01076         rValue >>= 1;
01077     }
01078 
01079     // shift the 1 bit out
01080     rValue >>= 1;
01081 
01082     // if another 1 left => not 2^
01083     if(rValue)
01084         return false;
01085     else            
01086         return true;
01087 }
01088 
01092 inline
01093 bool osgispower2(Int32 rValue)
01094 {
01095     // find the lowest 1 bit
01096     while(rValue && ! (rValue & 1))
01097     {
01098         rValue >>= 1;
01099     }
01100 
01101     // shift the 1 bit out
01102     rValue >>= 1;
01103 
01104     // if another 1 left => not 2^
01105     if(rValue)
01106         return false;
01107     else
01108         return true;
01109 }
01110 
01112 /*---------------------------------------------------------------------*/
01119 inline
01120 UInt32 osgnextpower2(UInt32 rValue)
01121 {
01122     UInt32 result = 1, oresult = 0;
01123 
01124     while(result < rValue && result > oresult)
01125     {
01126         oresult = result;
01127         result <<= 1;
01128     }
01129 
01130     return (result > oresult)? result : 0;
01131 }
01132 
01136 inline
01137 Int32 osgnextpower2(Int32 rValue)
01138 {
01139     Int32 result = 1, oresult = 0;
01140 
01141     while(result < rValue && result > oresult)
01142     {
01143         oresult = result;
01144         result <<= 1;
01145     }
01146 
01147     return (result > oresult)? result : 0;
01148 }
01149 
01153 inline
01154 Int64 osgnextpower2(Int64 rValue)
01155 {
01156     Int64 result = 1, oresult = 0;
01157 
01158     while(result < rValue && result > oresult)
01159     {
01160         oresult = result;
01161         result <<= 1;
01162     }
01163 
01164     return (result > oresult)? result : 0;
01165 }
01166 
01170 template <> inline
01171 UInt64 osgnextpower2<UInt64>(UInt64 rValue)
01172 {
01173     UInt64 result = 1, oresult = 0;
01174 
01175     while(result < rValue && result > oresult)
01176     {
01177         oresult = result;
01178         result <<= 1;
01179     }
01180 
01181     return (result > oresult)? result : 0;
01182 }
01183 
01184 #ifdef SIZE_T_NEQ_UINT32
01185 
01189 inline
01190 size_t osgnextpower2(size_t rValue)
01191 {
01192     size_t result = 1, oresult = 0;
01193 
01194     while(result < rValue && result > oresult)
01195     {
01196         oresult = result;
01197         result <<= 1;
01198     }
01199 
01200     return (result > oresult)? result : 0;
01201 }
01202 
01203 #endif
01204 
01205 //-----------------------------------------------------------------------
01206 // String Functions
01207 //-----------------------------------------------------------------------
01208 
01213 inline
01214 void stringDup(const Char8 *szInput, Char8 *&szOutput)
01215 {
01216     delete [] szOutput;
01217 
01218     szOutput = NULL;
01219 
01220     if(szInput != NULL)
01221     {
01222         szOutput = new char[::strlen(szInput) + 1];
01223 
01224         ::strcpy(szOutput, szInput);
01225     }
01226 }
01227 
01232 inline
01233 Int32 stringncmp(const Char8 *string1, const Char8 *string2, size_t count)
01234 {
01235     return ::strncmp(string1, string2, count);
01236 }
01237 
01242 inline
01243 Int32 stringlen(const Char8 *string1)
01244 {
01245     return ::strlen(string1);
01246 }
01247 
01252 inline
01253 Int32 stringcmp(const Char8 *string1, const Char8 *string2)
01254 {
01255     return ::strcmp(string1, string2);
01256 }
01257 
01262 inline
01263 Int32 stringcasecmp(const Char8 *string1,
01264                     const Char8 *string2)
01265 {
01266 #if !defined(WIN32)
01267     return ::strcasecmp(string1, string2);
01268 #else
01269     return _stricmp  (string1, string2);
01270 #endif
01271 }
01272 
01273 // Indirector
01274 
01275 template <class ContentT, class IndexT>
01276 indirector<ContentT, IndexT>::indirector(ContentT cont) : _cont(cont)
01277 {}
01278         
01279 template <class ContentT, class IndexT>
01280 bool indirector<ContentT, IndexT>::operator()(IndexT a, IndexT b)
01281 {
01282     if(_cont[a] < _cont[b])
01283         return true;
01284 
01285     return false;
01286 }
01287 
01288 //---------------------------------------------------------------------------
01289 // Misc Functions
01290 //---------------------------------------------------------------------------
01291 
01297 template<class VecPntT> inline
01298 UInt32 getMaxIndexAbs3(const VecPntT &v)
01299 {
01300     return 
01301         osgabs(v[0]) > osgabs(v[1]) ?
01302             (osgabs(v[0]) > osgabs(v[2]) ? 0 : 2) : 
01303             (osgabs(v[1]) > osgabs(v[2]) ? 1 : 2);
01304 }
01305 
01310 inline
01311 Int32 putenv(Char8 *string)
01312 {
01313 #if !defined(WIN32) || defined(BCC)
01314     return ::putenv(string);
01315 #else
01316     return ::_putenv(string);
01317 #endif
01318 }
01319 
01320 
01325 inline
01326 void osgsleep(UInt32 millisecs)
01327 {
01328 #ifdef WIN32
01329     Sleep(millisecs);
01330 #else
01331 
01332 #if 0 // replaced by nanosleep to work around IRIX problems
01333     // Rough aproximation, have to find a better way soon (GV)
01334 
01335     if( (millisecs * 1000) > 1000000)
01336     {
01337         ::sleep(millisecs / 1000);
01338     }
01339     else
01340     {
01341         ::usleep(millisecs * 1000);
01342     }
01343     
01344 #else
01345 
01346     struct timespec req;
01347     int ns;
01348     
01349     req.tv_sec = static_cast<long>(millisecs / 1000);
01350     req.tv_nsec = static_cast<long>((millisecs % 1000) * 1000);
01351     
01352     while((req.tv_sec > 0 || req.tv_nsec > 0) &&
01353           (ns = nanosleep(&req, &req)) < 0)
01354     {
01355         if(ns < 0 && errno != EINTR)
01356         {
01357             break;
01358         }
01359     }
01360         
01361 #endif
01362 #endif
01363 }
01364 
01370 inline
01371 Real32 osgrand(void)
01372 {
01373     return rand() / (Real32) RAND_MAX;
01374 }
01375 
01380 inline
01381 bool osgIsBigEndian(void)
01382 {
01383     return (BYTE_ORDER) == (BIG_ENDIAN);   
01384 }
01385 
01386 #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER <= 1200  // VC6
01387 inline
01388 std::ostream &operator<<(std::ostream &os, UInt64 v)
01389 {
01390     char buf[30];
01391     sprintf(buf, "%I64u", v);
01392     return os << buf;
01393 }
01394 
01395 inline
01396 std::ostream &operator<<(std::ostream &os, Int64 v)
01397 {
01398     char buf[30];
01399     sprintf(buf, "%I64d", v);
01400     return os << buf;
01401 }
01402 #endif
01403 
01404 /*---------------------------------------------------------------------*/
01405 /*               big/little endian conversion functions                */
01406 
01407 #if BYTE_ORDER == LITTLE_ENDIAN
01408 
01409 // host to network
01410 
01411 inline
01412 UInt16 osghtons(UInt16 src)
01413 {
01414     return (src >> 8) | (src << 8);
01415 }
01416 
01417 inline
01418 UInt32 osghtonl(UInt32 src)
01419 {
01420     return ((src&0x000000ff) << 24) |
01421            ((src&0x0000ff00) << 8 ) |
01422            ((src&0x00ff0000) >> 8 ) |
01423            ((src&0xff000000) >> 24);
01424 }
01425 
01426 inline
01427 UInt64 osghtonll(UInt64 src)
01428 {
01429 #ifdef OSG_LONGLONG_HAS_LL
01430     return ((src&0x00000000000000ffLL) << 56) |
01431            ((src&0x000000000000ff00LL) << 40) |
01432            ((src&0x0000000000ff0000LL) << 24) |
01433            ((src&0x00000000ff000000LL) << 8 ) |
01434            ((src&0x000000ff00000000LL) >> 8 ) |
01435            ((src&0x0000ff0000000000LL) >> 24) |
01436            ((src&0x00ff000000000000LL) >> 40) |
01437            ((src&0xff00000000000000LL) >> 56);
01438 #else
01439     return ((src&0x00000000000000ff) << 56) |
01440            ((src&0x000000000000ff00) << 40) |
01441            ((src&0x0000000000ff0000) << 24) |
01442            ((src&0x00000000ff000000) << 8 ) |
01443            ((src&0x000000ff00000000) >> 8 ) |
01444            ((src&0x0000ff0000000000) >> 24) |
01445            ((src&0x00ff000000000000) >> 40) |
01446            ((src&0xff00000000000000) >> 56);
01447 #endif
01448 }
01449 
01450 inline
01451 Real32 osghtonf(Real32 src)
01452 {
01453     UInt8 *p = (UInt8 *) &src;
01454     std::swap(p[0], p[3]);
01455     std::swap(p[1], p[2]);
01456     return src;
01457 }
01458 
01459 inline
01460 Real64 osghtond(Real64 src)
01461 {
01462     UInt8 *p = (UInt8 *) &src;
01463     std::swap(p[0], p[7]);
01464     std::swap(p[1], p[6]);
01465     std::swap(p[2], p[5]);
01466     std::swap(p[3], p[4]);
01467     return src;
01468 }
01469 
01470 inline
01471 Real128 osghtondd(Real128 src)
01472 {
01473     UInt8 *p = (UInt8 *) &src;
01474     std::swap(p[0], p[15]);
01475     std::swap(p[1], p[14]);
01476     std::swap(p[2], p[13]);
01477     std::swap(p[3], p[12]);
01478     std::swap(p[4], p[11]);
01479     std::swap(p[5], p[10]);
01480     std::swap(p[6], p[9]);
01481     std::swap(p[7], p[8]);
01482     return src;
01483 }
01484 
01485 // network to host
01486 
01487 inline
01488 UInt16 osgntohs(UInt16 src)
01489 {
01490     return (src >> 8) | (src << 8);
01491 }
01492 
01493 inline
01494 UInt32 osgntohl(UInt32 src)
01495 {
01496     return ((src&0x000000ff) << 24) |
01497            ((src&0x0000ff00) << 8 ) |
01498            ((src&0x00ff0000) >> 8 ) |
01499            ((src&0xff000000) >> 24);
01500 }
01501 
01502 inline
01503 UInt64 osgntohll(UInt64 src)
01504 {
01505 #ifdef OSG_LONGLONG_HAS_LL
01506     return ((src&0x00000000000000ffLL) << 56) |
01507            ((src&0x000000000000ff00LL) << 40) |
01508            ((src&0x0000000000ff0000LL) << 24) |
01509            ((src&0x00000000ff000000LL) << 8 ) |
01510            ((src&0x000000ff00000000LL) >> 8 ) |
01511            ((src&0x0000ff0000000000LL) >> 24) |
01512            ((src&0x00ff000000000000LL) >> 40) |
01513            ((src&0xff00000000000000LL) >> 56);
01514 #else
01515     return ((src&0x00000000000000ff) << 56) |
01516            ((src&0x000000000000ff00) << 40) |
01517            ((src&0x0000000000ff0000) << 24) |
01518            ((src&0x00000000ff000000) << 8 ) |
01519            ((src&0x000000ff00000000) >> 8 ) |
01520            ((src&0x0000ff0000000000) >> 24) |
01521            ((src&0x00ff000000000000) >> 40) |
01522            ((src&0xff00000000000000) >> 56);
01523 #endif
01524 }
01525 
01526 inline
01527 Real32 osgntohf(Real32 src)
01528 {
01529     UInt8 *p = (UInt8 *) &src;
01530     std::swap(p[0], p[3]);
01531     std::swap(p[1], p[2]);
01532     return src;
01533 }
01534 
01535 inline
01536 Real64 osgntohd(Real64 src)
01537 {
01538     UInt8 *p = (UInt8 *) &src;
01539     std::swap(p[0], p[7]);
01540     std::swap(p[1], p[6]);
01541     std::swap(p[2], p[5]);
01542     std::swap(p[3], p[4]);
01543     return src;
01544 }
01545 
01546 inline
01547 Real128 osgntohdd(Real128 src)
01548 {
01549     UInt8 *p = (UInt8 *) &src;
01550     std::swap(p[0], p[15]);
01551     std::swap(p[1], p[14]);
01552     std::swap(p[2], p[13]);
01553     std::swap(p[3], p[12]);
01554     std::swap(p[4], p[11]);
01555     std::swap(p[5], p[10]);
01556     std::swap(p[6], p[9]);
01557     std::swap(p[7], p[8]);
01558     return src;
01559 }
01560 
01561 #else
01562 
01563 // host to network
01564 // perhaps we should use macros here ...
01565 
01566 inline
01567 UInt16 osghtons(UInt16 src)
01568 {
01569     return src;
01570 }
01571 
01572 inline
01573 UInt32 osghtonl(UInt32 src)
01574 {
01575     return src;
01576 }
01577 
01578 inline
01579 UInt64 osghtonll(UInt64 src)
01580 {
01581     return src;
01582 }
01583 
01584 inline
01585 Real32 osghtonf(Real32 src)
01586 {
01587     return src;
01588 }
01589 
01590 inline
01591 Real64 osghtond(Real64 src)
01592 {
01593     return src;
01594 }
01595 
01596 inline
01597 Real128 osghtondd(Real128 src)
01598 {
01599     return src;
01600 }
01601 
01602 // network to host
01603 
01604 inline
01605 UInt16 osgntohs(UInt16 src)
01606 {
01607     return src;
01608 }
01609 
01610 inline
01611 UInt32 osgntohl(UInt32 src)
01612 {
01613     return src;
01614 }
01615 
01616 inline
01617 UInt64 osgntohll(UInt64 src)
01618 {
01619     return src;
01620 }
01621 
01622 inline
01623 Real32 osgntohf(Real32 src)
01624 {
01625     return src;
01626 }
01627 
01628 inline
01629 Real64 osgntohd(Real64 src)
01630 {
01631     return src;
01632 }
01633 
01634 inline
01635 Real128 osgntohdd(Real128 src)
01636 {
01637     return src;
01638 }
01639 
01640 #endif
01641 
01642 //---------------------------------------------------------------------------
01643 // Reference Count Functions
01644 //---------------------------------------------------------------------------
01645 
01649 template <class T> inline
01650 void setRefP(T *&pObject, T *&pNewObject)
01651 {
01652     if(pObject != NULL)
01653         pObject->subRef();
01654 
01655     pObject = pNewObject;
01656 
01657     if(pObject != NULL)
01658         pObject->addRef();
01659 }
01660 
01664 template <class T> inline
01665 void addRefP(T *&pObject)
01666 {
01667     if(pObject != NULL)
01668         pObject->addRef();
01669 }
01670 
01674 template <class T> inline
01675 void subRefP(T *&pObject)
01676 {
01677     if(pObject != NULL)
01678         pObject->subRef();
01679 }
01680 
01684 template <class T> inline
01685 void clearRefP(T *&pObject)
01686 {
01687     if(pObject != NULL)
01688         pObject->subRef();
01689 
01690     pObject = NULL;
01691 }
01692 
01696 template <class T> inline
01697 void clearRefPVector(std::vector<T *> &vVector)
01698 {
01699     for(UInt32 i = 0; i < vVector.size(); i++)
01700         vVector[i]->subRef();
01701 
01702     vVector.erase(vVector.begin(), vVector.end());
01703 }
01704 
01708 template <class T> inline
01709 void clearRefPVectorP(std::vector<T *> *pVector)
01710 {
01711     if(pVector != NULL)
01712     {
01713         for(UInt32 uiIndex = 0; uiIndex < pVector->size(); uiIndex++)
01714             (*pVector)[uiIndex]->subRef();
01715 
01716         pVector->erase(pVector->begin(), pVector->end());
01717     }
01718 }
01719 
01720 OSG_END_NAMESPACE
01721 
01722 #define OSGBASEFUNCTIONS_INLINE_CVSID "@(#)$Id: $"

Generated on Thu Aug 25 04:01:10 2005 for OpenSG by  doxygen 1.4.3