Skip to content

Commit 41252d2

Browse files
committed
Closed #8: Added ScalableNoise.
1 parent fdf3142 commit 41252d2

5 files changed

Lines changed: 73 additions & 39 deletions

File tree

FantasyWorldSimulation/src/jfws/editor/map/MapEditorController.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import jfws.util.map.OutsideMapException;
3232
import jfws.util.map.ToCellMapper;
3333
import jfws.util.math.interpolation.BiTwoValueInterpolator;
34+
import jfws.util.math.noise.ScalableNoise;
3435
import jfws.util.math.noise.SimplexNoise;
3536
import jfws.util.math.random.GeneratorWithRandom;
3637
import jfws.util.rendering.CanvasRenderer;
@@ -100,7 +101,9 @@ enum MapType {
100101
private ElevationInterpolator elevationInterpolator = new ElevationInterpolator(BiTwoValueInterpolator.createBiCosineInterpolator());
101102

102103
private ElevationNoiseManager<RegionCell> elevationNoiseManager = new ElevationNoiseManager<>();
103-
private ElevationNoiseWithInterpolation elevationNoise = new ElevationNoiseWithInterpolation("hill", BiTwoValueInterpolator.createBilinearInterpolator(), new SimplexNoise(), 50.0, 0);
104+
private ScalableNoise scalableNoise = new ScalableNoise(new SimplexNoise(), 50.0);
105+
private ElevationNoiseWithInterpolation elevationNoise = new ElevationNoiseWithInterpolation("hill",
106+
BiTwoValueInterpolator.createBilinearInterpolator(), scalableNoise, 0);
104107

105108
private MapType mapToRender = MapType.SKETCH_MAP;
106109
private MapRenderer mapRenderer;
@@ -138,11 +141,11 @@ private void initialize() {
138141
renderStyleComboBox.setItems(FXCollections.observableArrayList(colorSelectorMap.getNames()));
139142
renderStyleComboBox.getSelectionModel().select(colorSelectorMap.getDefaultColorSelector().getName());
140143

141-
hillNoiseSpinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 100, (int) elevationNoise.getResolution()));
144+
hillNoiseSpinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 100, (int) scalableNoise.getResolution()));
142145

143146
hillNoiseSpinner.valueProperty().addListener((observable, oldValue, newValue) -> {
144147
log.info("onHillNoiseSpinnerChanged(): {} -> {}", oldValue, newValue);
145-
elevationNoise.setResolution(newValue);
148+
scalableNoise.setResolution(newValue);
146149
render();
147150
});
148151

@@ -240,11 +243,6 @@ public void onSaveMap() {
240243
}
241244
}
242245

243-
@FXML
244-
public void onReloadTerrainTypes() {
245-
log.info("onReloadTerrainTypes()");
246-
}
247-
248246
@FXML
249247
public void onTerrainTypeSelected() {
250248
String selectedName = terrainTypeComboBox.getSelectionModel().getSelectedItem();

FantasyWorldSimulation/src/jfws/features/elevation/noise/ElevationNoiseWithInterpolation.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ public class ElevationNoiseWithInterpolation<T extends NoiseAmplitudeStorage, U
2020

2121
private Noise noise;
2222

23-
@Getter
24-
@Setter
25-
private double resolution;
26-
2723
private int index;
2824

29-
public ElevationNoiseWithInterpolation(String name, Interpolator2d interpolator, Noise noise, double resolution, int index) {
25+
public ElevationNoiseWithInterpolation(String name, Interpolator2d interpolator, Noise noise, int index) {
3026
super(interpolator);
3127
this.name = name;
3228
this.noise = noise;
33-
this.resolution = resolution;
3429
this.index = index;
3530
}
3631

@@ -42,7 +37,7 @@ public double getSourceValue(NoiseAmplitudeStorage sourceStorage) {
4237
@Override
4338
public void setTargetValue(U targetCell, int targetX, int targetY, double noiseFactor) {
4439
double oldElevation = targetCell.getElevation();
45-
double scaledNoise = noise.calculateNoise(targetX / resolution, targetY/ resolution) * noiseFactor;
40+
double scaledNoise = noise.calculateNoise(targetX, targetY) * noiseFactor;
4641
double newElevation = oldElevation + scaledNoise;
4742
targetCell.setElevation(newElevation);
4843
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jfws.util.math.noise;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
7+
@AllArgsConstructor
8+
public class ScalableNoise implements Noise {
9+
10+
private Noise noise;
11+
12+
@Getter
13+
@Setter
14+
private double resolution;
15+
16+
@Override
17+
public double calculateNoise(double x, double y) {
18+
return noise.calculateNoise(x / resolution, y / resolution);
19+
}
20+
}

FantasyWorldSimulation/test/jfws/features/elevation/noise/ElevationNoiseWithInterpolationTest.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ class ElevationNoiseWithInterpolationTest {
2525

2626
public static final double HILL_NOISE = 3.3;
2727

28-
public static final double RESOLUTION0 = 2.0;
29-
public static final double RESOLUTION1 = 0.5;
30-
3128
public static final double NOISE_FACTOR = 5.0;
3229
public static final double NOISE_VALUE = 4.0;
3330

34-
public static final int TARGET_X = 3;
35-
public static final int TARGET_Y = 24;
31+
public static final double TARGET_X = 3;
32+
public static final double TARGET_Y = 24;
3633
public static final int INDEX = 3;
3734

3835
private SketchCell sourceCell;
@@ -49,7 +46,7 @@ public void setUp() {
4946
interpolator = mock(Interpolator2d.class);
5047
noise = mock(Noise.class);
5148

52-
elevationNoise = new ElevationNoiseWithInterpolation<>(NAME, interpolator, noise, RESOLUTION0, INDEX);
49+
elevationNoise = new ElevationNoiseWithInterpolation<>(NAME, interpolator, noise, INDEX);
5350
}
5451

5552
private void verifyNoCall() {
@@ -68,22 +65,6 @@ public void testGetName() {
6865
verifyNoCall();
6966
}
7067

71-
@Test
72-
public void testGetResolution() {
73-
assertThat(elevationNoise.getResolution(), is(equalTo(RESOLUTION0)));
74-
75-
verifyNoCall();
76-
}
77-
78-
@Test
79-
public void testSetResolution() {
80-
elevationNoise.setResolution(RESOLUTION1);
81-
82-
assertThat(elevationNoise.getResolution(), is(equalTo(RESOLUTION1)));
83-
84-
verifyNoCall();
85-
}
86-
8768
@Test
8869
public void testGetSourceValue() {
8970
when(sourceCell.getNoiseAmplitude(INDEX)).thenReturn(HILL_NOISE);
@@ -103,12 +84,12 @@ public void testSetTargetValue() {
10384
when(regionCell.getElevation()).thenReturn(ELEVATION);
10485
when(noise.calculateNoise(anyDouble(), anyDouble())).thenReturn(NOISE_VALUE);
10586

106-
elevationNoise.setTargetValue(regionCell, TARGET_X, TARGET_Y, NOISE_FACTOR);
87+
elevationNoise.setTargetValue(regionCell, (int)TARGET_X, (int)TARGET_Y, NOISE_FACTOR);
10788

10889
verifyNoCallToInterpolate();
10990
verify(regionCell).getElevation();
11091
verify(regionCell).setElevation(ELEVATION + NOISE_VALUE * NOISE_FACTOR);
111-
verify(noise).calculateNoise(eq(TARGET_X / RESOLUTION0), eq(TARGET_Y / RESOLUTION0));
92+
verify(noise).calculateNoise(eq(TARGET_X), eq(TARGET_Y));
11293
}
11394

11495
// addTo()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package jfws.util.math.noise;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.Matchers.is;
8+
import static org.hamcrest.core.IsEqual.equalTo;
9+
import static org.mockito.Mockito.*;
10+
11+
class ScalableNoiseTest {
12+
13+
public static final double RESOLUTION = 10.5;
14+
public static final double X = 1.0;
15+
public static final double Y = 2.0;
16+
public static final double RESULT = 5.0;
17+
18+
private Noise noise;
19+
private ScalableNoise scalableNoise;
20+
21+
@BeforeEach
22+
public void setUp() {
23+
noise = mock(Noise.class);
24+
scalableNoise = new ScalableNoise(noise, RESOLUTION);
25+
}
26+
27+
@Test
28+
public void testGetResolution() {
29+
assertThat(scalableNoise.getResolution(), is(equalTo(RESOLUTION)));
30+
}
31+
32+
@Test
33+
public void testCalculateNoise() {
34+
when(noise.calculateNoise(X/RESOLUTION, Y/RESOLUTION)).thenReturn(RESULT);
35+
36+
assertThat(scalableNoise.calculateNoise(X, Y), is(equalTo(RESULT)));
37+
38+
verify(noise, times(1)).calculateNoise(X/RESOLUTION, Y/RESOLUTION);
39+
}
40+
}

0 commit comments

Comments
 (0)