mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-09 09:52:34 +01:00
Min length of line experiment
This commit is contained in:
parent
1d3a26d092
commit
bd06a25f7f
3 changed files with 86 additions and 4 deletions
|
|
@ -621,9 +621,14 @@ TCAD.TWO.Segment = function(a, b) {
|
|||
TCAD.TWO.utils.extend(TCAD.TWO.Segment, TCAD.TWO.SketchObject);
|
||||
|
||||
TCAD.TWO.Segment.prototype._class = 'TCAD.TWO.Segment';
|
||||
TCAD.TWO.Segment.MIN_LENGTH = 0.1; // 0.08; // if length < 0.08 canvas doesn't even draw a line
|
||||
|
||||
TCAD.TWO.Segment.prototype.validate = function() {
|
||||
return TCAD.math.distanceAB(this.a, this.b) > TCAD.TOLERANCE;
|
||||
return TCAD.math.distanceAB(this.a, this.b) > TCAD.TWO.Segment.MIN_LENGTH;
|
||||
};
|
||||
|
||||
TCAD.TWO.Segment.prototype.addFixingGeomConstraints = function(constrs) {
|
||||
constrs.push(new TCAD.TWO.Constraints.MinLength(this.a, this.b, TCAD.TWO.Segment.MIN_LENGTH));
|
||||
};
|
||||
|
||||
TCAD.TWO.Segment.prototype.collectParams = function(params) {
|
||||
|
|
@ -925,7 +930,7 @@ TCAD.TWO.DragTool.prototype.mousedown = function(e) {
|
|||
this.origin.x = e.offsetX;
|
||||
this.origin.y = e.offsetY;
|
||||
this.viewer.screenToModel2(e.offsetX, e.offsetY, this._point);
|
||||
this.prepareSolver();
|
||||
this.prepareSolver([]);
|
||||
};
|
||||
|
||||
TCAD.TWO.DragTool.prototype.mouseup = function(e) {
|
||||
|
|
@ -945,6 +950,22 @@ TCAD.TWO.DragTool.prototype.mousewheel = function(e) {
|
|||
TCAD.TWO.DragTool.prototype.solveRequest = function(rough) {
|
||||
this.solver.solve(rough, 1);
|
||||
this.solver.sync();
|
||||
if (false) {
|
||||
var fixingConstrs = [];
|
||||
this.viewer.accept(function(obj) {
|
||||
if (obj._class === 'TCAD.TWO.Segment') {
|
||||
if (!obj.validate()) {
|
||||
obj.addFixingGeomConstraints(fixingConstrs);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (fixingConstrs.length !== 0) {
|
||||
this.prepareSolver(fixingConstrs);
|
||||
this.solver.solve(rough, 1);
|
||||
this.solver.sync();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TCAD.TWO.DragTool.prototype.getParamsToLock = function() {
|
||||
|
|
@ -959,7 +980,7 @@ TCAD.TWO.DragTool.prototype.getParamsToLock = function() {
|
|||
return params;
|
||||
};
|
||||
|
||||
TCAD.TWO.DragTool.prototype.prepareSolver = function() {
|
||||
TCAD.TWO.DragTool.prototype.prepareSolver = function(extraConstraints) {
|
||||
var locked = this.getParamsToLock();
|
||||
this.lockedShifts = [];
|
||||
this.lockedValues = [];
|
||||
|
|
@ -967,7 +988,7 @@ TCAD.TWO.DragTool.prototype.prepareSolver = function() {
|
|||
this.lockedShifts[i] = this._point.x - locked[i].get();
|
||||
this.lockedShifts[i + 1] = this._point.y - locked[i + 1].get();
|
||||
}
|
||||
this.solver = this.viewer.parametricManager.prepare(locked);
|
||||
this.solver = this.viewer.parametricManager.prepare(locked, extraConstraints);
|
||||
//this.enableRecording();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ TCAD.constraints.create = function(name, params, values) {
|
|||
return new TCAD.constraints.Equal(params);
|
||||
case "equalsTo":
|
||||
return new TCAD.constraints.EqualsTo(params, values[0]);
|
||||
case "MinLength":
|
||||
return new TCAD.constraints.MinLength(params, values[0]);
|
||||
case "perpendicular":
|
||||
return new TCAD.constraints.Perpendicular(params);
|
||||
case "parallel":
|
||||
|
|
@ -42,6 +44,45 @@ TCAD.constraints.Equal = function(params) {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
TCAD.constraints.MinLength = function(params, distance) {
|
||||
|
||||
this.params = params;
|
||||
this.distance = distance;
|
||||
|
||||
var p1x = 0;
|
||||
var p1y = 1;
|
||||
var p2x = 2;
|
||||
var p2y = 3;
|
||||
|
||||
this.error = function() {
|
||||
var dx = params[p1x].get() - params[p2x].get();
|
||||
var dy = params[p1y].get() - params[p2y].get();
|
||||
var d = Math.sqrt(dx * dx + dy * dy);
|
||||
return d < this.distance ? (d - this.distance) : 0;
|
||||
};
|
||||
|
||||
this.gradient = function(out) {
|
||||
var dx = params[p1x].get() - params[p2x].get();
|
||||
var dy = params[p1y].get() - params[p2y].get();
|
||||
var d = Math.sqrt(dx * dx + dy * dy);
|
||||
if (d == 0) {
|
||||
d = 0.000001;
|
||||
}
|
||||
if (d >= this.distance) {
|
||||
out[p1x] = 0;
|
||||
out[p1y] = 0;
|
||||
out[p2x] = 0;
|
||||
out[p2y] = 0;
|
||||
}
|
||||
out[p1x] = dx / d;
|
||||
out[p1y] = dy / d;
|
||||
out[p2x] = -dx / d;
|
||||
out[p2y] = -dy / d;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** @constructor */
|
||||
TCAD.constraints.ConstantWrapper = function(constr, mask) {
|
||||
|
||||
|
|
|
|||
|
|
@ -877,6 +877,26 @@ TCAD.TWO.Constraints.P2LDistance.prototype.SettableFields = {'d' : "Enter the di
|
|||
|
||||
// ------------------------------------------------------------------------------------------------------------------ //
|
||||
|
||||
/** @constructor */
|
||||
TCAD.TWO.Constraints.MinLength = function(a, b, min) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.min = min;
|
||||
this.aux = true;
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.MinLength.prototype.NAME = 'MinLength';
|
||||
TCAD.TWO.Constraints.MinLength.prototype.UI_NAME = 'MinLength';
|
||||
|
||||
TCAD.TWO.Constraints.MinLength.prototype.getSolveData = function() {
|
||||
var params = [];
|
||||
this.a.collectParams(params);
|
||||
this.b.collectParams(params);
|
||||
return [[this.NAME, params, [this.min]]];
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------ //
|
||||
|
||||
/** @constructor */
|
||||
TCAD.TWO.Constraints.P2LDistanceV = function(p, l, d) {
|
||||
this.p = p;
|
||||
|
|
|
|||
Loading…
Reference in a new issue