Difference between revisions of "Interpolation"

From Second Life Wiki
Jump to navigation Jump to search
(Revising entire Interpolation Library to something like Geometric Library is)
 
(18 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
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
|}
| Graph goes here, k.
|}
<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"|
Cosine
|-
|
Cosine interpolation of f0 and f1 with fraction t.
<lsl>
float fCos(float v0,float 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%"
== Interpolation Library ==
|
{|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
|}
| 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