-
Notifications
You must be signed in to change notification settings - Fork 37
/
DemoFFTShape01.kt
156 lines (130 loc) · 5.65 KB
/
DemoFFTShape01.kt
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
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.fft.FFT
import org.openrndr.extra.noise.scatter
import org.openrndr.extra.shapes.hobbycurve.hobbyCurve
import org.openrndr.extra.shapes.splines.catmullRom
import org.openrndr.extra.shapes.splines.toContour
import org.openrndr.math.Vector2
import org.openrndr.math.smoothstep
import org.openrndr.math.transforms.buildTransform
import org.openrndr.shape.LineSegment
import kotlin.math.*
import kotlin.random.Random
/**
* Demonstration of using FFT to filter a two-dimensional shape. Mouse xy-position is mapped
* to lowpass and highpass settings of the filter.
*/
fun main() {
application {
configure {
width = 720
height = 720
}
program {
val fftSize = 512
val fft = FFT(fftSize)
fun List<Vector2>.toFloatArrays(x: FloatArray, y: FloatArray) {
for ((index, segment) in this.withIndex()) {
x[index] = segment.x.toFloat()
y[index] = segment.y.toFloat()
}
}
fun vectorsFromFloatArrays(x: FloatArray, y: FloatArray): List<Vector2> {
val n = x.size
val result = mutableListOf<Vector2>()
for (i in 0 until n) {
result.add(Vector2(x[i].toDouble(), y[i].toDouble()))
}
return result
}
fun lp(t: Double, c: Double): Double {
return smoothstep(c, c - 0.1, t)
}
fun hp(t: Double, c: Double): Double {
return smoothstep(c, c + 0.1, t)
}
val c = hobbyCurve(
drawer.bounds.scatter(40.0, distanceToEdge = 100.0, random = Random(0)),
true
).transform(buildTransform { translate(-drawer.bounds.center) })
val x = FloatArray(fftSize)
val y = FloatArray(fftSize)
val xFiltered = FloatArray(fftSize)
val yFiltered = FloatArray(fftSize)
extend {
c.equidistantPositions(fftSize).toFloatArrays(x, y)
// process x-component
fft.forward(x)
drawer.stroke = ColorRGBa.GRAY.shade(0.5)
drawer.lineSegments((0 until fft.size / 2).map {
LineSegment(
it.toDouble() * 2.0 + 0.5,
height * 0.5,
it.toDouble() * 2.0 + 0.5,
height * 0.5 - fft.magnitude(it) / 200.0,
)
})
val xpower = fft.magnitudeSum()
val lpc = mouse.position.x / width
val hpc = mouse.position.y / height
for (i in 1..fftSize / 2) {
val t = i.toDouble() / (fftSize / 2 - 1)
val f = if (hpc <= lpc) lp(t, lpc) * hp(t, hpc) else max(lp(t, lpc), hp(t, hpc))
fft.scaleBand(i, f.toFloat())
}
val xfpower = fft.magnitudeSum().coerceAtLeast(1.0)
fft.scaleAll((xpower / xfpower).toFloat())
drawer.stroke = ColorRGBa.PINK.opacify(0.8)
drawer.lineSegments((0 until fft.size / 2).map {
LineSegment(
it.toDouble() * 2.0 + 0.5,
height * 0.5,
it.toDouble() * 2.0 + 0.5,
height * 0.5 - fft.magnitude(it) / 200.0
)
})
fft.inverse(xFiltered)
// process y-component
fft.forward(y)
val ypower = fft.magnitudeSum()
drawer.stroke = ColorRGBa.GRAY.shade(0.5)
drawer.lineSegments((0 until fft.size / 2).map {
LineSegment(
it.toDouble() * 2.0 + 0.5,
height * 0.5,
it.toDouble() * 2.0 + 0.5,
height * 0.5 + fft.magnitude(it) / 200.0,
)
})
for (i in 1..fftSize / 2) {
val t = i.toDouble() / (fftSize / 2 - 1)
val f = if (hpc <= lpc) lp(t, lpc) * hp(t, hpc) else max(lp(t, lpc), hp(t, hpc))
fft.scaleBand(i, f.toFloat())
}
val yfpower = fft.magnitudeSum().coerceAtLeast(1.0)
fft.scaleAll((ypower / yfpower).toFloat())
drawer.stroke = ColorRGBa.PINK.opacify(0.7)
drawer.lineSegments((0 until fft.size / 2).map {
LineSegment(
it.toDouble() * 2.0 + 0.5,
height * 0.5,
it.toDouble() * 2.0 + 0.5,
height * 0.5 + fft.magnitude(it) / 200.0,
)
})
fft.inverse(yFiltered)
val cr = vectorsFromFloatArrays(xFiltered, yFiltered).catmullRom(closed = true).toContour()
//val cr = ShapeContour.fromPoints(vectorsFromFloatArrays(xr, yr), closed=true)
val recenteredShape = cr.transform(buildTransform {
translate(drawer.bounds.center)
})
drawer.fill = null
drawer.stroke = ColorRGBa.WHITE
drawer.lineSegment(mouse.position.x / width * 512, 0.0, mouse.position.x / width * 512, height * 1.0)
drawer.lineSegment(mouse.position.y / height * 512, 0.0, mouse.position.y / height * 512, height * 1.0)
drawer.contour(recenteredShape)
}
}
}
}