-
Notifications
You must be signed in to change notification settings - Fork 47
/
RectanglesHelper.kt
154 lines (128 loc) · 5.98 KB
/
RectanglesHelper.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
/*
* 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.util
import org.jetbrains.letsPlot.commons.geometry.DoubleRectangle
import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.intern.typedGeometry.algorithms.AdaptiveResampler
import org.jetbrains.letsPlot.commons.intern.typedGeometry.algorithms.AdaptiveResampler.Companion.resample
import org.jetbrains.letsPlot.core.commons.geometry.PolylineSimplifier
import org.jetbrains.letsPlot.core.plot.base.*
import org.jetbrains.letsPlot.core.plot.base.render.svg.lineString
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgNode
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgPathDataBuilder
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgPathElement
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgRectElement
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimElements
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimGroup
class RectanglesHelper(
private val myAesthetics: Aesthetics,
pos: PositionAdjustment,
coord: CoordinateSystem,
ctx: GeomContext,
private val geometryFactory: (DataPointAesthetics) -> DoubleRectangle?
) : GeomHelper(pos, coord, ctx) {
// TODO: Replace with SvgRectHelper
fun createNonLinearRectangles(handler: (DataPointAesthetics, SvgNode, List<DoubleVector>) -> Unit) {
myAesthetics.dataPoints().forEach { p ->
geometryFactory(p)?.let { rect ->
val polyRect = resample(
precision = AdaptiveResampler.PIXEL_PRECISION,
points = listOf(
DoubleVector(rect.left, rect.top),
DoubleVector(rect.right, rect.top),
DoubleVector(rect.right, rect.bottom),
DoubleVector(rect.left, rect.bottom),
DoubleVector(rect.left, rect.top)
)
) { toClient(it, p) }
val svgPoly = SvgPathElement()
svgPoly.d().set(SvgPathDataBuilder().lineString(polyRect).build())
decorate(svgPoly, p)
handler(p, svgPoly, polyRect)
}
}
}
fun createRectangles(handler: (DataPointAesthetics, SvgNode, DoubleRectangle) -> Unit) {
myAesthetics.dataPoints().forEach { p ->
geometryFactory(p)?.let { rect ->
val clientRect = toClient(rect, p) ?: return@let
val svgRect = SvgRectElement(clientRect)
decorate(svgRect, p)
handler(p, svgRect, clientRect)
}
}
}
fun createRectangles(): MutableList<SvgNode> {
val result = ArrayList<SvgNode>()
for (index in 0 until myAesthetics.dataPointCount()) {
val p = myAesthetics.dataPointAt(index)
val clientRect = geometryFactory(p) ?: continue
val svgRect = SvgRectElement(clientRect)
decorate(svgRect, p)
result.add(svgRect)
}
return result
}
fun iterateRectangleGeometry(
iterator: (DataPointAesthetics, DoubleRectangle) -> Unit
) {
for (index in 0 until myAesthetics.dataPointCount()) {
val p = myAesthetics.dataPointAt(index)
geometryFactory(p)?.let { rect ->
iterator(p, rect)
}
}
}
fun createSvgRectHelper(): SvgRectHelper {
return SvgRectHelper()
}
inner class SvgRectHelper {
private var onGeometry: (DataPointAesthetics, DoubleRectangle?, List<DoubleVector>?) -> Unit = { _, _, _ -> }
private var myResamplingEnabled = false
private var myResamplingPrecision = AdaptiveResampler.PIXEL_PRECISION
fun setResamplingEnabled(b: Boolean) {
myResamplingEnabled = b
}
fun setResamplingPrecision(precision: Double) {
myResamplingPrecision = precision
}
fun onGeometry(handler: (DataPointAesthetics, DoubleRectangle?, List<DoubleVector>?) -> Unit) {
onGeometry = handler
}
fun createSlimRectangles(): SvgSlimGroup {
val pointCount = myAesthetics.dataPointCount()
val group = SvgSlimElements.g(pointCount)
for (index in 0 until pointCount) {
val p = myAesthetics.dataPointAt(index)
val rect = geometryFactory(p) ?: continue
if (myResamplingEnabled) {
val polyRect = resample(
precision = myResamplingPrecision,
points = listOf(
DoubleVector(rect.left, rect.top),
DoubleVector(rect.right, rect.top),
DoubleVector(rect.right, rect.bottom),
DoubleVector(rect.left, rect.bottom),
DoubleVector(rect.left, rect.top)
)
) { toClient(it, p) }
// Resampling of a tiny rectangle still can produce a very small polygon - simplify it.
val simplified = PolylineSimplifier.douglasPeucker(polyRect).setWeightLimit(PolylineSimplifier.DOUGLAS_PEUCKER_PIXEL_THRESHOLD).points
onGeometry(p, null, simplified)
val slimShape = SvgSlimElements.path(SvgPathDataBuilder().lineString(simplified).build())
decorateSlimShape(slimShape, p)
slimShape.appendTo(group)
} else {
val clientRect = toClient(rect, p) ?: continue
onGeometry(p, clientRect, null)
val slimShape = SvgSlimElements.rect(clientRect.left, clientRect.top, clientRect.width, clientRect.height)
decorateSlimShape(slimShape, p)
slimShape.appendTo(group)
}
}
return group
}
}
}