Skip to content

Commit

Permalink
Add RotateSpec for parameters count minimization
Browse files Browse the repository at this point in the history
  • Loading branch information
RYangazov committed May 14, 2024
1 parent fd6d22c commit d3885fa
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ internal abstract class SlimBase protected constructor(val elementName: String)

override fun setRotation(angle: Double, centerX: Double, centerY: Double) {
if (angle != 0.0) {
//setAttribute(transform, "rotate($angle, $centerX, $centerY)")
SvgTransformBuilder().rotate(angle, centerX, centerY).build().let {
setAttribute(transform, it.toString())
}
Expand Down
16 changes: 8 additions & 8 deletions docs/dev/notebooks/rotate_points.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

package org.jetbrains.letsPlot.core.plot.base.aes

import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.Aes
import org.jetbrains.letsPlot.core.plot.base.DataPointAesthetics
import org.jetbrains.letsPlot.core.plot.base.render.point.RotationSpec
import org.jetbrains.letsPlot.core.plot.base.render.point.UpdatableShape
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgShape
import org.jetbrains.letsPlot.datamodel.svg.dom.SvgUtils
Expand All @@ -32,8 +31,7 @@ object AestheticsUtil {
solid: Boolean,
p: DataPointAesthetics,
strokeWidth: Double,
angle: Double,
location: DoubleVector
rotationSpec: RotationSpec?
) {
val fill = fill(filled, solid, p)
val stroke = p.color()!!
Expand All @@ -48,7 +46,7 @@ object AestheticsUtil {
strokeAlpha = alpha(stroke, p)
}

shape.update(fill, fillAlpha, stroke, strokeAlpha, strokeWidth, angle, location.x, location.y)
shape.update(fill, fillAlpha, stroke, strokeAlpha, strokeWidth, rotationSpec)
}

fun alpha(color: Color, p: DataPointAesthetics): Double {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.jetbrains.letsPlot.core.plot.base.render.point.symbol.Glyphs
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimElements
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimObject

typealias RotationSpec = Pair<Double, DoubleVector>

object PointShapeSvg {
fun create(shape: PointShape, location: DoubleVector, p: DataPointAesthetics, fatten: Double = 1.0): SvgSlimObject {
if (shape == TinyPointShape) {
Expand All @@ -41,10 +43,12 @@ object PointShapeSvg {
val r = SvgSlimElements.rect(location.x - 0.5, location.y - 0.5, 1.0, 1.0)
val color = p.color()!!
val alpha = AestheticsUtil.alpha(color, p)
val angle = p.finiteOrNull(Aes.ANGLE) ?: 0.0
r.setFill(color, alpha)
r.setStrokeWidth(0.0)
r.setRotation(angle, location.x, location.y)

val angle = p.finiteOrNull(Aes.ANGLE)
angle?.let { r.setRotation(it, location.x, location.y) }

return r
}

Expand All @@ -61,9 +65,10 @@ object PointShapeSvg {
shape.strokeWidth(p)
)

val angle = p.finiteOrNull(Aes.ANGLE).takeIf { glyph !is CircleGlyph } ?: 0.0
val angle = p.finiteOrNull(Aes.ANGLE)
val rotationSpec = if (glyph is CircleGlyph) null else angle?.let { RotationSpec(it, location) }

AestheticsUtil.decorate(glyph, shape.isFilled, shape.isSolid, p, shape.strokeWidth(p), angle, location)
AestheticsUtil.decorate(glyph, shape.isFilled, shape.isSolid, p, shape.strokeWidth(p), rotationSpec)
return glyph
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ interface UpdatableShape {
stroke: Color,
strokeAlpha: Double,
strokeWidth: Double,
angle: Double,
centerX: Double,
centerY: Double
rotationSpec: RotationSpec?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.jetbrains.letsPlot.core.plot.base.render.point.symbol

import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.render.point.RotationSpec
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimGroup

internal class GlyphPair(private val myG1: Glyph, private val myG2: Glyph) :
Expand All @@ -17,12 +18,10 @@ internal class GlyphPair(private val myG1: Glyph, private val myG2: Glyph) :
stroke: Color,
strokeAlpha: Double,
strokeWidth: Double,
angle: Double,
centerX: Double,
centerY: Double
rotationSpec: RotationSpec?
) {
myG1.update(fill, fillAlpha, stroke, strokeAlpha, strokeWidth, angle, centerX, centerY)
myG2.update(fill, fillAlpha, stroke, strokeAlpha, strokeWidth, angle, centerX, centerY)
myG1.update(fill, fillAlpha, stroke, strokeAlpha, strokeWidth, rotationSpec)
myG2.update(fill, fillAlpha, stroke, strokeAlpha, strokeWidth, rotationSpec)
}

override fun appendTo(g: SvgSlimGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.jetbrains.letsPlot.core.plot.base.render.point.symbol

import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.render.point.RotationSpec
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimShape

internal abstract class MultiShapeGlyph : Glyph {
Expand All @@ -16,13 +17,13 @@ internal abstract class MultiShapeGlyph : Glyph {
stroke: Color,
strokeAlpha: Double,
strokeWidth: Double,
angle: Double,
centerX: Double,
centerY: Double
rotationSpec: RotationSpec?
) {
shape?.setFill(fill, fillAlpha)
shape?.setStroke(stroke, strokeAlpha)
shape?.setStrokeWidth(strokeWidth)
shape?.setRotation(angle, centerX, centerY)
rotationSpec?.let { (angle, center) ->
shape?.setRotation(angle, center.x, center.y)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package org.jetbrains.letsPlot.core.plot.base.render.point.symbol

import org.jetbrains.letsPlot.commons.geometry.DoubleVector
import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.render.point.RotationSpec
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimGroup
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimShape

Expand All @@ -29,14 +30,14 @@ abstract class SingletonGlyph : Glyph {
stroke: Color,
strokeAlpha: Double,
strokeWidth: Double,
angle: Double,
centerX: Double,
centerY: Double
rotationSpec: RotationSpec?
) {
myShape.setFill(fill, fillAlpha)
myShape.setStroke(stroke, strokeAlpha)
myShape.setStrokeWidth(strokeWidth)
myShape.setRotation(angle, centerX, centerY)
rotationSpec?.let { (angle, center) ->
myShape.setRotation(angle, center.x, center.y)
}
}

override fun appendTo(g: SvgSlimGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.jetbrains.letsPlot.core.plot.base.render.point.symbol

import org.jetbrains.letsPlot.commons.values.Color
import org.jetbrains.letsPlot.core.plot.base.render.point.RotationSpec
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimGroup
import org.jetbrains.letsPlot.datamodel.svg.dom.slim.SvgSlimShape

Expand All @@ -24,12 +25,10 @@ internal abstract class TwoShapeGlyph : MultiShapeGlyph() {
stroke: Color,
strokeAlpha: Double,
strokeWidth: Double,
angle: Double,
centerX: Double,
centerY: Double
rotationSpec: RotationSpec?
) {
update(myS1, fill, fillAlpha, stroke, strokeAlpha, strokeWidth, angle, centerX, centerY)
update(myS2, fill, fillAlpha, stroke, strokeAlpha, strokeWidth, angle, centerX, centerY)
update(myS1, fill, fillAlpha, stroke, strokeAlpha, strokeWidth, rotationSpec)
update(myS2, fill, fillAlpha, stroke, strokeAlpha, strokeWidth, rotationSpec)
}

override fun appendTo(g: SvgSlimGroup) {
Expand Down

0 comments on commit d3885fa

Please sign in to comment.