[orx-mesh] Add bounds properties

This commit is contained in:
Edwin Jakobs
2024-09-27 23:29:09 +02:00
parent 7c2e55b82b
commit 8d50947e46
2 changed files with 40 additions and 1 deletions

View File

@@ -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 <T : LinearType<T>> bc(barycentric: Vector3, items: List<T>): 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<IIndexedPolygon>.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)
}
}

View File

@@ -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)
}
}
/**
* Evaluate mesh bounds
*/
val IMeshData.bounds: Box
get() = polygons.bounds(vertexData)