Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1452 chris 1
#region --- License ---
2
/* Licensed under the MIT/X11 license.
3
 * Copyright (c) 2006-2008 the OpenTK Team.
4
 * This notice may not be removed from any source distribution.
5
 * See license.txt for licensing detailed licensing details.
6
 *
7
 * Contributions by Andy Gill, James Talton and Georg Wächter.
8
 */
9
#endregion
10
 
11
using System;
12
using System.Collections.Generic;
13
using System.Text;
14
 
15
namespace OpenTK
16
{
17
    /// <summary>
18
    /// Contains mathematical functions for the OpenTK.Math toolkit.
19
    /// </summary>
20
    [Obsolete("Use OpenTK.MathHelper instead.")]
21
    public static class Functions
22
    {
23
        #region NextPowerOfTwo
24
 
25
        /// <summary>
26
        /// Returns the next power of two that is larger than the specified number.
27
        /// </summary>
28
        /// <param name="n">The specified number.</param>
29
        /// <returns>The next power of two.</returns>
30
        public static long NextPowerOfTwo(long n)
31
        {
32
            if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
33
            return (long)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
34
        }
35
 
36
        /// <summary>
37
        /// Returns the next power of two that is larger than the specified number.
38
        /// </summary>
39
        /// <param name="n">The specified number.</param>
40
        /// <returns>The next power of two.</returns>
41
        public static int NextPowerOfTwo(int n)
42
        {
43
            if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
44
            return (int)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
45
        }
46
 
47
        /// <summary>
48
        /// Returns the next power of two that is larger than the specified number.
49
        /// </summary>
50
        /// <param name="n">The specified number.</param>
51
        /// <returns>The next power of two.</returns>
52
        public static float NextPowerOfTwo(float n)
53
        {
54
            if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
55
            return (float)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
56
        }
57
 
58
        /// <summary>
59
        /// Returns the next power of two that is larger than the specified number.
60
        /// </summary>
61
        /// <param name="n">The specified number.</param>
62
        /// <returns>The next power of two.</returns>
63
        public static double NextPowerOfTwo(double n)
64
        {
65
            if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
66
            return System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
67
        }
68
 
69
        #endregion
70
 
71
        #region Factorial
72
 
73
        /// <summary>Calculates the factorial of a given natural number.
74
        /// </summary>
75
        /// <param name="n">The number.</param>
76
        /// <returns>n!</returns>
77
        public static long Factorial(int n)
78
        {
79
            long result = 1;
80
 
81
            for (; n > 1; n--)
82
                result *= n;
83
 
84
            return result;
85
        }
86
 
87
        #endregion
88
 
89
        #region BinomialCoefficient
90
 
91
        /// <summary>
92
        /// Calculates the binomial coefficient <paramref name="n"/> above <paramref name="k"/>.
93
        /// </summary>
94
        /// <param name="n">The n.</param>
95
        /// <param name="k">The k.</param>
96
        /// <returns>n! / (k! * (n - k)!)</returns>
97
        public static long BinomialCoefficient(int n, int k)
98
        {
99
            return Factorial(n) / (Factorial(k) * Factorial(n - k));
100
        }
101
 
102
        #endregion
103
 
104
        #region InverseSqrtFast
105
 
106
        /// <summary>
107
        /// Returns an approximation of the inverse square root of left number.
108
        /// </summary>
109
        /// <param name="x">A number.</param>
110
        /// <returns>An approximation of the inverse square root of the specified number, with an upper error bound of 0.001</returns>
111
        /// <remarks>
112
        /// This is an improved implementation of the the method known as Carmack's inverse square root
113
        /// which is found in the Quake III source code. This implementation comes from
114
        /// http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see
115
        /// http://www.beyond3d.com/content/articles/8/
116
        /// </remarks>
117
        public static float InverseSqrtFast(float x)
118
        {
119
            unsafe
120
            {
121
                float xhalf = 0.5f * x;
122
                int i = *(int*)&x;              // Read bits as integer.
123
                i = 0x5f375a86 - (i >> 1);      // Make an initial guess for Newton-Raphson approximation
124
                x = *(float*)&i;                // Convert bits back to float
125
                x = x * (1.5f - xhalf * x * x); // Perform left single Newton-Raphson step.
126
                return x;
127
            }
128
        }
129
 
130
        /// <summary>
131
        /// Returns an approximation of the inverse square root of left number.
132
        /// </summary>
133
        /// <param name="x">A number.</param>
134
        /// <returns>An approximation of the inverse square root of the specified number, with an upper error bound of 0.001</returns>
135
        /// <remarks>
136
        /// This is an improved implementation of the the method known as Carmack's inverse square root
137
        /// which is found in the Quake III source code. This implementation comes from
138
        /// http://www.codemaestro.com/reviews/review00000105.html. For the history of this method, see
139
        /// http://www.beyond3d.com/content/articles/8/
140
        /// </remarks>
141
        public static double InverseSqrtFast(double x)
142
        {
143
            return InverseSqrtFast((float)x);
144
            // TODO: The following code is wrong. Fix it, to improve precision.
145
#if false
146
            unsafe
147
            {
148
                double xhalf = 0.5f * x;
149
                int i = *(int*)&x;              // Read bits as integer.
150
                i = 0x5f375a86 - (i >> 1);      // Make an initial guess for Newton-Raphson approximation
151
                x = *(float*)&i;                // Convert bits back to float
152
                x = x * (1.5f - xhalf * x * x); // Perform left single Newton-Raphson step.
153
                return x;
154
            }
155
#endif
156
        }
157
 
158
        #endregion
159
 
160
        #region DegreesToRadians
161
 
162
        /// <summary>
163
        /// Convert degrees to radians
164
        /// </summary>
165
        /// <param name="degrees">An angle in degrees</param>
166
        /// <returns>The angle expressed in radians</returns>
167
        public static float DegreesToRadians(float degrees)
168
        {
169
            const float degToRad = (float)System.Math.PI / 180.0f;
170
            return degrees * degToRad;
171
        }
172
 
173
        /// <summary>
174
        /// Convert radians to degrees
175
        /// </summary>
176
        /// <param name="radians">An angle in radians</param>
177
        /// <returns>The angle expressed in degrees</returns>
178
        public static float RadiansToDegrees(float radians)
179
        {
180
            const float radToDeg = 180.0f / (float)System.Math.PI;
181
            return radians * radToDeg;
182
        }
183
 
184
        #endregion
185
 
186
        #region Mathematical constants
187
 
188
        /// <summary>
189
        /// Obsolete. Do not use.
190
        /// </summary>
191
        public static readonly float PIF = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382f;
192
 
193
        /// <summary>
194
        /// Obsolete. Do not use.
195
        /// </summary>
196
        public static readonly float RTODF = 180.0f / PIF;
197
 
198
        /// <summary>
199
        /// Obsolete. Do not use.
200
        /// </summary>
201
        public static readonly float DTORF = PIF / 180.0f;
202
 
203
        /// <summary>
204
        /// Obsolete. Do not use.
205
        /// </summary>
206
        public static readonly double PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382d;
207
 
208
        /// <summary>
209
        /// Obsolete. Do not use.
210
        /// </summary>
211
        public static readonly double RTOD = 180.0d / PIF;
212
 
213
        /// <summary>
214
        /// Obsolete. Do not use.
215
        /// </summary>
216
        public static readonly double DTOR = PIF / 180.0d;
217
 
218
        #endregion
219
 
220
        #region Swap
221
 
222
        /// <summary>
223
        /// Swaps two float values.
224
        /// </summary>
225
        /// <param name="a">The first value.</param>
226
        /// <param name="b">The second value.</param>
227
        public static void Swap(ref double a, ref double b)
228
        {
229
            double temp = a;
230
            a = b;
231
            b = temp;
232
        }
233
 
234
        /// <summary>
235
        /// Swaps two float values.
236
        /// </summary>
237
        /// <param name="a">The first value.</param>
238
        /// <param name="b">The second value.</param>
239
        public static void Swap(ref float a, ref float b)
240
        {
241
            float temp = a;
242
            a = b;
243
            b = temp;
244
        }
245
 
246
        #endregion
247
    }
248
 
249
#if false
250
    public static partial class Math
251
    {
252
    #region --- Vectors ---
253
 
254
    #region --- Addition ---
255
 
256
        /// <summary>
257
        /// Adds the given Vector2 to the current Vector3.
258
        /// </summary>
259
        /// <param name="right">The right operand of the addition.</param>
260
        /// <returns>A new Vector3 containing the result of the addition.</returns>
261
        public static Vector2 Add(Vector2 left, Vector2 right)
262
        {
263
            return new Vector2(left).Add(right);
264
        }
265
 
266
        /// <summary>
267
        /// Adds the given Vector3 to the current Vector3.
268
        /// </summary>
269
        /// <param name="right">The right operand of the addition.</param>
270
        /// <returns>A new Vector3 containing the result of the addition.</returns>
271
        public static Vector3 Add(Vector2 left, Vector3 right)
272
        {
273
            return new Vector3(left).Add(right);
274
        }
275
 
276
        /// <summary>
277
        /// Adds the given Vector4 to the current Vector3. W-coordinate remains unaffected.
278
        /// </summary>
279
        /// <param name="right">The right operand of the addition.</param>
280
        /// <returns>A new Vector4 containing the result of the addition.</returns>
281
        public static Vector4 Add(Vector2 left, Vector4 right)
282
        {
283
            return new Vector4(left).Add(right);
284
        }
285
 
286
        /// <summary>
287
        /// Adds the given Vector2 to the current Vector3.
288
        /// </summary>
289
        /// <param name="right">The right operand of the addition.</param>
290
        /// <returns>A new Vector3 containing the result of the addition.</returns>
291
        public static Vector3 Add(Vector3 left, Vector2 right)
292
        {
293
            return new Vector3(left).Add(right);
294
        }
295
 
296
        /// <summary>
297
        /// Adds the given Vector3 to the current Vector3.
298
        /// </summary>
299
        /// <param name="right">The right operand of the addition.</param>
300
        /// <returns>A new Vector3 containing the result of the addition.</returns>
301
        public static Vector3 Add(Vector3 left, Vector3 right)
302
        {
303
            return new Vector3(left).Add(right);
304
        }
305
 
306
        /// <summary>
307
        /// Adds the given Vector4 to the current Vector3. W-coordinate remains unaffected.
308
        /// </summary>
309
        /// <param name="right">The right operand of the addition.</param>
310
        /// <returns>A new Vector4 containing the result of the addition.</returns>
311
        public static Vector4 Add(Vector3 left, Vector4 right)
312
        {
313
            return new Vector4(left).Add(right);
314
        }
315
 
316
        /// <summary>
317
        /// Adds the given Vector2 to the current Vector3.
318
        /// </summary>
319
        /// <param name="right">The right operand of the addition.</param>
320
        /// <returns>A new Vector3 containing the result of the addition.</returns>
321
        public static Vector4 Add(Vector4 left, Vector2 right)
322
        {
323
            return new Vector4(left).Add(right);
324
        }
325
 
326
        /// <summary>
327
        /// Adds the given Vector3 to the current Vector3.
328
        /// </summary>
329
        /// <param name="right">The right operand of the addition.</param>
330
        /// <returns>A new Vector3 containing the result of the addition.</returns>
331
        public static Vector4 Add(Vector4 left, Vector3 right)
332
        {
333
            return new Vector4(left).Add(right);
334
        }
335
 
336
        /// <summary>
337
        /// Adds the given Vector4 to the current Vector3. W-coordinate remains unaffected.
338
        /// </summary>
339
        /// <param name="right">The right operand of the addition.</param>
340
        /// <returns>A new Vector4 containing the result of the addition.</returns>
341
        public static Vector4 Add(Vector4 left, Vector4 right)
342
        {
343
            return new Vector4(left).Add(right);
344
        }
345
 
346
        #endregion
347
 
348
    #region --- Subtraction ---
349
 
350
 
351
 
352
        #endregion
353
 
354
    #region --- Cross ---
355
 
356
        /// <summary>
357
        /// Computes the cross product between the current and the given Vector3. The current Vector3 is set to the result of the computation.
358
        /// </summary>
359
        /// <param name="right">The right operand of the cross product</param>
360
        /// <returns>The current </returns>
361
        public static Vector3 Cross(Vector3 left, Vector3 right)
362
        {
363
            return new Vector3(left).Cross(right);
364
        }
365
 
366
        #endregion
367
 
368
        #endregion
369
    }
370
#endif
371
}