Skip to content

Commit

Permalink
Refactor APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Dec 14, 2023
1 parent 6331101 commit 9220af2
Show file tree
Hide file tree
Showing 61 changed files with 597 additions and 635 deletions.
2 changes: 1 addition & 1 deletion packages/redgeometry-app/src/animationframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type AnimationFrameCallbackContext<T> = {
export class AnimationFrameContext<T> {
private callbackContext: AnimationFrameCallbackContext<T>;

constructor(provider: AnimationFrameProvider, target: T, callback: AnimationFrameCallback<T>) {
public constructor(provider: AnimationFrameProvider, target: T, callback: AnimationFrameCallback<T>) {
this.callbackContext = { provider, target, callback, requestHandle: 0 };
}

Expand Down
6 changes: 3 additions & 3 deletions packages/redgeometry-app/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type CanvasStyle = string | CanvasGradient | CanvasPattern;
export class AppContext2D {
private context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;

constructor(context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) {
public constructor(context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) {
this.context = context;
}

Expand Down Expand Up @@ -230,7 +230,7 @@ export class AppContext2D {

this.addMeshFaceToContext(ctx, face);

ctx.fillStyle = createRandomColor(random, 0.25, 1, 1).style;
ctx.fillStyle = createRandomColor(random, 0.25, 1, 1).style();
ctx.fill();
}

Expand Down Expand Up @@ -352,7 +352,7 @@ export class AppContext2D {
let cIdx = 0;
let pIdx = 0;

let p0 = Point2.zero;
let p0 = Point2.ZERO;

while (cIdx < commands.length) {
const command = commands[cIdx++];
Expand Down
4 changes: 2 additions & 2 deletions packages/redgeometry-app/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ export function createSimplePolygon(
const center = box.getCenter();
const angleStep = (2 * Math.PI) / count;

const w = 0.5 * box.dx;
const h = 0.5 * box.dy;
const w = 0.5 * box.dx();
const h = 0.5 * box.dy();

for (let i = 0; i < count; i++) {
const a = i * angleStep + angleStep * random.nextFloatBetween(0, irregularity);
Expand Down
12 changes: 6 additions & 6 deletions packages/redgeometry-app/src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class AppInputElement {
private isBound: boolean;
private style?: string;

constructor(element: HTMLElement, id: string) {
public constructor(element: HTMLElement, id: string) {
element.id = id;

this.element = element;
Expand Down Expand Up @@ -84,7 +84,7 @@ export class AppValueInputElement extends AppInputElement {
private defaultValue: string;
private valueInputElement: HTMLInputElement | HTMLSelectElement;

constructor(element: HTMLInputElement | HTMLSelectElement, id: string, defaultValue: string) {
public constructor(element: HTMLInputElement | HTMLSelectElement, id: string, defaultValue: string) {
super(element, id);

element.value = defaultValue;
Expand Down Expand Up @@ -119,7 +119,7 @@ export class AppValueInputElement extends AppInputElement {
}

export class ButtonInputElement extends AppInputElement {
constructor(id: string, label: string) {
public constructor(id: string, label: string) {
const button = document.createElement("button");
button.textContent = label;

Expand All @@ -128,7 +128,7 @@ export class ButtonInputElement extends AppInputElement {
}

export class TextBoxInputElement extends AppValueInputElement {
constructor(id: string, defaultValue = "") {
public constructor(id: string, defaultValue = "") {
const input = document.createElement("input");
input.type = "text";

Expand All @@ -137,7 +137,7 @@ export class TextBoxInputElement extends AppValueInputElement {
}

export class RangeInputElement extends AppValueInputElement {
constructor(id: string, min: string, max: string, defaultValue = "0") {
public constructor(id: string, min: string, max: string, defaultValue = "0") {
const input = document.createElement("input");
input.type = "range";
input.className = "slider";
Expand All @@ -151,7 +151,7 @@ export class RangeInputElement extends AppValueInputElement {
export class ComboBoxInputElement extends AppValueInputElement {
private selectElement: HTMLSelectElement;

constructor(id: string, defaultValue = "") {
public constructor(id: string, defaultValue = "") {
const selectElement = document.createElement("select");

super(selectElement, id, defaultValue);
Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry-app/src/parts/bplustree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class BPlusTree<T> {
private leafSize: number;
private root: Node<T>;

constructor(compareFn: (a: T, b: T) => number, branchSize?: number, leafSize?: number) {
public constructor(compareFn: (a: T, b: T) => number, branchSize?: number, leafSize?: number) {
this.root = this.leafNodeCreate(undefined, 0, [], undefined);
this.compareFn = compareFn;

Expand Down
4 changes: 2 additions & 2 deletions packages/redgeometry-app/src/parts/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ export class MatrixAppPart implements AppPart {
const s = 0.2 * Math.min(canvasWidth, canvasHeight);
const d = (rotation * Math.PI) / 180;

const model = Matrix4x4.identity;
const model = Matrix4x4.createIdentity();
model.rotateXAnglePre(1.1 * d);
model.rotateYAnglePre(1.3 * d);
model.rotateZAnglePre(1.7 * d);

const view = Matrix4x4.identity;
const view = Matrix4x4.createIdentity();
view.translatePre(0, 0, -10);

let projection;
Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry-app/src/parts/path-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class PathAreaAppPart implements AppPart {
this.inputCount.setStyle("width: 80px");

this.input = new Path2();
this.bounds = Box2.empty;
this.bounds = Box2.createEmpty();
this.isInside = false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/redgeometry-app/src/parts/path-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class PathOverlayAppPart implements AppPart {
const step = 1 / this.tagEntries.length;
for (let h = 0; h < 1; h += step) {
const c = ColorRgba.fromHsv(h, 0.25, 1, 1);
styles.push(c.style);
styles.push(c.style());
}

for (const face of this.mesh.getFaces()) {
Expand Down
32 changes: 16 additions & 16 deletions packages/redgeometry-app/src/parts/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ export class SkeletonAppPart implements AppPart {

MeshEdge2.detachPair(e1);

e1.p0 = Point2.zero;
e1.sym.p0 = Point2.zero;
e1.p0 = Point2.ZERO;
e1.sym.p0 = Point2.ZERO;

// Not a vertex anymore
e0.data = undefined;
Expand Down Expand Up @@ -243,7 +243,7 @@ export class SkeletonAppPart implements AppPart {
const v1 = vb.sub(va);
const v2 = pb.sub(pa);

return v1.dot(v2.neg) / v1.dot(v1);
return v1.dot(v2.neg()) / v1.dot(v1);
}

/**
Expand All @@ -257,7 +257,7 @@ export class SkeletonAppPart implements AppPart {

const v1 = p1.sub(p0);
const v2 = p2.sub(p1);
const n2 = v2.unit.normal;
const n2 = v2.unit().normal();

return v1.dot(n2) / (1 + v0.dot(n2));
}
Expand Down Expand Up @@ -437,9 +437,9 @@ export class SkeletonAppPart implements AppPart {
const p1 = vtx1.getPositionAt(t1);
const p2 = vtx2.getPositionAt(t1);

const d0 = p1.sub(p0).lengthSq;
const d1 = p2.sub(p1).lengthSq;
const d2 = p0.sub(p2).lengthSq;
const d0 = p1.sub(p0).lenSq();
const d1 = p2.sub(p1).lenSq();
const d2 = p0.sub(p2).lenSq();

let d = d0;
let e = e0;
Expand Down Expand Up @@ -481,9 +481,9 @@ export class SkeletonAppPart implements AppPart {
const p1 = vtx1.getPositionAt(tv);
const p2 = vtx2.getPositionAt(tv);

const d0 = p1.sub(p0).lengthSq;
const d1 = p2.sub(p1).lengthSq;
const d2 = p0.sub(p2).lengthSq;
const d0 = p1.sub(p0).lenSq();
const d1 = p2.sub(p1).lenSq();
const d2 = p0.sub(p2).lenSq();

let d = d0;
let e = e0;
Expand Down Expand Up @@ -582,7 +582,7 @@ export class SkeletonAppPart implements AppPart {
for (const e of edge.getOnextIterator()) {
log.infoDebug(" [{}] p0: {}", i, e.p0);
log.infoDebug(" p1: {}", e.p1);
log.infoDebug(" angle: {}", (e.p1.sub(e.p0).angle * 180) / Math.PI);
log.infoDebug(" angle: {}", (e.p1.sub(e.p0).angle() * 180) / Math.PI);

if (validate) {
assertDebug(e.data === edge.data);
Expand Down Expand Up @@ -744,7 +744,7 @@ class KineticVertex {
public t1: number;
public vel: Vector2;

constructor(orig: Point2, n1: Vector2, n2: Vector2, t0: number) {
public constructor(orig: Point2, n1: Vector2, n2: Vector2, t0: number) {
this.orig = orig;
this.vel = KineticVertex.getVelocity(n1, n2);
this.n1 = n1;
Expand All @@ -759,15 +759,15 @@ class KineticVertex {
const e1 = KineticVertex.getWavefrontEdgeCw(e);
const e2 = KineticVertex.getWavefrontEdgeCcw(e);

const n1 = e1.p1.sub(e1.p0).unit.normal.neg;
const n2 = e2.p0.sub(e2.p1).unit.normal.neg;
const n1 = e1.p1.sub(e1.p0).unit().normal().neg();
const n2 = e2.p0.sub(e2.p1).unit().normal().neg();

return new KineticVertex(orig, n1, n2, 0);
}

public static getVelocity(n1: Vector2, n2: Vector2): Vector2 {
const k = n1.add(n2);
return k.mul(2).div(k.lengthSq);
return k.mul(2).div(k.lenSq());
}

public static getWavefrontEdgeCcw(e: MeshEdge2): MeshEdge2 {
Expand Down Expand Up @@ -812,7 +812,7 @@ class KineticEvent {
public t1: number;
public type: KineticEventType;

constructor(type: KineticEventType, e: MeshEdge2, t1: number) {
public constructor(type: KineticEventType, e: MeshEdge2, t1: number) {
this.type = type;
this.e = e;
this.t1 = t1;
Expand Down
8 changes: 4 additions & 4 deletions packages/redgeometry-app/src/parts/webgpu-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ function initRemoteSystem(world: World): void {
});

const mainCamera = world.createEntity<CameraBundle>(
{ componentId: "camera", projection: Matrix4x4.identity },
{ componentId: "transform", local: Matrix4x4.identity },
{ componentId: "camera", projection: Matrix4x4.createIdentity() },
{ componentId: "transform", local: Matrix4x4.createIdentity() },
);

world.writeData<SceneData>({
Expand Down Expand Up @@ -263,7 +263,7 @@ function beginFrameSystem(world: World): void {

const a = (0.25 * time + rotation * Math.PI) / 180;

const view = Matrix4x4.identity;
const view = Matrix4x4.createIdentity();
view.rotateYAnglePre(a);
view.rotateXAnglePre(0.5);
view.translatePre(0, 0, -15);
Expand Down Expand Up @@ -306,7 +306,7 @@ function spawnSystem(world: World): void {
const { random } = appRemoteData;

for (let i = currCount; i < nextCount; i++) {
const local = Matrix4x4.identity;
const local = Matrix4x4.createIdentity();
local.rotateXAnglePre(random.nextFloatBetween(-5, 5));
local.rotateYAnglePre(random.nextFloatBetween(-5, 5));
local.rotateZAnglePre(random.nextFloatBetween(-5, 5));
Expand Down
4 changes: 2 additions & 2 deletions packages/redgeometry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This package provides optimized bundles for CJS and ESM via `redgeometry` import
import { Vector2, log } from "redgeometry";

const v = new Vector2(1, 2);
log.info("length = {}", v.length);
log.infoDebug("length = {}", v.length());
```

## Advanced
Expand All @@ -22,7 +22,7 @@ import { Vector2 } from "redgeometry/src/primitives/vector.js";
import { log } from "redgeometry/src/utility/debug.js";

const v = new Vector2(1, 2);
log.info("length = {}", v.length);
log.infoDebug("length = {}", v.length());
```

The behavior is controlled by global environment variables, which can be replaced at compile time, e.g. by esbuild with [defines](https://esbuild.github.io/api/#define):
Expand Down
Loading

0 comments on commit 9220af2

Please sign in to comment.