From 9623d54f0c0e9969e5c949cbeb1d0f8eb2f7ddb0 Mon Sep 17 00:00:00 2001 From: Eugene Maksymenko Date: Tue, 3 Dec 2024 14:29:19 +0200 Subject: [PATCH] Move boundingRectForUnitSquare from WWMath to Matrix4. --- .../kotlin/earth/worldwind/geom/Matrix4.kt | 35 ++++++++++++++++-- .../atmosphere/DrawableGroundAtmosphere.kt | 2 +- .../kotlin/earth/worldwind/shape/Label.kt | 3 +- .../kotlin/earth/worldwind/shape/Placemark.kt | 7 ++-- .../earth/worldwind/util/math/WWMath.kt | 36 ------------------- 5 files changed, 37 insertions(+), 46 deletions(-) diff --git a/worldwind/src/commonMain/kotlin/earth/worldwind/geom/Matrix4.kt b/worldwind/src/commonMain/kotlin/earth/worldwind/geom/Matrix4.kt index 135d5eefd..16321a16e 100644 --- a/worldwind/src/commonMain/kotlin/earth/worldwind/geom/Matrix4.kt +++ b/worldwind/src/commonMain/kotlin/earth/worldwind/geom/Matrix4.kt @@ -1242,9 +1242,9 @@ open class Matrix4 private constructor( r[2][2] = 1.0 r[1][1] = r[2][2] r[0][0] = r[1][1] - for (a in 0 until MAX_SWEEPS) { + repeat(MAX_SWEEPS) { // Exit if off-diagonal entries small enough - if (abs(m12) < EPSILON && abs(m13) < EPSILON && abs(m23) < EPSILON) break + if (abs(m12) < EPSILON && abs(m13) < EPSILON && abs(m23) < EPSILON) return@repeat // Annihilate (1,2) entry. if (m12 != 0.0) { @@ -1465,6 +1465,37 @@ open class Matrix4 private constructor( return true } + /** + * Computes the bounding rectangle for a unit square after applying a transformation matrix to the square's four + * corners. + * + * @param result a pre-allocated Viewport in which to return the computed bounding rectangle + * + * @return the result argument set to the computed bounding rectangle + */ + fun boundingRectForUnitSquare(result: Viewport): Viewport { + // transform of (0, 0) + val x1 = m[3] + val y1 = m[7] + + // transform of (1, 0) + val x2 = m[0] + m[3] + val y2 = m[4] + m[7] + + // transform of (0, 1) + val x3 = m[1] + m[3] + val y3 = m[5] + m[7] + + // transform of (1, 1) + val x4 = m[0] + m[1] + m[3] + val y4 = m[4] + m[5] + m[7] + val minX = min(min(x1, x2), min(x3, x4)).toInt() + val maxX = max(max(x1, x2), max(x3, x4)).toInt() + val minY = min(min(y1, y2), min(y3, y4)).toInt() + val maxY = max(max(y1, y2), max(y3, y4)).toInt() + return result.set(minX, minY, maxX - minX, maxY - minY) + } + override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is Matrix4) return false diff --git a/worldwind/src/commonMain/kotlin/earth/worldwind/layer/atmosphere/DrawableGroundAtmosphere.kt b/worldwind/src/commonMain/kotlin/earth/worldwind/layer/atmosphere/DrawableGroundAtmosphere.kt index 8286e130f..9b304302d 100644 --- a/worldwind/src/commonMain/kotlin/earth/worldwind/layer/atmosphere/DrawableGroundAtmosphere.kt +++ b/worldwind/src/commonMain/kotlin/earth/worldwind/layer/atmosphere/DrawableGroundAtmosphere.kt @@ -78,7 +78,7 @@ open class DrawableGroundAtmosphere : Drawable { program.loadModelviewProjection(mvpMatrix) // Use a tex coord matrix that registers the night texture correctly on each terrain. - if (textureBound && nightTexture != null) { + if (textureBound) { texCoordMatrix.copy(nightTexture.coordTransform) texCoordMatrix.multiplyByTileTransform(terrain.sector, fullSphereSector) program.loadTexCoordMatrix(texCoordMatrix) diff --git a/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Label.kt b/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Label.kt index 7957252f9..aa8fc8da6 100644 --- a/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Label.kt +++ b/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Label.kt @@ -9,7 +9,6 @@ import earth.worldwind.render.AbstractRenderable import earth.worldwind.render.Color import earth.worldwind.render.RenderContext import earth.worldwind.render.program.BasicShaderProgram -import earth.worldwind.util.math.boundingRectForUnitSquare import kotlin.jvm.JvmOverloads /** @@ -189,7 +188,7 @@ open class Label @JvmOverloads constructor( // Apply the label's translation and scale according to its text size. renderData.unitSquareTransform.multiplyByScale(w * s, h * s, 1.0) - boundingRectForUnitSquare(renderData.unitSquareTransform, renderData.screenBounds) + renderData.unitSquareTransform.boundingRectForUnitSquare(renderData.screenBounds) if (!rc.frustum.intersectsViewport(renderData.screenBounds)) return // the text is outside the viewport // Obtain a pooled drawable and configure it to draw the label's text. diff --git a/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Placemark.kt b/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Placemark.kt index f173755c8..e52381f60 100644 --- a/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Placemark.kt +++ b/worldwind/src/commonMain/kotlin/earth/worldwind/shape/Placemark.kt @@ -13,7 +13,6 @@ import earth.worldwind.render.Texture import earth.worldwind.render.image.ImageSource import earth.worldwind.render.program.BasicShaderProgram import earth.worldwind.render.program.TriangleShaderProgram -import earth.worldwind.util.math.boundingRectForUnitSquare import earth.worldwind.util.math.encodeOrientationVector import kotlin.jvm.JvmOverloads import kotlin.jvm.JvmStatic @@ -302,7 +301,7 @@ open class Placemark @JvmOverloads constructor( prepareImageTransform(rc.camera, offsetX, offsetY, scaleX, scaleY) // If the placemark's icon is visible, enqueue a drawable icon for processing on the OpenGL thread. - boundingRectForUnitSquare(imageTransform, imageBounds) + imageTransform.boundingRectForUnitSquare(imageBounds) if (rc.frustum.intersectsViewport(imageBounds)) { val pool = rc.getDrawablePool() val drawable = DrawableScreenTexture.obtain(pool) @@ -325,7 +324,7 @@ open class Placemark @JvmOverloads constructor( screenPlacePoint.z ) labelTransform.setScale(w * s, h * s, 1.0) - boundingRectForUnitSquare(labelTransform, labelBounds) + labelTransform.boundingRectForUnitSquare(labelBounds) if (rc.frustum.intersectsViewport(labelBounds)) { val pool = rc.getDrawablePool() val drawable = DrawableScreenTexture.obtain(pool) @@ -594,7 +593,5 @@ open class Placemark @JvmOverloads constructor( fun createWithImageAndLabel( position: Position, imageSource: ImageSource, label: String ) = Placemark(position, PlacemarkAttributes.createWithImage(imageSource), label) - - protected fun nextCacheKey() = Any() } } \ No newline at end of file diff --git a/worldwind/src/commonMain/kotlin/earth/worldwind/util/math/WWMath.kt b/worldwind/src/commonMain/kotlin/earth/worldwind/util/math/WWMath.kt index fb58723a3..b09a2e53d 100644 --- a/worldwind/src/commonMain/kotlin/earth/worldwind/util/math/WWMath.kt +++ b/worldwind/src/commonMain/kotlin/earth/worldwind/util/math/WWMath.kt @@ -1,7 +1,5 @@ package earth.worldwind.util.math -import earth.worldwind.geom.Matrix4 -import earth.worldwind.geom.Viewport import kotlin.math.* /** @@ -54,40 +52,6 @@ fun interpolate(amount: Double, value1: Double, value2: Double) = (1 - amount) * */ fun mod(value: Int, modulus: Int) = (value % modulus + modulus) % modulus -/** - * Computes the bounding rectangle for a unit square after applying a transformation matrix to the square's four - * corners. - * - * @param unitSquareTransform the matrix to apply to the unit square - * @param result a pre-allocated Viewport in which to return the computed bounding rectangle - * - * @return the result argument set to the computed bounding rectangle - */ -fun boundingRectForUnitSquare(unitSquareTransform: Matrix4, result: Viewport): Viewport { - val m = unitSquareTransform.m - - // transform of (0, 0) - val x1 = m[3] - val y1 = m[7] - - // transform of (1, 0) - val x2 = m[0] + m[3] - val y2 = m[4] + m[7] - - // transform of (0, 1) - val x3 = m[1] + m[3] - val y3 = m[5] + m[7] - - // transform of (1, 1) - val x4 = m[0] + m[1] + m[3] - val y4 = m[4] + m[5] + m[7] - val minX = min(min(x1, x2), min(x3, x4)).toInt() - val maxX = max(max(x1, x2), max(x3, x4)).toInt() - val minY = min(min(y1, y2), min(y3, y4)).toInt() - val maxY = max(max(y1, y2), max(y3, y4)).toInt() - return result.set(minX, minY, maxX - minX, maxY - minY) -} - /** * Indicates whether a specified value is a power of two. *