-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.cs
More file actions
305 lines (242 loc) · 8.49 KB
/
Network.cs
File metadata and controls
305 lines (242 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using UnityEngine;
using System.Collections;
public class Network : MonoBehaviour {
//import SimpleJSON;
static KDTree kdtree;
static Vector3[] uniquePoints;
static Color[] colorList;
static Color[] ptColorList;
static int[] identityList;
static string[] idList;
static Hashtable positionLookup= new Hashtable();
static Hashtable colorLookup= new Hashtable();
static Hashtable neighborLookup= new Hashtable();
static Hashtable indexLookup= new Hashtable();
static int[] indicesMaster;
static int selectedIndex = -1;
static Color savedColor= new Color(0,0,0,0);
static Color highlightColor= new Color(1,1,1,0.1f);
public Transform finger;
public GameObject tooltip;
public HandInputController HIC1;
public GameObject imagePlane;
bool highLighted= false;
float fps= 0;
float frames= 0;
static float opacity = 0.05f;
static float scaling= 500.0f;
static float cccc= 0;
static Mesh nodesMesh;
static Mesh edgesMesh;
public float offset= -7.22f;
public GameObject sphere;
Hashtable content= new Hashtable();
int handStatus;
public string url = "localhost:9000/api/list";
void Start (){
WWW www = new WWW(url);
StartCoroutine (loadData (www));
}
IEnumerator loadData(WWW www){
yield return www;
Debug.Log(www.error);
content = JsonUtility.FromJson<Hashtable>(www.text);
Debug.Log(content);
processData(content);
UpdateMeshes();
}
void Update (){
fps += 1.0f / Time.deltaTime;
frames++;
if(frames%600 ==0){
Debug.Log("AVG FPS: " + fps / frames);
}
}
void testUpdate (){
}
void ColorMap (int index){
}
public static void unHighlight (int index){
GameObject nodesChild = GameObject.Find("Network/Nodes");
GameObject edgesChild = GameObject.Find("Network/Edges");
MeshFilter nodeMeshFilter = nodesChild.GetComponent<MeshFilter>();
Mesh nodesMesh = nodeMeshFilter.mesh;
MeshFilter edgeMeshFilter = edgesChild.GetComponent<MeshFilter>();
Mesh edgesMesh = edgeMeshFilter.mesh;
if( selectedIndex >= 0 ){
Color nsColor= new Color(savedColor[0],savedColor[0],savedColor[0],1);
Color esColor= new Color(savedColor[0],savedColor[0],savedColor[0], opacity);
nodesMesh.colors[selectedIndex] = nsColor;
edgesMesh.colors[selectedIndex] = esColor;
}
selectedIndex = -1;
}
void highlight (){
//Vector3 p = new Vector3(finger.position.x , finger.position.y, finger.position.z);
Vector3 p= sphere.transform.position;
offset = this.transform.position.y;
p -= this.transform.position;
p /= this.transform.localScale.x;
int index = kdtree.FindNearest(p);
highlightIndex(index);
}
void highlightIndex ( int index ){
GameObject edgesChild = GameObject.Find("Edges");
Color[] cs = colorList;
Color[] ptCs = ptColorList;
if( selectedIndex >= 0 ){
Color nsColor= new Color(savedColor[0],savedColor[1],savedColor[2],1);
Color esColor= new Color(savedColor[0],savedColor[1],savedColor[2], opacity);
cs[selectedIndex] = esColor;
ptCs[selectedIndex] = nsColor;
}
savedColor = cs[index];
cs[index] = highlightColor;
ptCs[index] = highlightColor;
selectedIndex = index;
nodesMesh.vertices = uniquePoints;
edgesMesh.vertices = uniquePoints;
nodesMesh.colors = ptCs;
edgesMesh.colors = cs;
Hashtable ht = (Hashtable) positionLookup[index];
Vector3 location = (Vector3) uniquePoints[index];
location *= this.transform.localScale.x;
location += this.transform.position;
if(tooltip.GetComponent<TextMesh>().text != ht["screen_name"]){
tooltip.transform.position = location + new Vector3(0,0.03f,0);
tooltip.GetComponent<TextMesh>().text = (string) ht["screen_name"];
}
tooltip.transform.LookAt(2 * tooltip.transform.position - Camera.main.transform.position);
}
IEnumerator loadImage(string url){
WWW www = new WWW(url);
yield return www;
if(www.error != null){
Debug.Log(url);
Renderer renderer = imagePlane.GetComponent<Renderer>();
www.LoadImageIntoTexture((Texture2D) renderer.material.mainTexture);
}
else{
Debug.Log(www.error);
}
}
void processData ( Hashtable content ){
print (content);
foreach(string key in content.Keys)
{
print(string.Format("{0}: ", key));
}
ArrayList nodes = new ArrayList ();
ArrayList edges = new ArrayList ();
positionLookup = processNodes(nodes);
Debug.Log("done w/ nodes");
Debug.Log("node count: " + content["nodes"]);
indicesMaster = processEdges((ArrayList) content["edges"], indicesMaster);
Debug.Log("done w/ edges");
Debug.Log("edge count * 2: " + content["edges"]);
}
void createMeshObject ( Mesh mesh , string name , Vector3[] points , Color[] colors , int[] indices , MeshTopology topoType ){
GameObject meshObject = new GameObject(name);
Material material= new Material(Shader.Find ("Mobile/Particles/Additive"));
if( name == "Nodes" ){
material = new Material(Shader.Find ("Mobile/Particles/Alpha Blended"));
}
meshObject.transform.parent = this.transform;
meshObject.transform.localScale = new Vector3(1,1,1);
meshObject.transform.localPosition = new Vector3(0,0,0);
meshObject.AddComponent<MeshRenderer>();
MeshRenderer renderer = meshObject.GetComponent<MeshRenderer>();
renderer.material = material;
meshObject.AddComponent<MeshFilter>();
MeshFilter filter = meshObject.GetComponent <MeshFilter>();
filter.mesh = mesh;
mesh.MarkDynamic();
mesh.vertices = points;
mesh.colors = colors;
mesh.SetIndices(indices, topoType, 0);
}
void UpdateMeshes (){
nodesMesh = new Mesh();
edgesMesh = new Mesh();
print (uniquePoints);
print (indicesMaster);
createMeshObject(edgesMesh, "Edges", uniquePoints, colorList, indicesMaster, MeshTopology.Lines);
//createMeshObject(nodesMesh, "Nodes", uniquePoints, ptColorList, identityList, MeshTopology.Points);
}
Hashtable processNodes ( ArrayList nodes ){
Hashtable nodeLookup= new Hashtable();
int ind= 0;
// set global vars
uniquePoints = new Vector3[nodes.Count];
idList = new string[nodes.Count];
colorList = new Color[nodes.Count];
ptColorList = new Color[nodes.Count];
identityList = new int[nodes.Count];
for(int i= 0; i < nodes.Count; i++){
identityList[i] = i;
}
foreach(Hashtable ht in nodes){
string id= ht["id"].ToString();
ArrayList poslist = (ArrayList) ht["positions"];
ArrayList pos = (ArrayList) poslist[0];
ArrayList rgblist = (ArrayList) ht["colors"];
ArrayList rgb = (ArrayList) rgblist[0];
float x= float.Parse(pos[0].ToString());
float y= float.Parse(pos[1].ToString());
float z= float.Parse(pos[2].ToString());
nodeLookup[ind] = ht;
uniquePoints[ind] = new Vector3(x,y,z);
idList[ind] = id;
indexLookup[id] = ind;
float r = (float) rgb[0];
float g = (float) rgb[1];
float b = (float) rgb[2];
float div = 256.00000f;
Color c;
Color cpt;
c = new Color(r/div, g/div, b/div, opacity);
cpt = new Color(r/div, g/div, b/div, 1);
colorLookup[id] = c;
colorList[ind] = c;
ptColorList[ind] = cpt;
ind ++;
}
kdtree = KDTree.MakeFromPoints(uniquePoints);
return nodeLookup;
}
void addNeighbor ( Hashtable lookup , string source , string target , int index ){
if((bool)lookup[source]){
Hashtable h = (Hashtable) lookup[source];
ArrayList a = (ArrayList) h["indices"];
ArrayList b = (ArrayList) h["neighbors"];
a.Add(index.ToString());
b.Add(target);
}
else{
lookup[source] = new Hashtable();
Hashtable localHash = (Hashtable) lookup[source];
ArrayList aNew = new ArrayList();
ArrayList bNew = new ArrayList();
aNew.Add(index.ToString());
bNew.Add(target);
localHash["indices"] = aNew;
localHash["neighbors"] = bNew;
}
}
int[] processEdges ( ArrayList edges , int[] indices ){
indices = new int[2 * edges.Count];
int i= 0;
int g= 0;
foreach(Hashtable edge in edges){
string source= edge["source"].ToString();
string target= edge["target"].ToString();
//addNeighbor(neighborLookup, source, target, i);
//addNeighbor(neighborLookup, target, source, i + 1);
indices[i] = (int) indexLookup[source];
indices[i+1] = (int) indexLookup[target];
i += 2;
}
return indices;
}
}