Skip to content

Commit

Permalink
General code tidying and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
peabnuts123 committed Jan 12, 2019
1 parent d6419ed commit ffbeedc
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 178 deletions.
6 changes: 3 additions & 3 deletions src/builders/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default abstract class Builder {
* The destination noise map.
*
* The destination noise map will contain the coherent-noise values
* from this noise map after a successful call to the Build() method.
* from this noise map after a successful call to the build() method.
*
* The destination noise map must exist throughout the lifetime of
* this object unless another noise map replaces that noise map.
Expand All @@ -42,7 +42,7 @@ export default abstract class Builder {
* @param width The width of the destination noise map, in points
* @param height The height of the destination noise map, in points
*/
public constructor(sourceModule: Module, width: number, height: number) {
public constructor(sourceModule: Module, width?: number, height?: number) {
this.sourceModule = sourceModule;
this.noiseMap = new NoiseMap(width, height);
}
Expand All @@ -61,7 +61,7 @@ export default abstract class Builder {
* @param height The height of the destination noise map, in points.
*
* This method does not change the size of the destination noise map
* until the Build() method is called.
* until the build() method is called.
*/
public setSize(width: number, height: number): void {
this.width = width;
Expand Down
36 changes: 18 additions & 18 deletions src/builders/cylinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class NoiseMapBuilderCylinder extends Builder {
public get lowerAngleBound(): number {
return this._lowerAngleBound;
}
public set lowerAngleBound(v: number) {
if (v >= this.upperAngleBound) {
public set lowerAngleBound(value: number) {
if (value >= this.upperAngleBound) {
throw new Error('Lower angle bound cannot be equal to or exceed upper angle bound!');
}

this._lowerAngleBound = v;
this._lowerAngleBound = value;
}

/**
Expand All @@ -56,12 +56,12 @@ class NoiseMapBuilderCylinder extends Builder {
public get lowerHeightBound(): number {
return this._lowerHeightBound;
}
public set lowerHeightBound(v: number) {
if (v >= this.upperHeightBound) {
public set lowerHeightBound(value: number) {
if (value >= this.upperHeightBound) {
throw new Error('Lower angle height cannot be equal to or exceed upper angle height!');
}

this._lowerHeightBound = v;
this._lowerHeightBound = value;
}

/**
Expand All @@ -72,12 +72,12 @@ class NoiseMapBuilderCylinder extends Builder {
public get upperAngleBound(): number {
return this._upperAngleBound;
}
public set upperAngleBound(v: number) {
if (v <= this.lowerAngleBound) {
public set upperAngleBound(value: number) {
if (value <= this.lowerAngleBound) {
throw new Error('Upper angle bound cannot be equal to or less than lower angle bound!');
}

this._upperAngleBound = v;
this._upperAngleBound = value;
}

/**
Expand All @@ -89,32 +89,32 @@ class NoiseMapBuilderCylinder extends Builder {
public get upperHeightBound(): number {
return this._upperHeightBound;
}
public set upperHeightBound(v: number) {
if (v <= this.lowerHeightBound) {
public set upperHeightBound(value: number) {
if (value <= this.lowerHeightBound) {
throw new Error('Upper angle height cannot be equal to or less than lower angle height!');
}

this._upperHeightBound = v;
this._upperHeightBound = value;
}

public build(): NoiseMap {
// Create the cylinder model.
let cylinder = new Cylinder(this.sourceModule);
let xDelta = (this.upperAngleBound - this.lowerAngleBound) / this.width;
let yDelta = (this.upperHeightBound - this.lowerHeightBound) / this.height;
let curAngle = this.lowerAngleBound;
let curHeight = this.lowerHeightBound;
let currentAngle = this.lowerAngleBound;
let currentHeight = this.lowerHeightBound;

// Fill every point in the noise map with the output values from the model.
for (let y = 0; y < this.height; y++) {
curAngle = this.lowerAngleBound;
currentAngle = this.lowerAngleBound;

for (let x = 0; x < this.width; x++) {
this.noiseMap.setValue(x, y, cylinder.getValue(curAngle, curHeight));
curAngle += xDelta;
this.noiseMap.setValue(x, y, cylinder.getValue(currentAngle, currentHeight));
currentAngle += xDelta;
}

curHeight += yDelta;
currentHeight += yDelta;
}

return this.noiseMap;
Expand Down
67 changes: 31 additions & 36 deletions src/builders/plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NoiseMapBuilderPlane extends Builder {
* Enabling seamless tiling builds a noise map with no seams at the
* edges. This allows the noise map to be tileable.
*/
public seamless: boolean;
public isSeamless: boolean;
private _lowerXBound: number = 0;
private _lowerZBound: number = 0;
private _upperXBound: number = 1;
Expand All @@ -36,12 +36,12 @@ class NoiseMapBuilderPlane extends Builder {
* @param sourceModule The source module
* @param width The width of the destination noise map, in points
* @param height The height of the destination noise map, in points
* @param seamless A flag that enables or disables seamless tiling
* @param isSeamless A flag that enables or disables seamless tiling
*/
constructor(sourceModule: Module, width: number = 256, height: number = 256, seamless: boolean = false) {
constructor(sourceModule: Module, width?: number, height?: number, isSeamless: boolean = false) {
super(sourceModule, width, height);

this.seamless = seamless;
this.isSeamless = isSeamless;
}

/**
Expand All @@ -52,12 +52,12 @@ class NoiseMapBuilderPlane extends Builder {
public get lowerXBound(): number {
return this._lowerXBound;
}
public set lowerXBound(v: number) {
if (v >= this.upperXBound) {
public set lowerXBound(value: number) {
if (value >= this.upperXBound) {
throw new Error('Lower X bound cannot be equal to or exceed upper X bound!');
}

this._lowerXBound = v;
this._lowerXBound = value;
}

/**
Expand All @@ -68,12 +68,12 @@ class NoiseMapBuilderPlane extends Builder {
public get lowerZBound(): number {
return this._lowerZBound;
}
public set lowerZBound(v: number) {
if (v >= this.upperZBound) {
public set lowerZBound(value: number) {
if (value >= this.upperZBound) {
throw new Error('Lower Z bound cannot be equal to or exceed upper Z bound!');
}

this._lowerZBound = v;
this._lowerZBound = value;
}

/**
Expand All @@ -84,12 +84,12 @@ class NoiseMapBuilderPlane extends Builder {
public get upperXBound(): number {
return this._upperXBound;
}
public set upperXBound(v: number) {
if (v <= this.lowerXBound) {
public set upperXBound(value: number) {
if (value <= this.lowerXBound) {
throw new Error('Upper X bound cannot be equal to or less than lower X bound!');
}

this._upperXBound = v;
this._upperXBound = value;
}

/**
Expand All @@ -100,63 +100,58 @@ class NoiseMapBuilderPlane extends Builder {
public get upperZBound(): number {
return this._upperZBound;
}
public set upperZBound(v: number) {
if (v <= this.lowerZBound) {
public set upperZBound(value: number) {
if (value <= this.lowerZBound) {
throw new Error('Upper Z bound cannot be equal to or less than lower Z bound!');
}

this._upperZBound = v;
this._upperZBound = value;
}

public build(): NoiseMap {
let xExtent = this.upperXBound - this.lowerXBound;
let zExtent = this.upperZBound - this.lowerZBound;

// @TODO is this possible? seems to be covered by validation in setters
if (xExtent < 0 || zExtent < 0) {
throw new Error('Invalid bounds!');
}

// Create the plane model.
let plane = new Plane(this.sourceModule);
let xDelta = xExtent / this.width;
let zDelta = zExtent / this.height;
let curX = this.lowerXBound;
let curZ = this.lowerZBound;
let value;
let xBlend;

let currentX = this.lowerXBound;
let currentZ = this.lowerZBound;

// Fill every point in the noise map with the output values from the model.
for (let z = 0; z < this.height; z++) {
curX = this.lowerXBound;
currentX = this.lowerXBound;

for (let x = 0; x < this.width; x++) {
if (!this.seamless) {
value = plane.getValue(curX, curZ);
let value;
if (!this.isSeamless) {
value = plane.getValue(currentX, currentZ);
} else {
xBlend = 1.0 - ((curX - this.lowerXBound) / xExtent);
let xBlend = 1.0 - ((currentX - this.lowerXBound) / xExtent);

value = Interpolation.linear(
Interpolation.linear(
plane.getValue(curX, curZ),
plane.getValue(curX + xExtent, curZ),
plane.getValue(currentX, currentZ),
plane.getValue(currentX + xExtent, currentZ),
xBlend,
),
Interpolation.linear(
plane.getValue(curX, curZ + zExtent),
plane.getValue(curX + xExtent, curZ + zExtent),
plane.getValue(currentX, currentZ + zExtent),
plane.getValue(currentX + xExtent, currentZ + zExtent),
xBlend,
),
1.0 - ((curZ - this.lowerZBound) / zExtent),
1.0 - ((currentZ - this.lowerZBound) / zExtent),
);
}

this.noiseMap.setValue(x, z, value);

curX += xDelta;
currentX += xDelta;
}

curZ += zDelta;
currentZ += zDelta;
}

return this.noiseMap;
Expand Down
12 changes: 6 additions & 6 deletions src/builders/sphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ class NoiseMapBuilderSphere extends Builder {
let sphere = new Sphere(this.sourceModule);
let xDelta = (this.eastLonBound - this.westLonBound) / this.width;
let yDelta = (this.northLatBound - this.southLatBound) / this.height;
let curLon = this.westLonBound;
let curLat = this.eastLonBound;
let currentLongitude = this.westLonBound;
let currentLatitude = this.eastLonBound;

// Fill every point in the noise map with the output values from the model.
for (let y = 0; y < this.height; y++) {
curLon = this.westLonBound;
currentLongitude = this.westLonBound;

for (let x = 0; x < this.width; x++) {
this.noiseMap.setValue(x, y, sphere.getValue(curLat, curLon));
this.noiseMap.setValue(x, y, sphere.getValue(currentLatitude, currentLongitude));

curLon += xDelta;
currentLongitude += xDelta;
}

curLat += yDelta;
currentLatitude += yDelta;
}

return this.noiseMap;
Expand Down
30 changes: 14 additions & 16 deletions src/interpolation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const Interpolation = {
export default class Interpolation {
/**
* Performs cubic interpolation between two values bound between two other
* values.
*
* The alpha value should range from 0.0 to 1.0. If the alpha value is
* 0.0, this function returns @a n1. If the alpha value is 1.0, this
* function returns @a n2.
* 0.0, this function returns n1. If the alpha value is 1.0, this
* function returns n2.
*
* @param n0 The value before the first value.
* @param n1 The first value.
Expand All @@ -16,30 +16,30 @@ const Interpolation = {
* @returns The interpolated value.
*
*/
cubic(n0: number, n1: number, n2: number, n3: number, a: number): number {
public static cubic(n0: number, n1: number, n2: number, n3: number, a: number): number {
let p = ((n3 - n2) - (n0 - n1));
let q = ((n0 - n1) - p);
let r = (n2 - n0);
let s = (n1);
return (p * a * a * a + q * a * a + r * a + s);
},
}

/**
* Performs linear interpolation between two values.
*
* The alpha value should range from 0.0 to 1.0. If the alpha value is
* 0.0, this function returns @a n0. If the alpha value is 1.0, this
* function returns @a n1.
* 0.0, this function returns n0. If the alpha value is 1.0, this
* function returns n1.
*
* @param n0 The first value.
* @param n1 The second value.
* @param a The alpha value.
*
* @returns The interpolated value.
*/
linear(n0: number, n1: number, a: number): number {
public static linear(n0: number, n1: number, a: number): number {
return ((1.0 - a) * (n0)) + (a * (n1));
},
}

/**
* Maps a value onto a cubic S-curve.
Expand All @@ -49,9 +49,9 @@ const Interpolation = {
*
* @returns The mapped value.
*/
cubicSCurve(a: number): number {
public static cubicSCurve(a: number): number {
return (a * a * (3.0 - 2.0 * a));
},
}

/**
* Maps a value onto a quintic S-curve.
Expand All @@ -63,12 +63,10 @@ const Interpolation = {
*
* @returns The mapped value.
*/
quinticSCurve(a: number): number {
public static quinticSCurve(a: number): number {
let a3 = (a * a * a);
let a4 = (a3 * a);
let a5 = (a4 * a);
return ((6.0 * a5) - (15.0 * a4) + (10.0 * a3));
},
};

export default Interpolation;
}
}

0 comments on commit ffbeedc

Please sign in to comment.