Subversion Repositories gelsvn

Rev

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