From 8d50947e46bf843816d5e9d34e2a7cc6ddf8177a Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 27 Sep 2024 23:29:09 +0200 Subject: [PATCH] [orx-mesh] Add bounds properties --- .../kotlin/IndexedPolygonExtensions.kt | 32 +++++++++++++++++++ .../commonMain/kotlin/MeshDataExtensions.kt | 9 +++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/orx-mesh/src/commonMain/kotlin/IndexedPolygonExtensions.kt b/orx-mesh/src/commonMain/kotlin/IndexedPolygonExtensions.kt index cd8461a1..87536c7b 100644 --- a/orx-mesh/src/commonMain/kotlin/IndexedPolygonExtensions.kt +++ b/orx-mesh/src/commonMain/kotlin/IndexedPolygonExtensions.kt @@ -2,6 +2,9 @@ package org.openrndr.extra.mesh import org.openrndr.math.LinearType import org.openrndr.math.Vector3 +import org.openrndr.shape.Box +import kotlin.math.max +import kotlin.math.min internal fun > bc(barycentric: Vector3, items: List): T { return (items[0] * barycentric.x) + (items[1] * barycentric.y) + (items[2] * barycentric.z) @@ -30,4 +33,33 @@ fun IIndexedPolygon.point(vertexData: VertexData, barycentric: Vector3): Point { if (tangents.isNotEmpty()) bc(barycentric, tangents) else null, if (bitangents.isNotEmpty()) bc(barycentric, bitangents) else null ) +} + +/** + * Evaluate position bounds + */ +fun List.bounds(vertexData: IVertexData): Box { + if (isEmpty()) { + return Box.EMPTY + } else { + var px = Double.NEGATIVE_INFINITY + var py = Double.NEGATIVE_INFINITY + var pz = Double.NEGATIVE_INFINITY + var nx = Double.POSITIVE_INFINITY + var ny = Double.POSITIVE_INFINITY + var nz = Double.POSITIVE_INFINITY + + for (p in this) { + for (i in p.positions) { + val v = vertexData.positions[i] + px = max(px, v.x) + py = max(py, v.y) + pz = max(pz, v.z) + nx = min(nx, v.x) + ny = min(ny, v.y) + nz = min(nz, v.z) + } + } + return Box(nx, ny, nz, px - nx, py - ny, pz - nz) + } } \ No newline at end of file diff --git a/orx-mesh/src/commonMain/kotlin/MeshDataExtensions.kt b/orx-mesh/src/commonMain/kotlin/MeshDataExtensions.kt index b93f9585..140edf1f 100644 --- a/orx-mesh/src/commonMain/kotlin/MeshDataExtensions.kt +++ b/orx-mesh/src/commonMain/kotlin/MeshDataExtensions.kt @@ -3,6 +3,7 @@ package org.openrndr.extra.mesh import org.openrndr.color.ColorRGBa import org.openrndr.draw.* import org.openrndr.math.* +import org.openrndr.shape.Box /** * The [VertexFormat] for a [VertexBuffer] with positions, normals and texture coordinates. @@ -225,4 +226,10 @@ fun IMeshData.weld( } ) return MeshData(reindexedVertexData, reindexedPolygons) -} \ No newline at end of file +} + +/** + * Evaluate mesh bounds + */ +val IMeshData.bounds: Box + get() = polygons.bounds(vertexData) \ No newline at end of file