Skip to content

Commit

Permalink
pseudoCode
Browse files Browse the repository at this point in the history
  • Loading branch information
robp94 committed Aug 24, 2018
1 parent 7ea6429 commit d473871
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/graph/navmesh/ConvexPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,43 @@ class ConvexPolygon {

}

neighbouring( convexPolygon ) {

for ( let edgeA of this.edges ) {

for ( let edgeB of convexPolygon.edges ) {

if ( edgeA.equals( edgeB ) ) {

return true;

}

}

}
return false;

}

neighbouringEdge( convexPolygon ) {

for ( let edgeA of this.edges ) {

for ( let edgeB of convexPolygon.edges ) {

if ( edgeA.equals( edgeB ) ) {

return edgeA;

}

}

}

}

}

function area( a, b, c ) {
Expand All @@ -96,6 +133,22 @@ class Edge {

}

equals( edge ) {

if ( this.from === edge.from && this.to === edge.to ) {

return true;

}
if ( this.from === edge.to && this.to === edge.from ) {

return true;

}
return false;

}

}

export { ConvexPolygon };
135 changes: 134 additions & 1 deletion src/graph/navmesh/NavMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NavMesh {
const faces = geometry.faces;
const vertices = geometry.vertices;

//
//convert faces to polygons

for ( let face of faces ) {

Expand All @@ -33,10 +33,143 @@ class NavMesh {

}

const blocked = [];

while ( true ) {

let neighbours = this.getNeighboursWithLongestEdge();
if ( neighbours == null ) {

break;

}
let n1 = neighbours._1;
let n2 = neighbours._2;

const convexPolygon = this.createNewPolygon( n1, n2, n1.neighbouringEdge( n2 ) );

if ( convexPolygon.isConvex() ) {

this.regions.remove( n1 );
this.regions.remove( n2 );
//blocked remove all with n11 or n2

this.regions.add( convexPolygon );

} else {

blocked.push( neighbours );

}

}

return this;

}

createNewPolygon( p1, p2, edge ) {

//eigentlich immer p1 from p2 to
const convexPolygon = new ConvexPolygon();
let i = 0;
let from = false;
for ( let l = p1.indices.length; i < l; i ++ ) {

convexPolygon.indices.push( p1.indices[ i ] );
if ( p1.indices[ i ].equals( edge.from ) ) {

from = true;
break;

}
if ( p1.indices[ i ].equals( edge.from ) ) {

break;

}

}
//find indices of from or to in p2
let p2Indiece = 0;
if ( from ) {

p2Indiece = p2.indices.indexOf( edge.from );

} else {

p2Indiece = p2.indices.indexOf( edge.to );

}
//add all indices after edge
for ( let j = p2Indiece + 1, l = p2.indices.length; j < l; i ++ ) {

convexPolygon.indices.push( p2.indices[ j ] );

}
//add all indices before edge
for ( let j = 0, l = p2Indiece; j < l; i ++ ) {

convexPolygon.indices.push( p2.indices[ j ] );

}

i ++;// do not add from or to
for ( let l = p1.indices.length; i < l; i ++ ) {

convexPolygon.indices.push( p1.indices[ i ] );

}
convexPolygon.vertices = vertices;

convexPolygon.updateEdges();
return convexPolygon;

}


getNeighboursWithLongestEdge() {

const neighbours = this.getNeighbours();
let longest = null;
let longestEdgeLength = 0;

for ( let n of neighbours ) {

// if n is not in bocked
let edge = n._1.neighbouringEdge( n._2 ); //tupel element 1 und tupel element 2
if ( longestEdgeLength < edge.lenght ) {

longest = n;
longestEdgeLength = edge.lenght;

}

}
return longest;

}

getNeighbours() {

let neighbours = [];
for ( let i = 0, l = this.regions.length; i < l; i ++ ) {

for ( let j = i, l = this.regions.length; j < l; j ++ ) {

if ( this.regions[ i ].neighbouring( this.regions[ j ] ) ) {

neighbours.push( [ this.regions[ i ], this.regions[ j ] ] );

}

}

}
return neighbours;

}

}

export { NavMesh };

0 comments on commit d473871

Please sign in to comment.