mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-08 09:24:18 +01:00
make bezier curve shape pickable
This commit is contained in:
parent
c2816688c3
commit
56ad328e99
5 changed files with 40 additions and 13 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
import {Ref} from './ref'
|
import {Ref} from './ref'
|
||||||
import {SketchObject} from './sketch-object'
|
import {SketchObject} from './sketch-object'
|
||||||
import {Segment} from './segment'
|
import {Segment} from './segment'
|
||||||
|
import {LUT} from '../../math/bezier-cubic'
|
||||||
|
import * as draw_utils from '../shapes/draw-utils'
|
||||||
|
|
||||||
import * as math from '../../math/math';
|
import * as math from '../../math/math';
|
||||||
|
|
||||||
|
|
@ -27,8 +29,17 @@ export class BezierCurve extends SketchObject {
|
||||||
this.cp2.collectParams(params);
|
this.cp2.collectParams(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
normalDistance() {
|
normalDistance(aim, scale) {
|
||||||
return 1000000;
|
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) {
|
drawImpl(ctx, scale, viewer) {
|
||||||
|
|
@ -36,6 +47,13 @@ export class BezierCurve extends SketchObject {
|
||||||
ctx.moveTo(this.a.x, this.a.y);
|
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.bezierCurveTo(this.cp1.x, this.cp1.y, this.cp2.x, this.cp2.y, this.b.x, this.b.y);
|
||||||
ctx.stroke();
|
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';
|
BezierCurve.prototype._class = 'TCAD.TWO.BezierCurve';
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,10 @@ export class EndPoint extends SketchObject {
|
||||||
this.setXY(arr[0], arr[1]);
|
this.setXY(arr[0], arr[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toVector() {
|
||||||
|
return new Vector(this.x, this.y);
|
||||||
|
}
|
||||||
|
|
||||||
copy() {
|
copy() {
|
||||||
return new EndPoint(this.x, this.y);
|
return new EndPoint(this.x, this.y);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,16 +29,17 @@ export class Segment extends SketchObject {
|
||||||
this.a.collectParams(params);
|
this.a.collectParams(params);
|
||||||
this.b.collectParams(params);
|
this.b.collectParams(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
normalDistance(aim) {
|
normalDistance(aim) {
|
||||||
var x = aim.x;
|
return Segment.calcNormalDistance(aim, this.a, this.b);
|
||||||
var y = aim.y;
|
}
|
||||||
|
|
||||||
var ab = new Vector(this.b.x - this.a.x, this.b.y - this.a.y)
|
static calcNormalDistance(aim, segmentA, segmentB) {
|
||||||
var e = ab.normalize();
|
const ab = new Vector(segmentB.x - segmentA.x, segmentB.y - segmentA.y)
|
||||||
var a = new Vector(aim.x - this.a.x, aim.y - this.a.y);
|
const e = ab.normalize();
|
||||||
var b = e.multiply(a.dot(e));
|
const a = new Vector(aim.x - segmentA.x, aim.y - segmentA.y);
|
||||||
var n = a.minus(b);
|
const b = e.multiply(a.dot(e));
|
||||||
|
const n = a.minus(b);
|
||||||
|
|
||||||
//Check if vector b lays on the vector ab
|
//Check if vector b lays on the vector ab
|
||||||
if (b.length() > ab.length()) {
|
if (b.length() > ab.length()) {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,10 @@ export class SketchObject extends Shape {
|
||||||
this.layer = null;
|
this.layer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
normalDistance(aim, scale) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
addChild(child) {
|
addChild(child) {
|
||||||
this.children.push(child);
|
this.children.push(child);
|
||||||
child.parent = this;
|
child.parent = this;
|
||||||
|
|
|
||||||
|
|
@ -134,12 +134,12 @@ Viewer.prototype.search = function(x, y, buffer, deep, onlyPoints, filter) {
|
||||||
for (var j = 0; j < objs.length; j++) {
|
for (var j = 0; j < objs.length; j++) {
|
||||||
var l = unreachable + 1;
|
var l = unreachable + 1;
|
||||||
var before = pickResult.length;
|
var before = pickResult.length;
|
||||||
objs[j].accept(function(o) {
|
objs[j].accept((o) => {
|
||||||
if (!o.visible) return true;
|
if (!o.visible) return true;
|
||||||
if (onlyPoints && !isEndPoint(o)) {
|
if (onlyPoints && !isEndPoint(o)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
l = o.normalDistance(aim);
|
l = o.normalDistance(aim, this.scale);
|
||||||
if (l >= 0 && l <= buffer && !isFiltered(o)) {
|
if (l >= 0 && l <= buffer && !isFiltered(o)) {
|
||||||
pickResult.push(o);
|
pickResult.push(o);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue