mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-15 21:05:22 +01:00
fix update bounds
This commit is contained in:
parent
cff5e384d6
commit
b305388240
3 changed files with 67 additions and 55 deletions
|
|
@ -31,8 +31,13 @@ TCAD.TWO.Styles = {
|
|||
lineWidth : 1,
|
||||
strokeStyle : "#bcffc1",
|
||||
fillStyle : "#00FF00"
|
||||
}
|
||||
},
|
||||
|
||||
BOUNDS : {
|
||||
lineWidth : 2,
|
||||
strokeStyle : "#fff5c3",
|
||||
fillStyle : "#000000"
|
||||
}
|
||||
};
|
||||
|
||||
TCAD.TWO.utils = {};
|
||||
|
|
|
|||
|
|
@ -185,6 +185,58 @@ TCAD.IO.prototype._serializeSketch = function() {
|
|||
return sketch;
|
||||
};
|
||||
|
||||
TCAD.IO.prototype.updateBoundary = function (boundary) {
|
||||
if (this.boundaryLayer === undefined) {
|
||||
this.boundaryLayer = new TCAD.TWO.Layer("bounds", TCAD.TWO.Styles.BOUNDS);
|
||||
this.viewer.layers.push(this.boundaryLayer);
|
||||
}
|
||||
var edges = [];
|
||||
var bbox = [Number.MAX_VALUE, Number.MAX_VALUE, - Number.MAX_VALUE, - Number.MAX_VALUE];
|
||||
var flattenPolygon = function(points) {
|
||||
var n = points.length;
|
||||
for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
|
||||
edges.push([points[p].x, points[p].y, points[q].x, points[q].y]);
|
||||
bbox[0] = Math.min(bbox[0], points[p].x);
|
||||
bbox[1] = Math.min(bbox[1], points[p].y);
|
||||
bbox[2] = Math.max(bbox[2], points[q].x);
|
||||
bbox[3] = Math.max(bbox[3], points[q].y);
|
||||
}
|
||||
};
|
||||
|
||||
flattenPolygon(boundary.shell);
|
||||
for (var i = 0; i < boundary.holes.length; ++i ) {
|
||||
flattenPolygon(boundary.holes[i]);
|
||||
}
|
||||
// if (bbox[0] < Number.MAX_VALUE && bbox[1] < Number.MAX_VALUE && -bbox[2] < Number.MAX_VALUE && -bbox[3] < Number.MAX_VALUE) {
|
||||
// this.viewer.showBounds(bbox[0], bbox[1], bbox[2], bbox[3])
|
||||
// }
|
||||
|
||||
for (var l = 0; l < this.viewer.layers.length; ++l) {
|
||||
var layer = this.viewer.layers[l];
|
||||
for (var i = 0; i < layer.objects.length; ++i) {
|
||||
var obj = layer.objects[i];
|
||||
if (obj.edge !== undefined) {
|
||||
var edge = edges[obj.edge];
|
||||
if (edge !== undefined && edge != null) {
|
||||
obj.a.x = edge[0];
|
||||
obj.a.y = edge[1];
|
||||
obj.b.x = edge[2];
|
||||
obj.b.y = edge[3];
|
||||
edges[obj.edge] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < edges.length; ++i ) {
|
||||
var edge = edges[i];
|
||||
if (edge != null) {
|
||||
var seg = this.viewer.addSegment(edge[0], edge[1], edge[2], edge[3], this.boundaryLayer);
|
||||
seg.accept(function(o){o.aux = true; return true;});
|
||||
seg.edge = i;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TCAD.IO.prototype.parseConstr = function (c, index) {
|
||||
function find(id) {
|
||||
var p = index[id];
|
||||
|
|
@ -206,3 +258,12 @@ TCAD.IO.prototype.serializeConstr = function (c) {
|
|||
return c.serialize();
|
||||
};
|
||||
|
||||
TCAD.IO.prototype.svgExport = function (c) {
|
||||
|
||||
//
|
||||
//<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
|
||||
//</svg>
|
||||
//
|
||||
//var out = "<svg width="100" height="100">";
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,11 +1,6 @@
|
|||
|
||||
var magic_k = 500;
|
||||
|
||||
TCAD.App2D = function() {
|
||||
|
||||
this.viewer = new TCAD.TWO.Viewer(document.getElementById('viewer'));
|
||||
this.boundaryLayer = new TCAD.TWO.Layer("default", TCAD.TWO.Styles.DEFAULT);
|
||||
this.viewer.layers.push(this.boundaryLayer);
|
||||
var app = this;
|
||||
|
||||
this.actions = {};
|
||||
|
|
@ -149,55 +144,6 @@ TCAD.App2D.prototype.loadFromLocalStorage = function() {
|
|||
this.viewer.repaint();
|
||||
};
|
||||
|
||||
TCAD.App2D.prototype.updateBoundary = function (boundary) {
|
||||
|
||||
var edges = [];
|
||||
var bbox = [Number.MAX_VALUE, Number.MAX_VALUE, - Number.MAX_VALUE, - Number.MAX_VALUE];
|
||||
var flattenPolygon = function(points) {
|
||||
var n = points.length;
|
||||
for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
|
||||
edges.push([points[p].x, points[p].y, points[q].x, points[q].y]);
|
||||
bbox[0] = Math.min(bbox[0], points[p].x);
|
||||
bbox[1] = Math.min(bbox[1], points[p].y);
|
||||
bbox[2] = Math.max(bbox[2], points[q].x);
|
||||
bbox[3] = Math.max(bbox[3], points[q].y);
|
||||
}
|
||||
};
|
||||
|
||||
flattenPolygon(boundary.shell);
|
||||
for (var i = 0; i < boundary.holes.length; ++i ) {
|
||||
flattenPolygon(boundary.holes[i]);
|
||||
}
|
||||
// if (bbox[0] < Number.MAX_VALUE && bbox[1] < Number.MAX_VALUE && -bbox[2] < Number.MAX_VALUE && -bbox[3] < Number.MAX_VALUE) {
|
||||
// this.viewer.showBounds(bbox[0], bbox[1], bbox[2], bbox[3])
|
||||
// }
|
||||
|
||||
for (var l = 0; l < this.viewer.layers.length; ++l) {
|
||||
var layer = this.viewer.layers[l];
|
||||
for (var i = 0; i < layer.objects.length; ++i) {
|
||||
var obj = layer.objects[i];
|
||||
if (obj.edge !== undefined) {
|
||||
var edge = edges[obj.edge];
|
||||
if (edge !== undefined && edge != null) {
|
||||
obj.a.x = edge[0];
|
||||
obj.a.y = edge[1];
|
||||
obj.b.x = edge[2];
|
||||
obj.b.y = edge[3];
|
||||
edges[obj.edge] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < edges.length; ++i ) {
|
||||
var edge = edges[i];
|
||||
if (edge != null) {
|
||||
var seg = this.viewer.addSegment(edge[0], edge[1], edge[2], edge[3], this.boundaryLayer);
|
||||
seg.accept(function(o){o.aux = true; return true;});
|
||||
seg.edge = i;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TCAD.App2D.prototype.getSketchId = function() {
|
||||
var id = window.location.hash.substring(1);
|
||||
if (!id) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue