Subversion Repositories gelsvn

Rev

Rev 595 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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