Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Half, Vectors, Matrices, Quaternion, and Ray serializable #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.net.URL

plugins {
kotlin("multiplatform") version "1.7.21"
kotlin("plugin.serialization") version "1.7.21"
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
id("org.jetbrains.dokka") version "1.7.20"
id("maven-publish")
Expand Down Expand Up @@ -60,6 +61,7 @@ kotlin {
commonMain {
dependencies {
implementation(kotlin("stdlib-common"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.5.1")
}
}
commonTest {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=dev.romainguy
VERSION_NAME=1.5.3
VERSION_NAME=1.5.4

POM_DESCRIPTION=Graphics oriented math library for Kotlin

Expand Down
2 changes: 2 additions & 0 deletions src/commonMain/kotlin/dev/romainguy/kotlin/math/Half.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package dev.romainguy.kotlin.math

import dev.romainguy.kotlin.math.Half.Companion.POSITIVE_INFINITY
import dev.romainguy.kotlin.math.Half.Companion.POSITIVE_ZERO
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline

/**
Expand Down Expand Up @@ -251,6 +252,7 @@ fun Rational.toHalf() = toFloat().toHalf()
* This table shows that numbers higher than 1024 lose all fractional precision.
*/
@JvmInline
@Serializable
value class Half(private val v: UShort) : Comparable<Half> {
companion object {
/**
Expand Down
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/dev/romainguy/kotlin/math/Matrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package dev.romainguy.kotlin.math

import kotlinx.serialization.Serializable
import kotlin.math.*

enum class MatrixColumn {
Expand All @@ -36,6 +37,7 @@ enum class RotationsOrder(
ZYX(VectorComponent.Z, VectorComponent.Y, VectorComponent.X);
}

@Serializable
data class Mat2(
var x: Float2 = Float2(x = 1.0f),
var y: Float2 = Float2(y = 1.0f)) {
Expand Down Expand Up @@ -128,6 +130,7 @@ data class Mat2(
}
}

@Serializable
data class Mat3(
var x: Float3 = Float3(x = 1.0f),
var y: Float3 = Float3(y = 1.0f),
Expand Down Expand Up @@ -238,6 +241,7 @@ data class Mat3(
}
}

@Serializable
data class Mat4(
var x: Float4 = Float4(x = 1.0f),
var y: Float4 = Float4(y = 1.0f),
Expand Down
2 changes: 2 additions & 0 deletions src/commonMain/kotlin/dev/romainguy/kotlin/math/Quaternion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package dev.romainguy.kotlin.math

import kotlinx.serialization.Serializable
import kotlin.math.*
enum class QuaternionComponent {
X, Y, Z, W
Expand All @@ -28,6 +29,7 @@ enum class QuaternionComponent {
* The Quaternion will be normalized during construction
* Default: Identity
*/
@Serializable
data class Quaternion(
var x: Float = 0.0f,
var y: Float = 0.0f,
Expand Down
2 changes: 2 additions & 0 deletions src/commonMain/kotlin/dev/romainguy/kotlin/math/Rational.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package dev.romainguy.kotlin.math

import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline
import kotlin.math.abs
import kotlin.math.sign
Expand All @@ -31,6 +32,7 @@ fun Rational(value: Int) = Rational(pack(value, 1))
fun Rational(numerator: Int, denominator: Int) = Rational(pack(numerator, denominator))

@JvmInline
@Serializable
value class Rational(private val r: Long) : Comparable<Rational> {
companion object {
val NaN = Rational(0, 0)
Expand Down
3 changes: 3 additions & 0 deletions src/commonMain/kotlin/dev/romainguy/kotlin/math/Ray.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package dev.romainguy.kotlin.math

import kotlinx.serialization.Serializable

@Serializable
data class Ray(var origin: Float3 = Float3(), var direction: Float3)

fun pointAt(r: Ray, t: Float) = r.origin + r.direction * t
10 changes: 10 additions & 0 deletions src/commonMain/kotlin/dev/romainguy/kotlin/math/Vector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import kotlin.math.min
import kotlin.math.sqrt
import kotlin.math.acos
import kotlin.math.absoluteValue
import kotlinx.serialization.Serializable

enum class VectorComponent {
X, Y, Z, W,
R, G, B, A,
S, T, P, Q
}

@Serializable
data class Float2(var x: Float = 0.0f, var y: Float = 0.0f) {
constructor(v: Float) : this(v, v)
constructor(v: Float2) : this(v.x, v.y)
Expand Down Expand Up @@ -153,6 +155,7 @@ data class Float2(var x: Float = 0.0f, var y: Float = 0.0f) {
fun toFloatArray() = floatArrayOf(x, y)
}

@Serializable
data class Float3(var x: Float = 0.0f, var y: Float = 0.0f, var z: Float = 0.0f) {
constructor(v: Float) : this(v, v, v)
constructor(v: Float2, z: Float = 0.0f) : this(v.x, v.y, z)
Expand Down Expand Up @@ -342,6 +345,7 @@ data class Float3(var x: Float = 0.0f, var y: Float = 0.0f, var z: Float = 0.0f)
fun toFloatArray() = floatArrayOf(x, y, z)
}

@Serializable
data class Float4(
var x: Float = 0.0f,
var y: Float = 0.0f,
Expand Down Expand Up @@ -958,6 +962,7 @@ inline infix fun Float4.neq(b: Float4) = Bool4(x != b.x, y != b.y, z != b.z, w !
inline fun any(v: Bool4) = v.x || v.y || v.z || v.w
inline fun all(v: Bool4) = v.x && v.y && v.z && v.w

@Serializable
data class Bool2(var x: Boolean = false, var y: Boolean = false) {
constructor(v: Bool2) : this(v.x, v.y)

Expand Down Expand Up @@ -1045,6 +1050,7 @@ data class Bool2(var x: Boolean = false, var y: Boolean = false) {
}
}

@Serializable
data class Bool3(var x: Boolean = false, var y: Boolean = false, var z: Boolean = false) {
constructor(v: Bool2, z: Boolean = false) : this(v.x, v.y, z)
constructor(v: Bool3) : this(v.x, v.y, v.z)
Expand Down Expand Up @@ -1189,6 +1195,7 @@ data class Bool3(var x: Boolean = false, var y: Boolean = false, var z: Boolean
}
}

@Serializable
data class Bool4(
var x: Boolean = false,
var y: Boolean = false,
Expand Down Expand Up @@ -1403,6 +1410,7 @@ data class Bool4(
}
}

@Serializable
data class Half2(var x: Half = Half.POSITIVE_ZERO, var y: Half = Half.POSITIVE_ZERO) {
constructor(v: Half) : this(v, v)
constructor(v: Half2) : this(v.x, v.y)
Expand Down Expand Up @@ -1513,6 +1521,7 @@ data class Half2(var x: Half = Half.POSITIVE_ZERO, var y: Half = Half.POSITIVE_Z
fun toFloatArray() = floatArrayOf(x.toFloat(), y.toFloat())
}

@Serializable
data class Half3(
var x: Half = Half.POSITIVE_ZERO,
var y: Half = Half.POSITIVE_ZERO,
Expand Down Expand Up @@ -1690,6 +1699,7 @@ data class Half3(
fun toFloatArray() = floatArrayOf(x.toFloat(), y.toFloat(), z.toFloat())
}

@Serializable
data class Half4(
var x: Half = Half.POSITIVE_ZERO,
var y: Half = Half.POSITIVE_ZERO,
Expand Down