Skip to content

Commit

Permalink
Merge pull request #26 from svencc/feature/map-zoom
Browse files Browse the repository at this point in the history
Feature/map zoom
  • Loading branch information
svencc authored Feb 6, 2024
2 parents 5fe5cf1 + f583a86 commit dcf6cb6
Show file tree
Hide file tree
Showing 121 changed files with 779 additions and 401 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/libs/javafxdependencies/target/
/libs/goapcom/target/
/libs/observer/target/
/libs/rendertools/target/
/libs/commons/target/
/libs/maze/target/
/libs/tacviewfx/target/
/libs/dynamicproperties/target/
Expand Down
7 changes: 3 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# TODO LIST

# 1
* new InputComponent interacting with MapComponent
* (map) zooming (<<<<<<<--------------------------------)
* add sortable physics modifier components
* NULLImplementations -> Optionals ...
*
* only render visible part of screen! special renderer for screen. (<-)
* add sortable physics modifier components
*
* dynamic changeable render properties!
* optimize Layers so that they must not be reinstantiated every time on update. Reuse layers and remove/extend entities on update
*
Expand All @@ -17,7 +17,6 @@
* add menu



# 1.1
* load entity to environment
* load new environment
Expand Down
6 changes: 3 additions & 3 deletions libs/rendertools/pom.xml → libs/commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>recom-rendertools</artifactId>
<artifactId>recom-commons</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>RECOM Render Tools</name>
<description>RECOM Render Tools</description>
<name>RECOM Commons</name>
<description>RECOM Commons</description>

<dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.recom.rendertools.calculator;
package com.recom.commons.calculator;

import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;

@NoArgsConstructor
public class ARGBCalculator {

public int blend(final int foregroundColor, final int backgroundColour) {
public static int blend(final int foregroundColor, final int backgroundColour) {
final double alpha = getAlphaComponent(foregroundColor) / 255.0;
final double oneMinusAlpha = 1 - alpha;

Expand All @@ -19,23 +17,23 @@ public int blend(final int foregroundColor, final int backgroundColour) {
return compose(newAlpha, (int) newRed, (int) newGreen, (int) newBlue);
}

public int getAlphaComponent(final int color) {
public static int getAlphaComponent(final int color) {
return (color >> 24) & 0xff;
}

public int getRedComponent(final int color) {
public static int getRedComponent(final int color) {
return (color >> 16) & 0xff;
}

public int getGreenComponent(final int color) {
public static int getGreenComponent(final int color) {
return (color >> 8) & 0xff;
}

public int getBlueComponent(final int color) {
public static int getBlueComponent(final int color) {
return color & 0xff;
}

public int compose(final int alpha, final int red, final int green, final int blue) {
public static int compose(final int alpha, final int red, final int green, final int blue) {
return ((alpha & 0xFF) << 24) |
((red & 0xFF) << 16) |
((green & 0xFF) << 8) |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.rendertools.calculator;
package com.recom.commons.calculator;

import lombok.NoArgsConstructor;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.recom.commons.calculator;

public class TrigonometricCalculator {

public static double calculateRadiantBetweenPoints(
final double x1,
final double y1,
final double x2,
final double y2
) {
final double deltaX = x2 - x1;
final double deltaY = y2 - y1;

return Math.atan2(deltaY, deltaX);
}

public static double calculateDistanceBetweenPoints(
final double x1,
final double y1,
final double x2,
final double y2
) {
final double deltaX = x2 - x1;
final double deltaY = y2 - y1;

return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
}

}
16 changes: 16 additions & 0 deletions libs/commons/src/main/java/com/recom/commons/math/Round.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.recom.commons.math;

public class Round {

public static int halfUp(final double number) {
final int integerComponent = (int) Math.floor(number);
final double decimalComponent = number - integerComponent;

if (decimalComponent >= 0.5) {
return integerComponent + 1;
} else {
return integerComponent;
}
}

}
25 changes: 25 additions & 0 deletions libs/commons/src/main/java/com/recom/commons/math/Sign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.recom.commons.math;

public class Sign {

public static int of(final int number) {
if (number > 0) {
return 1;
} else if (number < 0) {
return -1;
} else {
return 0;
}
}

public static int of(final double number) {
if (number > 0) {
return 1;
} else if (number < 0) {
return -1;
} else {
return 0;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.rendertools.rasterizer;
package com.recom.commons.rasterizer;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.rendertools.rasterizer;
package com.recom.commons.rasterizer;

import lombok.NonNull;

Expand All @@ -9,23 +9,24 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class HeightmapRasterizer {

@NonNull
private final HeightmapScaler heightmapScaler;

public HeightmapRasterizer() {
heightmapScaler = new HeightmapScaler();
}

@NonNull
public ByteArrayOutputStream rasterizeHeightMapPNG(@NonNull final HeightMapDescriptor command) throws IOException {
@NonNull int[] pixelBuffer = rasterizeHeightMapRGB(command);
final int[] pixelBuffer = rasterizeHeightMapRGB(command);

final int width = command.getHeightMap().length;
final int height = command.getHeightMap()[0].length;
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// for (int x = 0; x < width; x++) {
// for (int z = 0; z < height; z++) {
// final int rgb = pixelBuffer[x + z * width]; // get the color from the int buffer array
// image.setRGB(x, z, rgb);
// }
// }

final int[] imagePixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
System.arraycopy(pixelBuffer, 0, imagePixels, 0, pixelBuffer.length);

Expand Down Expand Up @@ -69,15 +70,25 @@ public int[] rasterizeHeightMapRGB(@NonNull final HeightMapDescriptor command) {
return imageBuffer;
}

public int[] rasterizeHeightMapARGB(@NonNull final HeightMapDescriptor command) {
final int[] rgbPixels = rasterizeHeightMapRGB(command);

// add solid alpha channel xff000000 value to each pixel
for (int i = 0; i < rgbPixels.length; i++) {
rgbPixels[i] = 0xff000000 | rgbPixels[i];
}
public int[] rasterizeHeightMapRGB(
@NonNull final HeightMapDescriptor heightMapDescriptor,
final int scale
) {
final int[] originalHeightMap = rasterizeHeightMapRGB(heightMapDescriptor);

return rgbPixels;
return heightmapScaler.scaleMap(heightMapDescriptor, scale, originalHeightMap);
}


// public int[] rasterizeHeightMapARGB(@NonNull final HeightMapDescriptor command) {
// final int[] rgbPixels = rasterizeHeightMapRGB(command);
//
// // add solid alpha channel xff000000 value to each pixel
// for (int i = 0; i < rgbPixels.length; i++) {
// rgbPixels[i] = 0xff000000 | rgbPixels[i];
// }
//
// return rgbPixels;
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.recom.commons.rasterizer;

import com.recom.commons.calculator.ARGBCalculator;
import lombok.NonNull;

public class HeightmapScaler {


public int[] scaleMap(
@NonNull final HeightMapDescriptor heightMapDescriptor,
final int scale,
final int[] originalHeightMap
) {
final int originalHeight = heightMapDescriptor.getHeightMap().length;
final int originalWidth = heightMapDescriptor.getHeightMap()[0].length;

if (Math.abs(scale) == 1 || scale == 0) {
return originalHeightMap;
} else if (scale > 1) {
final int scaledHeight = heightMapDescriptor.getHeightMap().length * scale;
final int scaledWidth = heightMapDescriptor.getHeightMap()[0].length * scale;

final int[] scaledMap = new int[scaledHeight * scaledWidth];
for (int x = 0; x < originalHeight; x++) {
for (int z = 0; z < originalWidth; z++) {
final int color = originalHeightMap[x + z * originalWidth];
for (int scaledPixelX = 0; scaledPixelX < scale; scaledPixelX++) {
for (int scaledPixelZ = 0; scaledPixelZ < scale; scaledPixelZ++) {
scaledMap[(x * scale + scaledPixelX) + (z * scale + scaledPixelZ) * scaledWidth] = color;
}
}
}
}

return scaledMap;
} else {
// Nearest Neighbour Downsampling: https://en.wikipedia.org/wiki/Image_scaling
final int absScale = Math.abs(scale);
final int scaledHeight = heightMapDescriptor.getHeightMap().length / absScale;
final int scaledWidth = heightMapDescriptor.getHeightMap()[0].length / absScale;

final int[] scaledMap = new int[scaledHeight * scaledWidth];
for (int x = 0; x < scaledHeight; x++) {
for (int z = 0; z < scaledWidth; z++) {
int alphaComponentSum = 0;
int redComponentSum = 0;
int greenComponentSum = 0;
int blueComponentSum = 0;
for (int scaledPixelX = 0; scaledPixelX < absScale; scaledPixelX++) {
for (int scaledPixelZ = 0; scaledPixelZ < absScale; scaledPixelZ++) {
alphaComponentSum += ARGBCalculator.getAlphaComponent(originalHeightMap[(x * absScale + scaledPixelX) + (z * absScale + scaledPixelZ) * originalWidth]);
redComponentSum += ARGBCalculator.getRedComponent(originalHeightMap[(x * absScale + scaledPixelX) + (z * absScale + scaledPixelZ) * originalWidth]);
greenComponentSum += ARGBCalculator.getGreenComponent(originalHeightMap[(x * absScale + scaledPixelX) + (z * absScale + scaledPixelZ) * originalWidth]);
blueComponentSum += ARGBCalculator.getBlueComponent(originalHeightMap[(x * absScale + scaledPixelX) + (z * absScale + scaledPixelZ) * originalWidth]);
}
}
final int scalePow = (int) Math.pow(absScale, 2);
alphaComponentSum /= scalePow;
redComponentSum /= scalePow;
greenComponentSum /= scalePow;
blueComponentSum /= scalePow;

scaledMap[x + z * scaledWidth] = ARGBCalculator.compose(alphaComponentSum, redComponentSum, greenComponentSum, blueComponentSum);
}
}

return scaledMap;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.recom.commons.units;

import com.recom.commons.units.calc.ScalingTool;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;

@Data
@Builder
public class PixelCoordinate {

private int x;
private int y;

public static PixelCoordinate of(
final int x,
final int y
) {
return new PixelCoordinate(x, y);
}

public static PixelCoordinate of(
final double x,
final double y
) {
return new PixelCoordinate((int) x, (int) y);
}

@NonNull
public PixelCoordinate scaled(final int scaleFactor) {
return PixelCoordinate.of(ScalingTool.scaleDimension(getX(), scaleFactor), ScalingTool.scaleDimension(getY(), scaleFactor));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.recom.tacview.engine.units;
package com.recom.commons.units;

import lombok.Getter;
import lombok.NonNull;
Expand All @@ -12,10 +12,10 @@ public class PixelDimension {

@NonNull
private static final HashMap<String, PixelDimension> INSTANCE_LIST = new HashMap<>();

private final int widthX;
private final int heightY;


@NonNull
public static PixelDimension of(
final int widthX,
Expand Down
Loading

0 comments on commit dcf6cb6

Please sign in to comment.