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

Commit 937fe0b

Browse files
committed
more cleanup
1 parent e543e92 commit 937fe0b

35 files changed

+859
-7088
lines changed

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

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package curves
1717

1818
import java.util.*
19+
import kotlin.math.cos
20+
import kotlin.math.hypot
21+
import kotlin.math.sin
1922

2023
/**
2124
* This provides a curve fit system that stitches the x,y path together with
@@ -29,7 +32,9 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
2932
mArcs = arrayOfNulls(timePoints.size - 1)
3033
var mode = START_VERTICAL
3134
var last = START_VERTICAL
32-
for (i in mArcs.indices) {
35+
36+
mArcs = Array(timePoints.size - 1) {it->
37+
val i = it
3338
when (arcModes[i]) {
3439
ARC_START_VERTICAL -> {
3540
mode = START_VERTICAL
@@ -50,7 +55,7 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
5055
ARC_ABOVE -> mode = UP_ARC
5156
ARC_BELOW -> mode = DOWN_ARC
5257
}
53-
mArcs[i] = Arc(mode, timePoints[i], timePoints[i + 1], y[i][0], y[i][1], y[i + 1][0], y[i + 1][1])
58+
Arc(mode, timePoints[i], timePoints[i + 1], y[i][0], y[i][1], y[i + 1][0], y[i + 1][1])
5459
}
5560
}
5661

@@ -258,7 +263,7 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
258263
mX2 = x2
259264
mY1 = y1
260265
mY2 = y2
261-
mArcDistance = Math.hypot(dy.toDouble(), dx.toDouble()).toFloat()
266+
mArcDistance = hypot(dy, dx).toFloat()
262267
mArcVelocity = mArcDistance * mOneOverDeltaTime
263268
mEllipseCenterX = dx / (mTime2 - mTime1) // cache the slope in the unused center
264269
mEllipseCenterY = dy / (mTime2 - mTime1) // cache the slope in the unused center
@@ -279,8 +284,8 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
279284
fun setPoint(time: Float) {
280285
val percent = (if (mVertical) mTime2 - time else time - mTime1) * mOneOverDeltaTime
281286
val angle = Math.PI.toFloat() * 0.5f * lookup(percent)
282-
mTmpSinAngle = Math.sin(angle.toDouble()).toFloat()
283-
mTmpCosAngle = Math.cos(angle.toDouble()).toFloat()
287+
mTmpSinAngle = sin(angle)
288+
mTmpCosAngle = cos(angle)
284289
}
285290

286291
fun calcX(): Float {
@@ -294,14 +299,14 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
294299
fun calcDX(): Float {
295300
val vx = mEllipseA * mTmpCosAngle
296301
val vy = -mEllipseB * mTmpSinAngle
297-
val norm = mArcVelocity / Math.hypot(vx.toDouble(), vy.toDouble()).toFloat()
302+
val norm = mArcVelocity / hypot(vx, vy)
298303
return if (mVertical) -vx * norm else vx * norm
299304
}
300305

301306
fun calcDY(): Float {
302307
val vx = mEllipseA * mTmpCosAngle
303308
val vy = -mEllipseB * mTmpSinAngle
304-
val norm = mArcVelocity / Math.hypot(vx.toDouble(), vy.toDouble()).toFloat()
309+
val norm = mArcVelocity / hypot(vx, vy)
305310
return if (mVertical) -vy * norm else vy * norm
306311
}
307312

@@ -346,12 +351,12 @@ class ArcSpline(arcModes: IntArray, val timePoints: FloatArray, y: List<FloatArr
346351
var dist = 0f
347352
for (i in sOurPercent.indices) {
348353
val angle = Math.toRadians(90.0 * i / (sOurPercent.size - 1)).toFloat()
349-
val s = Math.sin(angle.toDouble()).toFloat()
350-
val c = Math.cos(angle.toDouble()).toFloat()
354+
val s = sin(angle)
355+
val c = cos(angle)
351356
val px = a * s
352357
val py = b * c
353358
if (i > 0) {
354-
dist += Math.hypot((px - lx).toDouble(), (py - ly).toDouble()).toFloat()
359+
dist += hypot((px - lx), (py - ly)).toFloat()
355360
sOurPercent[i] = dist
356361
}
357362
lx = px

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

+29-20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package curves
1717

1818
import java.util.*
19+
import kotlin.collections.ArrayList
1920
import kotlin.math.abs
2021
import kotlin.math.cos
2122
import kotlin.math.sign
@@ -35,7 +36,7 @@ class Cycles {
3536
private var mPI2 = Math.PI.toFloat() * 2
3637
private var mNormalized = false
3738
override fun toString(): String {
38-
return "pos =" + Arrays.toString(mPosition) + " period=" + Arrays.toString(mPeriod)
39+
return "pos =" + mPosition.contentToString() + " period=" + mPeriod.contentToString()
3940
}
4041

4142
// @TODO: add description
@@ -47,6 +48,16 @@ class Cycles {
4748
}
4849
}
4950

51+
/**
52+
* Clear points
53+
*/
54+
fun clearPoints() {
55+
mPosition = floatArrayOf()
56+
mPeriod = floatArrayOf()
57+
mArea = FloatArray(0)
58+
mNormalized = false
59+
}
60+
5061
/**
5162
* This adds a point in the cycle positions are from 0..1
5263
* period represents the number of oscillations.
@@ -83,7 +94,7 @@ class Cycles {
8394
for (i in 1 until mPeriod.size) {
8495
val h = (mPeriod[i - 1] + mPeriod[i]) / 2
8596
val w = mPosition[i] - mPosition[i - 1]
86-
totalArea = totalArea + w * h
97+
totalArea += w * h
8798
}
8899
// scale periods to normalize it
89100
for (i in mPeriod.indices) {
@@ -103,19 +114,18 @@ class Cycles {
103114
* that point in time.
104115
*/
105116
private fun getP(time: Float): Float {
106-
var time = time
107-
if (time < 0) {
108-
time = 0f
109-
} else if (time > 1) {
110-
time = 1f
117+
var t = time
118+
if (t < 0) {
119+
t = 0f
120+
} else if (t > 1) {
121+
t = 1f
111122
}
112-
var index = Arrays.binarySearch(mPosition, time)
123+
var index = Arrays.binarySearch(mPosition, t)
113124
var p = 0f
114125
if (index > 0) {
115126
p = 1f
116127
} else if (index != 0) {
117128
index = -index - 1
118-
val t = time
119129
val m = ((mPeriod[index] - mPeriod[index - 1])
120130
/ (mPosition[index] - mPosition[index - 1]))
121131
p =
@@ -149,31 +159,32 @@ class Cycles {
149159
}
150160

151161
/**
152-
* Get the differential dValue/dt
162+
* Get the differential of the phase(Dphase/Dt)
153163
*/
154164
fun getDP(time: Float): Float {
155-
var time = time
156-
if (time <= 0) {
157-
time = 0.00001f
158-
} else if (time >= 1) {
159-
time = .999999f
165+
var t = time
166+
if (t <= 0) {
167+
t = 0.00001f
168+
} else if (t >= 1) {
169+
t = .999999f
160170
}
161-
var index = Arrays.binarySearch(mPosition, time)
171+
var index = Arrays.binarySearch(mPosition, t)
162172
var p = 0f
163173
if (index > 0) {
164174
return 0f
165175
}
166176
if (index != 0) {
167177
index = -index - 1
168-
val t = time
169178
val m = ((mPeriod[index] - mPeriod[index - 1])
170179
/ (mPosition[index] - mPosition[index - 1]))
171180
p = m * t + (mPeriod[index - 1] - m * mPosition[index - 1])
172181
}
173182
return p
174183
}
175184

176-
// @TODO: add description
185+
/**
186+
* Get the differential of the value dv/dt needs the differential of the phase function
187+
*/
177188
fun getSlope(time: Float, phase: Float, dphase: Float): Float {
178189
val angle = phase + getP(time)
179190
val dangle_dtime = getDP(time) + dphase
@@ -204,8 +215,6 @@ class Cycles {
204215
/**
205216
* This builds a monotonic spline to be used as a wave function
206217
*/
207-
208-
209218
private fun buildWave(values: FloatArray): MonoSpline {
210219
val length = values.size * 3 - 2
211220
val len = values.size - 1

0 commit comments

Comments
 (0)