From bd06a25f7f7ea305c31e24184c1efdca91c5c5ca Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Thu, 10 Mar 2016 23:47:40 -0800 Subject: [PATCH] Min length of line experiment --- web/app/sketcher/canvas.js | 29 +++++++++++++++--- web/app/sketcher/constr/constraints.js | 41 ++++++++++++++++++++++++++ web/app/sketcher/parametric.js | 20 +++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/web/app/sketcher/canvas.js b/web/app/sketcher/canvas.js index 272a43e8..cf381119 100644 --- a/web/app/sketcher/canvas.js +++ b/web/app/sketcher/canvas.js @@ -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(); }; diff --git a/web/app/sketcher/constr/constraints.js b/web/app/sketcher/constr/constraints.js index 08dbd69e..f3ff76b6 100644 --- a/web/app/sketcher/constr/constraints.js +++ b/web/app/sketcher/constr/constraints.js @@ -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) { diff --git a/web/app/sketcher/parametric.js b/web/app/sketcher/parametric.js index 903b1d4c..8d751b71 100644 --- a/web/app/sketcher/parametric.js +++ b/web/app/sketcher/parametric.js @@ -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;