Subversion Repositories gelsvn

Rev

Rev 624 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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