-
Notifications
You must be signed in to change notification settings - Fork 47
/
GeomUtil.kt
207 lines (186 loc) · 6.71 KB
/
GeomUtil.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package jetbrains.datalore.plot.base.geom.util
import jetbrains.datalore.base.gcommon.collect.Ordering
import jetbrains.datalore.base.geometry.DoubleRectangle
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataPointAesthetics
import jetbrains.datalore.plot.common.data.SeriesUtil
object GeomUtil {
val TO_LOCATION_X_Y = { p: DataPointAesthetics ->
toLocationOrNull(
p.x(),
p.y()
)
}
val TO_LOCATION_X_YMIN = { p: DataPointAesthetics ->
toLocationOrNull(
p.x(),
p.ymin()
)
}
val TO_LOCATION_X_YMAX = { p: DataPointAesthetics ->
toLocationOrNull(
p.x(),
p.ymax()
)
}
val TO_LOCATION_X_ZERO = { p: DataPointAesthetics ->
toLocationOrNull(
p.x(),
0.0
)
}
val TO_RECTANGLE = { p: DataPointAesthetics ->
if (SeriesUtil.allFinite(p.xmin(), p.ymin(), p.xmax(), p.ymax())) {
rectToGeometry(
p.xmin()!!,
p.ymin()!!,
p.xmax()!!,
p.ymax()!!
)
} else {
emptyList()
}
}
private val ORDERING_X = Ordering.from(object : Comparator<DataPointAesthetics?> {
override fun compare(a: DataPointAesthetics?, b: DataPointAesthetics?): Int {
val x1 = a?.x()
val x2 = b?.x()
if (x1 == null || x2 == null)
return 0
else
return x1.compareTo(x2)
}
})
private val ORDERING_Y = Ordering.from(object : Comparator<DataPointAesthetics?> {
override fun compare(a: DataPointAesthetics?, b: DataPointAesthetics?): Int {
val y1 = a?.y()
val y2 = b?.y()
if (y1 == null || y2 == null)
return 0
else
return y1.compareTo(y2)
}
})
private val WITH_X_Y = { pointAes: DataPointAesthetics -> SeriesUtil.allFinite(pointAes.x(), pointAes.y()) }
private val WITH_X = { pointAes: DataPointAesthetics -> SeriesUtil.isFinite(pointAes.x()) }
private val WITH_Y = { pointAes: DataPointAesthetics -> SeriesUtil.isFinite(pointAes.y()) }
private fun toLocationOrNull(x: Double?, y: Double?): DoubleVector? {
return if (SeriesUtil.isFinite(x) && SeriesUtil.isFinite(y)) {
DoubleVector(x!!, y!!)
} else null
}
@Suppress("FunctionName")
fun with_X_Y(dataPoints: Iterable<DataPointAesthetics>): List<DataPointAesthetics> {
return dataPoints.filter { p -> WITH_X_Y.invoke(p) }
}
@Suppress("FunctionName")
fun with_X(dataPoints: Iterable<DataPointAesthetics>): List<DataPointAesthetics> {
return dataPoints.filter { p -> WITH_X.invoke(p) }
}
@Suppress("FunctionName")
fun with_Y(dataPoints: Iterable<DataPointAesthetics>): List<DataPointAesthetics> {
return dataPoints.filter { p -> WITH_Y.invoke(p) }
}
@Suppress("FunctionName")
fun ordered_X(dataPoints: Iterable<DataPointAesthetics>): Iterable<DataPointAesthetics> {
if (ORDERING_X.isOrdered(dataPoints)) {
return dataPoints
}
return ORDERING_X.sortedCopy(dataPoints)
}
@Suppress("FunctionName")
fun ordered_Y(dataPoints: Iterable<DataPointAesthetics>, reversed: Boolean): Iterable<DataPointAesthetics> {
val ordering = if (reversed) ORDERING_Y.reverse() else ORDERING_Y
if (ordering.isOrdered(dataPoints)) {
return dataPoints
}
return ordering.sortedCopy(dataPoints)
}
fun withDefined(dataPoints: Iterable<DataPointAesthetics>, aes: Aes<*>): Iterable<DataPointAesthetics> {
return dataPoints.filter { p -> p.defined(aes) }
}
fun withDefined(
dataPoints: Iterable<DataPointAesthetics>,
aes0: Aes<*>,
aes1: Aes<*>
): Iterable<DataPointAesthetics> {
return dataPoints.filter { p -> p.defined(aes0) && p.defined(aes1) }
}
fun withDefined(
dataPoints: Iterable<DataPointAesthetics>,
aes0: Aes<*>,
aes1: Aes<*>,
aes2: Aes<*>
): Iterable<DataPointAesthetics> {
return dataPoints.filter { p -> p.defined(aes0) && p.defined(aes1) && p.defined(aes2) }
}
fun withDefined(
dataPoints: Iterable<DataPointAesthetics>,
aes0: Aes<*>,
aes1: Aes<*>,
aes2: Aes<*>,
aes3: Aes<*>
): Iterable<DataPointAesthetics> {
return dataPoints.filter { p -> p.defined(aes0) && p.defined(aes1) && p.defined(aes2) && p.defined(aes3) }
}
fun withDefined(
dataPoints: Iterable<DataPointAesthetics>,
aesList: List<Aes<*>>
): Iterable<DataPointAesthetics> {
return dataPoints.filter { p ->
aesList.all { aes -> p.defined(aes) }
}
}
fun createGroups(dataPoints: Iterable<DataPointAesthetics>): Map<Int, List<DataPointAesthetics>> {
val pointsByGroup = HashMap<Int, MutableList<DataPointAesthetics>>()
for (p in dataPoints) {
val group = p.group()!!
if (!pointsByGroup.containsKey(group)) {
pointsByGroup[group] = ArrayList()
}
pointsByGroup[group]!!.add(p)
}
return pointsByGroup
}
fun rectToGeometry(minX: Double, minY: Double, maxX: Double, maxY: Double): List<DoubleVector> {
return listOf(
DoubleVector(minX, minY),
DoubleVector(minX, maxY),
DoubleVector(maxX, maxY),
DoubleVector(maxX, minY),
DoubleVector(minX, minY)
)
}
internal fun extend(
clientRect: DoubleRectangle,
flipped: Boolean,
widthExpand: Double = 0.0,
heightExpand: Double = 0.0
): DoubleRectangle {
val unflipped = if (flipped) {
clientRect.flip()
} else {
clientRect
}
val unflippedNewRect = DoubleRectangle.LTRB(
unflipped.left - widthExpand / 2,
unflipped.top - heightExpand / 2,
unflipped.right + widthExpand / 2,
unflipped.bottom + heightExpand / 2
)
return if (flipped) {
unflippedNewRect.flip()
} else {
unflippedNewRect
}
}
internal fun extendWidth(clientRect: DoubleRectangle, delta: Double, flipped: Boolean) =
extend(clientRect, flipped, widthExpand = delta)
internal fun extendHeight(clientRect: DoubleRectangle, delta: Double, flipped: Boolean) =
extend(clientRect, flipped, heightExpand = delta)
}