Subversion Repositories gelsvn

Rev

Rev 248 | Rev 595 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

// $Id: CGLA.h 277 2006-08-29 09:14:45Z jrf $

#ifndef __CGLA_CGLA_H__
#define __CGLA_CGLA_H__

#if (_MSC_VER >= 1200)
#pragma warning (disable: 4244 4800)
#endif

#include <cmath>
#include <cfloat>
#include <climits>
#include <cassert>
#include <algorithm>
#include <functional>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#endif

namespace CGLA 
{
                inline float cgla_nan() 
                {
                                static const float cgla_nan_value = log(-1.0f);
                                return cgla_nan_value;
                }
                
  /** Procedural definition of NAN */  
#define CGLA_NAN cgla_nan()

  /** NAN is used for initialization of vectors and matrices
      in debug mode */
#define CGLA_INIT_VALUE cgla_nan()

  /** Numerical constant representing something large.
      value is a bit arbitrary */
  const double BIG=10e+30;

  /** Numerical constant represents something extremely small.
      value is a bit arbitrary */
  const double MINUTE=10e-30;

  /** Numerical constant represents something very small.
      value is a bit arbitrary */
  const double TINY=3e-7;
        
  /** Numerical constant represents something small.
      value is a bit arbitrary */
  const double SMALL=10e-2;

                inline double sqrt3()
                {
                                static const double sqrt3_val = sqrt(3.0);
                                return sqrt3_val;
                }

#define SQRT3 sqrt3()

  /// Useful enum that represents coordiante axes.
  enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2};

  inline bool isnan(double x) { return x != x; }

  template<class Scalar>
  Scalar s_min(Scalar a, Scalar b)
  {
    return a<b ? a : b;
  }

  template<class Scalar>
  Scalar s_max(Scalar a, Scalar b)
  {
    return a>b ? a : b;
  }

  ///Template for a function that squares the argument.
  template <class Scalar>
  inline Scalar sqr(Scalar x) {///
    return x*x;}
        
  /// Scalaremplate for a function that returns the cube of the argument.
  template <class Scalar>
  inline Scalar qbe(Scalar x) {///
    return x*x*x;}

  template <class Scalar>
  inline bool is_zero(Scalar x) {return (x > -MINUTE && x < MINUTE);}

  template <class Scalar>
  inline bool is_tiny(Scalar x) {return (x > -TINY && x < TINY);}

  /** What power of 2 ?. if x is the argument, find the largest 
      y so that 2^y <= x */
  inline int two_to_what_power(unsigned int x) 
  {
    if (x<1) 
      return -1;
    int i = 0;
    while (x != 1) {x>>=1;i++;}
    return i;
  }

#ifdef __sgi
  inline int round(float x) {return int(rint(x));}
#else
  inline int round(float x) {return int(x+0.5);}
#endif

  template<class T>
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}

  /// Integer power function with O(log(n)) complexity
  template<class T>
  inline T int_pow(T a, unsigned int n)
  {
    T result = static_cast<T>(1);
    for(; n > 0; n >>= 1)
    {
      if(n & 1) result = result*a;
      a *= a;
    }
    return result;
  }

        /** raw_assign takes a CGLA vector, matrix or whatever has a get() function
                        as its first argument and a raw pointer to a (presumed scalar) entity 
                        as the second argument. the contents dereferenced by the pointer is 
                        copied to the entity given as first argument. */
  template<class T, class S>
  void raw_assign(T& a,  const S* b)
  {
    memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T));
  }
        
}

#endif