Skip to content

Commit

Permalink
Add clone methods for all primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
yzrmn committed Apr 17, 2024
1 parent 89a5bb0 commit ee1b1dc
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/redgeometry-app/src/parts/gpu-cube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ function cameraMoveSystem(world: World): void {
}

const camRot = transform.rotation;
const camPos = transform.translation;

if (mouse.isPressing(MouseButtons.Mouse3)) {
let dx = 0;
Expand Down Expand Up @@ -336,7 +337,7 @@ function cameraMoveSystem(world: World): void {
v = v.unitOrZero().mul(delta * vel);
v = camRot.mulVec(v);

transform.translation = transform.translation.add(v);
transform.translation = camPos.add(v);
}

world.updateComponent<TransformComponent>(mainCamera, "transform");
Expand Down
16 changes: 16 additions & 0 deletions packages/redgeometry/src/primitives/bezier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class Bezier1Curve2 {
return result;
}

public clone(): Bezier1Curve2 {
return new Bezier1Curve2(this.p0.clone(), this.p1.clone());
}

public getBounds(): Box2 {
return Box2.fromPoints(this.p0, this.p1);
}
Expand Down Expand Up @@ -200,6 +204,10 @@ export class Bezier2Curve2 {
return new Bezier2Curve2(p0, p1, p2);
}

public clone(): Bezier2Curve2 {
return new Bezier2Curve2(this.p0.clone(), this.p1.clone(), this.p2.clone());
}

public getBounds(): Box2 {
const box = Box2.fromPoints(this.p0, this.p2);
const [qqa, qqb] = this.getDerivativeCoefficients();
Expand Down Expand Up @@ -546,6 +554,10 @@ export class Bezier3Curve2 {
return new Bezier3Curve2(p0, p1, p2, p3);
}

public clone(): Bezier3Curve2 {
return new Bezier3Curve2(this.p0.clone(), this.p1.clone(), this.p2.clone(), this.p3.clone());
}

public getBounds(): Box2 {
const box = Box2.fromPoints(this.p0, this.p3);
const [qqa, qqb, qqc] = this.getDerivativeCoefficients();
Expand Down Expand Up @@ -915,6 +927,10 @@ export class BezierRCurve2 {
return v1.dot(v2) / Math.sqrt(v1.lenSq() * v2.lenSq());
}

public clone(): BezierRCurve2 {
return new BezierRCurve2(this.p0.clone(), this.p1.clone(), this.p2.clone(), this.w);
}

public getBounds(): Box2 {
const box = Box2.fromPoints(this.p0, this.p2);
const [qqa, qqb, qqc] = this.getDerivativeCoefficients();
Expand Down
8 changes: 8 additions & 0 deletions packages/redgeometry/src/primitives/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class Box2 {
return new Box2(x0, y0, x1, y1);
}

public clone(): Box2 {
return new Box2(this.x0, this.y0, this.x1, this.y1);
}

public contains(p: Point2): boolean {
return this.x0 < p.x && this.y0 < p.y && this.x1 > p.x && this.y1 > p.y;
}
Expand Down Expand Up @@ -173,6 +177,10 @@ export class Box3 {
return new Box3(x0, y0, z0, x1, y1, z1);
}

public clone(): Box3 {
return new Box3(this.x0, this.y0, this.z0, this.x1, this.y1, this.z1);
}

public contains(p: Point3): boolean {
return this.x0 < p.x && this.y0 < p.y && this.z0 < p.z && this.x1 > p.x && this.y1 > p.y && this.z1 > p.z;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/redgeometry/src/primitives/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export class ColorRgba {
return new ColorRgba(r / 255, g / 255, b / 255, a / 255);
}

public clone(): ColorRgba {
return new ColorRgba(this.r, this.g, this.b, this.a);
}

public style(): string {
const r = this.clampFloatToHex(this.r);
const g = this.clampFloatToHex(this.g);
Expand Down
4 changes: 4 additions & 0 deletions packages/redgeometry/src/primitives/complex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export class Complex {
return new Complex(this.a + z.a, this.b + z.b);
}

public clone(): Complex {
return new Complex(this.a, this.b);
}

public conjugate(): Complex {
return new Complex(this.a, -this.b);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/redgeometry/src/primitives/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ export class Edge2 {
return e1.p0.eq(e2.p1) && e1.p1.eq(e2.p0);
}

public clone(): Edge2 {
return new Edge2(this.p0.clone(), this.p1.clone());
}

public eq(e: Edge2): boolean {
return this.p0.eq(e.p0) && this.p1.eq(e.p1);
}
Expand Down Expand Up @@ -395,6 +399,10 @@ export class Edge3 {
return e1.p0.eq(e2.p1) && e1.p1.eq(e2.p0);
}

public clone(): Edge3 {
return new Edge3(this.p0.clone(), this.p1.clone());
}

public eq(e: Edge3): boolean {
return this.p0.eq(e.p0) && this.p1.eq(e.p1);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/redgeometry/src/primitives/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class Point2 {
return new Point2(x, y);
}

public clone(): Point2 {
return new Point2(this.x, this.y);
}

public copyTo(data: number[], offset = 0): void {
data[offset] = this.x;
data[offset + 1] = this.y;
Expand Down Expand Up @@ -214,6 +218,10 @@ export class Point3 {
return new Point3(x, y, z);
}

public clone(): Point3 {
return new Point3(this.x, this.y, this.z);
}

public copyTo(data: number[], offset = 0): void {
data[offset] = this.x;
data[offset + 1] = this.y;
Expand Down
5 changes: 5 additions & 0 deletions packages/redgeometry/src/primitives/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export class Polygon2 {
this.points = [];
}

public clone(): Polygon2 {
const points = this.points.map((p) => p.clone());
return new Polygon2(points);
}

public findClosestEdgePoint(p: Point2): Edge2 | undefined {
let minDistSq = Number.POSITIVE_INFINITY;
let closestEdge: Edge2 | undefined;
Expand Down
4 changes: 4 additions & 0 deletions packages/redgeometry/src/primitives/quaternion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ export class Quaternion {
return new Vector3(this.b, this.c, this.d);
}

public clone(): Quaternion {
return new Quaternion(this.a, this.b, this.c, this.d);
}

public conjugate(): Quaternion {
return new Quaternion(this.a, -this.b, -this.c, -this.d);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/redgeometry/src/primitives/ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class Ray2 {
return new Ray2(p, v);
}

public clone(): Ray2 {
return new Ray2(this.p.clone(), this.v.clone());
}

/**
* Returns the parameterized value where a point `p` is orthogonal on the ray.
*/
Expand Down Expand Up @@ -83,6 +87,10 @@ export class Ray3 {
return new Ray3(p, v);
}

public clone(): Ray3 {
return new Ray3(this.p.clone(), this.v.clone());
}

/**
* Returns the distance to where a point `p` is orthogonal to the ray.
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/redgeometry/src/primitives/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export class Vector2 {
return new Vector2(x, y);
}

public clone(): Vector2 {
return new Vector2(this.x, this.y);
}

public copyTo(data: number[], offset = 0): void {
data[offset] = this.x;
data[offset + 1] = this.y;
Expand Down Expand Up @@ -303,6 +307,10 @@ export class Vector3 {
return new Vector3(x, y, z);
}

public clone(): Vector3 {
return new Vector3(this.x, this.y, this.z);
}

public copyTo(data: number[], offset = 0): void {
data[offset] = this.x;
data[offset + 1] = this.y;
Expand Down

0 comments on commit ee1b1dc

Please sign in to comment.