2 normalize: function(node) {
5 if (node.component_name) { // wsn430-11.devlille.iot-lab.info
6 info = node.component_name.split(".");
7 } /*else if (node.hrn) { // iotlab.a8-11\.devgrenoble\.iot-lab\.info
8 var info = node.hrn.split("\\.");
9 info[0] = info[0].split(".")[1];
12 if (info && info[2] == "iot-lab" && info[3] == "info") {
13 node.arch = info[0].split("-")[0];
14 node.id = info[0].split("-")[1];
18 console.warn("could not normalize node :");
25 Senslab.Map = function() {
29 "Suspected": 0xFF3030,
33 function setColor(node) {
34 node.material.color.setHex(colors[node.boot_state] || colors["Selected"]);
43 function Map($container, options) {
52 this._notify = function(node) {
53 console.log("[Notify] node " + node.id + " is " + node.boot_state);
56 this.pointerDetectRay = new THREE.Raycaster();
57 this.pointerDetectRay.ray.direction.set(0, -1, 0);
58 this.projector = new THREE.Projector();
59 this.mouse2D = new THREE.Vector3(0, 0, 0);
61 this.renderer = new THREE.CanvasRenderer();
62 this.renderer.setSize(this.width, this.height);
64 this.camera = new THREE.PerspectiveCamera(75, this.width / this.height, 1, 10000);
66 this.scene = new THREE.Scene();
67 this.scene.add(this.camera);
69 this.updatePosition();
73 this.$nodeInputs = {};
75 $.each(archs, function(i, arch) {
76 self.$nodeInputs[arch] = $("<input type='text' placeholder='" + arch + "'>")
79 self.updateColor(arch, expand($(this).val()));
83 var $canvas = $(this.renderer.domElement)
84 .mousemove(function(e) {
85 self.mouse2D.x = ((e.pageX - $canvas.offset().left) / $canvas.width()) * 2 - 1;
86 self.mouse2D.y = -((e.pageY - $canvas.offset().top) / $canvas.height()) * 2 + 1;
89 self.theta -= e.pageX - self.mouse2D.pageX;
90 self.phi += e.pageY - self.mouse2D.pageY;
96 self.mouse2D.pageX = e.pageX;
97 self.mouse2D.pageY = e.pageY;
99 self.updatePosition();
102 }).mousedown(function(e) {
106 self.pointerDetectRay = self.projector.pickingRay(self.mouse2D.clone(), self.camera);
107 var intersects = self.pointerDetectRay.intersectObjects(self.scene.children);
108 if (intersects.length > 0) {
109 var node = intersects[0].object;
110 if (node.boot_state != "Suspected") {
111 node.boot_state = node.boot_state == "Alive" ? "Selected" : "Alive";
114 var $nodeInput = self.$nodeInputs[node.arch];
115 $nodeInput.val(factorize(self.getNodesId(node.arch)));
121 self.mouse2D.pageX = e.pageX;
122 self.mouse2D.pageY = e.pageY;
126 }).mouseup(function(e) {
133 }).mouseleave(function(e) {
135 }).mousewheel(function(e, delta) {
137 self.distance += delta * 5;
138 self.updatePosition();
142 $container.append($canvas);
145 Map.prototype.getNodesId = function(arch) {
146 var allNodes = this.scene.children;
148 for (var i = 0; i < allNodes.length; ++i) {
149 if (allNodes[i].arch == arch && allNodes[i].boot_state == "Selected") {
150 nodes.push(parseInt(allNodes[i].id));
156 Map.prototype.addNodes = function(nodes) {
157 var center = getCenter(nodes);
159 nodes.sort(function(a, b) {
163 for(var i = 0; i < nodes.length; ++i) {
164 var material = new THREE.ParticleCanvasMaterial({program: circle});
165 var particle = new THREE.Particle(material);
166 particle.id = nodes[i].id;
167 particle.arch = nodes[i].arch;
168 particle.boot_state = nodes[i].boot_state;
169 particle.position.x = (nodes[i].x - center.x) * 10;
170 particle.position.y = (nodes[i].y - center.y) * 10;
171 particle.position.z = (nodes[i].z - center.z) * 10;
172 particle.scale.x = particle.scale.y = 1;
174 this.scene.add(particle);
179 Map.prototype.updateColor = function(arch, selected) {
180 var nodes = this.scene.children;
181 for (var i = 0; i < nodes.length; ++i) {
182 if (nodes[i].arch == arch && nodes[i].boot_state != "Suspected") {
184 var id = parseInt(node.id);
185 var state = $.inArray(id, selected) == -1 ? "Alive" : "Selected";
186 if (node.boot_state != state) {
187 node.boot_state = state;
196 Map.prototype.updatePosition = function() {
197 this.camera.position.x = this.distance * Math.sin(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
198 this.camera.position.y = this.distance * Math.sin(this.phi * Math.PI / 360);
199 this.camera.position.z = this.distance * Math.cos(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
200 this.camera.lookAt(this.scene.position);
201 this.camera.updateMatrix();
204 Map.prototype.update = function() {
205 this.renderer.render(this.scene, this.camera);
208 function getCenter(nodes) {
209 var xmin = 0, ymin = 0, zmin = 0;
210 var xmax = 0, ymax = 0, zmax = 0;
212 for (var i = 0; i < nodes.length; ++i) {
213 if (nodes[i].x > xmax) xmax = nodes[i].x;
214 if (nodes[i].x < xmin) xmin = nodes[i].x;
215 if (nodes[i].y > ymax) ymax = nodes[i].y;
216 if (nodes[i].y < ymin) ymin = nodes[i].y;
217 if (nodes[i].z > zmax) zmax = nodes[i].z;
218 if (nodes[i].z < zmin) zmin = nodes[i].z;
220 return {x: (xmax + xmin) / 2, y: (ymax + ymin) / 2, z: (zmax + zmin) / 2};
223 function factorize(nodes) {
226 var intervalStart = 0;
228 for (var i = 0; i < nodes.length; ++i) {
230 if (nodes[i] == prev + 1) {
233 factorized.push(intervalStart + "-" + prev);
238 if (nodes[i] == prev + 1) {
239 intervalStart = prev;
242 prev && factorized.push(prev);
247 factorized.push(intervalStart ? intervalStart + "-" + prev : prev);
248 return factorized.join(",");
251 function expand(input) {
252 var factorized = input.split(",");
254 for (var i = 0; i < factorized.length; ++i) {
255 var d = factorized[i].split("-");
257 for (var j = parseInt(d[0]); j < parseInt(d[1]) + 1; j++) {
261 expanded.push(parseInt(factorized[i]));
265 expanded.sort(function(a, b) {
269 for (var i = 1; i < expanded.length; i++) {
270 if (expanded[i] == expanded[i - 1]) {
271 expanded.splice(i--, 1);
277 function circle(context) {
279 context.arc(0, 0, 1, 0, Math.PI * 2, true);