moving bbox and some typesafety

This commit is contained in:
Val Erastov (xibyte) 2020-07-19 22:18:57 -07:00
parent 08802f29c8
commit 945a7e27f1
8 changed files with 49 additions and 32 deletions

View file

@ -1,7 +1,16 @@
import Vector from 'math/vector';
import {XYZ} from "math/xyz";
import {Vec3} from "math/vec";
export default class BBox {
minX: number;
minY: number;
minZ: number;
maxX: number;
maxY: number;
maxZ: number;
constructor() {
this.minX = Number.MAX_VALUE;
this.minY = Number.MAX_VALUE;
@ -11,7 +20,7 @@ export default class BBox {
this.maxZ = -Number.MAX_VALUE;
}
checkBounds(x, y, z) {
checkBounds(x: number, y: number, z: number): void {
z = z || 0;
this.minX = Math.min(this.minX, x);
this.minY = Math.min(this.minY, y);
@ -21,41 +30,41 @@ export default class BBox {
this.maxZ = Math.max(this.maxZ, z);
}
checkPoint(p) {
checkPoint(p: XYZ): void {
this.checkBounds(p.x, p.y, p.z);
}
checkData([x, y, z]) {
checkData([x, y, z]: Vec3): void {
this.checkBounds(x, y, z);
}
center() {
center(): Vector {
return new Vector(this.minX + (this.maxX - this.minX) / 2,
this.minY + (this.maxY - this.minY) / 2,
this.minZ + (this.maxZ - this.minZ) / 2)
}
min() {
min(): Vector {
return new Vector(this.minX, this.minY, this.minZ)
}
max() {
max(): Vector {
return new Vector(this.maxX, this.maxY, this.maxZ)
}
width() {
width(): number {
return this.maxX - this.minX;
}
height() {
height(): number {
return this.maxY - this.minY;
}
depth() {
depth(): number {
return this.maxZ - this.minZ;
}
expand(delta) {
expand(delta: number): void {
this.minX -= delta;
this.minY -= delta;
this.minZ -= delta;
@ -64,7 +73,7 @@ export default class BBox {
this.maxZ += delta;
}
toPolygon() {
toPolygon(): [Vector, Vector, Vector, Vector] {
return [
new Vector(this.minX, this.minY, 0),
new Vector(this.maxX, this.minY, 0),

View file

@ -1,4 +1,4 @@
import BBox from "../../web/app/math/bbox";
import BBox from "./bbox";
import {TOLERANCE} from "math/equality";
import * as vec from "math/vec";

View file

@ -1,6 +1,7 @@
import {clamp} from "math/commons";
import {XYZ} from "math/xyz";
export default class Vector {
export default class Vector implements XYZ {
x: number;
y: number;
@ -26,7 +27,7 @@ export default class Vector {
return this;
}
setV(data: Vector): Vector {
setV(data: XYZ): Vector {
this.x = data.x;
this.y = data.y;
this.z = data.z;
@ -49,7 +50,7 @@ export default class Vector {
return this.set(this.x / scalar, this.y / scalar, this.z / scalar);
}
dot(vector: Vector): number {
dot(vector: XYZ): number {
return this.x * vector.x + this.y * vector.y + this.z * vector.z;
}
@ -65,19 +66,19 @@ export default class Vector {
return this.dot(this);
}
distanceToSquared(a: Vector): number {
distanceToSquared(a: XYZ): number {
return this.minus(a).lengthSquared();
}
distanceTo(a: Vector): number {
distanceTo(a: XYZ): number {
return Math.sqrt(this.distanceToSquared(a));
}
minus(vector: Vector): Vector {
minus(vector: XYZ): Vector {
return new Vector(this.x - vector.x, this.y - vector.y, this.z - vector.z);
}
_minus(vector: Vector): Vector {
_minus(vector: XYZ): Vector {
this.x -= vector.x;
this.y -= vector.y;
this.z -= vector.z;
@ -95,18 +96,18 @@ export default class Vector {
return new Vector(this.x + x, this.y + y, this.z + z);
}
plus(vector: Vector): Vector {
plus(vector: XYZ): Vector {
return new Vector(this.x + vector.x, this.y + vector.y, this.z + vector.z);
}
_plus(vector: Vector): Vector {
_plus(vector: XYZ): Vector {
this.x += vector.x;
this.y += vector.y;
this.z += vector.z;
return this;
}
normalize() {
normalize(): Vector {
let mag = this.length();
if (mag === 0.0) {
return new Vector(0.0, 0.0, 0.0);
@ -114,7 +115,7 @@ export default class Vector {
return new Vector(this.x / mag, this.y / mag, this.z / mag);
}
_normalize() {
_normalize(): Vector {
let mag = this.length();
if (mag === 0.0) {
return this.set(0, 0, 0)
@ -122,11 +123,11 @@ export default class Vector {
return this.set(this.x / mag, this.y / mag, this.z / mag)
};
cross(a: Vector): Vector {
cross(a: XYZ): Vector {
return this.copy()._cross(a);
};
_cross(a: Vector): Vector {
_cross(a: XYZ): Vector {
return this.set(
this.y * a.z - this.z * a.y,
this.z * a.x - this.x * a.z,
@ -150,13 +151,13 @@ export default class Vector {
return [this.x, this.y, this.z];
}
copyToData(data) {
copyToData(data: [number, number, number]): void {
data[0] = this.x;
data[1] = this.y;
data[2] = this.z;
}
angleBetween(vecB: Vector) {
angleBetween(vecB: XYZ): number {
const cosA = clamp(this.dot(vecB), -1, 1);
const sinA = clamp(this.cross(vecB).length(), -1, 1);
return Math.atan2(sinA, cosA);

7
modules/math/xyz.ts Normal file
View file

@ -0,0 +1,7 @@
export interface XYZ {
x: number;
y: number;
z: number;
}

View file

@ -5,7 +5,7 @@ import {Face} from './topo/face';
import {Loop} from './topo/loop';
import {Vertex} from './topo/vertex';
import {normalOfCCWSeq} from '../cad/cad-utils';
import BBox from '../math/bbox';
import BBox from '../../../modules/math/bbox';
import NurbsSurface from './geom/surfaces/nurbsSurface';
import {BrepSurface} from './geom/surfaces/brepSurface';
import EdgeIndex from './edgeIndex';

View file

@ -6,7 +6,7 @@ import {BrepSurface} from '../geom/surfaces/brepSurface';
import {Plane} from '../geom/impl/plane';
import Vector from '../../../../modules/math/vector';
import NullSurface from '../geom/surfaces/nullSurface';
import BBox from '../../math/bbox';
import BBox from 'math/bbox';
import NurbsCurve from '../geom/curves/nurbsCurve';
import BrepCurve from '../geom/curves/brepCurve';
import {BREPData} from "../../cad/craft/engine/brepData";

View file

@ -1,5 +1,5 @@
import Vector from 'math/vector';
import BBox from '../math/bbox'
import BBox from 'math/bbox'
import {MeshSceneSolid} from './scene/wrappers/meshSceneObject'
import {Matrix3x4} from 'math/matrix';
import {equal} from 'math/equality';

View file

@ -6,7 +6,7 @@ import CSys from 'math/csys';
import {MSketchLoop} from './mloop';
import {ProductionInfo} from './productionInfo';
import {MBrepShell, MShell} from "./mshell";
import BBox from "../../math/bbox";
import BBox from "math/bbox";
import {Basis, BasisForPlane} from "math/basis";
export class MFace extends MObject {