UnityMol  0.9.6-875
UnityMol viewer / In developement
Welding.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 
5 public class Welding {
6 
7 
8 
9  private static bool isIn(List<Vector3> vList, Vector3 vertex, float threshold) {
10  foreach(Vector3 v in vList) {
11  if(Vector3.Distance(v, vertex) <= threshold)
12  return true;
13  }
14  return false;
15  }
16 
17 
18 
19 
20  public static MeshData Weld(int[] triangles, Vector3[] vertices, Vector3 b0, Vector3 b1) {
21  Vector3[] verts = vertices;
22  VertexTree vertexTree = new VertexTree(b0, b1);
23 
24  // Build new vertex buffer and remove "duplicate" verticies
25  // that are within the given threshold.
26  List<Vector3> newVerts = new List<Vector3>();
27 
28  int vIndex = 0;
29  foreach (Vector3 vert in verts) {
30  // This adds the vertex to the tree, returns true if it was already there
31  if (!vertexTree.FindOrAddVertex(vert, vIndex)) {
32  newVerts.Add(vert);
33  vIndex++;
34  }
35  }
36  // Rebuild triangles using new verticies
37  int[] tris = triangles;
38  for (int i = 0; i < tris.Length; ++i) {
39  // We need to find the index of the new vertex closest to verts[tris[i]]
40  tris[i] = vertexTree.GetIndex(verts[tris[i]]);
41  }
42 
43  int max = -1;
44  foreach(int index in tris)
45  if (index > max)
46  max = index;
47 
48  MeshData result = new MeshData();
49  Debug.Log("GenerateMesh::AutoWeld > Number of new vertices in list, and max triangle index in new triangle array.");
50  Debug.Log(newVerts.Count.ToString());
51  Debug.Log(max.ToString());
52  //Debug.Log(vertices.Length.ToString());
53 
54  result.triangles = tris; // BEFORE VERTICES!!!
55  result.vertices = newVerts.ToArray();
56  //mesh.uv = newUVs.ToArray();
57  return result;
58  }
59 
60 }
int[] triangles
Definition: MeshData.cs:5
Vector3[] vertices
Definition: MeshData.cs:6
static bool isIn(List< Vector3 > vList, Vector3 vertex, float threshold)
Definition: Welding.cs:9
static MeshData Weld(int[] triangles, Vector3[] vertices, Vector3 b0, Vector3 b1)
Definition: Welding.cs:20