From 40b7fe7c6e211d9f09e9018d7a44ee8143cd31b5 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alex.rose@rcsb.org>
Date: Mon, 22 Oct 2018 15:55:54 -0700
Subject: [PATCH] checkForDuplicateVertices mesh helper method

---
 src/mol-geo/geometry/mesh/mesh.ts | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/mol-geo/geometry/mesh/mesh.ts b/src/mol-geo/geometry/mesh/mesh.ts
index e87de9bf2..ad4cec0ba 100644
--- a/src/mol-geo/geometry/mesh/mesh.ts
+++ b/src/mol-geo/geometry/mesh/mesh.ts
@@ -103,6 +103,28 @@ export namespace Mesh {
         mesh.normalsComputed = true;
     }
 
+    export function checkForDuplicateVertices(mesh: Mesh, fractionDigits = 3) {
+        const v = mesh.vertexBuffer.ref.value
+
+        const map = new Map<string, number>()
+        const hash = (v: Vec3, d: number) => `${v[0].toFixed(d)}|${v[1].toFixed(d)}|${v[2].toFixed(d)}`
+        let duplicates = 0
+
+        const a = Vec3.zero()
+        for (let i = 0, il = mesh.vertexCount; i < il; ++i) {
+            Vec3.fromArray(a, v, i * 3)
+            const k = hash(a, fractionDigits)
+            const count = map.get(k)
+            if (count !== undefined) {
+                duplicates += 1
+                map.set(k, count + 1)
+            } else {
+                map.set(k, 1)
+            }
+        }
+        return duplicates
+    }
+
     export function computeNormals(surface: Mesh): Task<Mesh> {
         return Task.create<Mesh>('Surface (Compute Normals)', async ctx => {
             if (surface.normalsComputed) return surface;
-- 
GitLab