Skip to content

Commit

Permalink
Strongly typing builder helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
peabnuts123 committed Jan 1, 2019
1 parent 05513e7 commit 6c64f80
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 98 deletions.
35 changes: 35 additions & 0 deletions src/builders/Builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Module from "@app/module";
import NoiseMap from "@app/noisemap";

export default abstract class Builder {
public sourceModule: Module;
public noiseMap: NoiseMap;

public constructor(sourceModule: Module, width: number, height: number) {
this.sourceModule = sourceModule;
this.noiseMap = new NoiseMap(width, height);
}

// Functions
public abstract build(): NoiseMap;

public setSize(width: number, height: number): void {
this.width = width;
this.height = height;
}

// Properties
public get width(): number {
return this.noiseMap.width;
}
public set width(width: number) {
this.noiseMap.width = width;
}

public get height(): number {
return this.noiseMap.height;
}
public set height(height: number) {
this.noiseMap.height = height;
}
}
27 changes: 6 additions & 21 deletions src/builders/cylinder.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import Cylinder from '@app/model/cylinder';
import Module from '@app/module';
import NoiseMap from '@app/noisemap';
import Builder from './Builder';

class NoiseMapBuilderCylinder {
private sourceModule: any;
private width: number;
private height: number;
private noiseMap: NoiseMap;
class NoiseMapBuilderCylinder extends Builder {
private _lowerAngleBound: number;
private _lowerHeightBound: number;
private _upperAngleBound: number;
private _upperHeightBound: number;

constructor(sourceModule?: any, width?: number, height?: number) {
this.sourceModule = sourceModule || null;
this.width = width || 256;
this.height = height || 256;
constructor(sourceModule: Module, width: number = 256, height: number = 256) {
super(sourceModule, width, height);

this.lowerAngleBound = 0.0;
this.lowerHeightBound = 0.0;
this.upperAngleBound = 1.0;
this.upperHeightBound = 1.0;

this.noiseMap = new NoiseMap(this.width, this.height);
}

public get lowerAngleBound() {
Expand Down Expand Up @@ -68,11 +62,7 @@ class NoiseMapBuilderCylinder {
this._upperHeightBound = v;
}

public build() {
if (!this.sourceModule) {
throw new Error('Invalid or missing module!');
}

public build(): NoiseMap {
// Create the cylinder model.
let cylinder = new Cylinder(this.sourceModule);
let xDelta = (this.upperAngleBound - this.lowerAngleBound) / this.width;
Expand All @@ -82,19 +72,14 @@ class NoiseMapBuilderCylinder {

// 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;

for (let x = 0; x < this.width; x++) {

this.noiseMap.setValue(x, y, cylinder.getValue(curAngle, curHeight));

curAngle += xDelta;

}

curHeight += yDelta;

}

return this.noiseMap;
Expand Down
2 changes: 2 additions & 0 deletions src/builders/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Builder from './Builder';
import Cylinder from './cylinder';
import Plane from './plane';
import Sphere from './sphere';

export {
Builder as default,
Cylinder,
Plane,
Sphere,
Expand Down
30 changes: 9 additions & 21 deletions src/builders/plane.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import Interpolation from '@app/interpolation';
import Plane from '@app/model/plane';
import Module from '@app/module';
import NoiseMap from '@app/noisemap';
import Builder from './Builder';

class NoiseMapBuilderPlane {
private sourceModule: any;
private width: number;
private height: number;
class NoiseMapBuilderPlane extends Builder {
private seamless: boolean;
private noiseMap: NoiseMap;
private _lowerXBound: number;
private _lowerYBound: number;
private _upperXBound: number;
private _upperYBound: number;

constructor(sourceModule?: any, width?: number, height?: number, seamless?: boolean) {
this.sourceModule = sourceModule || null;
this.width = width || 256;
this.height = height || 256;
this.seamless = seamless || false;
constructor(sourceModule: Module, width: number = 256, height: number = 256, seamless: boolean = false) {
super(sourceModule, width, height);

this.seamless = seamless;

this.lowerXBound = 0.0;
this.lowerYBound = 0.0;
this.upperXBound = 1.0;
this.upperYBound = 1.0;

this.noiseMap = new NoiseMap(this.width, this.height);
}

public get lowerXBound() {
Expand Down Expand Up @@ -71,20 +66,13 @@ class NoiseMapBuilderPlane {
this._upperYBound = v;
}

public build() {
public build(): NoiseMap {
let xExtent = this.upperXBound - this.lowerXBound;
let yExtent = this.upperYBound - this.lowerYBound;

// @TODO is this possible? seems to be covered by validation in setters
if (xExtent < 0 || yExtent < 0) {

throw new Error('Invalid bounds!');

}

if (!this.sourceModule) {

throw new Error('Invalid or missing module!');

}

// Create the plane model.
Expand Down
22 changes: 6 additions & 16 deletions src/builders/sphere.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import Sphere from '@app/model/sphere';
import Module from '@app/module';
import NoiseMap from '@app/noisemap';
import Builder from './Builder';

class NoiseMapBuilderSphere {
private sourceModule: any;
private width: number;
private height: number;
private noiseMap: NoiseMap;
class NoiseMapBuilderSphere extends Builder {
private _eastLonBound: number;
private _northLatBound: number;
private _southLatBound: number;
private _westLonBound: number;

constructor(sourceModule?: any, width?: number, height?: number) {
this.sourceModule = sourceModule || null;
this.width = width || 256;
this.height = height || 256;
constructor(sourceModule: Module, width: number = 256, height: number = 256) {
super(sourceModule, width, height);

this._northLatBound = 0.0;
this._southLatBound = 0.0;
this._eastLonBound = 0.0;
this._westLonBound = 0.0;

this.noiseMap = new NoiseMap(this.width, this.height);
}

public get eastLonBound() {
Expand Down Expand Up @@ -68,11 +62,7 @@ class NoiseMapBuilderSphere {
this._westLonBound = v;
}

public build() {
if (!this.sourceModule) {
throw new Error('Invalid or missing module!');
}

public build(): NoiseMap {
// Create the cylinder model.
let sphere = new Sphere(this.sourceModule);
let xDelta = (this.eastLonBound - this.westLonBound) / this.width;
Expand Down
4 changes: 4 additions & 0 deletions src/module/Module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default abstract class Module {
// Functions
public abstract getValue(x: number, y: number, z: number): number;
}
5 changes: 5 additions & 0 deletions src/module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Module from './Module';

export {
Module as default,
};
16 changes: 2 additions & 14 deletions test/builders/cylinder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';

import { Cylinder } from '@app/builders';
import Module from '@app/module';
import { Const } from '@app/module/generator';
import NoiseMap from '@app/noisemap';

Expand Down Expand Up @@ -184,24 +185,11 @@ describe("builders/cylinder", () => {
// Assert
expect(noiseMap).not.to.be.null;
});

it("calling build with missing source module throws an error", () => {
// Setup
const mockModule: Cylinder = new Cylinder();

// Test
const testFunc = () => {
mockModule.build();
};

// Assert
expect(testFunc).to.throw();
});
});

function createMockCylinder(): Cylinder {
const value: number = 2;
const sourceModule: any = new Const(value);
const sourceModule: Module = new Const(value);
const width: number = 10;
const height: number = 10;

Expand Down
13 changes: 0 additions & 13 deletions test/builders/plane.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,6 @@ describe("builders/plane", () => {
// Assert
expect(noiseMap).not.to.be.null;
});

it("calling build with missing source module throws an error", () => {
// Setup
const mockModule = new Plane();

// Test
const testFunc = () => {
mockModule.build();
};

// Assert
expect(testFunc).to.throw();
});
});

function createMockPlane() {
Expand Down
13 changes: 0 additions & 13 deletions test/builders/sphere.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,6 @@ describe("builders/sphere", () => {
// Assert
expect(noiseMap).not.to.be.null;
});

it("calling build with missing source module throws an error", () => {
// Setup
const mockModule = new Sphere();

// Test
const testFunc = () => {
mockModule.build();
};

// Assert
expect(testFunc).to.throw();
});
});

function createMockSphere() {
Expand Down

0 comments on commit 6c64f80

Please sign in to comment.