Skip to content

Commit

Permalink
misc: temporary test to compare with local decimated mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis.madsen committed Jan 23, 2024
1 parent 8dc8d27 commit 3c5fa9a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
36 changes: 18 additions & 18 deletions src/main/scala/scalismo/mesh/MeshOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,10 @@
package scalismo.mesh

import scalismo.common.{DifferentiableField, EuclideanSpace, Field, PointId, UnstructuredPoints3D}
import scalismo.geometry.{_3D, EuclideanVector, Point}
import scalismo.mesh.MeshBoundaryPredicates.{fillTriangleOnBorderMap, TriangleSortedPointIds}
import scalismo.mesh.boundingSpheres.{
BoundingSphereHelpers,
BoundingSpheres,
ClosestPoint,
ClosestPointInTriangle,
ClosestPointOnLine,
ClosestPointWithType,
LineTetrahedralMesh3DIntersectionIndex,
LineTriangleMesh3DIntersectionIndex,
SurfaceSpatialIndex,
TetrahedralMesh3DSpatialIndex,
TetrahedralizedVolumeIntersectionIndex,
TriangleMesh3DSpatialIndex,
TriangulatedSurfaceIntersectionIndex,
VolumeSpatialIndex
}
import scalismo.geometry.{EuclideanVector, Point, _3D}
import scalismo.mesh.MeshBoundaryPredicates.{TriangleSortedPointIds, fillTriangleOnBorderMap}
import scalismo.mesh.boundingSpheres.{BoundingSphereHelpers, BoundingSpheres, ClosestPoint, ClosestPointInTriangle, ClosestPointOnLine, ClosestPointWithType, LineTetrahedralMesh3DIntersectionIndex, LineTriangleMesh3DIntersectionIndex, SurfaceSpatialIndex, TetrahedralMesh3DSpatialIndex, TetrahedralizedVolumeIntersectionIndex, TriangleMesh3DSpatialIndex, TriangulatedSurfaceIntersectionIndex, VolumeSpatialIndex}
import scalismo.mesh.decimate.MeshDecimation
import scalismo.utils.MeshConversion

import scala.collection.parallel.immutable.ParVector
Expand Down Expand Up @@ -228,6 +214,20 @@ class TriangleMesh3DOperations(private val mesh: TriangleMesh[_3D]) {
* @return
* The decimated mesh
*/
def decimateOld(targetedNumberOfVertices: Int): TriangleMesh[_3D] = {
val refVtk = MeshConversion.meshToVtkPolyData(mesh)
val decimate = new vtk.vtkQuadricDecimation()

val reductionRate = 1.0 - (targetedNumberOfVertices / mesh.pointSet.numberOfPoints.toDouble)

decimate.SetTargetReduction(reductionRate)

decimate.SetInputData(refVtk)
decimate.Update()
val decimatedRefVTK = decimate.GetOutput()
MeshConversion.vtkPolyDataToTriangleMesh(decimatedRefVTK).get
}

def decimate(targetedNumberOfVertices: Int): TriangleMesh[_3D] = {
require(targetedNumberOfVertices > 0)
val fraction = 1.0 max (mesh.pointSet.numberOfPoints / targetedNumberOfVertices.toDouble)
Expand Down
Binary file added src/test/resources/facemesh_decimated.ply
Binary file not shown.
16 changes: 14 additions & 2 deletions src/test/scala/scalismo/mesh/MeshDecimationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@ class MeshDecimationTests extends ScalismoTestSuite {

val path = getClass.getResource("/facemesh.stl").getPath
val facemesh = MeshIO.readMesh(new File(URLDecoder.decode(path, "UTF-8"))).get
val pathDecimated = getClass.getResource("/facemesh_decimated.ply").getPath
val facemeshDecimated = MeshIO.readMesh(new File(URLDecoder.decode(pathDecimated, "UTF-8"))).get
val t0 = System.currentTimeMillis()
val reducedMesh = facemesh.operations.decimate(facemesh.pointSet.numberOfPoints / 3)
val t1 = System.currentTimeMillis()
val _ = facemesh.operations.decimateOld(facemesh.pointSet.numberOfPoints / 3)
val t2 = System.currentTimeMillis()
println(s"Decimation time (new): ${(t1 - t0) / 1000.0}")
println(s"Decimation time (old): ${(t2 - t1) / 1000.0}")

it("has a reduced number of points") {
val reducedMesh = facemesh.operations.decimate(facemesh.pointSet.numberOfPoints / 3)
val reductionRatio = reducedMesh.pointSet.numberOfPoints / facemesh.pointSet.numberOfPoints.toDouble
reductionRatio should be(0.3 +- 0.1)
}

it("has approximately preserves the surface") {
val reducedMesh = facemesh.operations.decimate(facemesh.pointSet.numberOfPoints / 4)
MeshMetrics.hausdorffDistance(reducedMesh, facemesh) < 1.0
}

it("that is equal to another decimated mesh") {
reducedMesh.triangulation.triangles.toSet should equal(facemeshDecimated.triangulation.triangles.toSet)
reducedMesh.pointSet.numberOfPoints should equal(facemeshDecimated.pointSet.numberOfPoints)
}
}
}

0 comments on commit 3c5fa9a

Please sign in to comment.