Any reason why interpolateLinear(…) is clamped? [Solved, use extrapolateLinear]

I’ve noticed that interpolateLinear is restrained from 0 to 1 and I’m wondering if there was a specific reason for it.



I’ve modified the method to remove those restraints when used with my lens flare and there’s no problem. The method interpolates correctly as seen in the screen shot below. The negative interpolations are circled yellow, those over 1 are circled blue. The interpolated vector is in purple. It works quite nicely, but I’m not sure there could be other ways to use that interpolation where it might or not work… Hence my posting.



The modified method is the one starting at line 106 in FastMath.

[java]

public static float interpolateLinear(float scale, float startValue, float endValue) {

if (startValue == endValue) {

return startValue;

}

// if (scale <= 0f) {

// return startValue;

// }

// if (scale >= 1f) {

// return endValue;

// }

return ((1f - scale) * startValue) + (scale * endValue);

}

[/java]



I can’t think of any, unless it is called internally within the engine and was put there as a check. IIRC Most, if not all of, the math package was pulled wholesale from jME2 so it could be a requirement from back then…



The LERP function from jME2’s FastMath is the best I could find… The FastMath class happens to be one that had a bunch of changes and added functionality…



[java]

/**

  • Linear interpolation from startValue to endValue by the given percent.
  • Basically: ((1 - percent) * startValue) + (percent * endValue)

    *
  • @param percent
  •        Percent value to use.<br />
    
  • @param startValue
  •        Begining value. 0% of f<br />
    
  • @param endValue
  •        ending value. 100% of f<br />
    
  • @return The interpolated value between startValue and endValue.

    */

    public static float LERP(float percent, float startValue, float endValue) {

    if (startValue == endValue) return startValue;

    return ((1 - percent) * startValue) + (percent * endValue);

    }

    [/java]

I think theres a new method that also extrapolates. The reason is probably that interpolation implies that its a value between the given values and not outside.

1 Like
@normen said:
I think theres a new method that also extrapolates. The reason is probably that interpolation implies that its a value between the given values and not outside.


Gah. Didn't notice extrapolateLinear(...).

After a quick look at the method I see that if the scale is smaller than 1, it calls interpolateLinear, which will not give a negative result (clamped between 0 and 1).

ExtrapolateLinear will also clamp if below 0.

Good point, that should probably be changed :slight_smile:

Haha, so extrapolates actually does the exact same thing as interpolates. :stuck_out_tongue:

After commenting and testing extrapolates seems to work fine. It gives the same results as the screen shot above.



[patch]

@@ -166,9 +166,9 @@

  • @return an extrapolation for the given parameters

    */

    public static float extrapolateLinear(float scale, float startValue, float endValue) {
  •    if (scale &lt;= 0f) {<br />
    
  •        return startValue;<br />
    
  •    }<br />
    

+// if (scale <= 0f) {

+// return startValue;

+// }

return ((1f - scale) * startValue) + (scale * endValue);

}



@@ -187,9 +187,9 @@

if (store == null) {

store = new Vector3f();

}

  •    if (scale &lt;= 1f) {<br />
    
  •        return interpolateLinear(scale, startValue, endValue, store);<br />
    
  •    }<br />
    

+// if (scale <= 1f) {

+// return interpolateLinear(scale, startValue, endValue, store);

+// }

store.x = extrapolateLinear(scale, startValue.x, endValue.x);

store.y = extrapolateLinear(scale, startValue.y, endValue.y);

store.z = extrapolateLinear(scale, startValue.z, endValue.z);

[/patch]

@sbook said:
I can't think of any, unless it is called internally within the engine and was put there as a check. IIRC Most, if not all of, the math package was pulled wholesale from jME2 so it could be a requirement from back then..

The LERP function from jME2's FastMath is the best I could find.. The FastMath class happens to be one that had a bunch of changes and added functionality..

[java]
/**
* Linear interpolation from startValue to endValue by the given percent.
* Basically: ((1 - percent) * startValue) + (percent * endValue)
*
* @param percent
* Percent value to use.
* @param startValue
* Begining value. 0% of f
* @param endValue
* ending value. 100% of f
* @return The interpolated value between startValue and endValue.
*/
public static float LERP(float percent, float startValue, float endValue) {
if (startValue == endValue) return startValue;
return ((1 - percent) * startValue) + (percent * endValue);
}
[/java]


I know it's a moot point but if you have a method that has to be clamped using interpolates, then I would think it's the method's creator to make sure it's clamped before calling interpolates. Just saying. :)
@madjack said:
I know it's a moot point but if you have a method that has to be clamped using interpolates, then I would think it's the method's creator to make sure it's clamped before calling interpolates. Just saying. :)


Agreed. Or throw an exception (though an exception might be superfluous in this case).

@normen Want me to commit those commented lines or do you want to thoroughly test it first?

Yeah you can commit them, seems sound.

It seems I was also removed from GoogleCode (when I was removed from here in the contributor’s list). :frowning: I can’t commit. GoogleCode doesn’t list jME3 in my list of project.

Forgot:


svn: Commit failed (details follow):
svn: MKACTIVITY of '/svn/!svn/act/4ca16e86-3301-0010-8f28-3560c47b1a70': 403 Forbidden (https://jmonkeyengine.googlecode.com)

PM me your Google address

Sent.

Committed.