From 2ffec1298984e6ba9cc1c1fd6322f4429317dcbc Mon Sep 17 00:00:00 2001 From: "Val Erastov (xibyte)" Date: Sun, 19 Jul 2020 15:50:33 -0700 Subject: [PATCH] organizing math module - move common geometry to euclidean module --- modules/math/commons.ts | 212 ------------------ modules/math/euclidean.ts | 204 +++++++++++++++++ modules/math/vector.ts | 10 + modules/scene/objects/meshLine.js | 2 +- web/app/brep/brep-enclose.js | 4 +- web/app/brep/geom/curves/frenetFrame.js | 2 +- web/app/brep/operations/evolve-face.js | 2 +- web/app/brep/topo/loop.ts | 5 +- web/app/brep/utils/pickPointInPolygon.js | 2 +- web/app/cad/assembly/dof/EEDOF.ts | 3 +- web/app/cad/cad-utils.js | 10 +- web/app/cad/legacy/mesh/workbench.js | 4 +- web/app/cad/scene/wrappers/brepSceneObject.js | 2 +- web/app/cad/sketch/sketchBoundaries.js | 2 +- web/app/cad/sketch/sketchModel.js | 3 +- web/app/cad/tess/nested-loops.js | 2 +- web/app/sketcher/shapes/bezier-curve.js | 7 +- web/app/sketcher/shapes/dim.js | 6 +- web/app/sketcher/shapes/textHelper.js | 3 +- web/app/sketcher/tools/bezier-curve.js | 6 +- web/app/sketcher/tools/offset.js | 6 +- 21 files changed, 246 insertions(+), 251 deletions(-) diff --git a/modules/math/commons.ts b/modules/math/commons.ts index 5c95ab52..5856523d 100644 --- a/modules/math/commons.ts +++ b/modules/math/commons.ts @@ -1,154 +1,3 @@ -import Vector from 'math/vector'; -import BBox from '../../web/app/math/bbox' -import * as vec from 'math/vec'; -import {perp2d} from 'math/vec'; -import {eqTol} from "../../web/app/brep/geom/tolerance"; -import {IDENTITY_BASIS3} from "math/basis"; -import {distance} from "math/distance"; -import {TOLERANCE} from "math/equality"; - - -export function circleFromPoints(p1, p2, p3) { - var center = new Vector(); - var offset = p2.x*p2.x + p2.y*p2.y; - var bc = ( p1.x*p1.x + p1.y*p1.y - offset )/2.0; - var cd = (offset - p3.x*p3.x - p3.y*p3.y)/2.0; - var det = (p1.x - p2.x) * (p2.y - p3.y) - (p2.x - p3.x)* (p1.y - p2.y); - - if (Math.abs(det) < TOLERANCE) { return null; } - - var idet = 1/det; - - center.x = (bc * (p2.y - p3.y) - cd * (p1.y - p2.y)) * idet; - center.y = (cd * (p1.x - p2.x) - bc * (p2.x - p3.x)) * idet; - return center; -} - -export function norm2(vec) { - var sq = 0; - for (var i = 0; i < vec.length; i++) { - sq += vec[i] * vec[i]; - } - return Math.sqrt(sq); -} - -export function rotate(px, py, angle) { - return rotateInPlace(px, py, angle, new Vector()); -} - -export function rotateInPlace(px, py, angle, out) { - out.x = px * Math.cos(angle) - py * Math.sin(angle); - out.y = px * Math.sin(angle) + py * Math.cos(angle); - return out; -} - -export function polygonOffsetXY(polygon, scaleX, scaleY) { - const origBBox = new BBox(); - const scaledBBox = new BBox(); - const result = []; - for (let point of polygon) { - const scaledPoint = new Vector(point.x * scaleX, point.y * scaleY); - result.push(scaledPoint); - origBBox.checkPoint(point); - scaledBBox.checkPoint(scaledPoint); - } - const alignVector = scaledBBox.center()._minus(origBBox.center()); - for (let point of result) { - point._minus(alignVector); - } - return result; -} - - -export function polygonOffset( polygon, scale ) { - return polygonOffsetXY( polygon, scale, scale ); -} - -export function polygonOffsetByDelta( polygon, delta ) { - const origBBox = new BBox(); - for (let point of polygon) { - origBBox.checkPoint(point); - } - const width = origBBox.width(); - const height = origBBox.height(); - return polygonOffsetXY(polygon, (width + delta) / width, (height + delta) / height); -} - -export function isPointInsidePolygon( inPt, inPolygon ) { - var EPSILON = TOLERANCE; - - var polyLen = inPolygon.length; - - // inPt on polygon contour => immediate success or - // toggling of inside/outside at every single! intersection point of an edge - // with the horizontal line through inPt, left of inPt - // not counting lowerY endpoints of edges and whole edges on that line - var inside = false; - for( var p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) { - var edgeLowPt = inPolygon[ p ]; - var edgeHighPt = inPolygon[ q ]; - - var edgeDx = edgeHighPt.x - edgeLowPt.x; - var edgeDy = edgeHighPt.y - edgeLowPt.y; - - if ( Math.abs(edgeDy) > EPSILON ) { // not parallel - if ( edgeDy < 0 ) { - edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx; - edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy; - } - if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue; - - if ( inPt.y == edgeLowPt.y ) { - if ( inPt.x == edgeLowPt.x ) return true; // inPt is on contour ? - // continue; // no intersection or edgeLowPt => doesn't count !!! - } else { - var perpEdge = edgeDy * (inPt.x - edgeLowPt.x) - edgeDx * (inPt.y - edgeLowPt.y); - if ( perpEdge == 0 ) return true; // inPt is on contour ? - if ( perpEdge < 0 ) continue; - inside = ! inside; // true intersection left of inPt - } - } else { // parallel or colinear - if ( inPt.y != edgeLowPt.y ) continue; // parallel - // egde lies on the same horizontal line as inPt - if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) || - ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour ! - // continue; - } - } - - return inside; -} - -// http://en.wikipedia.org/wiki/Shoelace_formula -export function area(contour) { - var n = contour.length; - var a = 0.0; - for ( var p = n - 1, q = 0; q < n; p = q ++ ) { - a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y; - } - return a * 0.5; -} - -export function isCCW(path2D) { - return area(path2D) >= 0; -} - -export function findLowestLeftPoint(poly) { - let heroIdx = 0; - for (let i = 1; i< poly.length; ++i) { - const point = poly[i]; - let hero = poly[heroIdx]; - if (point.y < hero.y) { - heroIdx = i; - } else if (hero.y == point.y) { - if (point.x < hero.x) { - heroIdx = i; - } - } - } - return heroIdx; -} - export const _360 = 2 * Math.PI; export const _90 = 0.5 * Math.PI; export const _270 = 1.5 * Math.PI; @@ -161,71 +10,10 @@ export function makeAngle0_360(angle) { return angle; } -export function perpendicularVector(v) { - v = vec.normalize(v); - return IDENTITY_BASIS3.map(axis => vec.cross(axis, v)).sort((a, b) => vec.lengthSq(b) - vec.lengthSq(a))[0]; -} - -export function radiusOfCurvature(d1, d2) { - let r1lsq = vec.lengthSq(d1); - let r1l = Math.sqrt(r1lsq); - return r1lsq * r1l / vec.length(vec.cross(d1, d2)); -} - -export function pointToLineSignedDistance(ax, ay, bx, by, px, py) { - let nx = - (by - ay); - let ny = bx - ax; - - const d = distance(ax, ay, bx, by); - - nx /= d; - ny /= d; - - let vx = px - ax; - let vy = py - ay; - - const proj = vx * ny + vy * (-nx); - - //Check if vector b lays on the vector ab - if (proj > d) { - return Number.NaN; - } - - if (proj < 0) { - return Number.NaN; - } - return vx * nx + vy * ny; -} - -export function lineLineIntersection2d(p1, p2, v1, v2) { - - // const n1 = perp2d(v1); - const n2 = perp2d(v2); - const cos = vec.dot(n2, v1); - if (eqTol(cos, 0)) { - return null; - } - const u1 = vec.dot(n2, vec.sub(p2, p1)) / cos; - // const u2 = vec.dot(n1, vec.sub(p1, p2)) / vec.dot(n1, v2); - - return [p1[0] + v1[0] * u1, p1[1] + v1[1] * u1]; -} - -export function lineLineIntersection(p1, p2, v1, v2) { - let zAx = v1.cross(v2); - const n1 = zAx.cross(v1)._normalize(); - const n2 = zAx.cross(v2)._normalize(); - return { - u1: n2.dot(p2.minus(p1)) / n2.dot(v1), - u2: n1.dot(p1.minus(p2)) / n1.dot(v2), - } -} - export const DEG_RAD = Math.PI / 180.0; export const sq = (a) => a * a; - export function clamp(num, min, max) { return Math.min(max, Math.max(num, min)) } diff --git a/modules/math/euclidean.ts b/modules/math/euclidean.ts index e69de29b..f823e6c6 100644 --- a/modules/math/euclidean.ts +++ b/modules/math/euclidean.ts @@ -0,0 +1,204 @@ +import BBox from "../../web/app/math/bbox"; + +import {TOLERANCE} from "math/equality"; +import * as vec from "math/vec"; +import {perp2d} from "math/vec"; +import {eqTol} from "../../web/app/brep/geom/tolerance"; +import {distance} from "math/distance"; +import {IDENTITY_BASIS3} from "math/basis"; +import Vector from "math/vector"; + +export function circleFromPoints(p1, p2, p3) { + var center = new Vector(); + var offset = p2.x * p2.x + p2.y * p2.y; + var bc = (p1.x * p1.x + p1.y * p1.y - offset) / 2.0; + var cd = (offset - p3.x * p3.x - p3.y * p3.y) / 2.0; + var det = (p1.x - p2.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p2.y); + + if (Math.abs(det) < TOLERANCE) { + return null; + } + + var idet = 1 / det; + + center.x = (bc * (p2.y - p3.y) - cd * (p1.y - p2.y)) * idet; + center.y = (cd * (p1.x - p2.x) - bc * (p2.x - p3.x)) * idet; + return center; +} + +export function rotate(px, py, angle) { + return rotateInPlace(px, py, angle, new Vector()); +} + +export function rotateInPlace(px, py, angle, out) { + out.x = px * Math.cos(angle) - py * Math.sin(angle); + out.y = px * Math.sin(angle) + py * Math.cos(angle); + return out; +} + +export function polygonOffsetXY(polygon, scaleX, scaleY) { + const origBBox = new BBox(); + const scaledBBox = new BBox(); + const result = []; + for (let point of polygon) { + const scaledPoint = new Vector(point.x * scaleX, point.y * scaleY); + result.push(scaledPoint); + origBBox.checkPoint(point); + scaledBBox.checkPoint(scaledPoint); + } + const alignVector = scaledBBox.center()._minus(origBBox.center()); + for (let point of result) { + point._minus(alignVector); + } + return result; +} + +export function polygonOffset(polygon, scale) { + return polygonOffsetXY(polygon, scale, scale); +} + +export function polygonOffsetByDelta(polygon, delta) { + const origBBox = new BBox(); + for (let point of polygon) { + origBBox.checkPoint(point); + } + const width = origBBox.width(); + const height = origBBox.height(); + return polygonOffsetXY(polygon, (width + delta) / width, (height + delta) / height); +} + +export function isPointInsidePolygon(inPt, inPolygon) { + var EPSILON = TOLERANCE; + + var polyLen = inPolygon.length; + + // inPt on polygon contour => immediate success or + // toggling of inside/outside at every single! intersection point of an edge + // with the horizontal line through inPt, left of inPt + // not counting lowerY endpoints of edges and whole edges on that line + var inside = false; + for (var p = polyLen - 1, q = 0; q < polyLen; p = q++) { + var edgeLowPt = inPolygon[p]; + var edgeHighPt = inPolygon[q]; + + var edgeDx = edgeHighPt.x - edgeLowPt.x; + var edgeDy = edgeHighPt.y - edgeLowPt.y; + + if (Math.abs(edgeDy) > EPSILON) { // not parallel + if (edgeDy < 0) { + edgeLowPt = inPolygon[q]; + edgeDx = -edgeDx; + edgeHighPt = inPolygon[p]; + edgeDy = -edgeDy; + } + if ((inPt.y < edgeLowPt.y) || (inPt.y > edgeHighPt.y)) continue; + + if (inPt.y == edgeLowPt.y) { + if (inPt.x == edgeLowPt.x) return true; // inPt is on contour ? + // continue; // no intersection or edgeLowPt => doesn't count !!! + } else { + var perpEdge = edgeDy * (inPt.x - edgeLowPt.x) - edgeDx * (inPt.y - edgeLowPt.y); + if (perpEdge == 0) return true; // inPt is on contour ? + if (perpEdge < 0) continue; + inside = !inside; // true intersection left of inPt + } + } else { // parallel or colinear + if (inPt.y != edgeLowPt.y) continue; // parallel + // egde lies on the same horizontal line as inPt + if (((edgeHighPt.x <= inPt.x) && (inPt.x <= edgeLowPt.x)) || + ((edgeLowPt.x <= inPt.x) && (inPt.x <= edgeHighPt.x))) return true; // inPt: Point on contour ! + // continue; + } + } + + return inside; +} + +export function area(contour) { + var n = contour.length; + var a = 0.0; + for (var p = n - 1, q = 0; q < n; p = q++) { + a += contour[p].x * contour[q].y - contour[q].x * contour[p].y; + } + return a * 0.5; +} + +export function isCCW(path2D) { + return area(path2D) >= 0; +} + +export function findLowestLeftPoint(poly) { + let heroIdx = 0; + for (let i = 1; i < poly.length; ++i) { + const point = poly[i]; + let hero = poly[heroIdx]; + if (point.y < hero.y) { + heroIdx = i; + } else if (hero.y == point.y) { + if (point.x < hero.x) { + heroIdx = i; + } + } + } + return heroIdx; +} + +export function perpendicularVector(v) { + v = vec.normalize(v); + return IDENTITY_BASIS3.map(axis => vec.cross(axis, v)).sort((a, b) => vec.lengthSq(b) - vec.lengthSq(a))[0]; +} + +export function radiusOfCurvature(d1, d2) { + let r1lsq = vec.lengthSq(d1); + let r1l = Math.sqrt(r1lsq); + return r1lsq * r1l / vec.length(vec.cross(d1, d2)); +} + +export function pointToLineSignedDistance(ax, ay, bx, by, px, py) { + let nx = -(by - ay); + let ny = bx - ax; + + const d = distance(ax, ay, bx, by); + + nx /= d; + ny /= d; + + let vx = px - ax; + let vy = py - ay; + + const proj = vx * ny + vy * (-nx); + + //Check if vector b lays on the vector ab + if (proj > d) { + return Number.NaN; + } + + if (proj < 0) { + return Number.NaN; + } + return vx * nx + vy * ny; +} + +export function lineLineIntersection2d(p1, p2, v1, v2) { + + // const n1 = perp2d(v1); + const n2 = perp2d(v2); + const cos = vec.dot(n2, v1); + if (eqTol(cos, 0)) { + return null; + } + const u1 = vec.dot(n2, vec.sub(p2, p1)) / cos; + // const u2 = vec.dot(n1, vec.sub(p1, p2)) / vec.dot(n1, v2); + + return [p1[0] + v1[0] * u1, p1[1] + v1[1] * u1]; +} + +export function lineLineIntersection(p1, p2, v1, v2) { + let zAx = v1.cross(v2); + const n1 = zAx.cross(v1)._normalize(); + const n2 = zAx.cross(v2)._normalize(); + return { + u1: n2.dot(p2.minus(p1)) / n2.dot(v1), + u2: n1.dot(p1.minus(p2)) / n1.dot(v2), + } +} \ No newline at end of file diff --git a/modules/math/vector.ts b/modules/math/vector.ts index cb9b50ff..00b02077 100644 --- a/modules/math/vector.ts +++ b/modules/math/vector.ts @@ -173,3 +173,13 @@ export default class Vector { scale: (scalar: number) => Vector = Vector.prototype.multiply; _scale: (scalar: number) => Vector = Vector.prototype._multiply; } + +const freeze = Object.freeze; + +export const ORIGIN = freeze(new Vector(0, 0, 0)); + +export const AXIS = freeze({ + X: freeze(new Vector(1, 0, 0)), + Y: freeze(new Vector(0, 1, 0)), + Z: freeze(new Vector(0, 0, 1)) +}); \ No newline at end of file diff --git a/modules/scene/objects/meshLine.js b/modules/scene/objects/meshLine.js index 47223be8..326cf128 100644 --- a/modules/scene/objects/meshLine.js +++ b/modules/scene/objects/meshLine.js @@ -1,6 +1,6 @@ import * as vec from 'math/vec'; -import {perpendicularVector} from '../../math/commons'; import {Face3, Geometry, Vector3} from 'three'; +import {perpendicularVector} from "../../math/euclidean"; export function createMeshLineGeometry(points, width) { const vThree = arr => new Vector3().fromArray(arr); diff --git a/web/app/brep/brep-enclose.js b/web/app/brep/brep-enclose.js index 65dac9b5..85aedd31 100644 --- a/web/app/brep/brep-enclose.js +++ b/web/app/brep/brep-enclose.js @@ -4,17 +4,17 @@ import {Edge} from './topo/edge'; import BrepCurve from './geom/curves/brepCurve'; import {Plane} from './geom/impl/plane'; import * as cad_utils from '../cad/cad-utils'; -import * as math from '../../../modules/math/commons'; import {createBoundingSurface} from './brep-builder'; import NurbsSurface from './geom/surfaces/nurbsSurface'; import {BrepSurface} from './geom/surfaces/brepSurface'; import {Matrix3} from 'math/matrix'; import {BasisForPlane} from "math/basis"; +import {isCCW as isCCWtest} from "math/euclidean"; function isCCW(points, normal) { const tr2d = new Matrix3().setBasis(BasisForPlane(normal)).invert(); const points2d = points.map(p => tr2d.apply(p)); - return math.isCCW(points2d); + return isCCWtest(points2d); } function checkCCW(points, normal) { diff --git a/web/app/brep/geom/curves/frenetFrame.js b/web/app/brep/geom/curves/frenetFrame.js index 849ac81b..0bf35abf 100644 --- a/web/app/brep/geom/curves/frenetFrame.js +++ b/web/app/brep/geom/curves/frenetFrame.js @@ -1,5 +1,5 @@ import * as vec from 'math/vec'; -import {perpendicularVector} from '../../../../../modules/math/commons'; +import {perpendicularVector} from "../../../../../modules/math/euclidean"; export function frenetFrame(D1, D2) { let T = vec.normalize(D1); diff --git a/web/app/brep/operations/evolve-face.js b/web/app/brep/operations/evolve-face.js index e2cd3bc9..e895325a 100644 --- a/web/app/brep/operations/evolve-face.js +++ b/web/app/brep/operations/evolve-face.js @@ -1,8 +1,8 @@ import {Face} from '../topo/face'; import {Vertex} from '../topo/vertex'; import Vector from 'math/vector'; -import {isCCW} from '../../../../modules/math/commons'; import PIP from '../../cad/tess/pip'; +import {isCCW} from "../../../../modules/math/euclidean"; export function evolveFace(originFace, loops) { let out = []; diff --git a/web/app/brep/topo/loop.ts b/web/app/brep/topo/loop.ts index cc4d6794..7d986f5a 100644 --- a/web/app/brep/topo/loop.ts +++ b/web/app/brep/topo/loop.ts @@ -1,9 +1,8 @@ import {TopoObject} from './topo-object' - -import * as math from 'math/commons' import {Face} from "./face"; import {BrepSurface} from "../geom/surfaces/brepSurface"; import {HalfEdge} from "./edge"; +import {findLowestLeftPoint} from "math/euclidean"; export class Loop extends TopoObject { @@ -58,7 +57,7 @@ export class Loop extends TopoObject { private static isPolygonCCWOnSurface(polygon: any[], surface: BrepSurface) { const tr = surface.simpleSurface.get2DTransformation(); const polygon2d = polygon.map(p => tr.apply(p)); - const lowestLeftIdx = math.findLowestLeftPoint(polygon2d); + const lowestLeftIdx = findLowestLeftPoint(polygon2d); const n = polygon.length; const nextIdx = ((lowestLeftIdx + 1) % n); const prevIdx = ((n + lowestLeftIdx - 1) % n); diff --git a/web/app/brep/utils/pickPointInPolygon.js b/web/app/brep/utils/pickPointInPolygon.js index 3bba0ef8..14cbd7b5 100644 --- a/web/app/brep/utils/pickPointInPolygon.js +++ b/web/app/brep/utils/pickPointInPolygon.js @@ -1,5 +1,5 @@ import libtess from 'libtess' -import {area} from "../../../../modules/math/commons"; +import {area} from "../../../../modules/math/euclidean"; export default function pickPointInside2dPolygon(polygon) { function vertexCallback(data, tr) { diff --git a/web/app/cad/assembly/dof/EEDOF.ts b/web/app/cad/assembly/dof/EEDOF.ts index b90719e1..8713c79b 100644 --- a/web/app/cad/assembly/dof/EEDOF.ts +++ b/web/app/cad/assembly/dof/EEDOF.ts @@ -1,13 +1,14 @@ import Vector from "math/vector"; import {FaceTouchAlignConstraint} from "../constraints/faceTouchAlign"; import {AssemblyDOF, ModificationResponse} from "./assemblyDOF"; -import {clamp, DEG_RAD, lineLineIntersection} from "math/commons"; +import {clamp, DEG_RAD} from "math/commons"; import {ConflictDOF} from "./conflictDOF"; import {EdgeAlignConstraint} from "../constraints/edgeAlign"; import {PPEEDOF} from "./PPEEDOF"; import {EEEEDOF} from "./EEEEDOF"; import {Matrix3} from "math/matrix"; import {areEqual} from "math/equality"; +import {lineLineIntersection} from "math/euclidean"; const ANGULAR_ALLOWANCE = 10 * DEG_RAD; diff --git a/web/app/cad/cad-utils.js b/web/app/cad/cad-utils.js index c95d2f64..d09166ad 100644 --- a/web/app/cad/cad-utils.js +++ b/web/app/cad/cad-utils.js @@ -1,9 +1,12 @@ import Vector from 'math/vector'; import BBox from '../math/bbox' -import * as math from '../../../modules/math/commons' import {MeshSceneSolid} from './scene/wrappers/meshSceneObject' import {Matrix3} from 'math/matrix'; import {equal} from 'math/equality'; +import {area, isCCW, isPointInsidePolygon} from "math/euclidean"; + +export {area, isCCW, isPointInsidePolygon}; + export const FACE_COLOR = 0xB0C4DE; @@ -220,8 +223,6 @@ export function fixCCW(path, normal) { return path; } -export const isPointInsidePolygon = math.isPointInsidePolygon; - export function someBasis2(normal) { var x = normal.cross(normal.randomNonParallelVector()); var y = normal.cross(x).unit(); @@ -259,9 +260,6 @@ export function normalOfCCWSeqTHREE(ccwSequence) { return b.sub(a).cross(c.sub(a)).normalize(); } -export const area = math.area; -export const isCCW = math.isCCW; - export function calculateExtrudedLid(sourcePolygon, normal, direction, expansionFactor) { var lid = []; var length = sourcePolygon.length; diff --git a/web/app/cad/legacy/mesh/workbench.js b/web/app/cad/legacy/mesh/workbench.js index 61a610dc..dc703182 100644 --- a/web/app/cad/legacy/mesh/workbench.js +++ b/web/app/cad/legacy/mesh/workbench.js @@ -1,12 +1,12 @@ import Vector from 'math/vector'; import * as cad_utils from '../../cad-utils' -import * as math from '../../../../../modules/math/commons' import {HashTable} from '../../../utils/hashmap' import {Mesh} from '../mesh' import revolve from './revolve' import {Triangulate} from '../../tess/triangulation' import {distanceAB3} from "../../../../../modules/math/distance"; import {areEqual, equal, strictEqual} from "../../../../../modules/math/equality"; +import {isPointInsidePolygon} from "../../../../../modules/math/euclidean"; export function sortPolygons(polygons) { function Loop(polygon) { @@ -16,7 +16,7 @@ export function sortPolygons(polygons) { } function contains(polygon, other) { for (let point of other._2D) { - if (!math.isPointInsidePolygon(point, polygon._2D)) { + if (!isPointInsidePolygon(point, polygon._2D)) { return false; } } diff --git a/web/app/cad/scene/wrappers/brepSceneObject.js b/web/app/cad/scene/wrappers/brepSceneObject.js index 8c411443..13930f85 100644 --- a/web/app/cad/scene/wrappers/brepSceneObject.js +++ b/web/app/cad/scene/wrappers/brepSceneObject.js @@ -4,7 +4,7 @@ import brepTess from '../../tess/brep-tess'; import tessellateSurface from '../../../brep/geom/surfaces/surfaceTess'; import {setAttribute} from '../../../../../modules/scene/objectData'; import * as vec from 'math/vec'; -import {perpendicularVector} from '../../../../../modules/math/commons'; +import {perpendicularVector} from "math/euclidean"; const SMOOTH_RENDERING = true; diff --git a/web/app/cad/sketch/sketchBoundaries.js b/web/app/cad/sketch/sketchBoundaries.js index 96fc3886..1c7c2e28 100644 --- a/web/app/cad/sketch/sketchBoundaries.js +++ b/web/app/cad/sketch/sketchBoundaries.js @@ -1,4 +1,3 @@ -import {circleFromPoints, radiusOfCurvature} from '../../../../modules/math/commons'; import * as vec from 'math/vec'; import {iteratePath} from '../cad-utils'; import NurbsCurve from '../../brep/geom/curves/nurbsCurve'; @@ -6,6 +5,7 @@ import {veqXYZ} from '../../brep/geom/tolerance'; import curveTess, {curveTessParams} from '../../brep/geom/impl/curve/curve-tess'; import {distanceAB} from "math/distance"; import {areEqual, TOLERANCE} from "math/equality"; +import {circleFromPoints, radiusOfCurvature} from "../../../../modules/math/euclidean"; export function getSketchBoundaries(sceneFace) { const boundary = {lines: [], arcs: [], circles: [], nurbses: []}; diff --git a/web/app/cad/sketch/sketchModel.js b/web/app/cad/sketch/sketchModel.js index 71f8b2e7..58251ae4 100644 --- a/web/app/cad/sketch/sketchModel.js +++ b/web/app/cad/sketch/sketchModel.js @@ -3,11 +3,12 @@ import BrepCurve from '../../brep/geom/curves/brepCurve'; import NurbsCurve from '../../brep/geom/curves/nurbsCurve'; import {Point} from '../../brep/geom/point' import {LUT} from '../../math/bezier-cubic' -import {isCCW, makeAngle0_360} from '../../../../modules/math/commons' +import {makeAngle0_360} from '../../../../modules/math/commons' import {normalizeCurveEnds} from '../../brep/geom/impl/nurbs-ext'; import Vector, {AXIS, ORIGIN} from '../../../../modules/math/vector'; import CSys from "../../../../modules/math/csys"; import {distanceAB} from "../../../../modules/math/distance"; +import {isCCW} from "../../../../modules/math/euclidean"; const RESOLUTION = 20; diff --git a/web/app/cad/tess/nested-loops.js b/web/app/cad/tess/nested-loops.js index 8ce4b69d..7a9e7282 100644 --- a/web/app/cad/tess/nested-loops.js +++ b/web/app/cad/tess/nested-loops.js @@ -1,5 +1,5 @@ import pip from "./pip"; -import {isCCW} from "../../../../modules/math/commons"; +import {isCCW} from "../../../../modules/math/euclidean"; export default function(loops) { const loops = loops.map(loop => new NestedLoop(loop)); diff --git a/web/app/sketcher/shapes/bezier-curve.js b/web/app/sketcher/shapes/bezier-curve.js index a467b7b4..c1d12dbf 100644 --- a/web/app/sketcher/shapes/bezier-curve.js +++ b/web/app/sketcher/shapes/bezier-curve.js @@ -4,8 +4,7 @@ import {LUT} from '../../math/bezier-cubic' import {ConvexHull2D} from '../../math/convex-hull' import * as draw_utils from '../shapes/draw-utils' -import * as math from '../../../../modules/math/commons'; -import {EndPoint} from "./point"; +import {isPointInsidePolygon, polygonOffset} from "../../../../modules/math/euclidean"; export class BezierCurve extends SketchObject { @@ -53,8 +52,8 @@ export class BezierCurve extends SketchObject { normalDistance(aim, scale) { this.hull = ConvexHull2D([this.a, this.b, this.cp1, this.cp2]); - this.hull = math.polygonOffset(this.hull, 1 + (0.3 / scale)); - if (math.isPointInsidePolygon(aim, this.hull)) { + this.hull = polygonOffset(this.hull, 1 + (0.3 / scale)); + if (isPointInsidePolygon(aim, this.hull)) { this.lut = LUT(this.a, this.b, this.cp1, this.cp2, scale); return this.closestNormalDistance(aim, this.lut) } diff --git a/web/app/sketcher/shapes/dim.js b/web/app/sketcher/shapes/dim.js index 7d1f1870..1326b64c 100644 --- a/web/app/sketcher/shapes/dim.js +++ b/web/app/sketcher/shapes/dim.js @@ -1,9 +1,6 @@ import { DEG_RAD, - lineLineIntersection2d, - makeAngle0_360, - pointToLineSignedDistance -} from '../../../../modules/math/commons' + makeAngle0_360} from '../../../../modules/math/commons' import * as vec from 'math/vec' import Vector from 'math/vector'; import {Styles} from "../styles"; @@ -12,6 +9,7 @@ import {isInstanceOf} from "../actions/matchUtils"; import {Arc} from "./arc"; import {SketchObject} from "./sketch-object"; import {distance, distanceAB} from "../../../../modules/math/distance"; +import {lineLineIntersection2d, pointToLineSignedDistance} from "../../../../modules/math/euclidean"; const ARROW_W_PX = 15; const ARROW_H_PX = 4; diff --git a/web/app/sketcher/shapes/textHelper.js b/web/app/sketcher/shapes/textHelper.js index 364249ee..abdc6135 100644 --- a/web/app/sketcher/shapes/textHelper.js +++ b/web/app/sketcher/shapes/textHelper.js @@ -1,5 +1,6 @@ -import {_270, _90, makeAngle0_360, pointToLineSignedDistance} from "../../../../modules/math/commons"; +import {_270, _90, makeAngle0_360} from "../../../../modules/math/commons"; import {_negate} from "math/vec"; +import {pointToLineSignedDistance} from "../../../../modules/math/euclidean"; export class TextHelper { diff --git a/web/app/sketcher/tools/bezier-curve.js b/web/app/sketcher/tools/bezier-curve.js index da48e3fe..1be8d267 100644 --- a/web/app/sketcher/tools/bezier-curve.js +++ b/web/app/sketcher/tools/bezier-curve.js @@ -1,10 +1,8 @@ import {Tool} from './tool' -import {EndPoint} from '../shapes/point' import {BezierCurve} from '../shapes/bezier-curve' -import {Constraints} from '../parametric' import Vector from 'math/vector'; -import * as math from '../../../../modules/math/commons' import {AlgNumConstraint, ConstraintDefinitions} from "../constr/ANConstraints"; +import {rotate} from "math/euclidean"; export class BezierCurveTool extends Tool { @@ -61,7 +59,7 @@ export class BezierCurveTool extends Tool { this.curve.b.setFromPoint(p); const axis = this._v.set(this.curve.b.x - this.curve.a.x, this.curve.b.y - this.curve.a.y)._multiply(0.7); //controlSegment = {x: -controlSegment.y, y: controlSegment.x}; - const controlSegment = math.rotate(- axis.y, axis.x, - Math.PI * 0.25); + const controlSegment = rotate(- axis.y, axis.x, - Math.PI * 0.25); if (this.otherCurveEndPoint != null) { const ctrlLength = axis.length(); this.curve.cp1.x = this.curve.a.x + this.snappedControl.x * ctrlLength; diff --git a/web/app/sketcher/tools/offset.js b/web/app/sketcher/tools/offset.js index 5f36405d..e3d3668d 100644 --- a/web/app/sketcher/tools/offset.js +++ b/web/app/sketcher/tools/offset.js @@ -1,10 +1,8 @@ import {LoopPickTool} from './loop-pick' import {Constraints} from '../parametric' -import * as math from '../../../../modules/math/commons'; import Vector from 'math/vector'; -import {swap} from '../../utils/utils' -import {EndPoint} from '../shapes/point' import {Arc} from '../shapes/arc' +import {isCCW} from "../../../../modules/math/euclidean"; export class OffsetTool extends LoopPickTool { @@ -36,7 +34,7 @@ export class OffsetTool extends LoopPickTool { return (i + low) % length; } - const mainInverse = !this.twoConnectedArcs() && math.isCCW([loopPoints[pos(0)], loopPoints[pos(1)], loopPoints[pos(length - 1)]]); + const mainInverse = !this.twoConnectedArcs() && isCCW([loopPoints[pos(0)], loopPoints[pos(1)], loopPoints[pos(length - 1)]]); const pm = this.viewer.parametricManager; const offsetConstant = createOffsetConstant(pm, delta);