From 43b27d18c5cbb5ae79805258422b1ff4da855b55 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Tue, 19 Sep 2017 17:26:00 -0700 Subject: [PATCH] refactor brep-builder --- web/app/3d/debug.js | 6 ++++++ web/app/3d/tess/brep-tess.js | 15 ++++++++++++++- web/app/brep/brep-builder.js | 18 +++++++----------- web/app/brep/geom/impl/nurbs.js | 8 ++++++++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/web/app/3d/debug.js b/web/app/3d/debug.js index 01e8ca0c..289c5407 100644 --- a/web/app/3d/debug.js +++ b/web/app/3d/debug.js @@ -116,6 +116,12 @@ function addGlobalDebugActions(app) { AddCurve: (curve, color) => { __DEBUG__.AddPolyLine(curve.verb.tessellate().map(v => new Vector().set3(v)), color); }, + AddNurbsCorners: (nurbs) => { + __DEBUG__.AddPoint(nurbs.point(0, 0), 0xff0000); + __DEBUG__.AddPoint(nurbs.point(1, 0), 0x00ff00); + __DEBUG__.AddPoint(nurbs.point(1, 1), 0x0000ff); + __DEBUG__.AddPoint(nurbs.point(0, 1), 0x00ffff); + }, HideSolids: () => { app.findAllSolidsOnScene().forEach(s => s.cadGroup.traverse(o => o.visible = false)); app.viewer.render(); diff --git a/web/app/3d/tess/brep-tess.js b/web/app/3d/tess/brep-tess.js index 9960cfc7..1bdfd4a5 100644 --- a/web/app/3d/tess/brep-tess.js +++ b/web/app/3d/tess/brep-tess.js @@ -17,7 +17,13 @@ export default function(face) { // tessy.gluTessProperty(libtess.gluEnum.GLU_TESS_WINDING_RULE, libtess.windingRule.GLU_TESS_WINDING_POSITIVE); tessy.gluTessCallback(libtess.gluEnum.GLU_TESS_VERTEX_DATA, vertexCallback); - tessy.gluTessNormal(0, 0, 1); + const mirrored = isMirrored(face.surface); + + if (mirrored) { + tessy.gluTessNormal(0, 0, -1); + } else { + tessy.gluTessNormal(0, 0, 1); + } const params = []; tessy.gluTessBeginPolygon(params); @@ -87,3 +93,10 @@ function analyzeCurvature(nurbs, triangles) { return triangles; } + +function isMirrored(surface) { + let a = surface.point(0, 0); + let b = surface.point(1, 0); + let c = surface.point(1, 1); + return b.minus(a).cross(c.minus(a))._normalize().dot(surface.normalUV(0, 0)) < 0; +} diff --git a/web/app/brep/brep-builder.js b/web/app/brep/brep-builder.js index c1071a9b..5fd224b7 100644 --- a/web/app/brep/brep-builder.js +++ b/web/app/brep/brep-builder.js @@ -43,7 +43,7 @@ export function createPrism(basePoints, height) { basePath.add(NurbsCurve.createLinearNurbs(basePoints[i], basePoints[j]), basePoints[i], null); lidPath.add(NurbsCurve.createLinearNurbs(lidPoints[i], lidPoints[j]), lidPoints[i], null); } - return enclose(basePath, lidPath, baseSurface, lidSurface, () => {}); + return enclose(basePath, lidPath, baseSurface, lidSurface); } export function enclose(basePath, lidPath, basePlane, lidPlane) { @@ -54,13 +54,10 @@ export function enclose(basePath, lidPath, basePlane, lidPlane) { const walls = []; - const baseVertices = basePath.points.map(p => new Vertex(p)); - const lidVertices = lidPath.points.map(p => new Vertex(p)); - const n = basePath.points.length; for (let i = 0; i < n; i++) { let j = (i + 1) % n; - const wall = createWall(basePath.curves[i], lidPath.curves[i], baseVertices[j], baseVertices[i], lidVertices[i], lidVertices[j]); + const wall = createWall(basePath.curves[i], lidPath.curves[i]); walls.push(wall); } return assemble(walls, basePlane, lidPlane) @@ -85,14 +82,14 @@ function assemble(walls, basePlane, lidPlane) { let next = wallEdges[j]; let wall = walls[i]; - let baseEdge = new Edge(wall.isoCurveAlignU(0), curr.halfEdge1.vertexA, next.halfEdge1.vertexA); - let lidEdge = new Edge(wall.isoCurveAlignU(1), curr.halfEdge1.vertexB, next.halfEdge1.vertexB); + let baseEdge = new Edge(wall.isoCurveAlignU(1), curr.halfEdge1.vertexB, next.halfEdge1.vertexB); + let lidEdge = new Edge(wall.isoCurveAlignU(0), curr.halfEdge1.vertexA, next.halfEdge1.vertexA); baseEdges.push(baseEdge); lidEdges.push(lidEdge); let wallFace = new Face(wall); - wallFace.outerLoop.halfEdges.push(baseEdge.halfEdge2, curr.halfEdge1, lidEdge.halfEdge1, next.halfEdge2); + wallFace.outerLoop.halfEdges.push(baseEdge.halfEdge2, curr.halfEdge2, lidEdge.halfEdge1, next.halfEdge1); wallFace.outerLoop.link(); shell.faces.push(wallFace); } @@ -320,12 +317,11 @@ function createBoundingNurbs(points, plane) { polygon = polygon.map(p => to3D._apply(p)); const nurbs = new NurbsSurface(new verb.geom.ExtrudedSurface(new verb.geom.Line( - polygon[1].data(), polygon[0].data()), polygon[2].minus(polygon[1]).data())); + polygon[0].data(), polygon[1].data()), polygon[2].minus(polygon[1]).data())); return nurbs; } - export function linkHalfEdges(edge, halfEdge1, halfEdge2) { halfEdge1.edge = edge; halfEdge2.edge = edge; @@ -393,7 +389,7 @@ export function createWall(curve1, curve2) { if (bothClassOf(curve1, curve2, 'Line')) { throw 'unsupported' } else if (bothClassOf(curve1, curve2, 'NurbsCurve')) { - return new NurbsSurface(verb.geom.NurbsSurface.byLoftingCurves([curve1.verb, curve2.verb], 1)); + return new NurbsSurface(verb.geom.NurbsSurface.byLoftingCurves([curve2.verb, curve1.verb], 1)); } else { throw 'unsupported'; } diff --git a/web/app/brep/geom/impl/nurbs.js b/web/app/brep/geom/impl/nurbs.js index 98d9e438..235e4218 100644 --- a/web/app/brep/geom/impl/nurbs.js +++ b/web/app/brep/geom/impl/nurbs.js @@ -114,6 +114,14 @@ export class NurbsSurface extends Surface { return normal; } + normalUV(u, v) { + let normal = new Vector().set3(this.verb.normal(u, v)); + if (this.inverted) { + normal._negate(); + } + return normal; + } + normalInMiddle(point) { let normal = new Vector().set3(this.verb.normal(0.5, 0.5)); if (this.inverted) {