Performance fix for vertices index

This commit is contained in:
Alexander Nozik 2019-07-02 15:25:32 +03:00
parent 397cc4b679
commit 4274dfff88
2 changed files with 10 additions and 6 deletions

View File

@ -11,7 +11,6 @@ import hep.dataforge.vis.spatial.ThreeFactory.Companion.updateMesh
import hep.dataforge.vis.spatial.three.ConvexBufferGeometry import hep.dataforge.vis.spatial.three.ConvexBufferGeometry
import hep.dataforge.vis.spatial.three.EdgesGeometry import hep.dataforge.vis.spatial.three.EdgesGeometry
import hep.dataforge.vis.spatial.three.euler import hep.dataforge.vis.spatial.three.euler
import hep.dataforge.vis.spatial.three.toBufferGeometry
import info.laht.threekt.core.BufferGeometry import info.laht.threekt.core.BufferGeometry
import info.laht.threekt.core.Object3D import info.laht.threekt.core.Object3D
import info.laht.threekt.geometries.BoxBufferGeometry import info.laht.threekt.geometries.BoxBufferGeometry
@ -106,7 +105,7 @@ object ThreeShapeFactory : MeshThreeFactory<Shape>(Shape::class) {
override fun buildGeometry(obj: Shape): BufferGeometry { override fun buildGeometry(obj: Shape): BufferGeometry {
return obj.run { return obj.run {
ThreeGeometryBuilder().apply { buildGeometry() }.build() ThreeGeometryBuilder().apply { buildGeometry() }.build()
}.toBufferGeometry() }
} }
} }

View File

@ -3,22 +3,27 @@ package hep.dataforge.vis.spatial
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.meta.get import hep.dataforge.meta.get
import hep.dataforge.meta.int import hep.dataforge.meta.int
import hep.dataforge.vis.spatial.three.toBufferGeometry
import info.laht.threekt.core.BufferGeometry
import info.laht.threekt.core.Face3 import info.laht.threekt.core.Face3
import info.laht.threekt.core.Geometry import info.laht.threekt.core.Geometry
import info.laht.threekt.math.Color import info.laht.threekt.math.Color
import info.laht.threekt.math.Vector3 import info.laht.threekt.math.Vector3
class ThreeGeometryBuilder : GeometryBuilder<Geometry> { class ThreeGeometryBuilder : GeometryBuilder<BufferGeometry> {
private val vertices = ArrayList<Point3D>() private val vertices = ArrayList<Point3D>()
private val faces = ArrayList<Face3>() private val faces = ArrayList<Face3>()
private val vertexCache = HashMap<Point3D, Int>()
private fun append(vertex: Point3D): Int { private fun append(vertex: Point3D): Int {
val index = vertices.indexOf(vertex) val index = vertexCache[vertex] ?: -1//vertices.indexOf(vertex)
return if (index > 0) { return if (index > 0) {
index index
} else { } else {
vertices.add(vertex) vertices.add(vertex)
vertexCache[vertex] = vertices.size - 1
vertices.size - 1 vertices.size - 1
} }
} }
@ -38,10 +43,10 @@ class ThreeGeometryBuilder : GeometryBuilder<Geometry> {
) )
} }
override fun build(): Geometry { override fun build(): BufferGeometry {
return Geometry().apply { return Geometry().apply {
vertices = this@ThreeGeometryBuilder.vertices.map { it.asVector() }.toTypedArray() vertices = this@ThreeGeometryBuilder.vertices.map { it.asVector() }.toTypedArray()
faces = this@ThreeGeometryBuilder.faces.toTypedArray() faces = this@ThreeGeometryBuilder.faces.toTypedArray()
} }.toBufferGeometry()
} }
} }