Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit c92531c

Browse files
committed
fix cycles (phase 1) graph print demos
1 parent 93f1276 commit c92531c

File tree

3 files changed

+399
-107
lines changed

3 files changed

+399
-107
lines changed

desktop/interpolationEngines/src/main/kotlin/curves/Cycles.kt

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
package curves
1717

1818
import java.util.*
19+
import kotlin.math.abs
20+
import kotlin.math.cos
21+
import kotlin.math.sign
22+
import kotlin.math.sin
1923

2024
/**
2125
* This generates variable frequency oscillation curves
@@ -26,7 +30,7 @@ class Cycles {
2630
private var mPeriod = floatArrayOf()
2731
private var mPosition = floatArrayOf()
2832
private lateinit var mArea: FloatArray
29-
private var mCustomType: String? = null
33+
private var mCustomType: FloatArray? = null
3034
private var mCustomCurve: MonoSpline? = null
3135
private var mType = 0
3236
private var mPI2 = Math.PI.toFloat() * 2
@@ -36,10 +40,10 @@ class Cycles {
3640
}
3741

3842
// @TODO: add description
39-
fun setType(type: Int, customType: String) {
43+
fun setType(type: Int, customType: FloatArray?) {
4044
mType = type
4145
mCustomType = customType
42-
if (mCustomType != null) {
46+
if (customType != null) {
4347
mCustomCurve = buildWave(customType)
4448
}
4549
}
@@ -87,7 +91,7 @@ class Cycles {
8791
mNormalized = true
8892
}
8993

90-
fun getP(time: Float): Float {
94+
private fun getP(time: Float): Float {
9195
var time = time
9296
if (time < 0) {
9397
time = 0f
@@ -103,7 +107,8 @@ class Cycles {
103107
val t = time
104108
val m = ((mPeriod[index] - mPeriod[index - 1])
105109
/ (mPosition[index] - mPosition[index - 1]))
106-
p = mArea[index - 1] + (mPeriod[index - 1] - m * mPosition[index - 1]) * (t - mPosition[index - 1]) + m * (t * t - mPosition[index - 1] * mPosition[index - 1]) / 2
110+
p =
111+
mArea[index - 1] + (mPeriod[index - 1] - m * mPosition[index - 1]) * (t - mPosition[index - 1]) + m * (t * t - mPosition[index - 1] * mPosition[index - 1]) / 2
107112
}
108113
return p
109114
}
@@ -112,19 +117,19 @@ class Cycles {
112117
fun getValue(time: Float, phase: Float): Float {
113118
val angle = phase + getP(time) // angle is / by 360
114119
return when (mType) {
115-
SIN_WAVE -> Math.sin((mPI2 * angle).toDouble()).toFloat()
116-
SQUARE_WAVE -> Math.signum(0.5 - angle % 1).toFloat()
117-
TRIANGLE_WAVE -> 1 - Math.abs((angle * 4 + 1) % 4 - 2)
120+
SIN_WAVE -> sin(mPI2 * angle)
121+
SQUARE_WAVE -> sign(0.5f - angle % 1)
122+
TRIANGLE_WAVE -> 1 - abs((angle * 4 + 1) % 4 - 2)
118123
SAW_WAVE -> (angle * 2 + 1) % 2 - 1
119124
REVERSE_SAW_WAVE -> 1 - (angle * 2 + 1) % 2
120-
COS_WAVE -> Math.cos((mPI2 * (phase + angle)).toDouble()).toFloat()
125+
COS_WAVE -> cos(mPI2 * (phase + angle))
121126
BOUNCE -> {
122127
val x = 1 - Math.abs(angle * 4 % 4 - 2)
123128
1 - x * x
124129
}
125130

126131
CUSTOM -> mCustomCurve!!.getPos((angle % 1), 0)
127-
else -> Math.sin((mPI2 * angle).toDouble()).toFloat()
132+
else -> sin(mPI2 * angle)
128133
}
129134
}
130135

@@ -155,20 +160,19 @@ class Cycles {
155160
val angle = phase + getP(time)
156161
val dangle_dtime = getDP(time) + dphase
157162
return when (mType) {
158-
SIN_WAVE -> (mPI2 * dangle_dtime * Math.cos((mPI2 * angle).toDouble())).toFloat()
163+
SIN_WAVE -> (mPI2 * dangle_dtime * cos((mPI2 * angle)))
159164
SQUARE_WAVE -> 0f
160165
TRIANGLE_WAVE -> 4 * dangle_dtime * Math.signum((angle * 4 + 3) % 4 - 2)
161166
SAW_WAVE -> dangle_dtime * 2
162167
REVERSE_SAW_WAVE -> -dangle_dtime * 2
163-
COS_WAVE -> (-mPI2 * dangle_dtime * Math.sin((mPI2 * angle).toDouble())).toFloat()
168+
COS_WAVE -> -mPI2 * dangle_dtime * sin(mPI2 * angle)
164169
BOUNCE -> 4 * dangle_dtime * ((angle * 4 + 2) % 4 - 2)
165170
CUSTOM -> mCustomCurve!!.getSlope((angle % 1), 0)
166-
else -> (mPI2 * dangle_dtime * Math.cos((mPI2 * angle).toDouble())).toFloat()
171+
else -> mPI2 * dangle_dtime * cos((mPI2 * angle))
167172
}
168173
}
169174

170175
companion object {
171-
var TAG = "Oscillator"
172176
const val SIN_WAVE = 0 // theses must line up with attributes
173177
const val SQUARE_WAVE = 1
174178
const val TRIANGLE_WAVE = 2
@@ -177,45 +181,33 @@ class Cycles {
177181
const val COS_WAVE = 5
178182
const val BOUNCE = 6
179183
const val CUSTOM = 7
184+
}
185+
186+
/**
187+
* This builds a monotonic spline to be used as a wave function
188+
*/
180189

181-
/**
182-
* This builds a monotonic spline to be used as a wave function
183-
*/
184-
fun buildWave(configString: String): MonoSpline {
185-
// done this way for efficiency
186-
val values = FloatArray(configString.length / 2)
187-
var start = configString.indexOf('(') + 1
188-
var off1 = configString.indexOf(',', start)
189-
var count = 0
190-
while (off1 != -1) {
191-
val tmp = configString.substring(start, off1).trim { it <= ' ' }
192-
values[count++] = tmp.toFloat()
193-
off1 = configString.indexOf(',', off1 + 1.also { start = it })
194-
}
195-
off1 = configString.indexOf(')', start)
196-
val tmp = configString.substring(start, off1).trim { it <= ' ' }
197-
values[count++] = tmp.toFloat()
198-
return buildWave(Arrays.copyOf(values, count))
199-
}
200190

201-
private fun buildWave(values: FloatArray): MonoSpline {
202-
val length = values.size * 3 - 2
203-
val len = values.size - 1
204-
val gap = 1.0f / len
205-
val points = ArrayList<FloatArray>(length)
206-
val time = FloatArray(length)
207-
for (i in values.indices) {
208-
val v = values[i]
209-
points[i + len][0] = v
210-
time[i + len] = i * gap
211-
if (i > 0) {
212-
points[i + len * 2][0] = v + 1
213-
time[i + len * 2] = i * gap + 1
214-
points[i - 1][0] = v - 1 - gap
215-
time[i - 1] = i * gap + -1 - gap
216-
}
191+
private fun buildWave(values: FloatArray): MonoSpline {
192+
val length = values.size * 3 - 2
193+
val len = values.size - 1
194+
val gap = 1.0f / len
195+
val points = ArrayList<FloatArray>(length)
196+
for (i in 0 until length) {
197+
points.add(FloatArray(1))
198+
}
199+
val time = FloatArray(length)
200+
for (i in values.indices) {
201+
val v = values[i]
202+
points[i + len][0] = v
203+
time[i + len] = i * gap
204+
if (i > 0) {
205+
points[i + len * 2][0] = v + 1
206+
time[i + len * 2] = i * gap + 1
207+
points[i - 1][0] = v - 1 - gap
208+
time[i - 1] = i * gap + -1 - gap
217209
}
218-
return MonoSpline(time, points)
219210
}
211+
return MonoSpline(time, points)
220212
}
221-
}
213+
}

0 commit comments

Comments
 (0)