diff --git a/web/app/sketcher/shapes/bezier-curve.js b/web/app/sketcher/shapes/bezier-curve.js index 0d503bfe..6133e077 100644 --- a/web/app/sketcher/shapes/bezier-curve.js +++ b/web/app/sketcher/shapes/bezier-curve.js @@ -1,6 +1,8 @@ import {Ref} from './ref' import {SketchObject} from './sketch-object' import {Segment} from './segment' +import {LUT} from '../../math/bezier-cubic' +import * as draw_utils from '../shapes/draw-utils' import * as math from '../../math/math'; @@ -27,8 +29,17 @@ export class BezierCurve extends SketchObject { this.cp2.collectParams(params); } - normalDistance() { - return 1000000; + normalDistance(aim, scale) { + this.lut = LUT(this.a, this.b, this.cp1, this.cp2, scale); + const lut = this.lut; + let hero = -1; + for (let p = lut.length - 1, q = 0; q < lut.length; p = q ++) { + const dist = Math.min(Segment.calcNormalDistance(aim, lut[p], lut[q])); + if (dist != -1) { + hero = hero == -1 ? dist : Math.min(dist, hero); + } + } + return hero; } drawImpl(ctx, scale, viewer) { @@ -36,6 +47,13 @@ export class BezierCurve extends SketchObject { ctx.moveTo(this.a.x, this.a.y); ctx.bezierCurveTo(this.cp1.x, this.cp1.y, this.cp2.x, this.cp2.y, this.b.x, this.b.y); ctx.stroke(); + + //debug lut + if (this.lut) { + for (let p of this.lut) { + draw_utils.DrawPoint(ctx, p.x, p.y, 3, scale); + } + } } } BezierCurve.prototype._class = 'TCAD.TWO.BezierCurve'; diff --git a/web/app/sketcher/shapes/point.js b/web/app/sketcher/shapes/point.js index bedbe8f1..4dfff6be 100644 --- a/web/app/sketcher/shapes/point.js +++ b/web/app/sketcher/shapes/point.js @@ -49,6 +49,10 @@ export class EndPoint extends SketchObject { this.setXY(arr[0], arr[1]); } + toVector() { + return new Vector(this.x, this.y); + } + copy() { return new EndPoint(this.x, this.y); } diff --git a/web/app/sketcher/shapes/segment.js b/web/app/sketcher/shapes/segment.js index 93db4272..2e618db2 100644 --- a/web/app/sketcher/shapes/segment.js +++ b/web/app/sketcher/shapes/segment.js @@ -29,16 +29,17 @@ export class Segment extends SketchObject { this.a.collectParams(params); this.b.collectParams(params); } - + normalDistance(aim) { - var x = aim.x; - var y = aim.y; - - var ab = new Vector(this.b.x - this.a.x, this.b.y - this.a.y) - var e = ab.normalize(); - var a = new Vector(aim.x - this.a.x, aim.y - this.a.y); - var b = e.multiply(a.dot(e)); - var n = a.minus(b); + return Segment.calcNormalDistance(aim, this.a, this.b); + } + + static calcNormalDistance(aim, segmentA, segmentB) { + const ab = new Vector(segmentB.x - segmentA.x, segmentB.y - segmentA.y) + const e = ab.normalize(); + const a = new Vector(aim.x - segmentA.x, aim.y - segmentA.y); + const b = e.multiply(a.dot(e)); + const n = a.minus(b); //Check if vector b lays on the vector ab if (b.length() > ab.length()) { diff --git a/web/app/sketcher/shapes/sketch-object.js b/web/app/sketcher/shapes/sketch-object.js index d1db2a20..bb44fd22 100644 --- a/web/app/sketcher/shapes/sketch-object.js +++ b/web/app/sketcher/shapes/sketch-object.js @@ -12,6 +12,10 @@ export class SketchObject extends Shape { this.layer = null; } + normalDistance(aim, scale) { + return -1; + } + addChild(child) { this.children.push(child); child.parent = this; diff --git a/web/app/sketcher/viewer2d.js b/web/app/sketcher/viewer2d.js index a7ff5d41..57fad9c7 100644 --- a/web/app/sketcher/viewer2d.js +++ b/web/app/sketcher/viewer2d.js @@ -134,12 +134,12 @@ Viewer.prototype.search = function(x, y, buffer, deep, onlyPoints, filter) { for (var j = 0; j < objs.length; j++) { var l = unreachable + 1; var before = pickResult.length; - objs[j].accept(function(o) { + objs[j].accept((o) => { if (!o.visible) return true; if (onlyPoints && !isEndPoint(o)) { return true; } - l = o.normalDistance(aim); + l = o.normalDistance(aim, this.scale); if (l >= 0 && l <= buffer && !isFiltered(o)) { pickResult.push(o); return false;