Difference between revisions of "Interpolation"

From Second Life Wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{LSL Header}}
{{LSL Header}}
{{RightToc|clear:right;}}


== Interpolation Library ==
Interpolation is a way of constructing new data points within a range of known data points.


The range of applications is varied, but here in SL it can most directly be used for animating and/or moving prims or linksets via any attribute you can creatively make use of.


=== Float Functions ===
Attributes:
* Prim/Object Position
* Prim/Object Rotation
* Texture Scale/Offsets
* or you can just simply interpolate internal values


<!--############# FLOAT LINEAR #############-->
Potential applications:
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
* Movement
!style="color: #000000; background-color: #aaaaff;" height="20px"|
* Animation
====Float Linear====
* User Interfaces
|-
* Graphics
|
* Games
Linear interpolation of f0 and f1 with fraction t.
<lsl>
float fLin(float f0,float f1,float t) {
    return f0*(1-t) + f1*t;
}
</lsl>


{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
There are many different types of interpolation, which may exhibit different preferred or controllable behaviour.
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| float f0
| Start, 0.0
|-
| float f1
| End, 1.0
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fLin
| Returns linear interpolation of two floats
|}
| [[Image:Interp_Chart1.png|center]]
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}


<!--############# FLOAT COSINE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Float Cosine====
|-
|
Cosine interpolation of f0 and f1 with fraction t.
<lsl>
float fCos(float f0,float f1,float t) {
    float F = (1 - llCos(t*PI))/2;
    return f0*(1-F)+f1*F;
}
</lsl>


{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| float f0
| Start, 0.0
|-
| float f1
| End, 1.0
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fCos
| Returns cosine interpolation of two floats
|}
| [[Image:Interp_Chart2.png|center]]
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}


<!--############# FLOAT CUBIC #############-->
== Interpolation Library ==
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Float Cubic====
|-
|
Cubic interpolation of f0, f1, f2 and f3 with fraction t.
<lsl>
float fCub(float f0,float f1,float f2,float f3,float t) {
    float P = (f3-f2)-(f0-f1);float Q = (f0-f1)-P;float R = f2-f0;float S = f1;
    return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;
}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| float f0
| Modifier, 0.33~
|-
| float f1
| Start, 0.0
|-
| float f2
| End, 1.0
|-
| float f3
| Modifier, 0.66~
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fCub
| Returns cubic interpolation of four floats
|}
| [[Image:Interp_Chart3.png|center]]
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# FLOAT HERMITE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Float Hermite====
|-
|
Hermite interpolation of f0, f1, f2 and f3 with fraction t, tension and bias.
<lsl>
float fHem(float f0,float f1,float f2,float f3,float t,float tens,float bias){
    float t2 = t*t;float t3 = t2*t;
    float a0 =  (f1-f0)*(1+bias)*(1-tens)/2;
          a0 += (f2-f1)*(1-bias)*(1-tens)/2;
    float a1 =  (f2-f1)*(1+bias)*(1-tens)/2;
          a1 += (f3-f2)*(1-bias)*(1-tens)/2;
    float b0 =  2*t3 - 3*t2 + 1;
    float b1 =    t3 - 2*t2 + t;
    float b2 =    t3 -  t2;
    float b3 = -2*t3 + 3*t2;
    return (  b0  *  f1+b1  *  a0+b2  *  a1+b3  *  f2  );
}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| float f0
| Modifier, 0.33~
|-
| float f1
| Start, 0.0
|-
| float f2
| End, 1.0
|-
| float f3
| Modifier, 0.66~
|-
| float t
| Fraction of interpolation
|-
| float tens
| Tension of interpolation
|-
| float bias
| Bias of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fHem
| Returns hermite interpolation of four floats with tension and bias
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# FLOAT CATMULL-ROM #########-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Float Cubic Catmull-Rom====
|-
|
Catmull-Rom cubic interpolation spline of four floats with fraction t. The four floats are stored in a compact rotation format.
<lsl>
rotation mCat1 = <-0.5,  1.0, -0.5,  0.0>;
rotation mCat2 = < 1.5, -2.5,  0.0,  1.0>;
rotation mCat3 = <-1.5,  2.0,  0.5,  0.0>;
rotation mCat4 = < 0.5, -0.5,  0.0,  0.0>;
float fCatmullRom(rotation H, float t) {
    rotation ABCD = <
        (H.x * mCat1.x) + (H.y * mCat2.x) + (H.z * mCat3.x) + (H.s * mCat4.x),
        (H.x * mCat1.y) + (H.y * mCat2.y) + (H.z * mCat3.y) + (H.s * mCat4.y),
        (H.x * mCat1.z) + (H.y * mCat2.z) + (H.z * mCat3.z) + (H.s * mCat4.z),
        (H.x * mCat1.s) + (H.y * mCat2.s) + (H.z * mCat3.s) + (H.s * mCat4.s)
    >;
    rotation T; T.s = 1.0; T.z = t; T.y = T.z*T.z; T.x = T.y*T.z;
    return T.x*ABCD.x + T.y*ABCD.y + T.z*ABCD.z + T.s*ABCD.s;
}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| rotation H
| <float f0, float f1, float f2, float f3>
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fCatmullRom
| Returns Catmull-Rom cubic interpolation
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
 
<!--############# FLOAT RESCALE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Float Rescale====
|-
|
Rescales a value from one range to another range.
<lsl>
float fScl( float from0, float from1, float to0, float to1, float t ) {
    return to0 + ( (to1 - to0) * ( (from0 - t) / (from0-from1) ) ); }
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| float from0
| 'From' Range minimum
|-
| float from1
| 'From' Range maximum
|-
| float to0
| 'To' Range minimum
|-
| float to1
| 'To' Range maximum
|-
| float t
| 'From' Range value
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fScl
| Returns rescaled value between two different ranges.
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# FLOAT RESCALE FIXED #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Float Rescale Fixed====
|-
|
Rescales a value from one range to another range. The value is clamped between the range.
<lsl>
float fSclFix( float from0, float from1, float to0, float to1, float t ) {
    t = to0 + ( (to1 - to0) * ( (from0 - t) / (from0-from1) ) );
    if(t < to0) t = to0; else if(t > to1) t = to1; return t;
}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| float from0
| 'From' Range minimum
|-
| float from1
| 'From' Range maximum
|-
| float to0
| 'To' Range minimum
|-
| float to1
| 'To' Range maximum
|-
| float t
| 'From' Range value
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fScl
| Returns rescaled and clamped value between two different ranges.
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# FLOAT TARGET ###############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Float Target====
|-
|
Steps float 'Now' closer using float 'Vel' towards 'Target' while clamping between 'Min' and 'Max'. Useful for games, simulations and vehicles. For example keeping realtime track of linear and angular acceleration and velocity of a vehicle.
<lsl>
float fTarget(float Now, float Target, float Min, float Max, float Vel) {
    if(llFabs(Target-Now) < Vel) return Target;
    if(Now < Target) Now += Vel; else Now -= Vel;
    if(Now < Min) Now = Min; else if(Now > Max) Now = Max;
    return Now;
}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| float Now
| 'Now' current value
|-
| float Target
| move 'Now' towards 'Target'
|-
| float Min
| Clamp output at minimum
|-
| float Max
| Clamp output at maximum
|-
| float Vel
| Move 'Now' towards 'Target' at 'Vel' (Velocity)
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return float fTarget
| Returns clamped output of a single step of 'Now' towards 'Target' using 'Vel'
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
 
 
=== Vector Functions ===
 
<!--############# VECTOR LINEAR #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Vector Linear====
|-
|
Linear interpolation of v0 and v1 with fraction t.
<lsl>
vector vLin(vector v0, vector v1,float t){
    return v0*(1-t) + v1*t;}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector v0
| Start, 0.0
|-
| vector v1
| End, 1.0
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return vector vLin
| Returns linear interpolation of two vectors
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# VECTOR COSINE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Vector Cosine====
|-
|
Cosine interpolation of v0 and v1 with fraction t.
<lsl>
vector vCos(vector v0,vector v1,float t){
    float F = (1 - llCos(t*PI))/2;
    return v0*(1-F)+v1*F;}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector v0
| Start, 0.0
|-
| vector v1
| End, 1.0
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return vector vCos
| Returns cosine interpolation of two vectors
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# VECTOR CUBIC #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Vector Cubic====
|-
|
Cubic interpolation of v0, v1, v2 and v3 with fraction t.
<lsl>
vector vCub(vector v0,vector v1,vector v2,vector v3,float t){
    vector P = (v3-v2)-(v1-v0);vector Q = (v1-v0)-P;vector R = v2-v1;vector S = v0;
    return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector v0
| Start Point
|-
| vector v1
| Start Tangent
|-
| vector v2
| End Point
|-
| vector v3
| End Tangent
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return vector vCub
| Returns cubic interpolation of four vectors
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# VECTOR HERMITE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Vector Hermite====
|-
|
Hermite interpolation of v0, v1, v2 and v3 with fraction t, tension and bias.
<lsl>
vector vHem(vector v0,vector v1,vector v2,vector v3,float t,float tens,float bias){
    float t2 = t*t;float t3 = t2*t;
    vector a0 =  (v1-v0)*(1+bias)*(1-tens)/2;
          a0 += (v2-v1)*(1-bias)*(1-tens)/2;
    vector a1 =  (v2-v1)*(1+bias)*(1-tens)/2;
          a1 += (v3-v2)*(1-bias)*(1-tens)/2;
    float b0 =  2*t3 - 3*t2 + 1;
    float b1 =    t3 - 2*t2 + t;
    float b2 =    t3 -  t2;
    float b3 = -2*t3 + 3*t2;
    return (  b0  *  v1+b1  *  a0+b2  *  a1+b3  *  v2  );}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| vector v0
| Modifier, 0.33~
|-
| vector v1
| Start, 0.0
|-
| vector v2
| End, 1.0
|-
| vector v3
| Modifier, 0.66~
|-
| float t
| Fraction of interpolation
|-
| float tens
| Tension of interpolation
|-
| float bias
| Bias of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return vector vHem
| Returns hermite interpolation of four vectors with tension and bias
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
=== Rotation Functions ===
 
<!--############# ROTATION LINEAR #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Rotation Linear====
|-
|
Spherical Linear interpolation of r0 and r1 with fraction t.
Also known as '''SLERP'''
<lsl>
rotation rLin(rotation r0,rotation r1,float t){
    // Spherical-Linear Interpolation
    float ang = llAngleBetween(r0, r1);
    if( ang > PI) ang -= TWO_PI;
    return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*t);}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| rotation r0
| Start, 0.0
|-
| rotation r1
| End, 1.0
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return rotation rLin
| Returns spherical linear interpolation of two rotations
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# ROTATION COSINE #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Rotation Cosine====
|-
|
Spherical Cosine interpolation of r0 and r1 with fraction t.
I liken to call it as '''SCORP'''
<lsl>
rotation rCos(rotation r0,rotation r1,float t){
    // Spherical-Cosine Interpolation
    float f = (1 - llCos(t*PI))/2;
    float ang = llAngleBetween(r0, r1);
    if( ang > PI) ang -= TWO_PI;
    return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*f);}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| rotation r0
| Start, 0.0
|-
| rotation r1
| End, 1.0
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return rotation rCos
| Returns spherical cosine interpolation of two rotations
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
<!--############# ROTATION CUBIC #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Rotation Cubic====
|-
|
Spherical Cubic interpolation of r0 and r1 with fraction t.
I liken to call it as '''SCURP'''
<lsl>
rotation rCub(rotation r0,rotation r1,rotation r2,rotation r3,float t){
    // Spherical-Cubic Interpolation
    // r0 = Start, r1 = End, r2 and r3 affect path of curve!
    return rLin( rLin(r0,r1,t), rLin(r2,r3,t), 2*t*(1-t) );}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| rotation r0
| Start, 0.0
|-
| rotation r1
| End, 1.0
|-
| rotation r2
| Modifier, 0.33~
|-
| rotation r3
| Modifier, 0.66~
|-
| float t
| Fraction of interpolation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return rotation rCub
| Returns spherical cubic interpolation of four rotations
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| function rotation rLin(rotation r0,rotation r1,float t)
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
=== Vector List ===
 
<!--############# VECTOR LIST LINEAR #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Vector List, Linear====
|-
|
Interpolates between two vectors in a list of vectors.
<lsl>
vector pLin(list v, float t, integer Loop ){
  float l = llGetListLength(v); t *= l-1;
  float f = (float)llFloor(t);
  integer i1 = 0; integer i2 = 0;
  if(Loop){ i1 = (integer)(f-llFloor(f/l)*l);
        ++f;i2 = (integer)(f-llFloor(f/l)*l);}
  else {
    if(  f > l-1 ) i1 = (integer)l-1;
    else if(  f >= 0 ) i1 = (integer)f;
    if(f+1 > l-1 ) i2 = (integer)l-1;
    else if(f+1 >= 0 ) i2 = (integer)f+1; }
  vector v1 = llList2Vector(v, i1);
  vector v2 = llList2Vector(v, i2);
  return vLin( v1, v2, t-f );}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| list v
| A path of vectors.
|-
| float t
| Interpolation, Start 0.0 - 1.0 End.
|-
| integer Loop
| Whether the list loops over.
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return vector pLin
| Returns a vector that is the linear interpolation of two vectors between 't'.
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
 
=== Speed Controlled ===
 
<!--############# ROTATION COSINE AIM #############-->
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
!style="color: #000000; background-color: #aaaaff;" height="20px"|
====Rotation Cosine Aim====
|-
|
Spherical Cosine interpolation of r0 and r1 with speed regulation.
Does the entire animation loop to rotate between r0 to r1 with a specific speed, with the cosine interpolation it makes it appear to accelerate and deccelerate realistically.
<lsl>
rCosAim( rotation r0, rotation r1, float speed ){
    float ang = llAngleBetween(r0, r1) * RAD_TO_DEG;
    if( ang > PI) ang -= TWO_PI;
    float x; float y = (ang/speed)/0.2;
    for( x = 0.0; x < y; x += 1.0 )
        llSetRot( rCos( r0, r1, x/y ) );
}
</lsl>
 
{|cellspacing="0" cellpadding="3" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #ffffff; border-collapse: collapse" width="80%"
|
{|cellspacing="0" cellpadding="6" border="1" style="border: 1px solid #aaaaaa; margin: 1em 1em 1em 0pt; background-color: #e0e0ff; border-collapse: collapse"
!style="background-color: #d0d0ee" | Input
!style="background-color: #d0d0ee" | Description
|-
| rotation r0
| Start, 0.0
|-
| rotation r1
| End, 1.0
|-
| float speed
| Speed of animation
|-
!style="background-color: #d0d0ee" | Output
!style="background-color: #d0d0ee" | Description
|-
| return rotation rCos
| Creates a spherical cosine animation of two rotations with a specific speed
|-
!style="background-color: #eed0d0" colspan="2"| Requirement
|-
|style="background-color: #eed0d0" colspan="2"| function rotation rCos(rotation r0,rotation r1,float t)
|}
| Graph goes here, k.
|}
<div style="float:right;font-size: 80%;">
By Nexii Malthus</div>
|}
 
== Old non-documented Library ==
 
Changes/
1.0-1.1 - Added rotation types
1.1-1.2 - Added Hermite for float and vector
 
[[Interpolation/Examples|Example Script]]
 
<lsl>
//===================================================//
//              Interpolation Library 1.2          //
//              "May 12 2008", "6:16:20 GMT-0"      //
//  Copyright (C) 2008, Nexii Malthus (cc-by)        //
//    http://creativecommons.org/licenses/by/3.0/    //
//===================================================//
 
float fLin(float v0, float v1,float t){
    return v0*(1-t) + v1*t;}


float fCos(float v0,float v1,float t){
Linear Interpolation
    float F = (1 - llCos(t*PI))/2;
* [[Interpolation/Linear/Float|Float]]
    return v0*(1-F)+v1*F;}
* [[Interpolation/Linear/Vector|Vector]]
* [[Interpolation/Linear/Rotation|Rotation]]
* List of [[Interpolation/Linear/Vectors|Vectors]]


float fCub(float v0,float v1,float v2,float v3,float t){
    float P = (v3-v2)-(v0-v1);float Q = (v0-v1)-P;float R = v2-v0;float S = v1;
    return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;}


float fHem(float v0,float v1,float v2,float v3,float t,float tens,float bias){
Cosine Interpolation
    float t2 = t*t;float t3 = t2*t;
* [[Interpolation/Cosine/Float|Float]]
    float a0 =  (v1-v0)*(1+bias)*(1-tens)/2;
* [[Interpolation/Cosine/Vector|Vector]]
          a0 += (v2-v1)*(1-bias)*(1-tens)/2;
* [[Interpolation/Cosine/Rotation|Rotation]]
    float a1 =  (v2-v1)*(1+bias)*(1-tens)/2;
          a1 += (v3-v2)*(1-bias)*(1-tens)/2;
    float b0 =  2*t3 - 3*t2 + 1;
    float b1 =    t3 - 2*t2 + t;
    float b2 =    t3 -  t2;
    float b3 = -2*t3 + 3*t2;
    return (  b0  *  v1+b1  *  a0+b2  *  a1+b3  *  v2  );}


vector vLin(vector v0, vector v1,float t){
    return v0*(1-t) + v1*t;}
Cubic Interpolation
* [[Interpolation/Cubic/Float|Float]]
* [[Interpolation/Cubic/Vector|Vector]]
* [[Interpolation/Cubic/Rotation|Rotation]]


vector vCos(vector v0,vector v1,float t){
    float F = (1 - llCos(t*PI))/2;
    return v0*(1-F)+v1*F;}


vector vCub(vector v0,vector v1,vector v2,vector v3,float t){
Catmull-Rom Cubic Interpolation
    vector P = (v3-v2)-(v0-v1);vector Q = (v0-v1)-P;vector R = v2-v0;vector S = v1;
* [[Interpolation/Catmull-Rom/Float|Float]]
    return P*llPow(t,3) + Q*llPow(t,2) + R*t + S;}


vector vHem(vector v0,vector v1,vector v2,vector v3,float t,float tens,float bias){
    float t2 = t*t;float t3 = t2*t;
    vector a0 =  (v1-v0)*(1+bias)*(1-tens)/2;
          a0 += (v2-v1)*(1-bias)*(1-tens)/2;
    vector a1 =  (v2-v1)*(1+bias)*(1-tens)/2;
          a1 += (v3-v2)*(1-bias)*(1-tens)/2;
    float b0 =  2*t3 - 3*t2 + 1;
    float b1 =    t3 - 2*t2 + t;
    float b2 =    t3 -  t2;
    float b3 = -2*t3 + 3*t2;
    return (  b0  *  v1+b1  *  a0+b2  *  a1+b3  *  v2  );}


rotation rLin(rotation r0,rotation r1,float t){
Hermite Interpolation
    // Spherical-Linear Interpolation
* [[Interpolation/Hermite/Float|Float]]
    float ang = llAngleBetween(r0, r1);
* [[Interpolation/Hermite/Vector|Vector]]
    if( ang > PI) ang -= TWO_PI;
    return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*t);}


rotation rCos(rotation r0,rotation r1,float t){
Spline Interpolation
    // Spherical-Cosine Interpolation
* List of [[Interpolation/Spline/Vectors|Vectors]]
    float f = (1 - llCos(t*PI))/2;
    float ang = llAngleBetween(r0, r1);
    if( ang > PI) ang -= TWO_PI;
    return r0 * llAxisAngle2Rot( llRot2Axis(r1/r0)*r0, ang*f);}


rotation rCub(rotation r0,rotation r1,rotation r2,rotation r3,float t){
Rescale
    // Spherical-Cubic Interpolation
* [[Interpolation/Rescale/Float|Float]]
    // r0 = Start, r1 = End, r2 and r3 affect path of curve!
* [[Interpolation/Rescale/FloatFixed|Float Fixed]]
    return rLin( rLin(r0,r1,t), rLin(r2,r3,t), 2*t*(1-t) );}


default{state_entry(){}}


</lsl>
Target
* [[Interpolation/Target/Float|Float]]

Latest revision as of 12:10, 16 September 2011

Interpolation is a way of constructing new data points within a range of known data points.

The range of applications is varied, but here in SL it can most directly be used for animating and/or moving prims or linksets via any attribute you can creatively make use of.

Attributes:

  • Prim/Object Position
  • Prim/Object Rotation
  • Texture Scale/Offsets
  • or you can just simply interpolate internal values

Potential applications:

  • Movement
  • Animation
  • User Interfaces
  • Graphics
  • Games

There are many different types of interpolation, which may exhibit different preferred or controllable behaviour.


Interpolation Library

Linear Interpolation


Cosine Interpolation


Cubic Interpolation


Catmull-Rom Cubic Interpolation


Hermite Interpolation

Spline Interpolation

Rescale


Target