Skip to content

Commit

Permalink
add generic spatial index for (forest) map generation
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Mar 4, 2024
1 parent 1659b29 commit 5abfb98
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import com.recom.commons.map.rasterizer.configuration.LayerOrder;
import com.recom.commons.map.rasterizer.configuration.MapLayerRasterizer;
import com.recom.commons.map.rasterizer.mapdesignscheme.MapDesignScheme;
import com.recom.commons.model.DEMDescriptor;
import com.recom.commons.math.Round;
import com.recom.commons.model.maprendererpipeline.MapComposerWorkPackage;
import com.recom.commons.model.maprendererpipeline.MapLayerRasterizerConfiguration;
import com.recom.commons.model.maprendererpipeline.dataprovider.forest.ForestItem;
import com.recom.commons.model.maprendererpipeline.dataprovider.forest.SpacialIndex;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class ForestMapRasterizer implements MapLayerRasterizer {

@NonNull
public int[] rasterizeForestMap(
@NonNull final DEMDescriptor DEMDescriptor,
final SpacialIndex<ForestItem> spacialIndex,
@NonNull final MapDesignScheme mapScheme
) {
// final D8AlgorithmForSlopeAndAspectMap algorithmForSlopeAndAspect = new D8AlgorithmForSlopeAndAspectMap(5.0);
Expand All @@ -54,8 +55,8 @@ public int[] rasterizeForestMap(
//
// @TODO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

final int width = DEMDescriptor.getDemWidth();
final int height = DEMDescriptor.getDemHeight();
final int width = spacialIndex.getWidth();
final int height = spacialIndex.getHeight();

final int[] pixelBuffer = new int[width * height];
// for (int x = 0; x < width; x++) {
Expand Down Expand Up @@ -101,8 +102,20 @@ private CompletableFuture<List<ForestItem>> provideForestInFuture(@NonNull final
public void render(@NonNull MapComposerWorkPackage workPackage) {
if (maybePreperationTask.isPresent()) {
final List<ForestItem> forestEntities = maybePreperationTask.get().join();
// @TODO <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
final int[] rawForestMap = rasterizeForestMap(workPackage.getMapComposerConfiguration().getDemDescriptor(), workPackage.getMapComposerConfiguration().getMapDesignScheme());

// create spatial index for forest entities (pixel size)
final int demWidth = workPackage.getMapComposerConfiguration().getDemDescriptor().getDemWidth();
final int demHeight = workPackage.getMapComposerConfiguration().getDemDescriptor().getDemHeight();
final SpacialIndex<ForestItem> spatialIndex = new SpacialIndex<>(demWidth, demHeight);
forestEntities.stream().forEach(forestItem -> {
final int x = Round.halfUp(forestItem.getCoordinateX().doubleValue());
final int y = Round.halfUp(forestItem.getCoordinateY().doubleValue());
if (x >= 0 && x < demWidth && y >= 0 && y < demHeight) {
spatialIndex.put(x, y, forestItem);
}
});

final int[] rawForestMap = rasterizeForestMap(spatialIndex, workPackage.getMapComposerConfiguration().getMapDesignScheme());
workPackage.getPipelineArtifacts().addArtifact(this, rawForestMap);
} else {
log.error("ForestMapRasterizer is not prepared, and prepareAsync was not called in advance!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.recom.commons.model.maprendererpipeline.dataprovider.forest;

import lombok.Getter;
import lombok.NonNull;

import java.util.ArrayList;
import java.util.List;


public class SpacialIndex<T> {

@NonNull
private final List<T>[][] index;
@Getter
private final int width;
@Getter
private final int height;


@SuppressWarnings("unchecked")
public SpacialIndex(
final int width,
final int height
) {
this.width = width;
this.height = height;
this.index = (List<T>[][]) new List[width][height];

preInitializeIndex(width, height);
}

private void preInitializeIndex(
final int width,
final int height
) {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
this.index[x][y] = new ArrayList<T>();
}
}
}

public void put(
final int x,
final int y,
@NonNull final T value
) {
index[x][y].add(value);
}

@NonNull
public List<T> get(
final int x,
final int y
) {
return index[x][y];
}

}

0 comments on commit 5abfb98

Please sign in to comment.