UNPKG

awv3

Version:
58 lines (51 loc) 2.76 kB
import { getTangent } from '../geomutils'; import ConstraintAdder from './adder'; import { incidence, horizontality, tangency, verticality } from './type'; export default class ConstraintGenerator { constructor(sketch) { this.sketch = sketch; this.adder = new ConstraintAdder(this.sketch); this.generate = { horizontality: true, verticality: true, pointCoincidence: true, tangency: true }; } // generate implied constraints which are somehow related to "relatedTo" object generateImpliedConstraints(relatedTo) { for (let baseObj of relatedTo.descendants) { if (baseObj.isLine()) { // generate implied horizontality or verticality const vec = getTangent(baseObj.geomParams); // unit vector or zero if (vec.lengthSq() === 0) continue; if (Math.abs(vec.y) <= ConstraintGenerator.angularTolerance) this.adder.add(horizontality, [baseObj], { fixed: true, noVisualize: true }); if (Math.abs(vec.x) <= ConstraintGenerator.angularTolerance) this.adder.add(verticality, [baseObj], { fixed: true, noVisualize: true }); } if (!baseObj.isPoint()) continue; const basePar = baseObj.parent; for (let otherObj of this.sketch.descendants) { // generate implied incidence of points if (!otherObj.isPoint()) continue; const basePos = baseObj.geomParams.start, otherPos = otherObj.geomParams.start; if (basePos.distanceTo(otherPos) > ConstraintGenerator.linearTolerance) continue; this.adder.add(incidence, [baseObj, otherObj], { noVisualize: true }); const otherPar = otherObj.parent; // generate implied tangency of touching endpoints if (!baseObj.isSubPoint() || !otherObj.isSubPoint() || basePar.id === otherPar.id) continue; if (baseObj.isCenterPoint() || otherObj.isCenterPoint()) continue; const baseDir = getTangent(basePar.geomParams, basePos), otherDir = getTangent(otherPar.geomParams, otherPos); if (baseDir.lengthSq() === 0 || otherDir.lengthSq() === 0) continue; if (Math.abs(baseDir.cross(otherDir).z) > ConstraintGenerator.angularTolerance) continue; const type = basePar.isLine() && otherPar.isLine() ? incidence : tangency; this.adder.add(type, [basePar, otherPar], { noVisualize: true }); } } return this.adder.commit(); } static angularTolerance = 1e-6; static linearTolerance = 1e-6; }