@@ -20,26 +20,26 @@ package curves
20
20
*
21
21
*
22
22
*/
23
- class MonoSpline (time : DoubleArray , y : List <DoubleArray >) {
24
- val timePoints: DoubleArray
25
- var mY: ArrayList <DoubleArray >
26
- var mTangent: ArrayList <DoubleArray >
23
+ class MonoSpline (time : FloatArray , y : List <FloatArray >) {
24
+ val timePoints: FloatArray
25
+ var mY: ArrayList <FloatArray >
26
+ var mTangent: ArrayList <FloatArray >
27
27
private val mExtrapolate = true
28
- var mSlopeTemp: DoubleArray
29
- fun makeDoubleArray (a : Int , b : Int ): ArrayList <DoubleArray > {
30
- val ret = ArrayList <DoubleArray >() // new double [a][b];
28
+ var mSlopeTemp: FloatArray
29
+ fun makeFloatArray (a : Int , b : Int ): ArrayList <FloatArray > {
30
+ val ret = ArrayList <FloatArray >() // new Float [a][b];
31
31
for (i in 0 until a) {
32
- ret.add(DoubleArray (b))
32
+ ret.add(FloatArray (b))
33
33
}
34
34
return ret
35
35
}
36
36
37
37
init {
38
38
val n = time.size
39
39
val dim = y[0 ].size
40
- mSlopeTemp = DoubleArray (dim)
41
- val slope = makeDoubleArray (n - 1 , dim) // could optimize this out
42
- val tangent = makeDoubleArray (n, dim)
40
+ mSlopeTemp = FloatArray (dim)
41
+ val slope = makeFloatArray (n - 1 , dim) // could optimize this out
42
+ val tangent = makeFloatArray (n, dim)
43
43
for (j in 0 until dim) {
44
44
for (i in 0 until n - 1 ) {
45
45
val dt = time[i + 1 ] - time[i]
@@ -54,15 +54,15 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
54
54
}
55
55
for (i in 0 until n - 1 ) {
56
56
for (j in 0 until dim) {
57
- if (slope[i][j] == 0.0 ) {
58
- tangent[i][j] = 0.0
59
- tangent[i + 1 ][j] = 0.0
57
+ if (slope[i][j] == 0.0f ) {
58
+ tangent[i][j] = 0.0f
59
+ tangent[i + 1 ][j] = 0.0f
60
60
} else {
61
61
val a = tangent[i][j] / slope[i][j]
62
62
val b = tangent[i + 1 ][j] / slope[i][j]
63
- val h = Math .hypot(a, b)
63
+ val h = Math .hypot(a.toDouble() , b.toDouble()).toFloat( )
64
64
if (h > 9.0 ) {
65
- val t = 3.0 / h
65
+ val t = 3.0f / h
66
66
tangent[i][j] = t * a * slope[i][j]
67
67
tangent[i + 1 ][j] = t * b * slope[i][j]
68
68
}
@@ -74,15 +74,15 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
74
74
mTangent = tangent
75
75
}
76
76
77
- private fun copyData (y : List <DoubleArray >): ArrayList <DoubleArray > {
78
- val ret = ArrayList <DoubleArray >()
77
+ private fun copyData (y : List <FloatArray >): ArrayList <FloatArray > {
78
+ val ret = ArrayList <FloatArray >()
79
79
for (array in y) {
80
80
ret.add(array)
81
81
}
82
82
return ret
83
83
}
84
84
85
- fun getPos (t : Double , v : DoubleArray ) {
85
+ fun getPos (t : Float , v : FloatArray ) {
86
86
val n = timePoints.size
87
87
val dim = mY[0 ].size
88
88
if (mExtrapolate) {
@@ -135,7 +135,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
135
135
}
136
136
}
137
137
138
- fun getPos (t : Double , v : FloatArray ) {
138
+ fun getPos2 (t : Float , v : FloatArray ) {
139
139
val n = timePoints.size
140
140
val dim = mY[0 ].size
141
141
if (mExtrapolate) {
@@ -188,7 +188,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
188
188
}
189
189
}
190
190
191
- fun getPos (t : Double , j : Int ): Double {
191
+ fun getPos (t : Float , j : Int ): Float {
192
192
val n = timePoints.size
193
193
if (mExtrapolate) {
194
194
if (t <= timePoints[0 ]) {
@@ -219,10 +219,10 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
219
219
return interpolate(h, x, y1, y2, t1, t2)
220
220
}
221
221
}
222
- return 0.0 // should never reach here
222
+ return 0.0f // should never reach here
223
223
}
224
224
225
- fun getSlope (time : Double , v : DoubleArray ) {
225
+ fun getSlope (time : Float , v : FloatArray ) {
226
226
var t = time
227
227
val n = timePoints.size
228
228
val dim = mY[0 ].size
@@ -248,7 +248,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
248
248
return
249
249
}
250
250
251
- fun getSlope (time : Double , j : Int ): Double {
251
+ fun getSlope (time : Float , j : Int ): Float {
252
252
var t = time
253
253
val n = timePoints.size
254
254
if (t < timePoints[0 ]) {
@@ -267,21 +267,21 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
267
267
return diff(h, x, y1, y2, t1, t2) / h
268
268
}
269
269
}
270
- return 0.0 // should never reach here
270
+ return 0.0f // should never reach here
271
271
}
272
272
273
273
companion object {
274
274
/* *
275
275
* Cubic Hermite spline
276
276
*/
277
277
private fun interpolate (
278
- h : Double ,
279
- x : Double ,
280
- y1 : Double ,
281
- y2 : Double ,
282
- t1 : Double ,
283
- t2 : Double
284
- ): Double {
278
+ h : Float ,
279
+ x : Float ,
280
+ y1 : Float ,
281
+ y2 : Float ,
282
+ t1 : Float ,
283
+ t2 : Float
284
+ ): Float {
285
285
val x2 = x * x
286
286
val x3 = x2 * x
287
287
return (- 2 * x3 * y2 + 3 * x2 * y2 + 2 * x3 * y1 - 3 * x2 * y1 + y1 + h * t2 * x3 + h * t1 * x3 - h * t2 * x2 - 2 * h * t1 * x2
@@ -291,7 +291,7 @@ class MonoSpline(time: DoubleArray, y: List<DoubleArray>) {
291
291
/* *
292
292
* Cubic Hermite spline slope differentiated
293
293
*/
294
- private fun diff (h : Double , x : Double , y1 : Double , y2 : Double , t1 : Double , t2 : Double ): Double {
294
+ private fun diff (h : Float , x : Float , y1 : Float , y2 : Float , t1 : Float , t2 : Float ): Float {
295
295
val x2 = x * x
296
296
return - 6 * x2 * y2 + 6 * x * y2 + 6 * x2 * y1 - 6 * x * y1 + 3 * h * t2 * x2 + 3 * h * t1 * x2 - 2 * h * t2 * x - 4 * h * t1 * x + h * t1
297
297
}
0 commit comments