make bezier curve shape pickable

This commit is contained in:
Val Erastov 2016-12-09 02:34:16 -08:00
parent c2816688c3
commit 56ad328e99
5 changed files with 40 additions and 13 deletions

View file

@ -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';

View file

@ -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);
}

View file

@ -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()) {

View file

@ -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;

View file

@ -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;