Skip to content

Commit

Permalink
Split korlibs-math (#2189)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Mar 8, 2024
1 parent d24a0a1 commit 3dabfd6
Show file tree
Hide file tree
Showing 130 changed files with 67 additions and 20 deletions.
2 changes: 2 additions & 0 deletions korge-foundation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ dependencies {
//add("commonMainApi", libs.kotlinx.atomicfu)
//add("commonTestApi", project(":korge-test"))
commonTestApi(libs.kotlinx.coroutines.test)
commonMainApi(project(":korlibs-annotations"))
commonMainApi(project(":korlibs-time"))
commonMainApi(project(":korlibs-bignumber"))
commonMainApi(project(":korlibs-datastructure"))
commonMainApi(project(":korlibs-crypto"))
commonMainApi(project(":korlibs-platform"))
commonMainApi(project(":korlibs-math-core"))
commonMainApi(project(":korlibs-math"))
commonMainApi(project(":korlibs-memory"))
commonMainApi(project(":korlibs-number"))
commonMainApi(project(":korlibs-logger"))
Expand Down
1 change: 1 addition & 0 deletions korlibs-annotations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
14 changes: 14 additions & 0 deletions korlibs-annotations/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import korlibs.*

description = "Korlibs Annotations"

project.extensions.extraProperties.properties.apply {
applyProjectProperties(
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-annotations",
"Public Domain",
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-annotations/LICENSE"
)
}

dependencies {
}
1 change: 1 addition & 0 deletions korlibs-math/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
18 changes: 18 additions & 0 deletions korlibs-math/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import korlibs.*

description = "Korlibs Math"

project.extensions.extraProperties.properties.apply {
applyProjectProperties(
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-math",
"Public Domain",
"https://raw.githubusercontent.com/korlibs/korge/main/korlibs-math/LICENSE"
)
}

dependencies {
commonMainApi(project(":korlibs-util"))
commonMainApi(project(":korlibs-number"))
commonMainApi(project(":korlibs-math-core"))
commonMainApi(project(":korlibs-datastructure"))
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package korlibs.math

import korlibs.memory.*
import kotlin.math.*

const val PIF = PI.toFloat()
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package korlibs.math.geom

import korlibs.math.*
import korlibs.memory.*
import kotlin.math.*


Expand Down Expand Up @@ -136,7 +135,7 @@ data class Matrix4 private constructor(
}

fun copyToColumns(out: FloatArray = FloatArray(16), offset: Int = 0): FloatArray {
arraycopy(this.data, 0, out, offset, 16)
this.data.copyInto(out, offset, 0, 16)
return out
}
fun copyToRows(out: FloatArray = FloatArray(16), offset: Int = 0): FloatArray {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import korlibs.math.geom.convex.*
import korlibs.math.geom.shape.*
import korlibs.math.geom.vector.*
import korlibs.math.interpolation.*
import korlibs.memory.*
import korlibs.number.*
import kotlin.contracts.*
import kotlin.jvm.*
Expand Down Expand Up @@ -42,7 +41,7 @@ object Arc {
fun ellipsePath(out: VectorBuilder, p: Point, rsize: Size) {
val (x, y) = p
val (rw, rh) = rsize
val k = K.toDouble()
val k = K
val ox = (rw / 2) * k
val oy = (rh / 2) * k
val xe = x + rw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ fun List<HitTestable>.toHitTestable(): HitTestable {
}
}

private inline fun Int.extract(offset: Int): Boolean = ((this ushr offset) and 0b1) != 0
private inline fun Int.insert(value: Boolean, offset: Int): Int = if (value) this or (1 shl offset) else this
private fun Int.extract(offset: Int): Boolean = ((this ushr offset) and 1) != 0
private fun Int.insert(value: Boolean, offset: Int): Int {
val bits = (1 shl offset)
return if (value) this or bits else this and bits.inv()
}

/*
import korlibs.datastructure.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package korlibs.math.geom.slice
import korlibs.datastructure.*
import korlibs.math.*
import korlibs.math.geom.*
import korlibs.memory.*

data class RectCoords(
override val tlX: Float, override val tlY: Float,
Expand Down Expand Up @@ -375,13 +374,13 @@ enum class SliceRotation {
inline class SliceOrientation(
val raw: Int,
) {
val rotation: SliceRotation get() = SliceRotation[raw.extract2(0)]
val flipX: Boolean get() = raw.extractBool(2)
val rotation: SliceRotation get() = SliceRotation[raw.extract(0, 2)]
val flipX: Boolean get() = raw.extract(2, 1) != 0

constructor(rotation: SliceRotation = SliceRotation.R0, flipX: Boolean = false) : this(0.insert2(rotation.ordinal, 0).insert(flipX, 2))
constructor(rotation: SliceRotation = SliceRotation.R0, flipX: Boolean = false) : this(0.insert(rotation.ordinal, 0, 2).insert(flipX, 2))

/** Indices represent TL, TR, BR, BL */
val indices: IntArray get() = INDICES[raw.extract3(0)]
val indices: IntArray get() = INDICES[raw.extract(0, 3)]

val isRotatedDeg90CwOrCcw: Boolean get() = rotation == SliceRotation.R90 || rotation == SliceRotation.R270

Expand All @@ -402,7 +401,7 @@ inline class SliceOrientation(
out = out.rotatedRight(orientation.rotation.ordinal)
return out
}
override fun toString(): String = NAMES[raw.extract3(0)]
override fun toString(): String = NAMES[raw.extract(0, 3)]

fun getX(width: Int, height: Int, x: Int, y: Int): Int {
val w1 = width - 1
Expand Down Expand Up @@ -477,3 +476,15 @@ inline class SliceOrientation(
)
}
}

private fun Int.mask(): Int = (1 shl this) - 1
private fun Int.extract(offset: Int, bits: Int): Int = (this ushr offset) and bits.mask()
private fun Int.insert(value: Boolean, offset: Int): Int {
val bits = (1 shl offset)
return if (value) this or bits else this and bits.inv()
}
private fun Int.insert(value: Int, offset: Int, bits: Int): Int {
val mask = bits.mask() shl offset
val ovalue = (value shl offset) and mask
return (this and mask.inv()) or ovalue
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import korlibs.math.geom.*
import korlibs.math.geom.bezier.*
import korlibs.math.geom.shape.*
import korlibs.math.interpolation.*
import korlibs.memory.*

// @TODO: Implement LineCap + LineJoin
// @TODO: Use Curves and reuse code from [CurvesToStrokes]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import korlibs.math.interpolation.*
import korlibs.number.*
import kotlin.math.*


@Deprecated("Use immutable BoundsBuilder instead")
class MBoundsBuilder {
val tempRect = MRectangle()
Expand Down
10 changes: 5 additions & 5 deletions korlibs-memory/src/korlibs/memory/Bits.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ public fun Int.extractScaledFFDefault(offset: Int, count: Int, default: Int): In

/** Replaces [this] bits from [offset] to [offset]+[count] with [value] and returns the result of doing such replacement */
public fun Int.insert(value: Int, offset: Int, count: Int): Int {
val mask = count.mask()
val clearValue = this and (mask shl offset).inv()
return clearValue or ((value and mask) shl offset)
val mask = count.mask() shl offset
val ovalue = (value shl offset) and mask
return (this and mask.inv()) or ovalue
}

public fun Int.insert24(value: Int, offset: Int): Int = insertMask(value, offset, 0xFFFFFF)
Expand Down Expand Up @@ -236,8 +236,8 @@ inline fun Int.insertMask(value: Int, offset: Int, mask: Int): Int {
}
/** Replaces 1 bit at [offset] with [value] and returns the result of doing such replacement */
public fun Int.insert(value: Boolean, offset: Int): Int {
val ivalue = if (value) 1 else 0
return (this and (1 shl offset).inv()) or (ivalue shl offset)
val bits = (1 shl offset)
return if (value) this or bits else this and bits.inv()
}

public fun Int.insertScaled(value: Int, offset: Int, count: Int, scale: Int): Int = insert((value * count.mask()) / scale, offset, count)
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ include(":korlibs-datastructure")
include(":korlibs-memory")
include(":korlibs-util")
include(":korlibs-math-core")
include(":korlibs-math")
include(":korlibs-inject")
include(":korlibs-time")
include(":korlibs-bignumber")
include(":korlibs-annotations")
include(":korlibs-crypto")
include(":korlibs-number")
include(":korlibs-serialization-yaml")
Expand Down

0 comments on commit 3dabfd6

Please sign in to comment.