Inline AnyScript implementation of a smooth step function with zero 1st and 2nd derivative at the edges.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// Inline AnyScript implementation of the following C++ function: | |
// https://en.wikipedia.org/wiki/Smoothstep | |
float smootherstep(float edge0, float edge1, float x) | |
{ | |
// Scale, and clamp x to 0..1 range | |
x = clamp((x - edge0)/(edge1 - edge0), 0.0, 1.0); | |
// Evaluate polynomial | |
return x*x*x*(x*(x*6 - 15) + 10); | |
} | |
Usage: | |
Include the file in your model. | |
#include "smootherstep.any" | |
*/ | |
// 1st and 2nd order derivatives at edge0 and edge1 are 0 | |
#define smootherstep(edge0, edge1, val) mult(iffun(ltfun((val-edge0)/(edge1-edge0),0.0), 0*val, iffun(gtfun((val-edge0)/(edge1-edge0),1.0),0*val+1.0, (val-edge0)/(edge1-edge0)))\ | |
,mult(iffun(ltfun((val-edge0)/(edge1-edge0),0.0), 0*val, iffun(gtfun((val-edge0)/(edge1-edge0),1.0),0*val+1.0, (val-edge0)/(edge1-edge0)))\ | |
,mult(iffun(ltfun((val-edge0)/(edge1-edge0),0.0), 0*val, iffun(gtfun((val-edge0)/(edge1-edge0),1.0),0*val+1.0, (val-edge0)/(edge1-edge0)))\ | |
,(mult(iffun(ltfun((val-edge0)/(edge1-edge0),0.0), 0*val, iffun(gtfun((val-edge0)/(edge1-edge0),1.0),0*val+1.0, (val-edge0)/(edge1-edge0)))\ | |
,(iffun(ltfun((val-edge0)/(edge1-edge0),0.0), 0*val, iffun(gtfun((val-edge0)/(edge1-edge0),1.0),0*val+1.0, (val-edge0)/(edge1-edge0)))\ | |
*6 - 15)) + 10) ))) | |
#if ANYBODY_FILENAME_MAINFILE == "smootherstep.any" | |
// Test code. Only active if the file is loaded as main | |
Main = | |
{ | |
// Run the function on a vector input | |
AnyVector x_test = farr(-1.0, 0.1, 100); | |
AnyVector y_test = smootherstep(0, 3, x_test); | |
AnyBodyStudy test_study = | |
{ | |
Gravity = {0,0,0}; | |
// Run the function on single values as part of a study. | |
AnyVar test_curve = smootherstep(0.3, 0.8, t); | |
}; | |
}; | |
#endif | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment