-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(charging): provide the charging stations within a certain area (#…
…446) * provide the charging stations within a certain area * ChargingStations discovery is synchronous * updates in ChargingStationIndex.java, tree is re-construced when the tree is queried IF a CS was added - implement KdTree.size() (returned constant 0) - unit test for ChargingStationIndex --------- Co-authored-by: William Knöpp <william.anton.knoepp@fokus.fraunhofer.de>
- Loading branch information
1 parent
7d0ab09
commit b1840be
Showing
11 changed files
with
431 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
.../mosaic/fed/application/ambassador/simulation/electric/objects/ChargingStationObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (c) 2025 Fraunhofer FOKUS and others. All rights reserved. | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contact: mosaic@fokus.fraunhofer.de | ||
*/ | ||
|
||
package org.eclipse.mosaic.fed.application.ambassador.simulation.electric.objects; | ||
|
||
import org.eclipse.mosaic.fed.application.ambassador.simulation.perception.index.objects.PointBoundingBox; | ||
import org.eclipse.mosaic.fed.application.ambassador.simulation.perception.index.objects.SpatialObject; | ||
import org.eclipse.mosaic.fed.application.ambassador.simulation.perception.index.objects.SpatialObjectBoundingBox; | ||
import org.eclipse.mosaic.lib.geo.CartesianPoint; | ||
import org.eclipse.mosaic.lib.objects.electricity.ChargingStationData; | ||
|
||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
|
||
public class ChargingStationObject extends SpatialObject<ChargingStationObject> { | ||
private static final long serialVersionUID = 1L; | ||
/** | ||
* The data object that stores all static and dynamic information of the charging station. | ||
*/ | ||
private ChargingStationData chargingStationData; | ||
|
||
/** | ||
* The bounding box of a charging station is represented by a single point. | ||
*/ | ||
private transient PointBoundingBox boundingBox; | ||
|
||
public ChargingStationObject(String id) { | ||
super(id); | ||
} | ||
|
||
public ChargingStationObject setChargingStationData(ChargingStationData chargingStationData) { | ||
this.chargingStationData = chargingStationData; | ||
return this; | ||
} | ||
|
||
public ChargingStationData getChargingStationData() { | ||
return chargingStationData; | ||
} | ||
|
||
@Override | ||
public ChargingStationObject setPosition(CartesianPoint position) { | ||
cartesianPosition.set(position); | ||
position.toVector3d(this); | ||
return this; | ||
} | ||
|
||
@Override | ||
public SpatialObjectBoundingBox getBoundingBox() { | ||
if (boundingBox == null) { | ||
boundingBox = new PointBoundingBox(this); | ||
} | ||
return boundingBox; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
|
||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
ChargingStationObject that = (ChargingStationObject) o; | ||
|
||
return new EqualsBuilder() | ||
.appendSuper(super.equals(o)) | ||
.append(chargingStationData, that.chargingStationData) | ||
.append(boundingBox, that.boundingBox) | ||
.isEquals(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder(5, 11) | ||
.appendSuper(super.hashCode()) | ||
.append(chargingStationData) | ||
.toHashCode(); | ||
} | ||
|
||
/** | ||
* Returns a hard copy of the {@link ChargingStationObject}, this should be used | ||
* when the data of a perceived traffic light is to be altered or stored in memory. | ||
* | ||
* @return a copy of the {@link ChargingStationObject} | ||
*/ | ||
@Override | ||
public ChargingStationObject copy() { | ||
return new ChargingStationObject(getId()) | ||
.setChargingStationData(chargingStationData) | ||
.setPosition(getProjectedPosition()); | ||
|
||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...mosaic/fed/application/ambassador/simulation/electric/providers/ChargingStationIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (c) 2025 Fraunhofer FOKUS and others. All rights reserved. | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contact: mosaic@fokus.fraunhofer.de | ||
*/ | ||
|
||
package org.eclipse.mosaic.fed.application.ambassador.simulation.electric.providers; | ||
|
||
import org.eclipse.mosaic.fed.application.ambassador.simulation.electric.objects.ChargingStationObject; | ||
import org.eclipse.mosaic.fed.application.ambassador.simulation.perception.index.objects.SpatialObjectAdapter; | ||
import org.eclipse.mosaic.lib.geo.CartesianPoint; | ||
import org.eclipse.mosaic.lib.geo.GeoCircle; | ||
import org.eclipse.mosaic.lib.geo.GeoPoint; | ||
import org.eclipse.mosaic.lib.objects.electricity.ChargingStationData; | ||
import org.eclipse.mosaic.lib.spatial.KdTree; | ||
import org.eclipse.mosaic.lib.spatial.SpatialTreeTraverser; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* A {@link ChargingStationIndex} holds Charging Stations in a tree structure, sorted by their position. | ||
* The tree is initialized and gets updated lazily when a search is performed. | ||
*/ | ||
public class ChargingStationIndex { | ||
private final int bucketSize; | ||
|
||
/** | ||
* Stores {@link ChargingStationObject}s for fast removal and position update. | ||
*/ | ||
final Map<String, ChargingStationObject> indexedChargingStations = new HashMap<>(); | ||
|
||
private KdTree<ChargingStationObject> chargingStationTree; | ||
|
||
private SpatialTreeTraverser.InRadius<ChargingStationObject> treeTraverser; | ||
|
||
private boolean needsTreeUpdate = false; | ||
|
||
public ChargingStationIndex(int bucketSize) { | ||
this.bucketSize = bucketSize; | ||
} | ||
|
||
/** | ||
* Inits a {@link ChargingStationIndex} with default bucket size of 20. | ||
* Bucket size describes the item capacity of one tree node. | ||
*/ | ||
public ChargingStationIndex() { | ||
this.bucketSize = 20; | ||
} | ||
|
||
/** | ||
* Adds a Charging Station to the tree. | ||
* Be sure to add {@link ChargingStationData} using {@link ChargingStationIndex#updateChargingStation(ChargingStationData)}. | ||
* <p>The CS is inserted into the tree when it is queried (e.g. {@link ChargingStationIndex#getChargingStationsInCircle(GeoCircle)}or | ||
* {@link ChargingStationIndex#getNumberOfChargingStations()}</p> | ||
* | ||
* @param id ID of the ChargingStation | ||
* @param position Position of the ChargingStation | ||
*/ | ||
public void addChargingStation(String id, GeoPoint position) { | ||
needsTreeUpdate = true; | ||
indexedChargingStations.computeIfAbsent(id, ChargingStationObject::new).setPosition(position.toCartesian()); | ||
} | ||
|
||
/** | ||
* Replaces the stations data object. | ||
*/ | ||
public void updateChargingStation(ChargingStationData chargingStationData) { | ||
CartesianPoint newPosition = chargingStationData.getPosition().toCartesian(); | ||
ChargingStationObject cs = indexedChargingStations.get(chargingStationData.getName()); | ||
|
||
if (cs.getPosition().toCartesian().equals(newPosition)) { | ||
cs.setChargingStationData(chargingStationData); | ||
return; | ||
} | ||
|
||
cs.setChargingStationData(chargingStationData).setPosition(newPosition); | ||
needsTreeUpdate = true; | ||
} | ||
|
||
private void updateSearchTree() { | ||
if (!needsTreeUpdate) { | ||
return; | ||
} | ||
|
||
List<ChargingStationObject> allChargingStations = new ArrayList<>(indexedChargingStations.values()); | ||
chargingStationTree = new KdTree<>(new SpatialObjectAdapter<>(), allChargingStations, bucketSize); | ||
treeTraverser = new SpatialTreeTraverser.InRadius<>(); | ||
needsTreeUpdate = false; | ||
} | ||
|
||
public List<ChargingStationObject> getChargingStationsInCircle(GeoCircle circle) { | ||
updateSearchTree(); | ||
treeTraverser.setup(circle.getCenter().toVector3d(), circle.getRadius()); | ||
treeTraverser.traverse(chargingStationTree); | ||
return treeTraverser.getResult(); | ||
} | ||
|
||
public int getNumberOfChargingStations() { | ||
updateSearchTree(); | ||
return chargingStationTree.getRoot().size(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.