1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.egg.landroid
18 
19 import kotlin.math.exp
20 import kotlin.math.pow
21 
22 /** smoothstep. Ken Perlin's version */
smoothnull23 fun smooth(x: Float): Float {
24     return x * x * x * (x * (x * 6 - 15) + 10)
25 }
26 
27 /** Kind of like an inverted smoothstep, but */
invsmoothishnull28 fun invsmoothish(x: Float): Float {
29     return 0.25f * ((2f * x - 1f).pow(5f) + 1f) + 0.5f * x
30 }
31 
32 /** Compute the fraction that progress represents between start and end (inverse of lerp). */
lexpnull33 fun lexp(start: Float, end: Float, progress: Float): Float {
34     return (progress - start) / (end - start)
35 }
36 
37 /** Exponentially smooth current toward target by a factor of speed. */
expSmoothnull38 fun expSmooth(current: Float, target: Float, dt: Float = 1f / 60, speed: Float = 5f): Float {
39     return current + (target - current) * (1 - exp(-dt * speed))
40 }
41