mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-09 18:02:50 +01:00
add perpendicular and parallel constraints
This commit is contained in:
parent
a1ae738ad3
commit
c5d053635a
4 changed files with 62 additions and 44 deletions
|
|
@ -5,6 +5,8 @@ import cad.gcs.GlobalSolver;
|
|||
import cad.gcs.Param;
|
||||
import cad.gcs.Solver;
|
||||
import cad.gcs.constr.Equal;
|
||||
import cad.gcs.constr.Parallel;
|
||||
import cad.gcs.constr.Perpendicular;
|
||||
import gnu.trove.map.TIntObjectMap;
|
||||
import gnu.trove.map.hash.TIntObjectHashMap;
|
||||
import jdk.nashorn.internal.parser.JSONParser;
|
||||
|
|
@ -35,7 +37,7 @@ public class SolveServer {
|
|||
|
||||
ResourceHandler rh = new ResourceHandler();
|
||||
rh.setDirectoriesListed(true);
|
||||
rh.setResourceBase("/home/xibyte/Dropbox/project/cadit/web");
|
||||
rh.setResourceBase("/home/verastov/Dropbox/project/cadit/web");
|
||||
handlers.addHandler(rh);
|
||||
|
||||
server.setHandler(handlers);
|
||||
|
|
@ -106,6 +108,12 @@ class SolveHandler extends AbstractHandler {
|
|||
case "equal":
|
||||
constraints.add(new Equal(h.get(0), h.get(1)));
|
||||
break;
|
||||
case "perpendicular":
|
||||
constraints.add(new Perpendicular(h.get(0), h.get(1), h.get(2), h.get(3), h.get(4), h.get(5), h.get(6), h.get(7)));
|
||||
break;
|
||||
case "parallel":
|
||||
constraints.add(new Parallel(h.get(0), h.get(1), h.get(2), h.get(3), h.get(4), h.get(5), h.get(6), h.get(7)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -156,6 +156,11 @@ TCAD.TWO.Viewer.prototype.repaint = function() {
|
|||
}
|
||||
};
|
||||
|
||||
TCAD.TWO.Viewer.prototype.showExtent = function(x1, y1, x2, y2) {
|
||||
this.translate.x = x1;
|
||||
this.translate.y = y1;
|
||||
};
|
||||
|
||||
TCAD.TWO.Viewer.prototype.screenToModel2 = function(x, y, out) {
|
||||
|
||||
out.x = x;
|
||||
|
|
@ -169,7 +174,7 @@ TCAD.TWO.Viewer.prototype.screenToModel2 = function(x, y, out) {
|
|||
};
|
||||
|
||||
TCAD.TWO.Viewer.prototype.screenToModel = function(point) {
|
||||
var out = {x: 0, y: 0}
|
||||
var out = {x: 0, y: 0};
|
||||
this.screenToModel2(point.x, point.y, out);
|
||||
return out;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -51,6 +51,14 @@ TCAD.App2D = function() {
|
|||
app.viewer.parametricManager.horizontal(app.viewer.selected);
|
||||
},
|
||||
|
||||
parallel : function() {
|
||||
app.viewer.parametricManager.parallel(app.viewer.selected);
|
||||
},
|
||||
|
||||
perpendicular : function() {
|
||||
app.viewer.parametricManager.perpendicular(app.viewer.selected);
|
||||
},
|
||||
|
||||
P2LDistance : function() {
|
||||
app.viewer.parametricManager.p2lDistance(app.viewer.selected);
|
||||
}
|
||||
|
|
@ -60,6 +68,8 @@ TCAD.App2D = function() {
|
|||
actionsF.add(actions, 'coincident');
|
||||
actionsF.add(actions, 'vertical');
|
||||
actionsF.add(actions, 'horizontal');
|
||||
actionsF.add(actions, 'parallel');
|
||||
actionsF.add(actions, 'perpendicular');
|
||||
actionsF.add(actions, 'P2LDistance');
|
||||
actionsF.open();
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,21 @@ TCAD.TWO.ParametricManager.prototype._fetchTwoPoints = function(objs) {
|
|||
throw "Illegal Argument. Constraint requires 2 points or 1 line."
|
||||
}
|
||||
return points;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TCAD.TWO.ParametricManager.prototype._fetchTwoLines = function(objs) {
|
||||
var lines = [];
|
||||
for (var i = 0; i < objs.length; ++i) {
|
||||
if (objs[i]._class == 'TCAD.TWO.Segment') {
|
||||
lines.push(objs[i]);
|
||||
}
|
||||
}
|
||||
if (lines.length < 2) {
|
||||
throw "Illegal Argument. Constraint requires 2 lines."
|
||||
}
|
||||
return lines;
|
||||
};
|
||||
|
||||
TCAD.TWO.ParametricManager.prototype.vertical = function(objs) {
|
||||
var p = this._fetchTwoPoints(objs);
|
||||
|
|
@ -37,6 +51,16 @@ TCAD.TWO.ParametricManager.prototype.horizontal = function(objs) {
|
|||
this.add(new TCAD.TWO.Constraints.Equal(p[0]._y, p[1]._y));
|
||||
};
|
||||
|
||||
TCAD.TWO.ParametricManager.prototype.parallel = function(objs) {
|
||||
var lines = this._fetchTwoLines(objs);
|
||||
this.add(new TCAD.TWO.Constraints.Parallel(lines[0], lines[1]));
|
||||
};
|
||||
|
||||
TCAD.TWO.ParametricManager.prototype.perpendicular = function(objs) {
|
||||
var lines = this._fetchTwoLines(objs);
|
||||
this.add(new TCAD.TWO.Constraints.Perpendicular(lines[0], lines[1]));
|
||||
};
|
||||
|
||||
TCAD.TWO.ParametricManager.prototype.p2lDistance = function(objs) {
|
||||
var target = null;
|
||||
var segment = null;
|
||||
|
|
@ -74,8 +98,9 @@ TCAD.TWO.ParametricManager.prototype.solve = function(locked, onSolved) {
|
|||
var pdict = {};
|
||||
var refsCounter = 0;
|
||||
var params = [];
|
||||
var i;
|
||||
var data = {params : [], constraints: [], locked: []};
|
||||
for (var i = 0; i < this.system.length; ++i) {
|
||||
for (i = 0; i < this.system.length; ++i) {
|
||||
var sdata = this.system[i].getSolveData();
|
||||
var prefs = [];
|
||||
var constr = [sdata[0], prefs, sdata[2]];
|
||||
|
|
@ -84,7 +109,7 @@ TCAD.TWO.ParametricManager.prototype.solve = function(locked, onSolved) {
|
|||
var param = sdata[1][p];
|
||||
var pref = pdict[param.id];
|
||||
if (pref === undefined) {
|
||||
var pref = refsCounter++;
|
||||
pref = refsCounter++;
|
||||
data.params.push(param.get());
|
||||
params.push(param);
|
||||
pdict[param.id] = pref;
|
||||
|
|
@ -94,7 +119,7 @@ TCAD.TWO.ParametricManager.prototype.solve = function(locked, onSolved) {
|
|||
}
|
||||
|
||||
if (locked !== undefined) {
|
||||
for (var i = 0; i < locked.length; ++i) {
|
||||
for (i = 0; i < locked.length; ++i) {
|
||||
var lp = pdict[locked[i].id];
|
||||
if (lp !== undefined) {
|
||||
data.locked.push(lp);
|
||||
|
|
@ -105,7 +130,7 @@ TCAD.TWO.ParametricManager.prototype.solve = function(locked, onSolved) {
|
|||
var xhr = new XMLHttpRequest();
|
||||
xhr.withCredentials = true;
|
||||
var pm = this;
|
||||
var request = {reqId : this.REQUEST_COUNTER ++, system : data}
|
||||
var request = {reqId : this.REQUEST_COUNTER ++, system : data};
|
||||
xhr.onreadystatechange=function() {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
var response = JSON.parse(xhr.responseText);
|
||||
|
|
@ -120,7 +145,7 @@ TCAD.TWO.ParametricManager.prototype.solve = function(locked, onSolved) {
|
|||
}
|
||||
pm.viewer.refresh();
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.open("POST", "http://localhost:8080/solve", true);
|
||||
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||||
xhr.send(JSON.stringify(request));
|
||||
|
|
@ -136,7 +161,7 @@ TCAD.TWO.Constraints.Equal.prototype.getSolveData = function() {
|
|||
};
|
||||
|
||||
TCAD.TWO.Constraints.Parallel = function(l1, l2) {
|
||||
this.l1 = l2;
|
||||
this.l1 = l1;
|
||||
this.l2 = l2;
|
||||
};
|
||||
|
||||
|
|
@ -147,19 +172,8 @@ TCAD.TWO.Constraints.Parallel.prototype.getSolveData = function() {
|
|||
return ['parallel', params, []];
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.Parallel.prototype.setParams = function(params) {
|
||||
l1.a._x.set(params[0]);
|
||||
l1.a._y.set(params[1]);
|
||||
l1.b._x.set(params[2]);
|
||||
l1.b._y.set(params[3]);
|
||||
l2.a._x.set(params[4]);
|
||||
l2.a._y.set(params[5]);
|
||||
l2.b._x.set(params[6]);
|
||||
l2.b._y.set(params[7]);
|
||||
}
|
||||
|
||||
TCAD.TWO.Constraints.Perpendicular = function(l1, l2) {
|
||||
this.l1 = l2;
|
||||
this.l1 = l1;
|
||||
this.l2 = l2;
|
||||
};
|
||||
|
||||
|
|
@ -170,35 +184,16 @@ TCAD.TWO.Constraints.Perpendicular.prototype.getSolveData = function() {
|
|||
return ['perpendicular', params, []];
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.Perpendicular.prototype.setParams = function(params) {
|
||||
l1.a._x.set(params[0]);
|
||||
l1.a._y.set(params[1]);
|
||||
l1.b._x.set(params[2]);
|
||||
l1.b._y.set(params[3]);
|
||||
l2.a._x.set(params[4]);
|
||||
l2.a._y.set(params[5]);
|
||||
l2.b._x.set(params[6]);
|
||||
l2.b._y.set(params[7]);
|
||||
}
|
||||
|
||||
|
||||
TCAD.TWO.Constraints.P2LDistance = function(l, p, d) {
|
||||
this.l = l;
|
||||
this.p = p;
|
||||
this.d = d;
|
||||
this.functional = 'P2LDistance';
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.P2LDistance.prototype.getSolveData = function() {
|
||||
return ['P2LDistance', [p._x, p._y, l.a._x, l.a._y, l.b._x, l.b._y], [d]];
|
||||
var params = [];
|
||||
this.p.collectParams(params);
|
||||
this.l.collectParams(params);
|
||||
return ['P2LDistance', params, [this.d]];
|
||||
};
|
||||
|
||||
TCAD.TWO.Constraints.P2LDistance.prototype.setParams = function(params) {
|
||||
p._x .set(params[0]);
|
||||
p._y .set(params[1]);
|
||||
l.a._x.set(params[2]);
|
||||
l.a._y.set(params[3]);
|
||||
l.b._x.set(params[4]);
|
||||
l.b._y.set(params[5]);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue