From 2cc244d340315caa03fa11e3ab1e56553abb0a31 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Thu, 22 Jan 2015 00:07:02 -0800 Subject: [PATCH] some state --- web/app/engine.js | 13 +++++++ web/app/math/math.js | 55 ++++++++++++++++++++++++++ web/app/vector.js | 17 ++++++--- web/app/workbench.js | 91 ++++++++++++++++++++++++++++++++++++++------ web/index.html | 1 + web/sketcher.html | 2 +- 6 files changed, 161 insertions(+), 18 deletions(-) diff --git a/web/app/engine.js b/web/app/engine.js index 52072ce5..02f89be5 100644 --- a/web/app/engine.js +++ b/web/app/engine.js @@ -17,6 +17,8 @@ TCAD.utils.createSquare = function(width) { TCAD.utils.createBox = function(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)); }; @@ -521,6 +523,17 @@ TCAD.Polygon.prototype.triangulate = function() { // 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() { this.group = new THREE.Object3D(); diff --git a/web/app/math/math.js b/web/app/math/math.js index d599d911..682c4cfc 100644 --- a/web/app/math/math.js +++ b/web/app/math/math.js @@ -26,4 +26,59 @@ TCAD.math.distance = function(x1, y1, x2, y2) { var dx = x1 - x2; var dy = y1 - y2; 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; }; \ No newline at end of file diff --git a/web/app/vector.js b/web/app/vector.js index 600e4092..0fefd2ae 100644 --- a/web/app/vector.js +++ b/web/app/vector.js @@ -221,12 +221,19 @@ TCAD.Matrix.prototype.combine = function(transform) { }; 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 y = vector.y; var z = vector.z; - return new TCAD.Vector( - this.mxx * x + this.mxy * y + this.mxz * z + this.tx, - this.myx * x + this.myy * y + this.myz * z + this.ty, - this.mzx * x + this.mzy * y + this.mzz * z + this.tz); + return out.set( + this.mxx * x + this.mxy * y + this.mxz * z + this.tx, + this.myx * x + this.myy * y + this.myz * z + this.ty, + this.mzx * x + this.mzy * y + this.mzz * z + this.tz); }; - diff --git a/web/app/workbench.js b/web/app/workbench.js index d09ab4bb..a31c787c 100644 --- a/web/app/workbench.js +++ b/web/app/workbench.js @@ -184,12 +184,69 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) { } function prepare(__cgsPolygons) { + var counter = 0; var polygons = __cgsPolygons.map(function(cp) { + if (cp.plane.___ID === undefined) { + cp.plane.___ID = ++ counter; + } return { - vertices : cp.vertices.map(function(cv) {return roundV(vec(cv.pos))}), - normal : roundV(vec(cp.plane.normal)) + vertices : cp.vertices.map(function(cv) {return vec(cv.pos)}), + 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)) }); return polygons; } @@ -358,12 +415,12 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) { var paths = []; var path; var visited = {}; - function nextUnvisitedNormal(p, key) { + function nextUnvisitedPolygon(p, key) { var polygons = pointToPoly[key]; for (var pi = 0; pi < polygons.length; pi++) { var poly = polygons[pi]; var nkey = pnkey(p, poly.normal); - if (visited[nkey] === undefined) return poly.normal; + if (visited[nkey] === undefined) return poly; } return null; } @@ -399,10 +456,12 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) { for (var i = 0; i < points.length; i++) { var point = points[i]; key = pkey(point); - var normal = nextUnvisitedNormal(point, key); - if (normal == null) { + var unvPoly = nextUnvisitedPolygon(point, key); + if (unvPoly == null) { continue; } + var normal = unvPoly.normal; + var w = unvPoly.w; var normalKey = pkey(normal); pCurr = point; @@ -450,7 +509,8 @@ TCAD.craft._mergeCSGPolygons = function(__cgsPolygons) { if (path.length > 2) { paths.push({ vertices : path, - normal : normal + normal : normal, + w : w }); } console.log("-----") @@ -502,7 +562,8 @@ TCAD.craft._mergeCSGPolygonsTester = function(data) { vertices : points.map(function(e) { 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 { 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) { 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) { var pathTest = paths[piTest]; if (piTest === pi) continue; if (!pathTest.normal.equals(path.normal)) continue; - var depthTest = paths3D[piTest].vertices[0].dot(paths3D[piTest].normal); - if (!TCAD.utils.equal(depthTest, depth)) continue; +// var depthTest = paths3D[piTest].vertices[0].dot(paths3D[piTest].normal); +// if (!TCAD.utils.equal(depthTest, depth)) continue; + if (!TCAD.utils.areEqual(path.w, pathTest.w, 10E-6)) continue; if (pInP(pathTest.vertices, path.vertices)) { index[piTest].push(pi); @@ -716,5 +779,9 @@ TCAD.Craft.prototype.modify = function(solid, modification) { if (faces == null) return; this.app.viewer.scene.remove( solid.meshObject ); this.app.viewer.scene.add(TCAD.utils.createSolidMesh(faces)); + + //REMOVE IT + this.app._refreshSketches(); + this.app.viewer.render(); }; diff --git a/web/index.html b/web/index.html index ad34ed3b..0f07f4e1 100644 --- a/web/index.html +++ b/web/index.html @@ -20,6 +20,7 @@ + diff --git a/web/sketcher.html b/web/sketcher.html index a8480bc0..a6474173 100644 --- a/web/sketcher.html +++ b/web/sketcher.html @@ -190,6 +190,7 @@ + @@ -202,7 +203,6 @@ -