-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleSpline.ts
158 lines (131 loc) · 5.17 KB
/
SimpleSpline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { binarySearch, lerp } from "deepslate"
export class SimpleSpline {
constructor(
public points: {
location: number,
value: number,
derivative_left: number
derivative_right: number
}[]
) {
this.order()
}
public toJSON(coordinate: string){
const points = []
for (const point of this.points){
points.push({location: point.location, value: point.value, derivative: point.derivative_left})
if (point.derivative_left !== point.derivative_right){
points.push({location: point.location, value: point.value, derivative: point.derivative_right})
}
}
return {
coordinate: coordinate,
points: points
}
}
public static fromJSON(json: any): SimpleSpline {
if (json.coordinate !== "ridges" && json.coordinate !== "weirdness") {
console.warn("Trying to create SimpleSpline from non weirdness parameter")
return undefined
}
const points: {
location: number,
value: number,
derivative_left: number
derivative_right: number
}[] = []
const isRidge = json.coordinate === "ridges"
for (const point of json.points) {
const location = point.location
const derivative = point.derivative
const value = (typeof point.value === 'number') ? {apply(_w:number){return point.value}} : SimpleSpline.fromJSON(point.value)
if (!isRidge){
if (points.length > 0 && Math.abs(points[points.length-1].location - location) < 1e-5){
points[points.length-1].derivative_right = derivative
} else {
points.push({location: location, derivative_left: derivative, derivative_right: derivative, value: value.apply(location)})
}
} else {
const weirdnesses = this.inversPeaksAndValleys(location)
for (let i = 0 ; i < weirdnesses.weirdnesses.length ; i++){
const derivative_factor = weirdnesses.derivative_factors[i]
if (typeof derivative_factor === "number"){
points.push({location: weirdnesses.weirdnesses[i], derivative_left: derivative * derivative_factor, derivative_right: derivative * derivative_factor, value: value.apply(weirdnesses.weirdnesses[i])})
} else {
points.push({location: weirdnesses.weirdnesses[i], derivative_left: derivative * derivative_factor[0], derivative_right: derivative * derivative_factor[1], value: value.apply(weirdnesses.weirdnesses[i])})
}
}
}
}
return new SimpleSpline(points)
}
public static peaksAndValleys(weirdness: number) {
return -(Math.abs(Math.abs(weirdness) - 0.6666667) - 0.33333334) * 3.0
}
private static inversPeaksAndValleys(ridge: number): { weirdnesses: number[], derivative_factors: (number|[number, number])[] } {
if (ridge > 1 || ridge < -1) return { weirdnesses: [], derivative_factors: [] }
const a = ridge / 3.0 - 0.33333334
if (a - 0.6666667 < -1.5) {
return { weirdnesses: [-a - 0.6666667, a + 0.6666667], derivative_factors: [-3, 3] }
} else if (ridge === 1) {
return { weirdnesses: [-0.6666667, +0.6666667], derivative_factors: [[3, -3], [3, -3]]}
} else if (ridge === -1) {
return { weirdnesses: [-1.33333334, 0, +1.33333334], derivative_factors: [3, [-3, 3], -3]}
} else {
return { weirdnesses: [a - 0.6666667, -a - 0.6666667, a + 0.6666667, -a + 0.6666667], derivative_factors: [3, -3, 3, -3] }
}
}
public order() {
this.points.sort((a, b) => a.location - b.location)
}
public apply(c: number) {
const coordinate = c
const i = binarySearch(0, this.points.length, n => coordinate < this.points[n].location) - 1
const n = this.points.length - 1
if (i < 0) {
return this.points[0].value + this.points[0].derivative_left * (coordinate - this.points[0].location)
}
if (i === n) {
return this.points[n].value + this.points[n].derivative_right * (coordinate - this.points[n].location)
}
const loc0 = this.points[i].location
const loc1 = this.points[i + 1].location
const der0 = this.points[i].derivative_right
const der1 = this.points[i + 1].derivative_left
const f = (coordinate - loc0) / (loc1 - loc0)
const val0 = this.points[i].value
const val1 = this.points[i + 1].value
const f8 = der0 * (loc1 - loc0) - (val1 - val0)
const f9 = -der1 * (loc1 - loc0) + (val1 - val0)
const f10 = lerp(f, val0, val1) + f * (1.0 - f) * lerp(f, f8, f9)
return f10
}
public derivative(c: number, direction: "left" | "right" = "left"){
const coordinate = c
const exactPoint = this.points.find(p => p.location === coordinate)
if (exactPoint){
if (direction === "left")
return exactPoint.derivative_left
else
return exactPoint.derivative_right
}
const i = binarySearch(0, this.points.length, n => coordinate < this.points[n].location) - 1
const n = this.points.length - 1
if (i < 0) {
return this.points[0].derivative_left
}
if (i === n) {
return this.points[n].derivative_right
}
const loc0 = this.points[i].location
const loc1 = this.points[i + 1].location
const der0 = this.points[i].derivative_right
const der1 = this.points[i + 1].derivative_left
const f = (coordinate - loc0) / (loc1 - loc0)
const val0 = this.points[i].value
const val1 = this.points[i + 1].value
const f8 = der0 * (loc1 - loc0) - (val1 - val0)
const f9 = -der1 * (loc1 - loc0) + (val1 - val0)
return (-val0 + val1 + f8 + 2 * f * (f9 - 2*f8) + 3 * f * f * (f8 - f9)) / (loc1 - loc0)
}
}