Skip to content

Commit 3bb8f68

Browse files
committed
Refactor SmartInterpolator, remove javafx Data imports
1 parent cb5b6a4 commit 3bb8f68

File tree

7 files changed

+81
-71
lines changed

7 files changed

+81
-71
lines changed

src/main/java/ru/nucodelabs/gem/view/control/chart/ChartUtil.kt

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,46 @@ import javafx.scene.chart.XYChart
77
import javafx.scene.paint.Color
88
import ru.nucodelabs.gem.view.color.ColorMapper
99
import ru.nucodelabs.geo.ves.calc.interpolation.SmartInterpolator
10+
import ru.nucodelabs.util.Point
1011

11-
object ChartUtil {
12-
fun startListening(colorMapper: ColorMapper?, draw: () -> Unit) {
13-
colorMapper?.minValueProperty()?.addListener { _, _, _ -> draw() }
14-
colorMapper?.maxValueProperty()?.addListener { _, _, _ -> draw() }
15-
colorMapper?.numberOfSegmentsProperty()?.addListener { _, _, _ -> draw() }
16-
colorMapper?.logScaleProperty()?.addListener { _, _, _ -> draw() }
17-
}
12+
fun startListening(colorMapper: ColorMapper?, draw: () -> Unit) {
13+
colorMapper?.minValueProperty()?.addListener { _, _, _ -> draw() }
14+
colorMapper?.maxValueProperty()?.addListener { _, _, _ -> draw() }
15+
colorMapper?.numberOfSegmentsProperty()?.addListener { _, _, _ -> draw() }
16+
colorMapper?.logScaleProperty()?.addListener { _, _, _ -> draw() }
17+
}
1818

19-
@Suppress("UNCHECKED_CAST")
20-
fun initInterpolator(data: ObservableList<XYChart.Series<Number, Number>>, interpolator2D: SmartInterpolator) {
21-
if (!data.isEmpty()) {
22-
interpolator2D.build(data.flatMap { it.data as List<XYChart.Data<Double, Double>> })
23-
}
19+
fun initInterpolator(data: ObservableList<XYChart.Series<Number, Number>>, interpolator2D: SmartInterpolator) {
20+
if (!data.isEmpty()) {
21+
interpolator2D.build(
22+
data.flatMap { series ->
23+
series.data.map {
24+
Point(it.xValue.toDouble(), it.yValue.toDouble(), it.extraValue as Double)
25+
}
26+
}
27+
)
2428
}
29+
}
2530

26-
fun draw(canvas: Canvas, xAxis: Axis<Number>, yAxis: Axis<Number>, interpolator2D: SmartInterpolator, colorMapper: ColorMapper?) {
27-
for (x in 0 until canvas.width.toInt()) {
28-
for (y in 0 until canvas.height.toInt()) {
29-
val xValue = xAxis.getValueForDisplay(x.toDouble()).toDouble()
30-
val yValue = yAxis.getValueForDisplay(y.toDouble()).toDouble()
31-
canvas.graphicsContext2D.pixelWriter.run {
32-
setColor(x, y, colorMapper?.colorFor(interpolator2D.getValue(xValue, yValue)) ?: Color.WHITE)
33-
}
31+
fun draw(
32+
canvas: Canvas,
33+
xAxis: Axis<Number>,
34+
yAxis: Axis<Number>,
35+
interpolator2D: SmartInterpolator,
36+
colorMapper: ColorMapper?
37+
) {
38+
for (x in 0 until canvas.width.toInt()) {
39+
for (y in 0 until canvas.height.toInt()) {
40+
val xValue = xAxis.getValueForDisplay(x.toDouble()).toDouble()
41+
val yValue = yAxis.getValueForDisplay(y.toDouble()).toDouble()
42+
canvas.graphicsContext2D.pixelWriter.run {
43+
setColor(
44+
x,
45+
y,
46+
colorMapper?.colorFor(
47+
interpolator2D.getValue(xValue, yValue)
48+
) ?: Color.WHITE
49+
)
3450
}
3551
}
3652
}

src/main/java/ru/nucodelabs/gem/view/control/chart/CombinedChart.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ class CombinedChart @JvmOverloads constructor(
4646
canvas.heightProperty().bind(plotArea.heightProperty())
4747
canvas.viewOrder = 1.0
4848
colorMapperProperty().addListener { _, _, new ->
49-
ChartUtil.startListening(new) {
50-
draw()
51-
}
49+
startListening(new) { draw() }
5250
draw()
5351
}
54-
ChartUtil.startListening(colorMapper) { draw() }
52+
startListening(colorMapper) { draw() }
5553
}
5654

5755
override fun layoutPlotChildren() {
@@ -65,11 +63,11 @@ class CombinedChart @JvmOverloads constructor(
6563

6664

6765
private fun initInterpolator() {
68-
ChartUtil.initInterpolator(data, interpolator2D)
66+
initInterpolator(data, interpolator2D)
6967
}
7068

7169
private fun draw() {
72-
ChartUtil.draw(canvas, xAxis, yAxis, interpolator2D, colorMapper)
70+
draw(canvas, xAxis, yAxis, interpolator2D, colorMapper)
7371
}
7472

7573
override fun dataItemAdded(series: Series<Number, Number>?, itemIndex: Int, item: Data<Number, Number>?) {

src/main/java/ru/nucodelabs/gem/view/control/chart/SmartInterpolationMap.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class SmartInterpolationMap(
2424

2525
init {
2626
colorMapperProperty().addListener { _, _, new ->
27-
ChartUtil.startListening(new) { draw() }
27+
startListening(new) { draw() }
2828
draw()
2929
}
30-
ChartUtil.startListening(colorMapper) { draw() }
30+
startListening(colorMapper) { draw() }
3131
}
3232

3333
private val interpolator2D = SmartInterpolator(RBFSpatialInterpolator(), ApacheInterpolator2D())
@@ -57,10 +57,10 @@ class SmartInterpolationMap(
5757
}
5858

5959
private fun initInterpolator() {
60-
ChartUtil.initInterpolator(data, interpolator2D)
60+
initInterpolator(data, interpolator2D)
6161
}
6262

6363
private fun draw() {
64-
ChartUtil.draw(canvas, xAxis, yAxis, interpolator2D, colorMapper)
64+
draw(canvas, xAxis, yAxis, interpolator2D, colorMapper)
6565
}
6666
}

src/main/java/ru/nucodelabs/geo/ves/calc/interpolation/SmartInterpolator.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ru.nucodelabs.geo.ves.calc.interpolation
22

3-
import javafx.scene.chart.XYChart
43
import org.apache.commons.math3.analysis.interpolation.BicubicInterpolatingFunction
4+
import ru.nucodelabs.util.Point
55
import ru.nucodelabs.util.std.exp10
66
import kotlin.math.log10
77

@@ -18,7 +18,7 @@ class SmartInterpolator(
1818

1919
private lateinit var bicubicInterpolatingFunction: BicubicInterpolatingFunction
2020

21-
fun build(unorderedPoints: List<XYChart.Data<Double, Double>>) {
21+
fun build(unorderedPoints: List<Point>) {
2222
buildLeakyGrid(unorderedPoints)
2323
setXYRanges()
2424

@@ -36,19 +36,19 @@ class SmartInterpolator(
3636
this.yRange = Pair(this.y.first(), this.y.last())
3737
}
3838

39-
private fun buildLeakyGrid(points: List<XYChart.Data<Double, Double>>) {
40-
val xPoints = points.map { it.xValue }.distinct().sorted()
41-
val yPoints = points.map { it.yValue }.distinct().sorted()
39+
private fun buildLeakyGrid(points: List<Point>) {
40+
val xPoints = points.map { it.x }.distinct().sorted()
41+
val yPoints = points.map { it.y }.distinct().sorted()
4242

4343
this.x = xPoints.toTypedArray()
4444
this.y = yPoints.toTypedArray()
4545
this.f = Array(x.size) { Array(y.size) { null } }
4646

4747
for (point in points) {
48-
val xIdx = x.indexOf(point.xValue)
49-
val yIdx = y.indexOf(point.yValue)
48+
val xIdx = x.indexOf(point.x)
49+
val yIdx = y.indexOf(point.y)
5050

51-
this.f[xIdx][yIdx] = point.extraValue as Double
51+
this.f[xIdx][yIdx] = point.z
5252
}
5353
}
5454

src/test/java/ru/nucodelabs/gem/view/control/chart/SmartInterpolationMapTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ internal class SmartInterpolationMapTest : FXTest() {
3333
}
3434
).apply {
3535
data = observableListOf(
36-
XYChart.Series(AnisotropyTestData.points.toObservableList())
36+
XYChart.Series(AnisotropyTestData.points.map { (x, y, z) ->
37+
XYChart.Data(x as Number, y as Number, z)
38+
}.toObservableList())
3739
)
3840
}
3941
)
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11
package ru.nucodelabs.geo.ves.calc.interpolation
22

3-
import javafx.scene.chart.XYChart
43
import ru.nucodelabs.ShiraPicket
54
import ru.nucodelabs.geo.anisotropy.calc.map.xFromCenter
65
import ru.nucodelabs.geo.anisotropy.calc.map.yFromCenter
6+
import ru.nucodelabs.util.Point
77

88
object AnisotropyTestData {
9-
val points: List<XYChart.Data<Number, Number>>
9+
val points: List<Point>
10+
1011
init {
1112
val picket = ShiraPicket.picket
1213
val expData = picket.sortedExperimentalData
13-
val points1 = expData.map { e -> XYChart.Data(100.0, e.ab2, e.resistanceApparent) }
14+
val points1 = expData.map { e -> Point(100.0, e.ab2, e.resistanceApparent) }
1415
var angle = 60.0
1516
val points2 = points1.map { e ->
16-
XYChart.Data<Number, Number>(
17-
(xFromCenter(e.yValue, angle) + e.xValue),
18-
yFromCenter(e.yValue, angle),
19-
(e.extraValue as Double)
17+
Point(
18+
(xFromCenter(e.y, angle) + e.x),
19+
yFromCenter(e.y, angle),
20+
e.z
2021
)
2122
}
2223
angle = 120.0
2324
val points3 = points1.map { e ->
24-
XYChart.Data<Number, Number>(
25-
(xFromCenter(e.yValue, angle) + e.xValue),
26-
yFromCenter(e.yValue, angle),
27-
(e.extraValue as Double)
25+
Point(
26+
(xFromCenter(e.y, angle) + e.x),
27+
yFromCenter(e.y, angle),
28+
e.z
2829
)
2930
}
3031
angle = 180.0
3132
val points4 = points1.map { e ->
32-
XYChart.Data<Number, Number>(
33-
(xFromCenter(e.yValue, angle) + e.xValue),
34-
yFromCenter(e.yValue, angle),
35-
(e.extraValue as Double)
33+
Point(
34+
(xFromCenter(e.y, angle) + e.x),
35+
yFromCenter(e.y, angle),
36+
e.z
3637
)
3738
}
3839
angle = -60.0
3940
val points5 = points1.map { e ->
40-
XYChart.Data<Number, Number>(
41-
(xFromCenter(e.yValue, angle) + e.xValue),
42-
yFromCenter(e.yValue, angle),
43-
(e.extraValue as Double)
41+
Point(
42+
(xFromCenter(e.y, angle) + e.x),
43+
yFromCenter(e.y, angle),
44+
e.z
4445
)
4546
}
4647
angle = -120.0
4748
val points6 = points1.map { e ->
48-
XYChart.Data<Number, Number>(
49-
(xFromCenter(e.yValue, angle) + e.xValue),
50-
yFromCenter(e.yValue, angle),
51-
(e.extraValue as Double)
49+
Point(
50+
(xFromCenter(e.y, angle) + e.x),
51+
yFromCenter(e.y, angle),
52+
e.z
5253
)
5354
}
5455
val points = (points1 + points2 + points3 + points4 + points5 + points6).toMutableList()
5556
points.shuffle()
5657

57-
@Suppress("UNCHECKED_CAST")
58-
this.points = points.toList() as List<XYChart.Data<Number, Number>>
58+
this.points = points.toList()
5959
}
6060
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package ru.nucodelabs.geo.ves.calc.interpolation
22

3-
import javafx.scene.chart.XYChart
43
import org.junit.jupiter.api.Test
5-
import ru.nucodelabs.geo.ves.calc.interpolation.AnisotropyTestData.points
64

75
internal class SmartInterpolatorTest {
86

97
@Suppress("UNCHECKED_CAST")
108
@Test
119
fun build() {
1210
val smartInterpolator = SmartInterpolator(RBFSpatialInterpolator(), ApacheInterpolator2D())
13-
smartInterpolator.build(points as List<XYChart.Data<Double, Double>>)
14-
}
15-
16-
@Test
17-
fun getValue() {
11+
smartInterpolator.build(AnisotropyTestData.points)
1812
}
1913
}

0 commit comments

Comments
 (0)