Updated linting, changes to Vector3

This commit is contained in:
Lucas Dower 2022-04-17 22:13:59 +01:00
parent 390f55d548
commit 3533f13c85
9 changed files with 60 additions and 81 deletions

View File

@ -19,6 +19,7 @@
"object-curly-spacing": "off",
"max-len": "off",
"require-jsdoc": "off",
"valid-jsdoc": "off",
"indent": ["error", 4, { "SwitchCase": 1 }],
"no-multi-spaces": "off",
"no-array-constructor": "off",

View File

@ -19,7 +19,7 @@ export abstract class IExporter {
export(blockMesh: BlockMesh, filePath: string): boolean {
const bounds = blockMesh.getVoxelMesh()?.getBounds();
this._sizeVector = Vector3.sub(bounds.max, bounds.min).addScalar(1);
this._sizeVector = Vector3.sub(bounds.max, bounds.min).add(1);
const nbt = this.convertToNBT(blockMesh);

View File

@ -103,8 +103,8 @@ export class DebugGeometryTemplates {
}
public static cube(centre: Vector3, size: number, colour: RGB): AttributeData {
const min = Vector3.subScalar(centre, size/2);
const max = Vector3.addScalar(centre, size/2);
const min = Vector3.sub(centre, size/2);
const max = Vector3.add(centre, size/2);
const bounds = new Bounds(min, max);
return this.bounds(bounds, colour);
}

View File

@ -1,9 +1,9 @@
export abstract class Hashable {
abstract hash(): number;
abstract equals(other: Hashable): boolean;
export interface IHashable {
hash(): number;
equals(other: IHashable): boolean;
}
export class HashSet<T extends Hashable> {
export class HashSet<T extends IHashable> {
private _numBins: number;
protected _bins: Array<Array<T>>;
@ -40,7 +40,7 @@ export class HashSet<T extends Hashable> {
}
export class HashMap<K extends Hashable, V> {
export class HashMap<K extends IHashable, V> {
private _numBins: number;
protected _bins: Array<Array<{key: K, value: V}>>;

View File

@ -1,13 +1,12 @@
import { Hashable } from './hash_map';
import { IHashable } from './hash_map';
import { ASSERT } from './util';
export class Vector3 extends Hashable {
export class Vector3 implements IHashable {
public x: number;
public y: number;
public z: number;
constructor(x: number, y: number, z: number) {
super();
this.x = x;
this.y = y;
this.z = z;
@ -34,14 +33,6 @@ export class Vector3 extends Hashable {
return [this.x, this.y, this.z];
}
static add(vecA: Vector3, vecB: Vector3) {
return new Vector3(
vecA.x + vecB.x,
vecA.y + vecB.y,
vecA.z + vecB.z,
);
}
static random() {
return new Vector3(
Math.random(),
@ -61,56 +52,7 @@ export class Vector3 extends Hashable {
);
}
add(vec: Vector3) {
this.x += vec.x;
this.y += vec.y;
this.z += vec.z;
return this;
}
static addScalar(vec: Vector3, scalar: number) {
return new Vector3(
vec.x + scalar,
vec.y + scalar,
vec.z + scalar,
);
}
addScalar(scalar: number) {
this.x += scalar;
this.y += scalar;
this.z += scalar;
return this;
}
static sub(vecA: Vector3, vecB: Vector3) {
return new Vector3(
vecA.x - vecB.x,
vecA.y - vecB.y,
vecA.z - vecB.z,
);
}
sub(vec: Vector3) {
this.x -= vec.x;
this.y -= vec.y;
this.z -= vec.z;
return this;
}
static subScalar(vec: Vector3, scalar: number) {
return new Vector3(
vec.x - scalar,
vec.y - scalar,
vec.z - scalar,
);
}
static dot(vecA: Vector3, vecB: Vector3) {
return vecA.x * vecB.x + vecA.y * vecB.y + vecA.z * vecB.z;
}
static copy(vec: Vector3) {
public static copy(vec: Vector3) {
return new Vector3(
vec.x,
vec.y,
@ -118,12 +60,48 @@ export class Vector3 extends Hashable {
);
}
copy() {
return new Vector3(
this.x,
this.y,
this.z,
);
public static add(vec: Vector3, toAdd: (Vector3 | number)) {
return Vector3.copy(vec).add(toAdd);
}
public static sub(vec: Vector3, toAdd: (Vector3 | number)) {
return Vector3.copy(vec).sub(toAdd);
}
public add(toAdd: (Vector3 | number)) {
if (toAdd instanceof Vector3) {
this.x += toAdd.x;
this.y += toAdd.y;
this.z += toAdd.z;
return this;
} else {
this.x += toAdd;
this.y += toAdd;
this.z += toAdd;
return this;
}
}
public sub(toAdd: (Vector3 | number)) {
if (toAdd instanceof Vector3) {
this.x -= toAdd.x;
this.y -= toAdd.y;
this.z -= toAdd.z;
return this;
} else {
this.x -= toAdd;
this.y -= toAdd;
this.z -= toAdd;
return this;
}
}
static dot(vecA: Vector3, vecB: Vector3) {
return vecA.x * vecB.x + vecA.y * vecB.y + vecA.z * vecB.z;
}
public copy() {
return Vector3.copy(this);
}
static mulScalar(vec: Vector3, scalar: number) {
@ -258,14 +236,14 @@ export class Vector3 extends Hashable {
}
// Begin IHashable interface
override hash() {
public hash() {
const p0 = 73856093;
const p1 = 19349663;
const p2 = 83492791;
return (this.x * p0) ^ (this.y * p1) ^ (this.z * p2);
}
override equals(other: Vector3) {
public equals(other: Vector3) {
return this.x == other.x && this.y == other.y && this.z == other.z;
}
// End IHashable interface

View File

@ -12,7 +12,7 @@ export abstract class IVoxeliser {
StatusHandler.Get.add('info', `Voxel mesh has ${voxelMesh.getVoxelCount().toLocaleString()} voxels`);
const dim = voxelMesh.getBounds().getDimensions().addScalar(1);
const dim = voxelMesh.getBounds().getDimensions().add(1);
StatusHandler.Get.add('info', `Dimensions are ${dim.x.toLocaleString()}x${dim.y.toLocaleString()}x${dim.z.toLocaleString()} voxels`);
return voxelMesh;

View File

@ -38,7 +38,7 @@ export class BVHRayVoxeliser extends IVoxeliser {
bounds.min.floor();
bounds.max.ceil();
const planeDims = Vector3.sub(bounds.max, bounds.min).addScalar(1);
const planeDims = Vector3.sub(bounds.max, bounds.min).add(1);
const numRays = (planeDims.x * planeDims.y) + (planeDims.x * planeDims.z) + (planeDims.y * planeDims.z);
const rays = new Array<Ray>(numRays);
let rayIndex = 0;

View File

@ -92,7 +92,7 @@ export class NormalCorrectedRayVoxeliser extends IVoxeliser {
if (this._voxelMeshParams!.useMultisampleColouring) {
const samples: RGB[] = [];
for (let i = 0; i < AppConfig.MULTISAMPLE_COUNT; ++i) {
const samplePosition = Vector3.add(voxelPosition, Vector3.random().addScalar(-0.5));
const samplePosition = Vector3.add(voxelPosition, Vector3.random().add(-0.5));
samples.push(this.__getVoxelColour(triangle, materialName, samplePosition));
}
voxelColour = RGB.averageFrom(samples);

View File

@ -75,7 +75,7 @@ export class RayVoxeliser extends IVoxeliser {
if (this._voxelMeshParams!.useMultisampleColouring) {
const samples: RGB[] = [];
for (let i = 0; i < AppConfig.MULTISAMPLE_COUNT; ++i) {
const samplePosition = Vector3.add(voxelPosition, Vector3.random().addScalar(-0.5));
const samplePosition = Vector3.add(voxelPosition, Vector3.random().add(-0.5));
samples.push(this.__getVoxelColour(triangle, materialName, samplePosition));
}
voxelColour = RGB.averageFrom(samples);