Skip to content

Commit

Permalink
Move boundingRectForUnitSquare from WWMath to Matrix4.
Browse files Browse the repository at this point in the history
  • Loading branch information
ComBatVision committed Dec 3, 2024
1 parent 6c650ef commit 9623d54
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
35 changes: 33 additions & 2 deletions worldwind/src/commonMain/kotlin/earth/worldwind/geom/Matrix4.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<DrawableScreenTexture>()
val drawable = DrawableScreenTexture.obtain(pool)
Expand All @@ -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<DrawableScreenTexture>()
val drawable = DrawableScreenTexture.obtain(pool)
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package earth.worldwind.util.math

import earth.worldwind.geom.Matrix4
import earth.worldwind.geom.Viewport
import kotlin.math.*

/**
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 9623d54

Please sign in to comment.