diff --git a/modules/brep/io/brepIO.ts b/modules/brep/io/brepIO.ts index 99fe1646..dfc05dde 100644 --- a/modules/brep/io/brepIO.ts +++ b/modules/brep/io/brepIO.ts @@ -114,7 +114,7 @@ function readSurface(s, faceInverted, effectivelyInverted, face) { surface._mirrored = !s.direct; } else if (s.TYPE === 'PLANE') { - let normal = new Vector().set3(s.normal); + let normal = new Vector().set3(s.normal).asUnitVector(); let plane = new Plane(normal, normal.dot(new Vector().set3(s.origin))); if (effectivelyInverted) { plane = plane.invert(); diff --git a/modules/geom/surfaces/brepSurface.ts b/modules/geom/surfaces/brepSurface.ts index 559b1ed4..e23c22e7 100644 --- a/modules/geom/surfaces/brepSurface.ts +++ b/modules/geom/surfaces/brepSurface.ts @@ -1,5 +1,5 @@ import {Point} from '../point'; -import Vector from 'math/vector'; +import Vector, {UnitVector} from 'math/vector'; import {Plane} from '../impl/plane'; import BrepCurve from '../curves/brepCurve'; import {intersectNurbs} from './nurbsSurface'; @@ -55,13 +55,12 @@ export class BrepSurface { return normal; } - normalUV(u: number, v: number): Vector { + normalUV(u: number, v: number): UnitVector { let normal = pt(this.impl.normal(u, v)); if (this.inverted) { normal._negate(); } - normal._normalize(); - return normal; + return normal._normalize(); } normalInMiddle(): Vector { diff --git a/modules/math/vector.ts b/modules/math/vector.ts index 830634e5..0dfed654 100644 --- a/modules/math/vector.ts +++ b/modules/math/vector.ts @@ -1,5 +1,6 @@ import {clamp} from "math/commons"; import {XYZ} from "math/xyz"; +import {areEqual, TOLERANCE_SQ} from "math/equality"; export default class Vector implements XYZ { @@ -182,7 +183,14 @@ export default class Vector implements XYZ { const sinA = clamp(this.cross(vecB).length(), -1, 1); return Math.atan2(sinA, cosA); } - + + asUnitVector(): UnitVector { + if (areEqual(this.lengthSquared(), 1, TOLERANCE_SQ)) { + console.error("not unit vector is treated as unit"); + } + return this as any as UnitVector; + } + static fromData(arr: [number, number, number]): Vector { return new Vector().set3(arr); }