mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-06 16:33:15 +01:00
some state
This commit is contained in:
parent
8821e60616
commit
2cc244d340
6 changed files with 161 additions and 18 deletions
|
|
@ -17,6 +17,8 @@ TCAD.utils.createSquare = function(width) {
|
||||||
|
|
||||||
TCAD.utils.createBox = function(width) {
|
TCAD.utils.createBox = function(width) {
|
||||||
var square = TCAD.utils.createSquare(width);
|
var square = TCAD.utils.createSquare(width);
|
||||||
|
var rot = TCAD.math.rotateMatrix(3/4, TCAD.math.AXIS.Z, TCAD.math.ORIGIN);
|
||||||
|
square.eachVertex(function(path, i) { rot._apply(path[i]) } )
|
||||||
return TCAD.geom.extrude(square, square.normal.multiply(width));
|
return TCAD.geom.extrude(square, square.normal.multiply(width));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -521,6 +523,17 @@ TCAD.Polygon.prototype.triangulate = function() {
|
||||||
// return THREE.Shape.utils.triangulateShape( f2d.shell, f2d.holes );
|
// return THREE.Shape.utils.triangulateShape( f2d.shell, f2d.holes );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TCAD.Polygon.prototype.eachVertex = function(handler) {
|
||||||
|
var i, h;
|
||||||
|
for (i = 0; i < this.shell.length; ++i) {
|
||||||
|
if (handler(this.shell, i) === true) return;
|
||||||
|
}
|
||||||
|
for (h = 0; h < this.holes.length; ++h) {
|
||||||
|
for (i = 0; i < this.holes[h].length; ++i) {
|
||||||
|
if (handler(this.holes[h], i) === true) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
TCAD.Sketch = function() {
|
TCAD.Sketch = function() {
|
||||||
this.group = new THREE.Object3D();
|
this.group = new THREE.Object3D();
|
||||||
|
|
|
||||||
|
|
@ -27,3 +27,58 @@ TCAD.math.distance = function(x1, y1, x2, y2) {
|
||||||
var dy = y1 - y2;
|
var dy = y1 - y2;
|
||||||
return Math.sqrt(dx * dx + dy * dy);
|
return Math.sqrt(dx * dx + dy * dy);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TCAD.math.ORIGIN = new TCAD.Vector(0, 0, 0);
|
||||||
|
|
||||||
|
TCAD.math.AXIS = {
|
||||||
|
X : new TCAD.Vector(1, 0, 0),
|
||||||
|
Y : new TCAD.Vector(0, 1, 0),
|
||||||
|
Z : new TCAD.Vector(0, 0, 1)
|
||||||
|
};
|
||||||
|
|
||||||
|
TCAD.math.rotateMatrix = function(angle, axis, pivot) {
|
||||||
|
var sin = Math.sin(angle);
|
||||||
|
var cos = Math.cos(angle);
|
||||||
|
var axisX, axisY, axisZ;
|
||||||
|
var m = new TCAD.Matrix();
|
||||||
|
|
||||||
|
var AXIS = TCAD.math.AXIS;
|
||||||
|
|
||||||
|
if (axis === AXIS.X || axis === AXIS.Y || axis === AXIS.Z) {
|
||||||
|
axisX = axis.x;
|
||||||
|
axisY = axis.y;
|
||||||
|
axisZ = axis.z;
|
||||||
|
} else {
|
||||||
|
// normalize
|
||||||
|
var mag = axis.length();
|
||||||
|
|
||||||
|
if (mag == 0.0) {
|
||||||
|
return m;
|
||||||
|
} else {
|
||||||
|
axisX = axis.x / mag;
|
||||||
|
axisY = axis.y / mag;
|
||||||
|
axisZ = axis.z / mag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var px = pivot.x;
|
||||||
|
var py = pivot.y;
|
||||||
|
var pz = pivot.z;
|
||||||
|
|
||||||
|
m.mxx = cos + axisX * axisX * (1 - cos);
|
||||||
|
m.mxy = axisX * axisY * (1 - cos) - axisZ * sin;
|
||||||
|
m.mxz = axisX * axisZ * (1 - cos) + axisY * sin;
|
||||||
|
|
||||||
|
m.tx = px * (1 - m.mxx) - py * m.mxy - pz * m.mxz;
|
||||||
|
|
||||||
|
m.myx = axisY * axisX * (1 - cos) + axisZ * sin;
|
||||||
|
m.myy = cos + axisY * axisY * (1 - cos);
|
||||||
|
m.myz = axisY * axisZ * (1 - cos) - axisX * sin;
|
||||||
|
m.ty = py * (1 - m.myy) - px * m.myx - pz * m.myz;
|
||||||
|
|
||||||
|
m.mzx = axisZ * axisX * (1 - cos) - axisY * sin;
|
||||||
|
m.mzy = axisZ * axisY * (1 - cos) + axisX * sin;
|
||||||
|
m.mzz = cos + axisZ * axisZ * (1 - cos);
|
||||||
|
m.tz = pz * (1 - m.mzz) - px * m.mzx - py * m.mzy;
|
||||||
|
return m;
|
||||||
|
};
|
||||||
|
|
@ -221,12 +221,19 @@ TCAD.Matrix.prototype.combine = function(transform) {
|
||||||
};
|
};
|
||||||
|
|
||||||
TCAD.Matrix.prototype.apply = function(vector) {
|
TCAD.Matrix.prototype.apply = function(vector) {
|
||||||
|
return this.__apply(vector, new TCAD.Vector())
|
||||||
|
};
|
||||||
|
|
||||||
|
TCAD.Matrix.prototype._apply = function(vector) {
|
||||||
|
return this.__apply(vector, vector);
|
||||||
|
};
|
||||||
|
|
||||||
|
TCAD.Matrix.prototype.__apply = function(vector, out) {
|
||||||
var x = vector.x;
|
var x = vector.x;
|
||||||
var y = vector.y;
|
var y = vector.y;
|
||||||
var z = vector.z;
|
var z = vector.z;
|
||||||
return new TCAD.Vector(
|
return out.set(
|
||||||
this.mxx * x + this.mxy * y + this.mxz * z + this.tx,
|
this.mxx * x + this.mxy * y + this.mxz * z + this.tx,
|
||||||
this.myx * x + this.myy * y + this.myz * z + this.ty,
|
this.myx * x + this.myy * y + this.myz * z + this.ty,
|
||||||
this.mzx * x + this.mzy * y + this.mzz * z + this.tz);
|
this.mzx * x + this.mzy * y + this.mzz * z + this.tz);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -184,12 +184,69 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepare(__cgsPolygons) {
|
function prepare(__cgsPolygons) {
|
||||||
|
var counter = 0;
|
||||||
var polygons = __cgsPolygons.map(function(cp) {
|
var polygons = __cgsPolygons.map(function(cp) {
|
||||||
|
if (cp.plane.___ID === undefined) {
|
||||||
|
cp.plane.___ID = ++ counter;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
vertices : cp.vertices.map(function(cv) {return roundV(vec(cv.pos))}),
|
vertices : cp.vertices.map(function(cv) {return vec(cv.pos)}),
|
||||||
normal : roundV(vec(cp.plane.normal))
|
normal : vec(cp.plane.normal),
|
||||||
|
w : cp.plane.w
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var points = [];
|
||||||
|
|
||||||
|
for (var pi = 0; pi < polygons.length; ++pi) {
|
||||||
|
var poly = polygons[pi];
|
||||||
|
for (var vi = 0; vi < poly.vertices.length; ++vi) {
|
||||||
|
var vert = poly.vertices[vi];
|
||||||
|
points.push(vert);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var tol = 1E-6;
|
||||||
|
for (var i = 0; i < points.length; i++) {
|
||||||
|
var a = points[i];
|
||||||
|
for (var j = i + 1; j < points.length; j++) {
|
||||||
|
var b = points[j];
|
||||||
|
if (
|
||||||
|
TCAD.utils.areEqual(a.x, b.x, tol) &&
|
||||||
|
TCAD.utils.areEqual(a.y, b.y, tol) &&
|
||||||
|
TCAD.utils.areEqual(a.z, b.z, tol)
|
||||||
|
) {
|
||||||
|
b.setV(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var i = 0; i < polygons.length; i++) {
|
||||||
|
var a = polygons[i];
|
||||||
|
for (var j = i + 1; j < polygons.length; j++) {
|
||||||
|
var b = polygons[j];
|
||||||
|
if (
|
||||||
|
TCAD.utils.areEqual(a.normal.x, b.normal.x, tol) &&
|
||||||
|
TCAD.utils.areEqual(a.normal.y, b.normal.y, tol) &&
|
||||||
|
TCAD.utils.areEqual(a.normal.z, b.normal.z, tol)
|
||||||
|
) {
|
||||||
|
b.normal.setV(a.normal);
|
||||||
|
}
|
||||||
|
if (TCAD.utils.areEqual(a.w, b.w, tol)) {
|
||||||
|
b.w = a.w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// for (var i = 0; i < points.length; i++) {
|
||||||
|
// roundV(points[i]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
for (var i = 0; i < points.length; i++) {
|
||||||
|
// console.log(points[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//polygons = polygons.filter(function(e){return e.normal.equals(new TCAD.Vector(-1,0,0)) });
|
//polygons = polygons.filter(function(e){return e.normal.equals(new TCAD.Vector(-1,0,0)) });
|
||||||
return polygons;
|
return polygons;
|
||||||
}
|
}
|
||||||
|
|
@ -358,12 +415,12 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) {
|
||||||
var paths = [];
|
var paths = [];
|
||||||
var path;
|
var path;
|
||||||
var visited = {};
|
var visited = {};
|
||||||
function nextUnvisitedNormal(p, key) {
|
function nextUnvisitedPolygon(p, key) {
|
||||||
var polygons = pointToPoly[key];
|
var polygons = pointToPoly[key];
|
||||||
for (var pi = 0; pi < polygons.length; pi++) {
|
for (var pi = 0; pi < polygons.length; pi++) {
|
||||||
var poly = polygons[pi];
|
var poly = polygons[pi];
|
||||||
var nkey = pnkey(p, poly.normal);
|
var nkey = pnkey(p, poly.normal);
|
||||||
if (visited[nkey] === undefined) return poly.normal;
|
if (visited[nkey] === undefined) return poly;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -399,10 +456,12 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) {
|
||||||
for (var i = 0; i < points.length; i++) {
|
for (var i = 0; i < points.length; i++) {
|
||||||
var point = points[i];
|
var point = points[i];
|
||||||
key = pkey(point);
|
key = pkey(point);
|
||||||
var normal = nextUnvisitedNormal(point, key);
|
var unvPoly = nextUnvisitedPolygon(point, key);
|
||||||
if (normal == null) {
|
if (unvPoly == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
var normal = unvPoly.normal;
|
||||||
|
var w = unvPoly.w;
|
||||||
var normalKey = pkey(normal);
|
var normalKey = pkey(normal);
|
||||||
|
|
||||||
pCurr = point;
|
pCurr = point;
|
||||||
|
|
@ -450,7 +509,8 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) {
|
||||||
if (path.length > 2) {
|
if (path.length > 2) {
|
||||||
paths.push({
|
paths.push({
|
||||||
vertices : path,
|
vertices : path,
|
||||||
normal : normal
|
normal : normal,
|
||||||
|
w : w
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
console.log("-----")
|
console.log("-----")
|
||||||
|
|
@ -502,7 +562,8 @@ TCAD.craft._mergeCSGPolygonsTester = function(data) {
|
||||||
vertices : points.map(function(e) {
|
vertices : points.map(function(e) {
|
||||||
return {pos: new TCAD.Vector(e[0], e[1], 0)};
|
return {pos: new TCAD.Vector(e[0], e[1], 0)};
|
||||||
}),
|
}),
|
||||||
plane: {normal : new TCAD.Vector(0,0,1)}
|
plane: {normal : new TCAD.Vector(0,0,1), w : 0}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -611,7 +672,8 @@ TCAD.craft.cut = function(app, face, faces, height) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
vertices : path.vertices.map(function(v) {return tr.apply(v);}),
|
vertices : path.vertices.map(function(v) {return tr.apply(v);}),
|
||||||
normal : path.normal
|
normal : path.normal,
|
||||||
|
w : path.w
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -623,13 +685,14 @@ TCAD.craft.cut = function(app, face, faces, height) {
|
||||||
|
|
||||||
for (var pi = 0; pi < paths.length; ++pi) {
|
for (var pi = 0; pi < paths.length; ++pi) {
|
||||||
var path = paths[pi];
|
var path = paths[pi];
|
||||||
var depth = paths3D[pi].vertices[0].dot(paths3D[pi].normal);
|
// var depth = paths3D[pi].vertices[0].dot(paths3D[pi].normal);
|
||||||
for (var piTest = 0; piTest < paths.length; ++piTest) {
|
for (var piTest = 0; piTest < paths.length; ++piTest) {
|
||||||
var pathTest = paths[piTest];
|
var pathTest = paths[piTest];
|
||||||
if (piTest === pi) continue;
|
if (piTest === pi) continue;
|
||||||
if (!pathTest.normal.equals(path.normal)) continue;
|
if (!pathTest.normal.equals(path.normal)) continue;
|
||||||
var depthTest = paths3D[piTest].vertices[0].dot(paths3D[piTest].normal);
|
// var depthTest = paths3D[piTest].vertices[0].dot(paths3D[piTest].normal);
|
||||||
if (!TCAD.utils.equal(depthTest, depth)) continue;
|
// if (!TCAD.utils.equal(depthTest, depth)) continue;
|
||||||
|
if (!TCAD.utils.areEqual(path.w, pathTest.w, 10E-6)) continue;
|
||||||
|
|
||||||
if (pInP(pathTest.vertices, path.vertices)) {
|
if (pInP(pathTest.vertices, path.vertices)) {
|
||||||
index[piTest].push(pi);
|
index[piTest].push(pi);
|
||||||
|
|
@ -716,5 +779,9 @@ TCAD.Craft.prototype.modify = function(solid, modification) {
|
||||||
if (faces == null) return;
|
if (faces == null) return;
|
||||||
this.app.viewer.scene.remove( solid.meshObject );
|
this.app.viewer.scene.remove( solid.meshObject );
|
||||||
this.app.viewer.scene.add(TCAD.utils.createSolidMesh(faces));
|
this.app.viewer.scene.add(TCAD.utils.createSolidMesh(faces));
|
||||||
|
|
||||||
|
//REMOVE IT
|
||||||
|
this.app._refreshSketches();
|
||||||
|
|
||||||
this.app.viewer.render();
|
this.app.viewer.render();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
<script src="app/viewer.js"></script>
|
<script src="app/viewer.js"></script>
|
||||||
<script src="app/engine.js"></script>
|
<script src="app/engine.js"></script>
|
||||||
<script src="app/vector.js"></script>
|
<script src="app/vector.js"></script>
|
||||||
|
<script src="app/math/math.js"></script>
|
||||||
<script src="app/workbench.js"></script>
|
<script src="app/workbench.js"></script>
|
||||||
<script src="app/math/graph.js"></script>
|
<script src="app/math/graph.js"></script>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -190,6 +190,7 @@
|
||||||
<script src="app/sketch/segment.js"></script>
|
<script src="app/sketch/segment.js"></script>
|
||||||
<script src="app/sketch/dim.js"></script>
|
<script src="app/sketch/dim.js"></script>
|
||||||
|
|
||||||
|
<script src="app/vector.js"></script>
|
||||||
<script src="app/math/math.js"></script>
|
<script src="app/math/math.js"></script>
|
||||||
<script src="app/math/matrix.js"></script>
|
<script src="app/math/matrix.js"></script>
|
||||||
<script src="app/math/optim.js"></script>
|
<script src="app/math/optim.js"></script>
|
||||||
|
|
@ -202,7 +203,6 @@
|
||||||
<script src="app/parametric.js"></script>
|
<script src="app/parametric.js"></script>
|
||||||
<script src="app/fetchers.js"></script>
|
<script src="app/fetchers.js"></script>
|
||||||
<script src="app/engine.js"></script>
|
<script src="app/engine.js"></script>
|
||||||
<script src="app/vector.js"></script>
|
|
||||||
<script src="app/main2d.js"></script>
|
<script src="app/main2d.js"></script>
|
||||||
<script src="app/workbench.js"></script>
|
<script src="app/workbench.js"></script>
|
||||||
<script src="app/ui.js"></script>
|
<script src="app/ui.js"></script>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue