Subversion Repositories gelsvn

Rev

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

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