660 |
khor |
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 |
* ----------------------------------------------------------------------- */
|
|
|
6 |
|
|
|
7 |
/** @file CGLA.h
|
|
|
8 |
* @brief CGLA main header: contains a number of constants and function decs.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
#ifndef __CGLA_CGLA_H__
|
|
|
12 |
#define __CGLA_CGLA_H__
|
|
|
13 |
|
|
|
14 |
#if (_MSC_VER >= 1200)
|
|
|
15 |
#pragma warning (disable: 4244 4800)
|
|
|
16 |
#endif
|
|
|
17 |
|
|
|
18 |
#include <cmath>
|
|
|
19 |
#include <cfloat>
|
|
|
20 |
#include <climits>
|
|
|
21 |
#include <cassert>
|
|
|
22 |
#include <algorithm>
|
|
|
23 |
#include <functional>
|
|
|
24 |
|
|
|
25 |
#ifdef _MSC_VER // if visual studio
|
|
|
26 |
#include <float.h>
|
|
|
27 |
#endif
|
|
|
28 |
|
|
|
29 |
#ifndef M_PI
|
|
|
30 |
#define M_PI 3.14159265358979323846
|
|
|
31 |
#define M_PI_2 1.57079632679489661923
|
|
|
32 |
#endif
|
|
|
33 |
|
|
|
34 |
#ifndef DEGREES_TO_RADIANS
|
|
|
35 |
#define DEGREES_TO_RADIANS (M_PI / 180.0)
|
|
|
36 |
#endif
|
|
|
37 |
|
|
|
38 |
namespace CGLA
|
|
|
39 |
{
|
|
|
40 |
inline float cgla_nan()
|
|
|
41 |
{
|
|
|
42 |
static const float cgla_nan_value = log(-1.0f);
|
|
|
43 |
return cgla_nan_value;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/** Procedural definition of NAN */
|
|
|
47 |
#define CGLA_NAN cgla_nan()
|
|
|
48 |
|
|
|
49 |
/** NAN is used for initialization of vectors and matrices
|
|
|
50 |
in debug mode */
|
|
|
51 |
#define CGLA_INIT_VALUE cgla_nan()
|
|
|
52 |
|
|
|
53 |
/** Numerical constant representing something large.
|
|
|
54 |
value is a bit arbitrary */
|
|
|
55 |
const double BIG=10e+30;
|
|
|
56 |
|
|
|
57 |
/** Numerical constant represents something extremely small.
|
|
|
58 |
value is a bit arbitrary */
|
|
|
59 |
const double MINUTE=10e-30;
|
|
|
60 |
|
|
|
61 |
/** Numerical constant represents something very small.
|
|
|
62 |
value is a bit arbitrary */
|
|
|
63 |
const double TINY=3e-7;
|
|
|
64 |
|
|
|
65 |
/** Numerical constant represents something small.
|
|
|
66 |
value is a bit arbitrary */
|
|
|
67 |
const double SMALL=10e-2;
|
|
|
68 |
|
|
|
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 |
|
|
|
73 |
inline double sqrt3()
|
|
|
74 |
{
|
|
|
75 |
static const double sqrt3_val = sqrt(3.0);
|
|
|
76 |
return sqrt3_val;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
#define SQRT3 sqrt3()
|
|
|
80 |
|
|
|
81 |
/// Useful enum that represents coordiante axes.
|
|
|
82 |
enum Axis {XAXIS=0,YAXIS=1,ZAXIS=2};
|
|
|
83 |
|
|
|
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 |
}
|
|
|
91 |
|
|
|
92 |
/// Template for a function that squares the argument.
|
|
|
93 |
template <class Scalar>
|
|
|
94 |
inline Scalar sqr(Scalar x) {///
|
|
|
95 |
return x*x;}
|
|
|
96 |
|
|
|
97 |
/// Scalar template for a function that returns the cube of the argument.
|
|
|
98 |
template <class Scalar>
|
|
|
99 |
inline Scalar qbe(Scalar x) {///
|
|
|
100 |
return x*x*x;}
|
|
|
101 |
|
|
|
102 |
template <class Scalar>
|
|
|
103 |
inline bool is_zero(Scalar x) {return (x > -MINUTE && x < MINUTE);}
|
|
|
104 |
|
|
|
105 |
template <class Scalar>
|
|
|
106 |
inline bool is_tiny(Scalar x) {return (x > -TINY && x < TINY);}
|
|
|
107 |
|
|
|
108 |
/** What power of 2 ?. if x is the argument, find the largest
|
|
|
109 |
y so that 2^y <= x */
|
|
|
110 |
inline int two_to_what_power(unsigned int x)
|
|
|
111 |
{
|
|
|
112 |
if (x<1)
|
|
|
113 |
return -1;
|
|
|
114 |
int i = 0;
|
|
|
115 |
while (x != 1) {x>>=1;i++;}
|
|
|
116 |
return i;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
#ifdef __sgi
|
|
|
120 |
inline int round(float x) {return int(rint(x));}
|
|
|
121 |
#else
|
|
|
122 |
inline int round(float x) {return int(x+0.5);}
|
|
|
123 |
#endif
|
|
|
124 |
|
|
|
125 |
template<class T>
|
|
|
126 |
inline T sign(T x) {return x>=T(0) ? 1 : -1;}
|
|
|
127 |
|
|
|
128 |
/// Integer power function with O(log(n)) complexity
|
|
|
129 |
template<class T>
|
|
|
130 |
inline T int_pow(T a, unsigned int n)
|
|
|
131 |
{
|
|
|
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;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/// Function that seeds the GEL pseudo-random number generator
|
|
|
142 |
void gel_srand(unsigned int seed);
|
|
|
143 |
|
|
|
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 |
|
|
|
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. */
|
|
|
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 |
}
|
|
|
164 |
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
#endif
|