Subversion Repositories gelsvn

Rev

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

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