Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Interpolator.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Runtime.CompilerServices;
5 using SiliconStudio.Core.Mathematics;
6 
7 namespace SiliconStudio.Paradox.DataModel
8 {
9  /// <summary>
10  /// Various helper functions for float, Vector3 and Quaternion interpolations.
11  /// </summary>
12  public static class Interpolator
13  {
14  public static class Vector3
15  {
16  public static void Cubic(ref Core.Mathematics.Vector3 value1, ref Core.Mathematics.Vector3 value2, ref Core.Mathematics.Vector3 value3, ref Core.Mathematics.Vector3 value4, float t, out Core.Mathematics.Vector3 result)
17  {
18  // http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Interpolation_on_the_unit_interval_without_exact_derivatives
19  float t2 = t * t;
20  float t3 = t2 * t;
21 
22  float factor0 = -t3 + 2.0f * t2 - t;
23  float factor1 = 3.0f * t3 - 5.0f * t2 + 2.0f;
24  float factor2 = -3.0f * t3 + 4.0f * t2 + t;
25  float factor3 = t3 - t2;
26 
27  // TODO: Use Vector3(ref,out) functions
28  result.X = 0.5f * (value1.X * factor0 + value2.X * factor1 + value3.X * factor2 + value4.X * factor3);
29  result.Y = 0.5f * (value1.Y * factor0 + value2.Y * factor1 + value3.Y * factor2 + value4.Y * factor3);
30  result.Z = 0.5f * (value1.Z * factor0 + value2.Z * factor1 + value3.Z * factor2 + value4.Z * factor3);
31  }
32 
33  [MethodImpl(MethodImplOptions.AggressiveInlining)]
34  public static void Linear(ref Core.Mathematics.Vector3 value1, ref Core.Mathematics.Vector3 value2, float t, out Core.Mathematics.Vector3 result)
35  {
36  Core.Mathematics.Vector3.Lerp(ref value1, ref value2, t, out result);
37  }
38  }
39 
40  public static class Quaternion
41  {
42  public static void Cubic(ref Core.Mathematics.Quaternion value1, ref Core.Mathematics.Quaternion value2, ref Core.Mathematics.Quaternion value3, ref Core.Mathematics.Quaternion value4, float t, out Core.Mathematics.Quaternion result)
43  {
44  // TODO Investigate: Squad doesn't seem to do the same thing as implicit derivatives
45  throw new NotImplementedException();
46  }
47 
48  [MethodImpl(MethodImplOptions.AggressiveInlining)]
49  public static void SphericalLinear(ref Core.Mathematics.Quaternion value1, ref Core.Mathematics.Quaternion value2, float t, out Core.Mathematics.Quaternion result)
50  {
51  Core.Mathematics.Quaternion.Slerp(ref value1, ref value2, t, out result);
52  }
53  }
54 
55  [MethodImpl(MethodImplOptions.AggressiveInlining)]
56  public static float Linear(float value1, float value2, float t)
57  {
58  return (1.0f - t) * value1 + t * value2;
59  }
60 
61  [MethodImpl(MethodImplOptions.AggressiveInlining)]
62  public static float Cubic(float value1, float value2, float value3, float value4, float t)
63  {
64  // http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Interpolation_on_the_unit_interval_without_exact_derivatives
65  float t2 = t * t;
66  float t3 = t2 * t;
67 
68  float factor0 = -t3 + 2.0f * t2 - t;
69  float factor1 = 3.0f * t3 - 5.0f * t2 + 2.0f;
70  float factor2 = -3.0f * t3 + 4.0f * t2 + t;
71  float factor3 = t3 - t2;
72 
73  return 0.5f * (value1 * factor0
74  + value2 * factor1
75  + value3 * factor2
76  + value4 * factor3);
77  }
78  }
79 }
static void SphericalLinear(ref Core.Mathematics.Quaternion value1, ref Core.Mathematics.Quaternion value2, float t, out Core.Mathematics.Quaternion result)
Definition: Interpolator.cs:49
static void Cubic(ref Core.Mathematics.Vector3 value1, ref Core.Mathematics.Vector3 value2, ref Core.Mathematics.Vector3 value3, ref Core.Mathematics.Vector3 value4, float t, out Core.Mathematics.Vector3 result)
Definition: Interpolator.cs:16
static float Cubic(float value1, float value2, float value3, float value4, float t)
Definition: Interpolator.cs:62
static void Cubic(ref Core.Mathematics.Quaternion value1, ref Core.Mathematics.Quaternion value2, ref Core.Mathematics.Quaternion value3, ref Core.Mathematics.Quaternion value4, float t, out Core.Mathematics.Quaternion result)
Definition: Interpolator.cs:42
static float Linear(float value1, float value2, float t)
Definition: Interpolator.cs:56
static void Linear(ref Core.Mathematics.Vector3 value1, ref Core.Mathematics.Vector3 value2, float t, out Core.Mathematics.Vector3 result)
Definition: Interpolator.cs:34
Various helper functions for float, Vector3 and Quaternion interpolations.
Definition: Interpolator.cs:12