Skip to content

Commit

Permalink
Initial work on mutable, immutable and value geometry classes (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Feb 14, 2023
1 parent ee31d52 commit 9b524cc
Show file tree
Hide file tree
Showing 327 changed files with 3,328 additions and 3,201 deletions.
7 changes: 3 additions & 4 deletions korge-sandbox/src/commonMain/kotlin/samples/MainArc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import com.soywiz.korev.Key
import com.soywiz.korge.input.keys
import com.soywiz.korge.scene.Scene
import com.soywiz.korge.view.*
import com.soywiz.korge.view.vector.gpuShapeView
import com.soywiz.korim.color.Colors
import com.soywiz.korma.geom.Point
import com.soywiz.korma.geom.MPoint
import com.soywiz.korma.geom.bezier.Arc
import com.soywiz.korma.geom.vector.StrokeInfo
import com.soywiz.korma.geom.vector.circle
Expand All @@ -16,8 +15,8 @@ class MainArc : Scene() {
override suspend fun SContainer.sceneMain() {
//graphics(renderer = GraphicsRenderer.GPU) { shape ->
graphics(renderer = GraphicsRenderer.SYSTEM) { shape ->
val p1 = Point(200, 100)
val p2 = Point(300, 200)
val p1 = MPoint(200, 100)
val p2 = MPoint(300, 200)
val radius = 100.0

stroke(Colors.BLUE, StrokeInfo(thickness = 10.0)) {
Expand Down
20 changes: 10 additions & 10 deletions korge-sandbox/src/commonMain/kotlin/samples/MainBVH.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import com.soywiz.korge.view.xy
import com.soywiz.korim.color.Colors
import com.soywiz.korim.font.*
import com.soywiz.korio.util.*
import com.soywiz.korma.geom.Point
import com.soywiz.korma.geom.MPoint
import com.soywiz.korma.geom.Ray
import com.soywiz.korma.geom.Rectangle
import com.soywiz.korma.geom.Size
import com.soywiz.korma.geom.MRectangle
import com.soywiz.korma.geom.MSize
import com.soywiz.korma.geom.cosine
import com.soywiz.korma.geom.ds.BVH2D
import com.soywiz.korma.geom.shape.buildVectorPath
Expand Down Expand Up @@ -60,11 +60,11 @@ class MainBVH : Scene() {
bvh.insertOrUpdate(view.getBounds(this), view)
}
}
val center = Point(width / 2, height / 2)
val dir = Point(-1, -1)
val center = MPoint(width / 2, height / 2)
val dir = MPoint(-1, -1)
val ray = Ray(center, dir)
val statusText = text("", font = DefaultTtfFontAsBitmap)
var selectedRectangle = Rectangle(Point(100, 100) - Point(50, 50), Size(100, 100))
var selectedRectangle = MRectangle(MPoint(100, 100) - MPoint(50, 50), MSize(100, 100))
val rayLine = line(center, center + (dir * 1000), Colors.WHITE)
val selectedRect = outline(buildVectorPath(VectorPath()) {
rect(selectedRectangle)
Expand All @@ -75,7 +75,7 @@ class MainBVH : Scene() {
var allObjectsSize = 0
var rayObjectsSize = 0
var rectangleObjectsSize = 0
val allObjects = bvh.search(Rectangle(0.0, 0.0, width, height))
val allObjects = bvh.search(MRectangle(0.0, 0.0, width, height))
val time = measureTime {
val rayObjects = bvh.intersect(ray)
val rectangleObjects = bvh.search(selectedRectangle)
Expand All @@ -93,7 +93,7 @@ class MainBVH : Scene() {
addUpdater {
//println("moved")
val mousePos = localMouseXY(views)
val angle = Point.angleFull(center, mousePos)
val angle = MPoint.angleFull(center, mousePos)
//println("center=$center, mousePos=$mousePos, angle = $angle")
dir.setTo(angle.cosine, angle.sine)
rayLine.setPoints(center, center + (dir * 1000))
Expand All @@ -103,13 +103,13 @@ class MainBVH : Scene() {

mouse {
onDown {
selectedRectangle = Rectangle(stage!!.mouseXY - Point(50, 50), Size(100, 100))
selectedRectangle = MRectangle(stage!!.mouseXY - MPoint(50, 50), MSize(100, 100))
selectedRect.vectorPath = buildVectorPath(VectorPath()) {
rect(selectedRectangle)
}
}
onMouseDrag {
selectedRectangle = Rectangle(stage.mouseXY - Point(50, 50), Size(100, 100))
selectedRectangle = MRectangle(stage.mouseXY - MPoint(50, 50), MSize(100, 100))
selectedRect.vectorPath = buildVectorPath(VectorPath()) {
rect(selectedRectangle)
}
Expand Down
5 changes: 2 additions & 3 deletions korge-sandbox/src/commonMain/kotlin/samples/MainBezier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import com.soywiz.korge.scene.Scene
import com.soywiz.korge.tween.get
import com.soywiz.korge.tween.tween
import com.soywiz.korge.view.*
import com.soywiz.korge.view.vector.gpuShapeView
import com.soywiz.korim.color.Colors
import com.soywiz.korim.vector.EmptyShape
import com.soywiz.korio.async.launch
import com.soywiz.korma.geom.Point
import com.soywiz.korma.geom.MPoint
import com.soywiz.korma.geom.bezier.Bezier
import com.soywiz.korma.geom.vector.circle
import com.soywiz.korma.geom.vector.curve
Expand All @@ -24,7 +23,7 @@ class MainBezier : Scene() {
//val shape = gpuShapeView(EmptyShape)
val shape = graphics(EmptyShape, renderer = GraphicsRenderer.SYSTEM)
//val shape = graphics(EmptyShape, renderer = GraphicsRenderer.GPU)
fun getRandomPoint() = Point(Random[100..500], Random[100..500])
fun getRandomPoint() = MPoint(Random[100..500], Random[100..500])
class Bez {
var p1 = getRandomPoint()
var p2 = getRandomPoint()
Expand Down
12 changes: 6 additions & 6 deletions korge-sandbox/src/commonMain/kotlin/samples/MainBezierSample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.soywiz.korim.color.ColorTransform
import com.soywiz.korim.color.Colors
import com.soywiz.korim.paint.Paint
import com.soywiz.korim.vector.ShapeBuilder
import com.soywiz.korma.geom.Point
import com.soywiz.korma.geom.MPoint
import com.soywiz.korma.geom.bezier.Bezier
import com.soywiz.korma.geom.vector.StrokeInfo
import com.soywiz.korma.geom.vector.cubic
Expand All @@ -31,10 +31,10 @@ import com.soywiz.korma.geom.vector.moveTo

class MainBezierSample : Scene() {
override suspend fun SContainer.sceneMain() {
val p0 = Point(109, 135)
val p1 = Point(25, 190)
val p2 = Point(210, 250)
val p3 = Point(234, 49)
val p0 = MPoint(109, 135)
val p1 = MPoint(25, 190)
val p2 = MPoint(210, 250)
val p3 = MPoint(234, 49)

val graphics = cpuGraphics(autoScaling = true)
val graphics2 = gpuShapeView().xy(0, 300)
Expand Down Expand Up @@ -78,7 +78,7 @@ class MainBezierSample : Scene() {
createPointController(p3, Colors.YELLOW) { updateGraphics() }
}

fun Container.createPointController(point: Point, color: Paint, onMove: () -> Unit) {
fun Container.createPointController(point: MPoint, color: Paint, onMove: () -> Unit) {
lateinit var circle: View
lateinit var text: Text
val anchorView = container {
Expand Down
3 changes: 1 addition & 2 deletions korge-sandbox/src/commonMain/kotlin/samples/MainBunnymark.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package samples

import com.soywiz.klock.*
import com.soywiz.kmem.*
import com.soywiz.korge.input.*
import com.soywiz.korge.resources.*
import com.soywiz.korge.scene.Scene
Expand Down Expand Up @@ -50,7 +49,7 @@ class Bunny(tex: BmpSlice) : FastSprite(tex) {
println("ag.isInstancedSupported=${ag.isInstancedSupported}")
//suspend fun main() = Korge(width = 800, height = 600, bgcolor = Colors["#2b2b9b"]) {
val wabbitTexture0 = resourcesVfs["bunnys.png"].readBitmap()
val GRAYSCALE_MATRIX = Matrix3D.fromColumns(
val GRAYSCALE_MATRIX = MMatrix3D.fromColumns(
0.33f, 0.33f, 0.33f, 0f,
0.59f, 0.59f, 0.59f, 0f,
0.11f, 0.11f, 0.11f, 0f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.soywiz.korim.bitmap.Bitmaps
import com.soywiz.korim.bitmap.slice
import com.soywiz.korim.format.readBitmap
import com.soywiz.korio.file.std.resourcesVfs
import com.soywiz.korma.geom.Rectangle
import com.soywiz.korma.geom.MRectangle

class MainColorPicker : Scene() {
override suspend fun SContainer.sceneMain() {
Expand All @@ -22,7 +22,7 @@ class MainColorPicker : Scene() {

mouse {
move {
val bmp = stage!!.unsafeRenderToBitmapSync(views!!.renderContext, Rectangle(views.stage.mouseX - 5.0, views.stage.mouseY - 5.0, 10.0, 10.0), views!!.globalToWindowScaleAvg)
val bmp = stage!!.unsafeRenderToBitmapSync(views!!.renderContext, MRectangle(views.stage.mouseX - 5.0, views.stage.mouseY - 5.0, 10.0, 10.0), views!!.globalToWindowScaleAvg)
magnifier.bitmap = bmp.slice()
invalidateRender()
}
Expand Down
4 changes: 2 additions & 2 deletions korge-sandbox/src/commonMain/kotlin/samples/MainCoroutine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.soywiz.korge.view.position
import com.soywiz.korge.view.solidRect
import com.soywiz.korim.color.Colors
import com.soywiz.korio.async.launchImmediately
import com.soywiz.korma.geom.Point
import com.soywiz.korma.geom.MPoint
import com.soywiz.korma.random.get
import kotlin.random.Random

Expand All @@ -26,7 +26,7 @@ class MainCoroutine : ScaledScene(512, 512) {
val targetX = random[0, 512].toDouble()
val targetY = random[0, 512].toDouble()

while (Point.distance(view.x, view.y, targetX, targetY) > 5.0) {
while (MPoint.distance(view.x, view.y, targetX, targetY) > 5.0) {
when {
view.x < targetX -> view.x += 2
view.x > targetX -> view.x -= 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.soywiz.korge.input.*
import com.soywiz.korge.scene.Scene
import com.soywiz.korge.ui.uiButton
import com.soywiz.korge.view.*
import com.soywiz.korge.view.filter.*
import com.soywiz.korge.view.vector.*
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.color.*
Expand Down Expand Up @@ -148,7 +147,7 @@ class MainGpuVectorRendering : Scene() {
globalAlpha = 0.75
fillStyle = BitmapPaint(
korgeBitmap,
Matrix().translate(50, 50).scale(0.125),
MMatrix().translate(50, 50).scale(0.125),
cycleX = CycleMethod.REPEAT,
cycleY = CycleMethod.REPEAT
)
Expand All @@ -159,7 +158,7 @@ class MainGpuVectorRendering : Scene() {
globalAlpha = 0.9
fillStyle =
//createLinearGradient(150.0, 0.0, 200.0, 50.0)
createLinearGradient(0.0, 0.0, 100.0, 100.0, transform = Matrix().scale(0.5).pretranslate(300, 0))
createLinearGradient(0.0, 0.0, 100.0, 100.0, transform = MMatrix().scale(0.5).pretranslate(300, 0))
//.addColorStop(0.0, Colors.BLACK).addColorStop(1.0, Colors.WHITE)
.addColorStop(0.0, Colors.RED).addColorStop(0.5, Colors.GREEN).addColorStop(1.0, Colors.BLUE)
clip({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import com.soywiz.korim.color.Colors
import com.soywiz.korim.text.TextAlignment
import com.soywiz.korim.vector.format.pathSvg
import com.soywiz.korma.geom.IPoint
import com.soywiz.korma.geom.Matrix
import com.soywiz.korma.geom.Point
import com.soywiz.korma.geom.Rectangle
import com.soywiz.korma.geom.MMatrix
import com.soywiz.korma.geom.MPoint
import com.soywiz.korma.geom.MRectangle
import com.soywiz.korma.geom.bezier.StrokePointsMode
import com.soywiz.korma.geom.bezier.toStrokePointsList
import com.soywiz.korma.geom.degrees
import com.soywiz.korma.geom.minus
import com.soywiz.korma.geom.shape.buildVectorPath
import com.soywiz.korma.geom.shape.getPoints2List
import com.soywiz.korma.geom.vector.LineJoin
Expand Down Expand Up @@ -50,7 +49,7 @@ class MainGpuVectorRendering3 : Scene() {
}.xy(pos)

//debugVertexView(pointsList.map { it.vector }, type = AGDrawType.POINTS)
text(desc, alignment = TextAlignment.BASELINE_LEFT).xy(pos - Point(0, 8))
text(desc, alignment = TextAlignment.BASELINE_LEFT).xy(pos - MPoint(0, 8))

debugVertexView(path.getPoints2List(), color = Colors.YELLOWGREEN, type = AGDrawType.LINE_STRIP).xy(pos).apply {
keys {
Expand All @@ -74,7 +73,7 @@ class MainGpuVectorRendering3 : Scene() {
val sx = index * 430 + 15

fun getPos(x: Int, y: Int): IPoint {
return Point(sx + x * 120, 50 + y * 130)
return MPoint(sx + x * 120, 50 + y * 130)
}

text("${strokeInfo.join}", color = Colors.YELLOWGREEN).xy(sx, 10)
Expand Down Expand Up @@ -107,7 +106,7 @@ class MainGpuVectorRendering3 : Scene() {
})

debugPath("Rect closed", getPos(0, 1), strokeInfo, buildVectorPath {
rect(Rectangle.fromBounds(0, 0, 100, 100))
rect(MRectangle.fromBounds(0, 0, 100, 100))
})

debugPath("Rect not closed", getPos(1, 1), strokeInfo, buildVectorPath {
Expand Down Expand Up @@ -153,7 +152,7 @@ class MainGpuVectorRendering3 : Scene() {
debugPath("Shape", getPos(2, 4), strokeInfo, buildVectorPath {
pathSvg(
"m262.15-119.2s2.05-8-2.35-3.6c0,0-6.4,5.2-13.2,5.2,0,0-13.2,2-17.2,14,0,0-3.6,24.4,3.6,29.6,0,0,4.4,6.8,10.8,0.8s20.35-33.6,18.35-46z",
Matrix().setTransform(x = -200.0, y = 150.0).scale(1.2)
MMatrix().setTransform(x = -200.0, y = 150.0).scale(1.2)
)
})
}
Expand Down
10 changes: 5 additions & 5 deletions korge-sandbox/src/commonMain/kotlin/samples/MainImageTrace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.soywiz.korim.bitmap.Bitmap32
import com.soywiz.korim.bitmap.context2d
import com.soywiz.korim.bitmap.trace.trace
import com.soywiz.korim.color.Colors
import com.soywiz.korma.geom.Rectangle
import com.soywiz.korma.geom.MRectangle
import com.soywiz.korma.geom.vector.Winding
import com.soywiz.korma.geom.vector.circle
import com.soywiz.korma.geom.vector.rect
Expand All @@ -23,10 +23,10 @@ class MainImageTrace : Scene() {
override suspend fun SContainer.sceneMain() {
val bmp = Bitmap32(300, 200).context2d {
fill(Colors.WHITE, winding = Winding.EVEN_ODD) {
rect(Rectangle.fromBounds(2, 2, 18, 18))
rectHole(Rectangle.fromBounds(6, 6, 9, 12))
rectHole(Rectangle.fromBounds(10, 5, 15, 12))
rect(Rectangle.fromBounds(50, 2, 68, 18))
rect(MRectangle.fromBounds(2, 2, 18, 18))
rectHole(MRectangle.fromBounds(6, 6, 9, 12))
rectHole(MRectangle.fromBounds(10, 5, 15, 12))
rect(MRectangle.fromBounds(50, 2, 68, 18))
circle(100, 100, 60)
circle(100, 100, 30)
roundRect(200, 50, 50, 50, 5, 5)
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/commonMain/kotlin/samples/MainMSDF.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class MainMSDF : Scene() {
solidRect(300, 100, Colors.DARKGREY)
if (n == 0) {
text("HELLO WORLD áéúóúñ cooool", textSize = 32.0, font = font1).also {
it.setTextBounds(Rectangle(0, 0, 300, 100))
it.setTextBounds(MRectangle(0, 0, 300, 100))
it.alignment = TextAlignment.MIDDLE_CENTER
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/commonMain/kotlin/samples/MainMasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MainMasks : Scene() {
circle(width * 0.5, height * 0.5, 300.0)
}
animate(looped = true) {
tween(circle3::pos[path], time = 2.seconds, easing = Easing.LINEAR)
tween(circle3::ipos[path], time = 2.seconds, easing = Easing.LINEAR)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.soywiz.kmem.*
import com.soywiz.korev.*
import com.soywiz.korge.component.*
import com.soywiz.korge.input.*
import com.soywiz.korge.scene.ScaledScene
import com.soywiz.korge.view.*
import com.soywiz.korim.color.*
import com.soywiz.korio.util.toStringDecimal
Expand Down Expand Up @@ -79,7 +78,7 @@ class MainOnScreenController : Scene() {
}

var dragging = false
val start = Point(0, 0)
val start = MPoint(0, 0)

view.addComponent(object : MouseComponent {
override val view: View = view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package samples
import com.soywiz.korge.scene.Scene
import com.soywiz.korge.view.SContainer
import com.soywiz.korge.view.image
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.format.readBitmapSlice
import com.soywiz.korio.file.std.resourcesVfs
import com.soywiz.korma.geom.Matrix
import com.soywiz.korma.geom.MMatrix
import com.soywiz.korma.geom.degrees
import com.soywiz.korma.geom.slice.*

Expand All @@ -16,7 +15,7 @@ class MainRotatedTexture : Scene() {
//val tex = resourcesVfs["korim.png"].readBitmapSlice().flipY()
//val tex = resourcesVfs["korim.png"].readBitmapSlice().transformed(Matrix().scale(.5f, .5f)).sliceWithSize(0, 0, 10, 10)
//val tex = resourcesVfs["korim.png"].readBitmapSlice().transformed(Matrix().scale(.5f, .5f))
val tex = resourcesVfs["korim.png"].readBitmapSlice().transformed(Matrix().skew(30.degrees, 0.degrees)).flippedX().rotatedRight()
val tex = resourcesVfs["korim.png"].readBitmapSlice().transformed(MMatrix().skew(30.degrees, 0.degrees)).flippedX().rotatedRight()
println("tex=$tex")
println("size=${tex.width},${tex.height}")
image(tex)
Expand Down
2 changes: 1 addition & 1 deletion korge-sandbox/src/commonMain/kotlin/samples/MainSDF.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MainSDF : Scene() {
open class CircleSDFView(width: Double = 100.0, height: Double = 100.0) : ShadedView(PROGRAM, width, height) {
var radius = 0.49
var feather = 0.005
var center = Point(0.5, 0.5)
var center = MPoint(0.5, 0.5)
var time = 0.0

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ class MainStrokesExperiment : Scene() {
val circle = circle(16.0, Colors.PURPLE).centered
launchImmediately {
while (true) {
circle.tween(circle::pos.get(path, includeLastPoint = true, reversed = false), time = 5.seconds, easing = Easing.LINEAR)
circle.tween(circle::pos.get(path, includeLastPoint = true, reversed = true), time = 5.seconds, easing = Easing.LINEAR)
circle.tween(circle::ipos.get(path, includeLastPoint = true, reversed = false), time = 5.seconds, easing = Easing.LINEAR)
circle.tween(circle::ipos.get(path, includeLastPoint = true, reversed = true), time = 5.seconds, easing = Easing.LINEAR)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MainTextMetrics : Scene() {
val baseAscent = solidRect(960 + 1200, 1, Colors.BLUE)
val baseDescent = solidRect(960 + 1200, 1, Colors.PURPLE)

var cachedBounds: Rectangle? = null
var cachedBounds: MRectangle? = null
fun updateBounds() {
val currentBounds = text1.getLocalBounds()
if (cachedBounds != currentBounds) {
Expand Down
Loading

0 comments on commit 9b524cc

Please sign in to comment.