web123456

threejs indoor navigation automatic wayfinding - obstacle marking

  • var gridWidth = 40; //Navigation grid width
  • //3D Obstacle Block
  • var objs = [
  • [{x:110,y:130},{x:110,y:150},{x:160,y:150},{x:160,y:130}],
  • [{x:85,y:20},{x:85,y:70},{x:110,y:20},{x:110,y:70}]
  • ];
  • // Generate matrix with all the same numbers, such as 0 or 1
  • //row:row,col:column
  • function produceSameNumber(row, col, number) {
  • var result = [];
  • var arr;
  • for ( var i = 0; i < row; i++ ) {
  • arr = (new Array(col)).join(',').split(',').map( function(){
  • return number;
  • });
  • result.push(arr);
  • }
  • return result;
  • }
  • // Generate a matrix of all 0s, row: row, col: column
  • function zeros(row, col) {
  • return produceSameNumber(row, col, 0);
  • }
  • //Get the object boundary value
  • function getBoundary(singleObj){
  • var max_X=null;
  • var max_Y=null;
  • var min_X=null;
  • var min_Y=null;
  • for(var i=0;i<4;i++){
  • var point = singleObj[i];
  • max_X = max_X == null || point.x > max_X ? point.x : max_X;
  • min_X = min_X == null || point.x < min_X ? point.x : min_X;
  • max_Y = max_Y == null || point.y > max_Y ? point.y : max_Y;
  • min_Y = min_Y == null || point.y < min_Y ? point.y : min_Y;
  • }
  • return {
  • max_X: max_X,
  • max_Y: max_Y,
  • min_X: min_X,
  • min_Y: min_Y
  • }
  • }
  • function markBlock(grids){
  • for(var i=0;i<grids.length;i++){
  • var cols = grids[i];
  • for(var j=0;j<cols.length;j++){
  • var point= {};
  • point.x = gridWidth * j;
  • point.y = gridWidth * i;
  • point.block = false; //Default is non-obstruction
  • checkBlock(point);
  • if(point.block){
  • grids[i][j] = 1;
  • }
  • }
  • }
  • return grids;
  • }
  • //Mark the grid points if there are obstacles
  • function checkBlock(point){
  • for(var i=0;i<objs.length;i++){
  • var obj = objs[i];
  • var boundary = getBoundary(obj); //Get boundary value
  • //Case 1: The object width is within the grid
  • if(point.x < boundary.min_X && (point.x + gridWidth) > boundary.max_X ){
  • if(point.y > boundary.min_Y && point.y < boundary.max_Y){
  • point.block = true;
  • }
  • if(point.y < boundary.min_Y && (point.y + gridWidth) > boundary.min_Y){
  • point.block = true;
  • }
  • }
  • //Case 2: The height of the object is in the grid
  • if(point.y < boundary.min_Y && (point.y + gridWidth) > boundary.max_Y){
  • if(point.x < boundary.min_X && (point.x + gridWidth) > boundary.min_X){
  • point.block = true;
  • }
  • if(point.x > boundary.min_X && point.x < boundary.max_X){
  • point.block = true;
  • }
  • }
  • //Case 3: The object width is full or exceeds the grid
  • if(point.x>=boundary.min_X && (point.x + gridWidth) <= boundary.max_X){
  • if((point.y>= boundary.min_Y && point.y< boundary.max_Y)
  • || ((point.y + gridWidth) > boundary.min_Y && (point.y + gridWidth) < boundary.max_Y)){
  • point.block = true;
  • }
  • }
  • //Case 4: The object height is full or exceeds the grid
  • if(point.y >=boundary.min_X && (point.y + gridWidth) <= boundary.max_Y){
  • if((point.x >=boundary.min_X && point.x < boundary.max_X)
  • || ((point.x + gridWidth) > boundary.min_X && (point.x + gridWidth) < boundary.max_X)){
  • point.block = true;
  • }
  • }
  • }
  • }
  • var grids = zeros(7,7,0);
  • console.log(grids);
  • console.log(markBlock(grids));