-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_json.html
148 lines (114 loc) · 3.93 KB
/
index_json.html
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
<!DOCTYPE html>
<html>
<head>
<title>Graph3d</title>
<style>canvas{width: 100%, height: 100%}</style>
<script type="text/javascript" src="http://threejs.org/build/three.min.js"></script>
<script type="text/javascript" src="TrackballControls.js"></script>
<script type="text/javascript" src="Graph.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript">
var scene, cam, main_canvas, renderer,
controls, geometry, material, tscale = 0.25;
function drawNode(node){
var draw_object =
new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({color: 0x009ACD}));
draw_object.position.x = node.position.x;
draw_object.position.y = node.position.y;
draw_object.position.z = node.position.z;
node.data.draw_object = draw_object;
scene.add(draw_object);
}
function drawEdge(source, target){
var draw_object =
new THREE.LineBasicMaterial(
{color:0xff0000, opacity: 1, linewidth: 0.5});
var tmp_geo = new THREE.Geometry();
tmp_geo.vertices.push(source.data.draw_object.position);
tmp_geo.vertices.push(target.data.draw_object.position);
line = new THREE.Line(tmp_geo, material, THREE.LinePieces);
line.scale.x = line.scale.y = line.scale.z = 1;
line.originalScale = 1;
scene.add(line);
}
function load_graph(files){
console.log("Loading Graph.");
scene = new THREE.Scene();
var graph = new Graph();
var file = files[0];
var reader = new FileReader();
reader.onload = function(e){
console.log("Reading Data");
var data = JSON.parse(e.target.result);
var nodes = data["nodes"];
var edges = data["edges"];
console.log(nodes["0"].position.toString())
for(node in nodes){
console.log(nodes[node].position)
var node_obj = new Node(node);
// node_obj.position = nodes[node].position;
node_obj.position.x = nodes[node].position[0];
node_obj.position.y = nodes[node].position[1];
node_obj.position.z = nodes[node].position[2]*tscale;
if(graph.addNode(node_obj))
drawNode(node_obj);
}
console.log("edges.length() = " + edges.length.toString());
for(edge in edges){
v0 = edges[edge][0];
v1 = edges[edge][1];
console.log(v0.toString() + "---" + v1.toString())
node0 = graph.getNode(v0);
node1 = graph.getNode(v1);
if(graph.addEdge(node0, node1))
drawEdge(node0, node1);
}
console.log("done drawing");
animate();
}
reader.readAsText(file);
}
function animate(){
requestAnimationFrame(animate);
controls.update();
render();
}
function render(){
renderer.render(scene, cam);
}
function init_scene(){
scene = new THREE.Scene();
cam = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
main_canvas = document.getElementById("main_canvas");
renderer = new THREE.WebGLRenderer({canvas:main_canvas, alpha: true});
renderer.setSize(window.innerWidth, window.innerHeight);
controls = new THREE.TrackballControls(cam);
controls.rotateSpeed = 0.5;
controls.zoomSpeed = 5.2;
controls.panSpeed = 1;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = false;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
cam.position.z = 25;
controls.addEventListener('change', render);
geometry = new THREE.SphereGeometry(0.25, 32, 32);
material = new THREE.MeshBasicMaterial({color: 0xff0000});
tscale = 0.25;
animate();
render();
}
</script>
</head>
<body onload="init_scene()">
<script>
</script>
<form>
<input type="file" id="graph_file" name="graph" onchange="load_graph(this.files)">
</form>
<canvas id="main_canvas"></canvas>
</body>
</html>