From 44de54d9f9d2c4488b43fae350746d529adbd089 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 8 Apr 2024 18:19:58 +0700 Subject: [PATCH] Some improvements to 2d vector hashing --- gradle.properties | 2 +- .../main/kotlin/ru/dbotthepony/kommons/util/AABB.kt | 4 ++++ .../main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt | 4 ++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt | 12 ++++++++++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt | 12 ++++++++++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt | 12 ++++++++++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector3d.kt | 4 ++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector3f.kt | 4 ++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector3i.kt | 4 ++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector4d.kt | 4 ++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector4f.kt | 4 ++++ .../kotlin/ru/dbotthepony/kommons/vector/Vector4i.kt | 4 ++++ 12 files changed, 69 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index e7b8ccd..b784920 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.12.3 +projectVersion=2.13.0 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt index 6b74d0e..3e1b8a9 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt @@ -63,6 +63,8 @@ data class AABB(val mins: Vector2d, val maxs: Vector2d) { return x in mins.x .. maxs.x && y in mins.y .. maxs.y } + operator fun contains(point: IStruct2d) = isInside(point) + /** * Checks whenever is this AABB intersect with [other] * @@ -183,6 +185,8 @@ data class AABB(val mins: Vector2d, val maxs: Vector2d) { } fun expand(value: IStruct2d) = expand(value.component1(), value.component2()) + fun padded(value: IStruct2d) = expand(value.component1(), value.component2()) + fun padded(x: Double, y: Double) = expand(x, y) /** * Returns AABB which edges are expanded by [x] and [y] along their normals diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt index ff8edbd..9c38486 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt @@ -53,6 +53,8 @@ data class AABBi(val mins: Vector2i, val maxs: Vector2i) { return x in mins.x .. maxs.x && y in mins.y .. maxs.y } + operator fun contains(point: IStruct2i) = isInside(point) + /** * Checks whenever is this AABBi intersect with [other] * @@ -96,6 +98,8 @@ data class AABBi(val mins: Vector2i, val maxs: Vector2i) { ) } + fun padded(x: Int, y: Int) = expand(x, y) + fun padded(value: IStruct2i) = expand(value.component1(), value.component2()) fun expand(value: IStruct2i) = expand(value.component1(), value.component2()) /** diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt index 832e2cf..8c56853 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2d.kt @@ -111,6 +111,18 @@ data class Vector2d( operator fun unaryMinus(): Vector2d = Vector2d(-x, -y) + override fun equals(other: Any?): Boolean { + return other is Vector2d && x == other.x && y == other.y + } + + override fun hashCode(): Int { + return x.hashCode().rotateLeft(16) xor y.hashCode() + } + + override fun toString(): String { + return "[$x, $y]" + } + /** * 2D vector cross product (Z coordinate) */ diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt index 58a3c53..36d3a32 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2f.kt @@ -111,6 +111,18 @@ data class Vector2f( operator fun unaryMinus(): Vector2f = Vector2f(-x, -y) + override fun equals(other: Any?): Boolean { + return other is Vector2f && x == other.x && y == other.y + } + + override fun hashCode(): Int { + return x.hashCode().rotateLeft(16) xor y.hashCode() + } + + override fun toString(): String { + return "[$x, $y]" + } + /** * 2D vector cross product (Z coordinate) */ diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt index 3cf5be6..faa0559 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector2i.kt @@ -88,6 +88,18 @@ data class Vector2i( operator fun unaryMinus(): Vector2i = Vector2i(-x, -y) + override fun equals(other: Any?): Boolean { + return other is Vector2i && x == other.x && y == other.y + } + + override fun hashCode(): Int { + return x.rotateLeft(16) xor y + } + + override fun toString(): String { + return "[$x, $y]" + } + /** * 2D vector cross product (Z coordinate) */ diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3d.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3d.kt index 2235524..21df5e6 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3d.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3d.kt @@ -208,6 +208,10 @@ data class Vector3d( fun toDoubleVector() = this fun toFloatVector() = Vector3f(x.toFloat(), y.toFloat(), z.toFloat()) + override fun toString(): String { + return "[$x, $y, $z]" + } + companion object { @JvmField val ZERO = Vector3d() @JvmField val POSITIVE_X = Vector3d(x = 1.0) diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3f.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3f.kt index 8aabfbe..a796219 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3f.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3f.kt @@ -208,6 +208,10 @@ data class Vector3f( fun toDoubleVector() = Vector3d(x.toDouble(), y.toDouble(), z.toDouble()) fun toFloatVector() = this + override fun toString(): String { + return "[$x, $y, $z]" + } + companion object { @JvmField val ZERO = Vector3f() @JvmField val POSITIVE_X = Vector3f(x = 1.0f) diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3i.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3i.kt index 242f810..eb22674 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3i.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector3i.kt @@ -171,6 +171,10 @@ data class Vector3i( fun toDoubleVector() = Vector3d(x.toDouble(), y.toDouble(), z.toDouble()) fun toFloatVector() = Vector3f(x.toFloat(), y.toFloat(), z.toFloat()) + override fun toString(): String { + return "[$x, $y, $z]" + } + companion object { @JvmField val ZERO = Vector3i() @JvmField val POSITIVE_X = Vector3i(x = 1) diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4d.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4d.kt index fffead9..d6aeb29 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4d.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4d.kt @@ -229,6 +229,10 @@ data class Vector4d( fun toDoubleVector() = this fun toFloatVector() = Vector4f(x.toFloat(), y.toFloat(), z.toFloat(), w.toFloat()) + override fun toString(): String { + return "[$x, $y, $z, $w]" + } + companion object { @JvmField val ZERO = Vector4d() @JvmField val POSITIVE_X = Vector4d(x = 1.0) diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4f.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4f.kt index 4808216..c1748ee 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4f.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4f.kt @@ -231,6 +231,10 @@ data class Vector4f( fun toDoubleVector() = Vector4d(x.toDouble(), y.toDouble(), z.toDouble(), w.toDouble()) fun toFloatVector() = this + override fun toString(): String { + return "[$x, $y, $z, $w]" + } + companion object { @JvmField val ZERO = Vector4f() @JvmField val POSITIVE_X = Vector4f(x = 1.0f) diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4i.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4i.kt index 70c3781..e030263 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4i.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/vector/Vector4i.kt @@ -199,6 +199,10 @@ data class Vector4i( fun toDoubleVector() = Vector4d(x.toDouble(), y.toDouble(), z.toDouble(), w.toDouble()) fun toFloatVector() = Vector4f(x.toFloat(), y.toFloat(), z.toFloat(), w.toFloat()) + override fun toString(): String { + return "[$x, $y, $z, $w]" + } + companion object { @JvmField val ZERO = Vector4i() @JvmField val POSITIVE_X = Vector4i(x = 1)