UnityMol  0.9.6-875
UnityMol viewer / In developement
MeshUtils.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 
5 /*
6  Useful mesh functions
7 */
8 public class MeshUtils : MonoBehaviour
9 {
10  // Finds a set of adjacent vertices for a given vertex
11  // Note the success of this routine expects only the set of neighboring faces to eacn contain one vertex corresponding
12  // to the vertex in question
13  public static List<Vector3> findAdjacentNeighbors ( Vector3[] v, int[] t, Vector3 vertex )
14  {
15  List<Vector3>adjacentV = new List<Vector3>();
16  List<int>facemarker = new List<int>();
17  int facecount = 0;
18 
19  // Find matching vertices
20  for (int i=0; i<v.Length; i++)
21  if (Mathf.Approximately (vertex.x, v[i].x) &&
22  Mathf.Approximately (vertex.y, v[i].y) &&
23  Mathf.Approximately (vertex.z, v[i].z))
24  {
25  int v1 = 0;
26  int v2 = 0;
27  bool marker = false;
28 
29  // Find vertex indices from the triangle array
30  for(int k=0; k<t.Length; k=k+3)
31  if(facemarker.Contains(k) == false)
32  {
33  v1 = 0;
34  v2 = 0;
35  marker = false;
36 
37  if(i == t[k])
38  {
39  v1 = t[k+1];
40  v2 = t[k+2];
41  marker = true;
42  }
43 
44  if(i == t[k+1])
45  {
46  v1 = t[k];
47  v2 = t[k+2];
48  marker = true;
49  }
50 
51  if(i == t[k+2])
52  {
53  v1 = t[k];
54  v2 = t[k+1];
55  marker = true;
56  }
57 
58  facecount++;
59  if(marker)
60  {
61  // Once face has been used mark it so it does not get used again
62  facemarker.Add(k);
63 
64  // Add non duplicate vertices to the list
65  if ( isVertexExist(adjacentV, v[v1]) == false )
66  {
67  adjacentV.Add(v[v1]);
68  //Debug.Log("Adjacent vertex index = " + v1);
69  }
70 
71  if ( isVertexExist(adjacentV, v[v2]) == false )
72  {
73  adjacentV.Add(v[v2]);
74  //Debug.Log("Adjacent vertex index = " + v2);
75  }
76  marker = false;
77  }
78  }
79  }
80 
81  //Debug.Log("Faces Found = " + facecount);
82 
83  return adjacentV;
84  }
85 
86 
87  // Finds a set of adjacent vertices indexes for a given vertex
88  // Note the success of this routine expects only the set of neighboring faces to eacn contain one vertex corresponding
89  // to the vertex in question
90  public static List<int> findAdjacentNeighborIndexes ( Vector3[] v, int[] t, Vector3 vertex )
91  {
92  List<int>adjacentIndexes = new List<int>();
93  List<Vector3>adjacentV = new List<Vector3>();
94  List<int>facemarker = new List<int>();
95  int facecount = 0;
96 
97  // Find matching vertices
98  for (int i=0; i<v.Length; i++)
99  if (Mathf.Approximately (vertex.x, v[i].x) &&
100  Mathf.Approximately (vertex.y, v[i].y) &&
101  Mathf.Approximately (vertex.z, v[i].z))
102  {
103  int v1 = 0;
104  int v2 = 0;
105  bool marker = false;
106 
107  // Find vertex indices from the triangle array
108  for(int k=0; k<t.Length; k=k+3)
109  if(facemarker.Contains(k) == false)
110  {
111  v1 = 0;
112  v2 = 0;
113  marker = false;
114 
115  if(i == t[k])
116  {
117  v1 = t[k+1];
118  v2 = t[k+2];
119  marker = true;
120  }
121 
122  if(i == t[k+1])
123  {
124  v1 = t[k];
125  v2 = t[k+2];
126  marker = true;
127  }
128 
129  if(i == t[k+2])
130  {
131  v1 = t[k];
132  v2 = t[k+1];
133  marker = true;
134  }
135 
136  facecount++;
137  if(marker)
138  {
139  // Once face has been used mark it so it does not get used again
140  facemarker.Add(k);
141 
142  // Add non duplicate vertices to the list
143  if ( isVertexExist(adjacentV, v[v1]) == false )
144  {
145  adjacentV.Add(v[v1]);
146  adjacentIndexes.Add(v1);
147  //Debug.Log("Adjacent vertex index = " + v1);
148  }
149 
150  if ( isVertexExist(adjacentV, v[v2]) == false )
151  {
152  adjacentV.Add(v[v2]);
153  adjacentIndexes.Add(v2);
154  //Debug.Log("Adjacent vertex index = " + v2);
155  }
156  marker = false;
157  }
158  }
159  }
160 
161  //Debug.Log("Faces Found = " + facecount);
162 
163  return adjacentIndexes;
164  }
165 
166  // Does the vertex v exist in the list of vertices
167  static bool isVertexExist(List<Vector3>adjacentV, Vector3 v)
168  {
169  bool marker = false;
170  foreach (Vector3 vec in adjacentV)
171  if (Mathf.Approximately(vec.x,v.x) && Mathf.Approximately(vec.y,v.y) && Mathf.Approximately(vec.z,v.z))
172  {
173  marker = true;
174  break;
175  }
176 
177  return marker;
178  }
179 }
static List< int > findAdjacentNeighborIndexes(Vector3[] v, int[] t, Vector3 vertex)
Definition: MeshUtils.cs:90
static bool isVertexExist(List< Vector3 >adjacentV, Vector3 v)
Definition: MeshUtils.cs:167
static List< Vector3 > findAdjacentNeighbors(Vector3[] v, int[] t, Vector3 vertex)
Definition: MeshUtils.cs:13