Subversion Repositories gelsvn

Rev

Rev 630 | 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
 
624 mono 30
#ifndef DEGREES_TO_RADIANS
31
#define DEGREES_TO_RADIANS (M_PI / 180.0)
32
#endif
33
 
12 jab 34
namespace CGLA 
35
{
129 jab 36
		inline float cgla_nan() 
37
		{
196 jrf 38
				static const float cgla_nan_value = log(-1.0f);
129 jab 39
				return cgla_nan_value;
40
		}
41
 
21 jrf 42
  /** Procedural definition of NAN */  
129 jab 43
#define CGLA_NAN cgla_nan()
21 jrf 44
 
45
  /** NAN is used for initialization of vectors and matrices
46
      in debug mode */
129 jab 47
#define CGLA_INIT_VALUE cgla_nan()
21 jrf 48
 
12 jab 49
  /** Numerical constant representing something large.
50
      value is a bit arbitrary */
51
  const double BIG=10e+30;
2 bj 52
 
12 jab 53
  /** Numerical constant represents something extremely small.
54
      value is a bit arbitrary */
55
  const double MINUTE=10e-30;
2 bj 56
 
12 jab 57
  /** Numerical constant represents something very small.
58
      value is a bit arbitrary */
59
  const double TINY=3e-7;
2 bj 60
 
12 jab 61
  /** Numerical constant represents something small.
62
      value is a bit arbitrary */
63
  const double SMALL=10e-2;
2 bj 64
 
417 jrf 65
  /** The GEL pseudo-random number generator uses
66
      UINT_MAX as RAND_MAX to avoid mod operations. */
67
  const unsigned int GEL_RAND_MAX=UINT_MAX;
68
 
129 jab 69
		inline double sqrt3()
70
		{
71
				static const double sqrt3_val = sqrt(3.0);
72
				return sqrt3_val;
73
		}
2 bj 74
 
129 jab 75
#define SQRT3 sqrt3()
76
 
12 jab 77
  /// Useful enum that represents coordiante axes.
78
  enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2};
2 bj 79
 
197 jrf 80
  inline bool isnan(double x) { return x != x; }
55 jab 81
 
417 jrf 82
  /// Template for a function that squares the argument.
12 jab 83
  template <class Scalar>
84
  inline Scalar sqr(Scalar x) {///
85
    return x*x;}
2 bj 86
 
417 jrf 87
  /// Scalar template for a function that returns the cube of the argument.
12 jab 88
  template <class Scalar>
89
  inline Scalar qbe(Scalar x) {///
90
    return x*x*x;}
2 bj 91
 
12 jab 92
  template <class Scalar>
93
  inline bool is_zero(Scalar x)	{return (x > -MINUTE && x < MINUTE);}
2 bj 94
 
12 jab 95
  template <class Scalar>
96
  inline bool is_tiny(Scalar x)	{return (x > -TINY && x < TINY);}
2 bj 97
 
12 jab 98
  /** What power of 2 ?. if x is the argument, find the largest 
99
      y so that 2^y <= x */
45 jab 100
  inline int two_to_what_power(unsigned int x) 
12 jab 101
  {
102
    if (x<1) 
103
      return -1;
104
    int i = 0;
105
    while (x != 1) {x>>=1;i++;}
106
    return i;
107
  }
2 bj 108
 
109
#ifdef __sgi
12 jab 110
  inline int round(float x) {return int(rint(x));}
2 bj 111
#else
12 jab 112
  inline int round(float x) {return int(x+0.5);}
2 bj 113
#endif
114
 
12 jab 115
  template<class T>
116
  inline T sign(T x) {return x>=T(0) ? 1 : -1;}
2 bj 117
 
277 jrf 118
  /// Integer power function with O(log(n)) complexity
12 jab 119
  template<class T>
277 jrf 120
  inline T int_pow(T a, unsigned int n)
12 jab 121
  {
277 jrf 122
    T result = static_cast<T>(1);
123
    for(; n > 0; n >>= 1)
124
    {
125
      if(n & 1) result = result*a;
126
      a *= a;
127
    }
128
    return result;
12 jab 129
  }
417 jrf 130
 
131
  /// Function that seeds the GEL pseudo-random number generator
132
  void gel_srand(unsigned int seed);
2 bj 133
 
417 jrf 134
  /** GEL provides a linear congruential pseudo-random number 
135
      generator which is optimized for speed. This version allows 
136
      an integer argument which is useful for grid-based noise
137
      functions. */
138
  unsigned int gel_rand(unsigned int k);
139
 
140
  /** GEL provides a linear congruential pseudo-random number 
141
      generator which is optimized for speed. This means
142
      that GEL_RAND_MAX==UINT_MAX. */
143
  unsigned int gel_rand();
144
 
39 bj 145
	/** raw_assign takes a CGLA vector, matrix or whatever has a get() function
146
			as its first argument and a raw pointer to a (presumed scalar) entity 
147
			as the second argument. the contents dereferenced by the pointer is 
148
			copied to the entity given as first argument. */
12 jab 149
  template<class T, class S>
150
  void raw_assign(T& a,  const S* b)
151
  {
152
    memcpy(static_cast<void*>(a.get()),static_cast<const void*>(b),sizeof(T));
153
  }
2 bj 154
 
155
}
156
 
157
#endif