Subversion Repositories gelsvn

Rev

Rev 417 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
248 bj 6
 
595 jab 7
/** @file CGLA.h
8
 * @brief CGLA main header: contains a number of constants and function decs.
9
 */
10
 
12 jab 11
#ifndef __CGLA_CGLA_H__
12
#define __CGLA_CGLA_H__
2 bj 13
 
102 bj 14
#if (_MSC_VER >= 1200)
195 jrf 15
#pragma warning (disable: 4244 4800)
102 bj 16
#endif
17
 
2 bj 18
#include <cmath>
66 bj 19
#include <cfloat>
2 bj 20
#include <climits>
21
#include <cassert>
22
#include <algorithm>
66 bj 23
#include <functional>
2 bj 24
 
25
#ifndef M_PI
26
#define M_PI 3.14159265358979323846
27
#define M_PI_2 1.57079632679489661923
28
#endif
29
 
12 jab 30
namespace CGLA 
31
{
129 jab 32
		inline float cgla_nan() 
33
		{
196 jrf 34
				static const float cgla_nan_value = log(-1.0f);
129 jab 35
				return cgla_nan_value;
36
		}
37
 
21 jrf 38
  /** Procedural definition of NAN */  
129 jab 39
#define CGLA_NAN cgla_nan()
21 jrf 40
 
41
  /** NAN is used for initialization of vectors and matrices
42
      in debug mode */
129 jab 43
#define CGLA_INIT_VALUE cgla_nan()
21 jrf 44
 
12 jab 45
  /** Numerical constant representing something large.
46
      value is a bit arbitrary */
47
  const double BIG=10e+30;
2 bj 48
 
12 jab 49
  /** Numerical constant represents something extremely small.
50
      value is a bit arbitrary */
51
  const double MINUTE=10e-30;
2 bj 52
 
12 jab 53
  /** Numerical constant represents something very small.
54
      value is a bit arbitrary */
55
  const double TINY=3e-7;
2 bj 56
 
12 jab 57
  /** Numerical constant represents something small.
58
      value is a bit arbitrary */
59
  const double SMALL=10e-2;
2 bj 60
 
417 jrf 61
  /** The GEL pseudo-random number generator uses
62
      UINT_MAX as RAND_MAX to avoid mod operations. */
63
  const unsigned int GEL_RAND_MAX=UINT_MAX;
64
 
129 jab 65
		inline double sqrt3()
66
		{
67
				static const double sqrt3_val = sqrt(3.0);
68
				return sqrt3_val;
69
		}
2 bj 70
 
129 jab 71
#define SQRT3 sqrt3()
72
 
12 jab 73
  /// Useful enum that represents coordiante axes.
74
  enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2};
2 bj 75
 
197 jrf 76
  inline bool isnan(double x) { return x != x; }
55 jab 77
 
12 jab 78
  template<class Scalar>
79
  Scalar s_min(Scalar a, Scalar b)
80
  {
81
    return a<b ? a : b;
82
  }
2 bj 83
 
12 jab 84
  template<class Scalar>
85
  Scalar s_max(Scalar a, Scalar b)
86
  {
87
    return a>b ? a : b;
88
  }
2 bj 89
 
417 jrf 90
  /// Template for a function that squares the argument.
12 jab 91
  template <class Scalar>
92
  inline Scalar sqr(Scalar x) {///
93
    return x*x;}
2 bj 94
 
417 jrf 95
  /// Scalar template for a function that returns the cube of the argument.
12 jab 96
  template <class Scalar>
97
  inline Scalar qbe(Scalar x) {///
98
    return x*x*x;}
2 bj 99
 
12 jab 100
  template <class Scalar>
101
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
2 bj 102
 
12 jab 103
  template <class Scalar>
104
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
2 bj 105
 
12 jab 106
  /** What power of 2 ?. if x is the argument, find the largest 
107
      y so that 2^y <= x */
45 jab 108
  inline int two_to_what_power(unsigned int x) 
12 jab 109
  {
110
    if (x<1) 
111
      return -1;
112
    int i = 0;
113
    while (x != 1) {x>>=1;i++;}
114
    return i;
115
  }
2 bj 116
 
117
#ifdef __sgi
12 jab 118
  inline int round(float x) {return int(rint(x));}
2 bj 119
#else
12 jab 120
  inline int round(float x) {return int(x+0.5);}
2 bj 121
#endif
122
 
12 jab 123
  template<class T>
124
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
2 bj 125
 
277 jrf 126
  /// Integer power function with O(log(n)) complexity
12 jab 127
  template<class T>
277 jrf 128
  inline T int_pow(T a, unsigned int n)
12 jab 129
  {
277 jrf 130
    T result = static_cast<T>(1);
131
    for(; n > 0; n >>= 1)
132
    {
133
      if(n & 1) result = result*a;
134
      a *= a;
135
    }
136
    return result;
12 jab 137
  }
417 jrf 138
 
139
  /// Function that seeds the GEL pseudo-random number generator
140
  void gel_srand(unsigned int seed);
2 bj 141
 
417 jrf 142
  /** GEL provides a linear congruential pseudo-random number 
143
      generator which is optimized for speed. This version allows 
144
      an integer argument which is useful for grid-based noise
145
      functions. */
146
  unsigned int gel_rand(unsigned int k);
147
 
148
  /** GEL provides a linear congruential pseudo-random number 
149
      generator which is optimized for speed. This means
150
      that GEL_RAND_MAX==UINT_MAX. */
151
  unsigned int gel_rand();
152
 
39 bj 153
	/** raw_assign takes a CGLA vector, matrix or whatever has a get() function
154
			as its first argument and a raw pointer to a (presumed scalar) entity 
155
			as the second argument. the contents dereferenced by the pointer is 
156
			copied to the entity given as first argument. */
12 jab 157
  template<class T, class S>
158
  void raw_assign(T& a,  const S* b)
159
  {
160
    memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T));
161
  }
2 bj 162
 
163
}
164
 
165
#endif