perf(astar): calculate distance on the fly instead of on init
This commit is contained in:
parent
a75f05b9a3
commit
386a227978
1 changed files with 11 additions and 3 deletions
|
@ -8,22 +8,30 @@ import org.insa.graphs.model.RoadInformation;
|
||||||
public class LabelStar extends Label {
|
public class LabelStar extends Label {
|
||||||
private double distanceToDestination;
|
private double distanceToDestination;
|
||||||
private int MaximumSpeed;
|
private int MaximumSpeed;
|
||||||
|
private Node destination;
|
||||||
|
|
||||||
public LabelStar(Node node, int MaximumSpeed, Node destination) {
|
public LabelStar(Node node, int MaximumSpeed, Node destination) {
|
||||||
super(node);
|
super(node);
|
||||||
this.MaximumSpeed = MaximumSpeed;
|
this.MaximumSpeed = MaximumSpeed;
|
||||||
|
this.distanceToDestination = -1;
|
||||||
|
this.destination = destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initializeDistance() {
|
||||||
// precision was never an answer
|
// precision was never an answer
|
||||||
if (this.MaximumSpeed < 0) {
|
if (this.MaximumSpeed < 0) {
|
||||||
distanceToDestination = (double) Point.distance(node.getPoint(), destination.getPoint());
|
distanceToDestination = (double) Point.distance(node.getPoint(), this.destination.getPoint());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
distanceToDestination = (double) Point.distance(node.getPoint(), destination.getPoint()) / (1000 * MaximumSpeed);
|
distanceToDestination = (double) Point.distance(node.getPoint(), this.destination.getPoint()) / (1000 * MaximumSpeed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getTotalCost() {
|
public double getTotalCost() {
|
||||||
return this.getCost() + distanceToDestination;
|
if (this.distanceToDestination < 0)
|
||||||
|
this.initializeDistance();
|
||||||
|
return this.getCost() + this.distanceToDestination;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue