From 1cb4e87fe6769bcd737687fb9e5bc614689782ac Mon Sep 17 00:00:00 2001 From: superblaubeere27 Date: Mon, 23 Dec 2024 16:26:57 +0100 Subject: [PATCH] Fix too many points being projected (#5026) --- .../command/commands/client/CommandConfig.kt | 2 +- .../utils/kotlin/ArrayExtensions.kt | 37 +++++++++---------- .../utils/math/geometry/PlaneSection.kt | 26 ++++++++++--- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/net/ccbluex/liquidbounce/features/command/commands/client/CommandConfig.kt b/src/main/kotlin/net/ccbluex/liquidbounce/features/command/commands/client/CommandConfig.kt index 02978cb550d..611795311bb 100644 --- a/src/main/kotlin/net/ccbluex/liquidbounce/features/command/commands/client/CommandConfig.kt +++ b/src/main/kotlin/net/ccbluex/liquidbounce/features/command/commands/client/CommandConfig.kt @@ -76,7 +76,7 @@ object CommandConfig : CommandFactory { // Load the config in a separate thread to prevent the client from freezing AutoConfig.startLoaderTask { runCatching { - if(name.startsWith("http")) { + if (name.startsWith("http")) { // Load the config from the specified URL get(name).reader() } else { diff --git a/src/main/kotlin/net/ccbluex/liquidbounce/utils/kotlin/ArrayExtensions.kt b/src/main/kotlin/net/ccbluex/liquidbounce/utils/kotlin/ArrayExtensions.kt index e08c1f92f54..35573f9c798 100644 --- a/src/main/kotlin/net/ccbluex/liquidbounce/utils/kotlin/ArrayExtensions.kt +++ b/src/main/kotlin/net/ccbluex/liquidbounce/utils/kotlin/ArrayExtensions.kt @@ -50,28 +50,25 @@ fun ClosedFloatingPointRange.proportionOfValue(value: Float): Float { infix fun ClosedRange.step(step: Double): DoubleIterable { require(start.isFinite()) require(endInclusive.isFinite()) + require(step > 0.0) return DoubleIterable { - if (step == 0.0) { - DoubleIterators.singleton(this.start) - } else { - object : DoubleIterator { - private var current = start - private var hasNextValue = current <= endInclusive - - override fun hasNext(): Boolean = hasNextValue - - override fun nextDouble(): Double { - if (!hasNextValue) throw NoSuchElementException() - val nextValue = current - current += step - if (current > endInclusive) hasNextValue = false - return nextValue - } - - override fun remove() { - throw UnsupportedOperationException("This iterator is read-only") - } + object : DoubleIterator { + private var current = start + private var hasNextValue = current <= endInclusive + + override fun hasNext(): Boolean = hasNextValue + + override fun nextDouble(): Double { + if (!hasNextValue) throw NoSuchElementException() + val nextValue = current + current += step + if (current > endInclusive) hasNextValue = false + return nextValue + } + + override fun remove() { + throw UnsupportedOperationException("This iterator is read-only") } } } diff --git a/src/main/kotlin/net/ccbluex/liquidbounce/utils/math/geometry/PlaneSection.kt b/src/main/kotlin/net/ccbluex/liquidbounce/utils/math/geometry/PlaneSection.kt index c33c722a87a..11e5f3eb833 100644 --- a/src/main/kotlin/net/ccbluex/liquidbounce/utils/math/geometry/PlaneSection.kt +++ b/src/main/kotlin/net/ccbluex/liquidbounce/utils/math/geometry/PlaneSection.kt @@ -3,8 +3,8 @@ package net.ccbluex.liquidbounce.utils.math.geometry import net.ccbluex.liquidbounce.utils.kotlin.step import net.ccbluex.liquidbounce.utils.math.plus import net.ccbluex.liquidbounce.utils.math.times +import net.minecraft.util.math.MathHelper import net.minecraft.util.math.Vec3d -import kotlin.jvm.optionals.getOrNull import kotlin.math.sqrt class PlaneSection( @@ -14,10 +14,7 @@ class PlaneSection( ) { inline fun castPointsOnUniformly(maxPoints: Int, consumer: (Vec3d) -> Unit) { - val nPoints = maxPoints - val aspectRatio = this.dirVec2.length() / this.dirVec1.length() - val dz = sqrt(1 / (aspectRatio * nPoints)) - val dy = sqrt(aspectRatio / nPoints) + val (dz, dy) = getFairStepSide(maxPoints) for (y in 0.0..1.0 step dy) { for (z in 0.0..1.0 step dz) { @@ -28,4 +25,23 @@ class PlaneSection( } } + fun getFairStepSide(nPoints: Int): Pair { + val aspectRatio = this.dirVec2.length() / this.dirVec1.length() + + val vec1zero = MathHelper.approximatelyEquals(this.dirVec1.length(), 0.0) + val vec2zero = MathHelper.approximatelyEquals(this.dirVec2.length(), 0.0) + + return when { + !vec1zero && !vec2zero -> { + val dz = sqrt(1 / (aspectRatio * nPoints)) + val dy = sqrt(aspectRatio / nPoints) + + dz to dy + } + vec1zero && vec2zero -> 1.0 to 1.0 + vec1zero -> 1.0 to (2.0 / nPoints) + else -> 2.0 / nPoints to 1.0 + } + } + }