Skip to content

Commit 100d8ac

Browse files
committed
Update
Use distance between two centroid in addition of cost to calculate g-score
1 parent 1f0345a commit 100d8ac

6 files changed

Lines changed: 64 additions & 40 deletions

File tree

src/AStar.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BinaryHeap } from './BinaryHeap';
1+
import { BinaryHeap } from './BinaryHeap.js';
22
import { Utils } from './Utils.js';
33

44
class AStar {
@@ -77,7 +77,7 @@ class AStar {
7777

7878
// The g score is the shortest distance from start to current node.
7979
// We need to check if the path we have arrived at this neighbour is the shortest one we have seen yet.
80-
const gScore = currentNode.g + neighbour.cost;
80+
const gScore = currentNode.g + neighbour.cost * this.heuristic(currentNode.centroid, neighbour.centroid);
8181
const beenVisited = neighbour.visited;
8282

8383
if (!beenVisited || gScore < neighbour.g) {
@@ -106,7 +106,7 @@ class AStar {
106106
}
107107

108108
static heuristic (pos1, pos2) {
109-
return Utils.distanceToSquared(pos1, pos2);
109+
return Math.sqrt(Utils.distanceToSquared(pos1, pos2));
110110
}
111111

112112
static neighbours (graph, node) {

src/Builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Vector3 } from 'three';
22

3-
import { Utils } from './Utils';
3+
import { Utils } from './Utils.js';
44

55
class Builder {
66
/**

src/Channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Utils } from './Utils';
1+
import { Utils } from './Utils.js';
22

33
class Channel {
44
constructor () {

src/Pathfinding.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
Triangle,
55
} from 'three';
66

7-
import { Utils } from './Utils';
8-
import { AStar } from './AStar';
9-
import { Builder } from './Builder';
10-
import { Channel } from './Channel';
7+
import { Utils } from './Utils.js';
8+
import { AStar } from './AStar.js';
9+
import { Builder } from './Builder.js';
10+
import { Channel } from './Channel.js';
1111

1212
/**
1313
* Defines an instance of the pathfinding module, with one or more zones.

src/Utils.js

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { BufferAttribute, BufferGeometry } from 'three';
1+
import { BufferAttribute, BufferGeometry, Vector3 } from 'three';
22

33
class Utils {
44

5-
static roundNumber (value, decimals) {
5+
static roundNumber(value, decimals) {
66
const factor = Math.pow(10, decimals);
77
return Math.round(value * factor) / factor;
88
}
99

10-
static sample (list) {
10+
static sample(list) {
1111
return list[Math.floor(Math.random() * list.length)];
1212
}
1313

14-
static distanceToSquared (a, b) {
14+
static distanceToSquared(a, b) {
1515

1616
var dx = a.x - b.x;
1717
var dy = a.y - b.y;
@@ -23,13 +23,13 @@ class Utils {
2323

2424
//+ Jonas Raoni Soares Silva
2525
//@ http://jsfromhell.com/math/is-point-in-poly [rev. #0]
26-
static isPointInPoly (poly, pt) {
26+
static isPointInPoly(poly, pt) {
2727
for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
2828
((poly[i].z <= pt.z && pt.z < poly[j].z) || (poly[j].z <= pt.z && pt.z < poly[i].z)) && (pt.x < (poly[j].x - poly[i].x) * (pt.z - poly[i].z) / (poly[j].z - poly[i].z) + poly[i].x) && (c = !c);
2929
return c;
3030
}
3131

32-
static isVectorInPolygon (vector, polygon, vertices) {
32+
static isVectorInPolygon(vector, polygon, vertices) {
3333

3434
// reference point will be the centroid of the polygon
3535
// We need to rotate the vector as well as all the points which the polygon uses
@@ -52,18 +52,42 @@ class Utils {
5252
return false;
5353
}
5454

55-
static triarea2 (a, b, c) {
55+
static triarea2(a, b, c) {
5656
var ax = b.x - a.x;
5757
var az = b.z - a.z;
5858
var bx = c.x - a.x;
5959
var bz = c.z - a.z;
6060
return bx * az - ax * bz;
6161
}
6262

63-
static vequal (a, b) {
63+
static vequal(a, b) {
6464
return this.distanceToSquared(a, b) < 0.00001;
6565
}
6666

67+
/**
68+
* Find the intersection of line ab projected on line cd (project along negative-y-axis)
69+
*/
70+
static p4intersect(a, b, c, d) {
71+
const x1 = a.x, z1 = a.z;
72+
const x2 = b.x, z2 = b.z;
73+
const x3 = c.x, z3 = c.z;
74+
const x4 = d.x, z4 = d.z;
75+
76+
const denom = (x1 - x2) * (z3 - z4) - (z1 - z2) * (x3 - x4);
77+
if (Math.abs(denom) < 1e-9) return null; // parallel or coincident
78+
79+
// t coefficient on AB
80+
const t = ((x1 - x3) * (z3 - z4) - (z1 - z3) * (x3 - x4)) / denom;
81+
// u coefficient on CD
82+
const u = ((x1 - x3) * (z1 - z2) - (z1 - z3) * (x1 - x2)) / denom;
83+
84+
const px = x3 + u * (x4 - x3);
85+
const pz = z3 + u * (z4 - z3);
86+
const py = c.y + u * (d.y - c.y);
87+
88+
return new Vector3(px, py, pz);
89+
}
90+
6791
/**
6892
* Modified version of BufferGeometryUtils.mergeVertices, ignoring vertex
6993
* attributes other than position.
@@ -72,15 +96,15 @@ class Utils {
7296
* @param {number} tolerance
7397
* @return {THREE.BufferGeometry>}
7498
*/
75-
static mergeVertices (geometry, tolerance = 1e-4) {
99+
static mergeVertices(geometry, tolerance = 1e-4) {
76100

77-
tolerance = Math.max( tolerance, Number.EPSILON );
101+
tolerance = Math.max(tolerance, Number.EPSILON);
78102

79103
// Generate an index buffer if the geometry doesn't have one, or optimize it
80104
// if it's already available.
81105
var hashToIndex = {};
82106
var indices = geometry.getIndex();
83-
var positions = geometry.getAttribute( 'position' );
107+
var positions = geometry.getAttribute('position');
84108
var vertexCount = indices ? indices.count : positions.count;
85109

86110
// Next value for triangle indices.
@@ -90,36 +114,36 @@ class Utils {
90114
var newPositions = [];
91115

92116
// Convert the error tolerance to an amount of decimal places to truncate to.
93-
var decimalShift = Math.log10( 1 / tolerance );
94-
var shiftMultiplier = Math.pow( 10, decimalShift );
117+
var decimalShift = Math.log10(1 / tolerance);
118+
var shiftMultiplier = Math.pow(10, decimalShift);
95119

96-
for ( var i = 0; i < vertexCount; i ++ ) {
120+
for (var i = 0; i < vertexCount; i++) {
97121

98-
var index = indices ? indices.getX( i ) : i;
122+
var index = indices ? indices.getX(i) : i;
99123

100124
// Generate a hash for the vertex attributes at the current index 'i'.
101125
var hash = '';
102126

103127
// Double tilde truncates the decimal value.
104-
hash += `${ ~ ~ ( positions.getX( index ) * shiftMultiplier ) },`;
105-
hash += `${ ~ ~ ( positions.getY( index ) * shiftMultiplier ) },`;
106-
hash += `${ ~ ~ ( positions.getZ( index ) * shiftMultiplier ) },`;
128+
hash += `${~ ~(positions.getX(index) * shiftMultiplier)},`;
129+
hash += `${~ ~(positions.getY(index) * shiftMultiplier)},`;
130+
hash += `${~ ~(positions.getZ(index) * shiftMultiplier)},`;
107131

108132
// Add another reference to the vertex if it's already
109133
// used by another index.
110-
if ( hash in hashToIndex ) {
134+
if (hash in hashToIndex) {
111135

112-
newIndices.push( hashToIndex[ hash ] );
136+
newIndices.push(hashToIndex[hash]);
113137

114138
} else {
115139

116-
newPositions.push( positions.getX( index ) );
117-
newPositions.push( positions.getY( index ) );
118-
newPositions.push( positions.getZ( index ) );
140+
newPositions.push(positions.getX(index));
141+
newPositions.push(positions.getY(index));
142+
newPositions.push(positions.getZ(index));
119143

120-
hashToIndex[ hash ] = nextIndex;
121-
newIndices.push( nextIndex );
122-
nextIndex ++;
144+
hashToIndex[hash] = nextIndex;
145+
newIndices.push(nextIndex);
146+
nextIndex++;
123147

124148
}
125149

@@ -128,14 +152,14 @@ class Utils {
128152
// Construct merged BufferGeometry.
129153

130154
const positionAttribute = new BufferAttribute(
131-
new Float32Array( newPositions ),
155+
new Float32Array(newPositions),
132156
positions.itemSize,
133157
positions.normalized
134158
);
135159

136160
const result = new BufferGeometry();
137-
result.setAttribute( 'position', positionAttribute );
138-
result.setIndex( newIndices );
161+
result.setAttribute('position', positionAttribute);
162+
result.setIndex(newIndices);
139163

140164
return result;
141165

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Pathfinding } from './Pathfinding';
2-
import { PathfindingHelper } from './PathfindingHelper';
1+
import { Pathfinding } from './Pathfinding.js';
2+
import { PathfindingHelper } from './PathfindingHelper.js';
33

44
export { Pathfinding, PathfindingHelper };

0 commit comments

Comments
 (0)