Skip to content

Commit 3a9cd8a

Browse files
Improve AdHocMesh performance (#8905)
Avoiding some Vec3Double/Vec3Float allocations and conversions. Measurements showed 10–20 percent improvement for a test box. I did not end up using CPP/JNI optimizations because C++ marching cubes, tested from elsewhere, is not significantly faster. The large line change count is almost completely due to the formatter introducing new line breaks in the marching cubes table. ### URL of deployed dev instance (used for testing): - https://adhoccpp.webknossos.xyz ### Steps to test: - Request ad-hoc meshes, should still work, be a little faster ------ - [x] Added changelog entry (create a `$PR_NUMBER.md` file in `unreleased_changes` or use `./tools/create-changelog-entry.py`) - [x] Considered [common edge cases](../blob/master/.github/common_edge_cases.md) - [x] Needs datastore update after deployment --------- Co-authored-by: MichaelBuessemeyer <39529669+MichaelBuessemeyer@users.noreply.github.com>
1 parent e1772b2 commit 3a9cd8a

File tree

7 files changed

+2857
-291
lines changed

7 files changed

+2857
-291
lines changed

unreleased_changes/8905.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Added
2+
- Improved speed of ad-hoc mesh computation by 10–20%

util/src/main/scala/com/scalableminds/util/geometry/Vec3Float.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ case class Vec3Float(x: Float, y: Float, z: Float) {
2525
}
2626

2727
object Vec3Float {
28+
def apply(p: Vec3Int): Vec3Float =
29+
Vec3Float(p.x.toFloat, p.y.toFloat, p.z.toFloat)
30+
31+
def apply(p: Vec3Double): Vec3Float =
32+
Vec3Float(p.x.toFloat, p.y.toFloat, p.z.toFloat)
33+
2834
implicit object Vec3FloatReads extends Reads[Vec3Float] {
2935
def reads(json: JsValue): JsResult[Vec3Float] = json match {
3036
case JsArray(ts) if ts.size == 3 =>

webknossos-datastore/app/com/scalableminds/webknossos/datastore/services/mcubes/MarchingCubes.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.scalableminds.webknossos.datastore.services.mcubes
22

3-
import com.scalableminds.util.geometry.{BoundingBox, Vec3Double, Vec3Int}
3+
import com.scalableminds.util.geometry.{BoundingBox, Vec3Float, Vec3Int}
44

55
import scala.collection.mutable
66

@@ -10,9 +10,9 @@ object MarchingCubes {
1010
dataDimensions: Vec3Int,
1111
boundingBox: BoundingBox,
1212
segmentId: T,
13-
offset: Vec3Double,
14-
scale: Vec3Double,
15-
vertexBuffer: mutable.ArrayBuffer[Vec3Double]): Unit = {
13+
offset: Vec3Float,
14+
scale: Vec3Float,
15+
vertexBuffer: mutable.ArrayBuffer[Float]): Unit = {
1616

1717
def getVoxelData(x: Int, y: Int, z: Int): T =
1818
data(x + (dataDimensions.x * y) + (dataDimensions.x * dataDimensions.y * z))
@@ -49,9 +49,10 @@ object MarchingCubes {
4949
if (getVoxelData(x, y + 1, z + 1) == segmentId) cubeIndex |= 128
5050
if (getVoxelData(x + 1, y + 1, z + 1) == segmentId) cubeIndex |= 64
5151

52-
val position = Vec3Double(x, y, z)
5352
MarchingCubesTable.triangleTable(cubeIndex).foreach { edgeDelta =>
54-
vertexBuffer += (position + edgeDelta + offset) * scale
53+
vertexBuffer += (x + edgeDelta.x + offset.x) * scale.x
54+
vertexBuffer += (y + edgeDelta.y + offset.y) * scale.y
55+
vertexBuffer += (z + edgeDelta.z + offset.z) * scale.z
5556
}
5657
}
5758
}

0 commit comments

Comments
 (0)