Subversion Repositories gelsvn

Rev

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