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

Commit c373aa9

Browse files
committed
graphs more cleanup
1 parent 59f16b5 commit c373aa9

File tree

3 files changed

+1225
-88
lines changed

3 files changed

+1225
-88
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,19 @@
1515
*/
1616
package curves
1717

18+
import kotlin.math.hypot
19+
1820
/**
1921
* This performs a spline interpolation in multiple dimensions
2022
*
2123
*/
2224
class MonoSpline(time: FloatArray, y: List<FloatArray>) {
2325
private val timePoints: FloatArray
24-
var mY: ArrayList<FloatArray>
26+
private var mY: ArrayList<FloatArray>
2527
private var mTangent: ArrayList<FloatArray>
2628
private val mExtrapolate = true
2729
private var mSlopeTemp: FloatArray
2830

29-
fun makeFloatArray(a: Int, b: Int): ArrayList<FloatArray> {
30-
val ret = ArrayList<FloatArray>() //new Float[a][b];
31-
for (i in 0 until a) {
32-
ret.add(FloatArray(b))
33-
}
34-
return ret
35-
}
36-
3731
init {
3832
val n = time.size
3933
val dim = y[0].size
@@ -60,7 +54,7 @@ class MonoSpline(time: FloatArray, y: List<FloatArray>) {
6054
} else {
6155
val a = tangent[i][j] / slope[i][j]
6256
val b = tangent[i + 1][j] / slope[i][j]
63-
val h = Math.hypot(a.toDouble(), b.toDouble()).toFloat()
57+
val h = hypot(a , b)
6458
if (h > 9.0) {
6559
val t = 3.0f / h
6660
tangent[i][j] = t * a * slope[i][j]
@@ -74,6 +68,14 @@ class MonoSpline(time: FloatArray, y: List<FloatArray>) {
7468
mTangent = tangent
7569
}
7670

71+
private fun makeFloatArray(a: Int, b: Int): ArrayList<FloatArray> {
72+
val ret = ArrayList<FloatArray>() //new Float[a][b];
73+
for (i in 0 until a) {
74+
ret.add(FloatArray(b))
75+
}
76+
return ret
77+
}
78+
7779
private fun copyData(y: List<FloatArray>): ArrayList<FloatArray> {
7880
val ret = ArrayList<FloatArray>()
7981
for (array in y) {

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

Lines changed: 67 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package curves
1717

18+
import kotlin.math.sqrt
19+
1820
/**
1921
* Provides spline interpolation code.
20-
* Currently not used but it is anticipated that we will be using it in the
22+
* Currently not used, but it is anticipated that we will be using it in the
2123
* KeyMotion
2224
*
2325
*
@@ -27,134 +29,124 @@ class Spline
2729
* Spline in N dimensions
2830
*
2931
* @param points [mPoints][dimensionality]
30-
*/(points: List<DoubleArray>) {
31-
var mPoints = 0
32-
var mCurve: ArrayList<Array<Cubic?>>? = null
33-
var mDimensionality = 0
34-
lateinit var mCurveLength: DoubleArray
35-
var mTotalLength = 0.0
36-
var mCtl: ArrayList<DoubleArray>? = null
32+
*/(points: List<FloatArray>) {
33+
private var mPoints = 0
34+
private lateinit var mCurve: ArrayList<Array<Cubic>>
35+
private var mDimensionality = 0
36+
private lateinit var mCurveLength: FloatArray
37+
private var mTotalLength = 0.0f
38+
private lateinit var mCtl: ArrayList<FloatArray>
3739

3840
init {
3941
setup(points)
4042
}
4143

42-
// @TODO: add description
43-
fun setup(points: List<DoubleArray>) {
44+
45+
private fun setup(points: List<FloatArray>) {
4446
mDimensionality = points[0].size
4547
mPoints = points.size
4648
mCtl = ArrayList()
4749
mCurve = ArrayList()
4850
for (d in 0 until mDimensionality) {
49-
val tmp = DoubleArray(mPoints)
50-
mCtl!!.add(tmp)
51+
val tmp = FloatArray(mPoints)
52+
mCtl.add(tmp)
5153
for (p in 0 until mPoints) {
5254
tmp[p] = points[p][d]
5355
}
5456
}
5557
for (d in 0 until mDimensionality) {
56-
mCurve!!.add(calcNaturalCubic(mCtl!![d].size, mCtl!![d]))
58+
mCurve.add(calcNaturalCubic(mCtl!![d].size, mCtl[d]))
5759
}
58-
mCurveLength = DoubleArray(mPoints - 1)
59-
mTotalLength = 0.0
60+
mCurveLength = FloatArray(mPoints - 1)
61+
mTotalLength = 0.0f
6062
val temp = arrayOfNulls<Cubic>(mDimensionality)
6163
for (p in mCurveLength.indices) {
6264
for (d in 0 until mDimensionality) {
63-
temp[d] = mCurve!![d][p]
65+
temp[d] = mCurve[d][p]
6466
}
6567
mCurveLength[p] = approxLength(temp)
6668
mTotalLength += mCurveLength[p]
6769
}
6870
}
6971

70-
// @TODO: add description
71-
fun getVelocity(p: Double, v: DoubleArray) {
72+
/**
73+
* get the velocity
74+
*/
75+
fun getVelocity(p: Float, v: FloatArray) {
7276
var pos = p * mTotalLength
7377
var k = 0
7478
while (k < mCurveLength.size - 1 && mCurveLength[k] < pos) {
7579
pos -= mCurveLength[k]
7680
k++
7781
}
7882
for (i in v.indices) {
79-
v[i] = mCurve!![i][k]!!.vel(pos / mCurveLength[k])
83+
v[i] = mCurve[i][k]!!.vel(pos / mCurveLength[k])
8084
}
8185
}
8286

83-
// @TODO: add description
84-
fun getPos(p: Double, x: DoubleArray) {
85-
var pos = p * mTotalLength
86-
var k = 0
87-
while (k < mCurveLength.size - 1 && mCurveLength[k] < pos) {
88-
pos -= mCurveLength[k]
89-
k++
90-
}
91-
for (i in x.indices) {
92-
x[i] = mCurve!![i][k]!!.eval(pos / mCurveLength[k])
93-
}
94-
}
9587

9688
// @TODO: add description
97-
fun getPos(p: Double, x: FloatArray) {
89+
fun getPos(p: Float, x: FloatArray) {
9890
var pos = p * mTotalLength
9991
var k = 0
10092
while (k < mCurveLength.size - 1 && mCurveLength[k] < pos) {
10193
pos -= mCurveLength[k]
10294
k++
10395
}
10496
for (i in x.indices) {
105-
x[i] = mCurve!![i][k]!!.eval(pos / mCurveLength[k]).toFloat()
97+
x[i] = mCurve[i][k]!!.eval(pos / mCurveLength[k])
10698
}
10799
}
108100

109101
// @TODO: add description
110-
fun getPos(p: Double, splineNumber: Int): Double {
102+
fun getPos(p: Float, splineNumber: Int): Float {
111103
var pos = p * mTotalLength
112104
var k = 0
113105
while (k < mCurveLength.size - 1 && mCurveLength[k] < pos) {
114106
pos -= mCurveLength[k]
115107
k++
116108
}
117-
return mCurve!![splineNumber][k]!!.eval(pos / mCurveLength[k])
109+
return mCurve[splineNumber][k]!!.eval(pos / mCurveLength[k])
118110
}
119111

120112
// @TODO: add description
121-
fun approxLength(curve: Array<Cubic?>): Double {
122-
var sum = 0.0
113+
fun approxLength(curve: Array<Cubic?>): Float {
114+
var sum = 0.0f
123115
val n = curve.size
124-
val old = DoubleArray(n)
125-
var i = 0.0
116+
val old = FloatArray(n)
117+
var i = 0.0f
126118
while (i < 1) {
127-
var s = 0.0
119+
var s = 0.0f
128120
for (j in 0 until n) {
129121
var tmp = old[j]
130122
old[j] = curve[j]!!.eval(i)
131123
tmp -= old[j]
132124
s += tmp * tmp
133125
}
134126
if (i > 0) {
135-
sum += Math.sqrt(s)
127+
sum += sqrt(s)
136128
}
137-
i += .1
129+
i += .1f
138130
}
139-
var s = 0.0
131+
var s = 0.0f
140132
for (j in 0 until n) {
141133
var tmp = old[j]
142-
old[j] = curve[j]!!.eval(1.0)
134+
old[j] = curve[j]!!.eval(1.0f)
143135
tmp -= old[j]
144136
s += tmp * tmp
145137
}
146-
sum += Math.sqrt(s)
138+
sum += sqrt(s)
147139
return sum
148140
}
149141

150-
class Cubic(var mA: Double, var mB: Double, var mC: Double, var mD: Double) {
142+
class Cubic(var mA: Float, var mB: Float, var mC: Float, var mD: Float) {
151143
// @TODO: add description
152-
fun eval(u: Double): Double {
144+
fun eval(u: Float): Float {
153145
return ((mD * u + mC) * u + mB) * u + mA
154146
}
155147

156148
// @TODO: add description
157-
fun vel(v: Double): Double {
149+
fun vel(v: Float): Float {
158150
// (((mD * u) + mC) * u + mB) * u + mA
159151
// = "mA + u*mB + u*u*mC+u*u*u*mD" a cubic expression
160152
// diff with respect to u = mB + u*mC/2+ u*u*mD/3
@@ -163,35 +155,32 @@ class Spline
163155
}
164156
}
165157

166-
companion object {
167-
fun calcNaturalCubic(n: Int, x: DoubleArray): Array<Cubic?> {
168-
var n = n
169-
val gamma = DoubleArray(n)
170-
val delta = DoubleArray(n)
171-
val d = DoubleArray(n)
172-
n -= 1
173-
gamma[0] = (1.0f / 2.0f).toDouble()
174-
for (i in 1 until n) {
175-
gamma[i] = 1 / (4 - gamma[i - 1])
176-
}
177-
gamma[n] = 1 / (2 - gamma[n - 1])
178-
delta[0] = 3 * (x[1] - x[0]) * gamma[0]
179-
for (i in 1 until n) {
180-
delta[i] = (3 * (x[i + 1] - x[i - 1]) - delta[i - 1]) * gamma[i]
181-
}
182-
delta[n] = (3 * (x[n] - x[n - 1]) - delta[n - 1]) * gamma[n]
183-
d[n] = delta[n]
184-
for (i in n - 1 downTo 0) {
185-
d[i] = delta[i] - gamma[i] * d[i + 1]
186-
}
187-
val c = arrayOfNulls<Cubic>(n)
188-
for (i in 0 until n) {
189-
c[i] = Cubic(
190-
x[i].toFloat().toDouble(), d[i], 3 * (x[i + 1] - x[i]) - (2
191-
* d[i]) - d[i + 1], 2 * (x[i] - x[i + 1]) + d[i] + d[i + 1]
192-
)
193-
}
194-
return c
158+
159+
private fun calcNaturalCubic(cubic: Int, x: FloatArray): Array<Cubic> {
160+
var n = cubic
161+
val gamma = FloatArray(n)
162+
val delta = FloatArray(n)
163+
val d = FloatArray(n)
164+
n -= 1
165+
gamma[0] = (1.0f / 2.0f)
166+
for (i in 1 until n) {
167+
gamma[i] = 1 / (4 - gamma[i - 1])
168+
}
169+
gamma[n] = 1 / (2 - gamma[n - 1])
170+
delta[0] = 3 * (x[1] - x[0]) * gamma[0]
171+
for (i in 1 until n) {
172+
delta[i] = (3 * (x[i + 1] - x[i - 1]) - delta[i - 1]) * gamma[i]
173+
}
174+
delta[n] = (3 * (x[n] - x[n - 1]) - delta[n - 1]) * gamma[n]
175+
d[n] = delta[n]
176+
for (i in n - 1 downTo 0) {
177+
d[i] = delta[i] - gamma[i] * d[i + 1]
178+
}
179+
return Array(n) {i->
180+
Cubic(
181+
x[i], d[i], 3 * (x[i + 1] - x[i]) - (2
182+
* d[i]) - d[i + 1], 2 * (x[i] - x[i + 1]) + d[i] + d[i + 1]
183+
)
195184
}
196185
}
197-
}
186+
}

0 commit comments

Comments
 (0)