forked from rbonghi/rosmapjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosmap.min.js
1 lines (1 loc) · 5.32 KB
/
rosmap.min.js
1
var ROSMAP=ROSMAP||{REVISION:"0.0.2"};ROSMAP.VALUE_OBSTACLE=100,ROSMAP.VALUE_FREE_SPACE=0,ROSMAP.VALUE_UNKNOWN=-1,ROSMAP.VALUE_UNWRITTEN=120,Array.prototype.repeat=function(a,b){for(;b;)this[--b]=a;return this},ROSMAP.square=function(a){a=a||{};var b=a.EditorMap,c=a.color||"red",d=a.lineWidth,e=b.width/b.scaleX,f=b.height/b.scaleY;b.context.beginPath(),b.context.lineWidth=d,b.context.rect(b.context.lineWidth/2,b.context.lineWidth/2,e-b.context.lineWidth,f-b.context.lineWidth),b.context.strokeStyle=c,b.context.stroke()},ROSMAP.Editor=function(a){var b=this;a=a||{};var c=a.client||null,d=a.ros,e=a.topic||"/map_editor",f=a.index||0;this.rootObject=a.rootObject||new createjs.Container,this.strokeSize=a.strokeSize||1,this.strokeColor=a.strokeColor||createjs.Graphics.getRGB(0,0,0);var g=new ROSLIB.Topic({ros:d,name:e,messageType:"nav_msgs/OccupancyGrid"}),h=new ROSLIB.Message({header:{seq:0,stamp:{secs:0,nsecs:100},frame_id:""},info:{map_load_time:{secs:0,nsecs:100},resolution:0,width:0,height:0,origin:0},data:0}),i=!1;this.map=new createjs.Shape,this.cPushArray=[],this.cStep=-1,this.rootObject.addChildAt(this.map,f),this.index=b.rootObject.getChildIndex(this.map),this.frameBorder=new ROSMAP.EditorMap({rootObject:this.rootObject,currentGrid:c.currentGrid}),this.rootObject.addChildAt(this.frameBorder,f+1);var j,k,l=function(a){b.cStep++,b.cStep<b.cPushArray.length&&(b.cPushArray.length=b.cStep),b.cPushArray.push(a.toDataURL())};this.sendMap=function(){h.data=this.map.getMatrix(),g.publish(h)},c.on("change",function(){h.info.resolution=c.currentGrid.scaleX,h.info.width=c.currentGrid.width/c.currentGrid.scaleX,h.info.height=c.currentGrid.height/c.currentGrid.scaleY,h.info.origin=c.currentGrid.pose,b.frameBorder.updateSize(c.currentGrid),b.rootObject.removeChild(b.map),b.map=new ROSMAP.EditorMap({currentGrid:c.currentGrid}),b.rootObject.addChildAt(b.map,b.index),l(b.map.canvas)});var m=function(a){if(a.primary){var c=b.rootObject.globalToRos(a.stageX,-a.stageY);c.x=(c.x-b.map.x)/b.map.scaleX,c.y=(c.y+b.map.y)/b.map.scaleY;var d=new createjs.Point(j.x+c.x>>1,j.y+c.y>>1);b.map.context.beginPath(),b.map.context.moveTo(d.x,d.y),b.map.context.quadraticCurveTo(j.x,j.y,k.x,k.y),b.map.context.lineWidth=b.strokeSize,b.map.context.strokeStyle=b.strokeColor,b.map.context.stroke(),b.map.context.lineCap="round",j.x=c.x,j.y=c.y,k.x=d.x,k.y=d.y}};this.handleMouseUp=function(a){i&&(i=!1,a.primary&&(b.rootObject.removeEventListener("stagemousemove",m),l(b.map.canvas),b.sendMap()))},this.handleMouseDown=function(a){if(i||(i=!0),a.primary){var c=b.rootObject.globalToRos(a.stageX,-a.stageY);c.x=(c.x-b.map.x)/b.map.scaleX,c.y=(c.y+b.map.y)/b.map.scaleY,j=new createjs.Point(c.x,c.y),k=j.clone(),b.rootObject.addEventListener("stagemousemove",m)}},this.rootObject.addEventListener("stagemousedown",this.handleMouseDown),this.rootObject.addEventListener("stagemouseup",this.handleMouseUp)},ROSMAP.Editor.prototype.undo=function(){if(this.cStep>0){var a=this;this.cStep--;var b=new Image;b.src=this.cPushArray[this.cStep],b.onload=function(){a.map.clearMap(),a.map.context.drawImage(b,0,0),a.sendMap()}}},ROSMAP.Editor.prototype.redo=function(){if(this.cStep<this.cPushArray.length-1){var a=this;this.cStep++;var b=new Image;b.src=this.cPushArray[this.cStep],b.onload=function(){a.map.clearMap(),a.map.context.drawImage(b,0,0),a.sendMap()}}},ROSMAP.Editor.prototype.enable=function(a){a?(ROSMAP.square({EditorMap:this.frameBorder,color:"blue",lineWidth:5}),this.rootObject.addEventListener("stagemousedown",this.handleMouseDown),this.rootObject.addEventListener("stagemouseup",this.handleMouseUp)):(null!==this.frameBorder&&this.frameBorder.clearMap(),this.rootObject.removeEventListener("stagemousedown",this.handleMouseDown),this.rootObject.removeEventListener("stagemouseup",this.handleMouseDown))},ROSMAP.EditorMap=function(a){a=a||{};var b=a.rootObject,c=a.currentGrid;this.canvas=document.createElement("canvas"),this.context=this.canvas.getContext("2d"),"undefined"!=typeof c.width?(this.width=c.width/c.scaleX,this.height=c.height/c.scaleY):(this.width=b.canvas.width,this.height=b.canvas.height),this.canvas.width=this.width,this.canvas.height=this.height,createjs.Bitmap.call(this,this.canvas),this.y=-this.height*c.scaleX,this.scaleX=c.scaleX,this.scaleY=c.scaleY,this.width*=this.scaleX,this.height*=this.scaleY,"undefined"!=typeof c.width&&(this.x+=c.pose.position.x,this.y-=c.pose.position.y)},ROSMAP.EditorMap.prototype.__proto__=createjs.Bitmap.prototype,ROSMAP.EditorMap.prototype.updateSize=function(a){var b=this,c=new Image;c.src=this.canvas.toDataURL(),this.width=a.width/a.scaleX,this.height=a.height/a.scaleY,this.canvas.width=b.width,this.canvas.height=b.height,this.y=-this.height*a.scaleX,this.scaleX=a.scaleX,this.scaleY=a.scaleY,this.width*=this.scaleX,this.height*=this.scaleY,this.x=a.pose.position.x,this.y-=a.pose.position.y,c.onload=function(){b.context.drawImage(c,0,0)}},ROSMAP.EditorMap.prototype.getMatrix=function(){for(var a=this.width/this.scaleX,b=this.height/this.scaleY,c=this.context.getImageData(0,0,a,b),d=[],e=b;e>0;e--)for(var f=0;a>f;f++){var g=4*(a*e+f);if(0===c.data[g+3])d.push(ROSMAP.VALUE_UNWRITTEN);else switch(c.data[g]){case 0:d.push(ROSMAP.VALUE_OBSTACLE);break;case 255:d.push(ROSMAP.VALUE_FREE_SPACE);break;default:d.push(ROSMAP.VALUE_UNKNOWN)}}return d},ROSMAP.EditorMap.prototype.clearMap=function(){this.context.clearRect(0,0,this.width/this.scaleX,this.height/this.scaleY)};