-
Notifications
You must be signed in to change notification settings - Fork 47
/
SpokeGeom.kt
145 lines (124 loc) · 4.75 KB
/
SpokeGeom.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
/*
* Copyright (c) 2023. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package org.jetbrains.letsPlot.core.plot.base.geom
import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.interval.DoubleSpan
import org.jetbrains.letsPlot.core.plot.base.*
import org.jetbrains.letsPlot.core.plot.base.geom.legend.HLineLegendKeyElementFactory
import org.jetbrains.letsPlot.core.plot.base.geom.util.ArrowSpec
import org.jetbrains.letsPlot.core.plot.base.geom.util.GeomHelper
import org.jetbrains.letsPlot.core.plot.base.geom.util.GeomUtil
import org.jetbrains.letsPlot.core.plot.base.geom.util.TargetCollectorHelper
import org.jetbrains.letsPlot.core.plot.base.render.LegendKeyElementFactory
import org.jetbrains.letsPlot.core.plot.base.render.SvgRoot
import kotlin.math.cos
import kotlin.math.sin
class SpokeGeom : GeomBase(), WithWidth, WithHeight {
var arrowSpec: ArrowSpec? = null
var pivot: Pivot = DEF_PIVOT
override val legendKeyElementFactory: LegendKeyElementFactory
get() = HLineLegendKeyElementFactory()
override fun buildIntern(
root: SvgRoot,
aesthetics: Aesthetics,
pos: PositionAdjustment,
coord: CoordinateSystem,
ctx: GeomContext
) {
val tooltipHelper = TargetCollectorHelper(GeomKind.SPOKE, ctx)
val geomHelper = GeomHelper(pos, coord, ctx)
val svgElementHelper = geomHelper.createSvgElementHelper()
svgElementHelper.setStrokeAlphaEnabled(true)
svgElementHelper.setGeometryHandler { aes, lineString ->
tooltipHelper.addLine(lineString, aes)
arrowSpec?.let {
val arrow = ArrowSpec.createArrows(aes, lineString, it)
arrow.forEach(root::add)
}
}
for (p in aesthetics.dataPoints()) {
val x = p.finiteOrNull(Aes.X) ?: continue
val y = p.finiteOrNull(Aes.Y) ?: continue
val spoke = toSpoke(p) ?: continue
val base = DoubleVector(x, y)
val start = getStart(base, spoke, pivot)
val end = getEnd(base, spoke, pivot)
val line = svgElementHelper.createLine(start, end, p) ?: continue
root.add(line)
}
}
override fun widthSpan(
p: DataPointAesthetics,
coordAes: Aes<Double>,
resolution: Double,
isDiscrete: Boolean
): DoubleSpan? {
return calculateSpan(p, coordAes, Aes.X)
}
override fun heightSpan(
p: DataPointAesthetics,
coordAes: Aes<Double>,
resolution: Double,
isDiscrete: Boolean
): DoubleSpan? {
return calculateSpan(p, coordAes, Aes.Y)
}
private fun calculateSpan(
p: DataPointAesthetics,
coordAes: Aes<Double>,
spanAxisAes: Aes<Double>
): DoubleSpan? {
val loc = GeomUtil.TO_LOCATION_X_Y(p) ?: return null
val base = loc.flipIf(coordAes != spanAxisAes)
val spoke = toSpoke(p) ?: return null
val start = getStart(base, spoke, pivot)
val end = getEnd(base, spoke, pivot)
return if (spanAxisAes == Aes.X) {
DoubleSpan(start.x, end.x)
} else {
DoubleSpan(start.y, end.y)
}
}
private fun toSpoke(p: DataPointAesthetics): DoubleVector? {
val angle = p.finiteOrNull(Aes.ANGLE) ?: return null
val radius = p.finiteOrNull(Aes.RADIUS) ?: return null
return getSpoke(angle, radius)
}
enum class Pivot {
TAIL, MIDDLE, TIP
}
companion object {
val DEF_PIVOT = Pivot.TAIL
fun createGeometry(
x: Double,
y: Double,
angle: Double,
radius: Double,
pivot: Pivot
): List<DoubleVector> {
val base = DoubleVector(x, y)
val spoke = getSpoke(angle, radius)
return listOf(getStart(base, spoke, pivot), getEnd(base, spoke, pivot))
}
private fun getStart(base: DoubleVector, spoke: DoubleVector, pivot: Pivot): DoubleVector {
return when (pivot) {
Pivot.TAIL -> base
Pivot.MIDDLE -> base.subtract(spoke.mul(0.5))
Pivot.TIP -> base.subtract(spoke)
}
}
private fun getEnd(base: DoubleVector, spoke: DoubleVector, pivot: Pivot): DoubleVector {
return when (pivot) {
Pivot.TAIL -> base.add(spoke)
Pivot.MIDDLE -> base.add(spoke.mul(0.5))
Pivot.TIP -> base
}
}
private fun getSpoke(angle: Double, radius: Double): DoubleVector {
return DoubleVector(radius * cos(angle), radius * sin(angle))
}
const val HANDLES_GROUPS = false
}
}