fix cut tool

This commit is contained in:
Val Erastov 2015-08-24 00:33:11 -07:00
parent bf312e6304
commit ae2d4fbe37
2 changed files with 34 additions and 16 deletions

View file

@ -226,7 +226,8 @@ TCAD.utils.sketchToPolygons = function(geom) {
polyPoints.push(new TCAD.Vector(point[0], point[1], 0));
}
polygons.push(new TCAD.Polygon(polyPoints));
console.warn("Points count < 3!");
if (polyPoints.length >= 3) polygons.push(new TCAD.Polygon(polyPoints));
}
return polygons;
};
@ -419,7 +420,7 @@ TCAD.SketchFace.prototype.syncSketches = function(geom) {
var lg = new THREE.Geometry();
var a = _3dTransformation.apply(new TCAD.Vector(l[0], l[1], depth));
var b = _3dTransformation.apply(new TCAD.Vector(l[2], l[3], depth));
lg.vertices.push(a.plus(offVector).three());
lg.vertices.push(b.plus(offVector).three());
var line = new THREE.Segment(lg, this.SKETCH_MATERIAL);
@ -429,9 +430,10 @@ TCAD.SketchFace.prototype.syncSketches = function(geom) {
this.sketchGeom.depth = depth;
};
TCAD.POLYGON_COUNTER = 0;
/** @constructor */
TCAD.Polygon = function(shell, holes, normal) {
this.id = TCAD.POLYGON_COUNTER ++;
if (!holes) {
holes = [];
}

View file

@ -594,7 +594,8 @@ TCAD.craft._makeFromPolygons = function(polygons) {
for ( var h = 0; h < poly.holes.length; h ++ ) {
Array.prototype.push.apply( points, poly.holes[h] );
}
var pid = poly.id;
var shared = new CSG.Polygon.Shared([pid, pid, pid, pid]);
var refs = poly.triangulate();
for ( var i = 0; i < refs.length; ++ i ) {
var a = refs[i][0] + off;
@ -604,7 +605,7 @@ TCAD.craft._makeFromPolygons = function(polygons) {
new CSG.Vertex(csgVec(points[a]), csgVec(poly.normal)),
new CSG.Vertex(csgVec(points[b]), csgVec(poly.normal)),
new CSG.Vertex(csgVec(points[c]), csgVec(poly.normal))
]);
], shared);
csgPolygons.push(csgPoly);
}
off = points.length;
@ -753,18 +754,33 @@ TCAD.craft.cut = function(app, face, faces, height) {
return separated;
}
var merged = TCAD.craft._mergeCSGPolygons(cut.polygons);
var sorted = sortPaths(merged);
return sorted.map(function(path) {
return new TCAD.Polygon(path.vertices, path.holes.map(function(path){return path.vertices}), path.normal);
});
var byShared = {};
for (var i = 0; i < cut.polygons.length; i++) {
var p = cut.polygons[i];
var tag = p.shared.getTag();
if (byShared[tag] === undefined) byShared[tag] = [];
byShared[tag].push(p);
}
var result = [];
for (var tag in byShared) {
var merged = TCAD.craft._mergeCSGPolygons(byShared[tag]);
var sorted = sortPaths(merged);
result.push.apply(result, sorted.map(function(path) {
return new TCAD.Polygon(path.vertices, path.holes.map(function(path){return path.vertices}), path.normal);
})
);
// return cut.polygons.map(function(e) {
// return new TCAD.Polygon(e.vertices.map(
// function(v) {
// return new TCAD.Vector(v.pos.x, v.pos.y, v.pos.z)
// }), [])
// });
}
console.log(result);
return result;
//return byShared[20].map(function(e) {
// return new TCAD.Polygon(e.vertices.map(
// function(v) {
// return new TCAD.Vector(v.pos.x, v.pos.y, v.pos.z)
// }), [])
//});
};