mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-13 11:54:01 +01:00
Point to Line and Poinr to Point distance constraints
This commit is contained in:
parent
f0d36b0a82
commit
6c4c72cc56
5 changed files with 96 additions and 6 deletions
|
|
@ -5,7 +5,7 @@ export default class CheckboxControl extends React.Component {
|
|||
render() {
|
||||
let {onChange, value} = this.props;
|
||||
return <input type='checkbox'
|
||||
defaultValue={value}
|
||||
checked={value}
|
||||
onChange={e => onChange(e.target.checked)} />
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -263,6 +263,48 @@ export default [
|
|||
}
|
||||
},
|
||||
|
||||
{
|
||||
id: 'DistancePL',
|
||||
shortName: 'Point to Line Distance',
|
||||
description: 'Distance between Point and Line',
|
||||
selectionMatcher: (selection, sortedByType) => matchTypes(sortedByType, EndPoint, 1, Segment, 1),
|
||||
|
||||
invoke: ctx => {
|
||||
const {viewer} = ctx;
|
||||
|
||||
const [pt, seg] = sortSelectionByType(viewer.selected);
|
||||
|
||||
const constr = new AlgNumConstraint(ConstraintDefinitions.DistancePL, [pt, seg]);
|
||||
constr.initConstants();
|
||||
|
||||
editConstraint(ctx, constr, () => {
|
||||
const pm = viewer.parametricManager;
|
||||
pm.add(constr);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
id: 'DistancePP',
|
||||
shortName: 'Two Point Distance',
|
||||
description: 'Distance between two Points',
|
||||
selectionMatcher: (selection, sortedByType) => matchTypes(sortedByType, EndPoint, 2),
|
||||
|
||||
invoke: ctx => {
|
||||
const {viewer} = ctx;
|
||||
|
||||
const [p1, p2] = sortSelectionByType(viewer.selected);
|
||||
|
||||
const constr = new AlgNumConstraint(ConstraintDefinitions.DistancePP, [p1, p2]);
|
||||
constr.initConstants();
|
||||
|
||||
editConstraint(ctx, constr, () => {
|
||||
const pm = viewer.parametricManager;
|
||||
pm.add(constr);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
id: 'Lock',
|
||||
shortName: 'Lock',
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import Stack from "ui/components/Stack";
|
|||
import ButtonGroup from "ui/components/controls/ButtonGroup";
|
||||
import Button from "ui/components/controls/Button";
|
||||
import {useStream} from "../../../../modules/ui/effects";
|
||||
import CheckboxControl from "../../../../modules/ui/components/controls/CheckboxControl";
|
||||
|
||||
export function ConstraintEditor() {
|
||||
|
||||
|
|
@ -46,6 +47,8 @@ export function ConstraintEditor() {
|
|||
const val = values[name];
|
||||
if (def.type === 'number') {
|
||||
return <NumberControl value={val} onChange={value => setValue(name, value)}/>
|
||||
} else if (def.type === 'boolean') {
|
||||
return <CheckboxControl value={val} onChange={value => setValue(name, value)}/>
|
||||
} else {
|
||||
return <span>{val}</span>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,6 +131,51 @@ export const ConstraintDefinitions = {
|
|||
|
||||
},
|
||||
|
||||
DistancePL: {
|
||||
id: 'DistancePL',
|
||||
name: 'Distance Between Point And Line',
|
||||
constants: {
|
||||
distance: {
|
||||
type: 'number',
|
||||
description: 'the distance between two points',
|
||||
initialValue: ([p, l]) => {
|
||||
return Math.abs(l.nx * p.x + l.ny* p.y - l.nx * l.a.x - l.ny * l.a.y);
|
||||
},
|
||||
},
|
||||
inverted: {
|
||||
type: 'boolean',
|
||||
description: 'whether constraint is being calculated on opposite side of the line',
|
||||
initialValue: ([p, l]) => {
|
||||
return l.nx * p.x + l.ny* p.y - l.nx * l.a.x - l.ny * l.a.y < 0;
|
||||
},
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
defineParamsScope: ([p, l], callback) => {
|
||||
p.visitParams(callback);
|
||||
callback(l.params.ang);
|
||||
l.a.visitParams(callback);
|
||||
},
|
||||
|
||||
collectPolynomials: (polynomials, [x, y, ang, ax, ay], {distance, inverted}) => {
|
||||
polynomials.push(new Polynomial( - (inverted ? -1:1) * distance )
|
||||
.monomial(-1)
|
||||
.term(x, POW_1_FN)
|
||||
.term(ang, SIN_FN)
|
||||
.monomial(1)
|
||||
.term(y, POW_1_FN)
|
||||
.term(ang, COS_FN)
|
||||
.monomial(1)
|
||||
.term(ax, POW_1_FN)
|
||||
.term(ang, SIN_FN)
|
||||
.monomial(-1)
|
||||
.term(ay, POW_1_FN)
|
||||
.term(ang, COS_FN));
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
Angle: {
|
||||
id: 'Angle',
|
||||
name: 'Absolute Line Angle',
|
||||
|
|
@ -490,12 +535,12 @@ export const ConstraintDefinitions = {
|
|||
|
||||
function tangentLCPolynomial(ang, ax, ay, cx, cy, r, inverted) {
|
||||
return new Polynomial(0)
|
||||
.monomial(1)
|
||||
.monomial(-1)
|
||||
.term(cx, POW_1_FN)
|
||||
.term(ang, COS_FN)
|
||||
.term(ang, SIN_FN)
|
||||
.monomial(1)
|
||||
.term(cy, POW_1_FN)
|
||||
.term(ang, SIN_FN)
|
||||
.term(ang, COS_FN)
|
||||
.monomial(1)
|
||||
.term(ax, POW_1_FN)
|
||||
.term(ang, SIN_FN)
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ function App2D() {
|
|||
});
|
||||
|
||||
this.registerAction('P2LDistanceConstraint', "Distance Between Point and Line", function () {
|
||||
app.viewer.parametricManager.p2lDistance(app.viewer.selected, prompt);
|
||||
getActionIfAvailable('DistancePL', app.viewer.selected, action => action.invoke(app.context));
|
||||
});
|
||||
|
||||
this.registerAction('mirrorConstraint', "Mirror Constraint", function () {
|
||||
|
|
@ -226,7 +226,7 @@ function App2D() {
|
|||
});
|
||||
|
||||
this.registerAction('P2PDistanceConstraint', "Distance Between two Points", function () {
|
||||
app.viewer.parametricManager.p2pDistance(app.viewer.selected, prompt);
|
||||
getActionIfAvailable('DistancePP', app.viewer.selected, action => action.invoke(app.context));
|
||||
});
|
||||
|
||||
this.registerAction('RadiusConstraint', "Radius Constraint", function () {
|
||||
|
|
|
|||
Loading…
Reference in a new issue