improve revolve performance

This commit is contained in:
Val Erastov 2016-12-15 02:32:16 -08:00
parent a3cff1a467
commit d9855c0791
5 changed files with 57 additions and 16 deletions

View file

@ -68,6 +68,24 @@ export function revolveToWireframe(polygons, axisSegment, angle, resolution) {
return out;
}
export function revolveToTriangles(polygons, axisSegment, angle, resolution) {
const out = [];
//add initial polygon
revolveIterator(polygons, axisSegment, angle, resolution, (pOrig, pRot, p, q) => {
//skip point if they are on the axis of revolving
if (!math.equal(0, math.distanceAB3(pOrig[q], pRot[q]))) {
out.push( [pOrig[p], pOrig[q], pRot[q]] );
}
if (!math.equal(0, math.distanceAB3(pOrig[p], pRot[p]))) {
out.push( [ pRot[q], pRot[p], pOrig[p]] );
}
});
if (angle < 0) {
out.forEach(tr => tr.reverse());
}
return out;
}
export function revolveIterator(polygons, axisSegment, angle, resolution, callback) {
if (resolution < 2) resolution = 2;

View file

@ -1,7 +1,7 @@
import {HashTable} from '../utils/hashmap'
import Vector from '../math/vector'
import Counters from './counters'
import {reconstructSketchBounds} from './workbench'
import {findOutline, segmentsToPaths} from './workbench'
import {Matrix3, AXIS} from '../math/l3space'
import {arrFlatten1L, isCurveClass} from './cad-utils'
import DPR from '../utils/dpr'
@ -91,7 +91,7 @@ Solid.prototype.setupGeometry = function() {
off = geom.vertices.length;
}
this.collectCurvedSurface(polyFace);
this.collectWires(polyFace);
this.collectWires(polyFace, group.polygons);
}
geom.mergeVertices();
@ -117,7 +117,7 @@ Solid.prototype.collectCurvedSurface = function(face) {
face.curvedSurfaces = surfaces;
};
Solid.prototype.collectWires = function(face) {
Solid.prototype.collectWires = function(face, facePolygons) {
function contains(planes, plane) {
for (var j = 0; j < planes.length; j++) {
@ -127,7 +127,10 @@ Solid.prototype.collectWires = function(face) {
}
return false;
}
var paths = reconstructSketchBounds(this.csg, face, true);
const outline = findOutline(facePolygons);
const paths = segmentsToPaths(outline);
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
var p, q, n = path.vertices.length;

View file

@ -3,8 +3,8 @@ import * as workbench from '../workbench'
import * as cad_utils from '../cad-utils'
import Vector from '../../math/vector'
import {Matrix3, ORIGIN} from '../../math/l3space'
import {revolveToWireframe} from '../revolve'
import {OpWizard, IMAGINE_MATERIAL, BASE_MATERIAL} from './wizard-commons'
import {revolveToTriangles} from '../revolve'
import {OpWizard, IMAGINARY_SURFACE_MATERIAL, } from './wizard-commons'
export function RevolveWizard(app, face, initParams) {
OpWizard.call(this, app.viewer, initParams);
@ -32,14 +32,26 @@ RevolveWizard.prototype.updatePolygons = function() {
};
RevolveWizard.prototype.update = function(angle, resolution) {
let linesCounter = 0;
const segments = revolveToWireframe(this.polygons, this.polygons[0], angle / 180 * Math.PI, resolution);
for (let segment of segments) {
this.setupLine(linesCounter ++, segment[0], segment[1], IMAGINE_MATERIAL);
if (this.mesh) {
this.mesh.geometry.dispose();
this.previewGroup.remove(this.mesh);
}
for (var i = 0; i < this.lines.length; i++) {
this.lines[i].visible = i < linesCounter;
const triangles = revolveToTriangles(this.polygons, this.polygons[0], angle / 180 * Math.PI, resolution);
const geometry = new THREE.Geometry();
for (let tr of triangles) {
const a = geometry.vertices.length;
const b = a + 1;
const c = a + 2;
const face = new THREE.Face3(a, b, c);
tr.forEach(v => geometry.vertices.push(v.three()));
geometry.faces.push(face);
}
geometry.mergeVertices();
geometry.computeFaceNormals();
this.mesh = new THREE.Mesh(geometry, IMAGINARY_SURFACE_MATERIAL);
this.previewGroup.add(this.mesh);
};
RevolveWizard.prototype.createUI = function (angle, resolution) {

View file

@ -1,7 +1,6 @@
import DPR from '../../utils/dpr'
import * as tk from '../../ui/toolkit'
const IMAGINE_MATERIAL = new THREE.LineBasicMaterial({
color: 0xFA8072,
linewidth: 1/DPR,
@ -16,6 +15,15 @@ const BASE_MATERIAL = new THREE.LineBasicMaterial({
depthTest: false
});
const IMAGINARY_SURFACE_MATERIAL = new THREE.MeshPhongMaterial({
vertexColors: THREE.FaceColors,
color: 0xFA8072,
transparent: true,
opacity: 0.5,
shininess: 0,
side : THREE.DoubleSide
});
export function Wizard(viewer, initParams) {
if (!initParams) initParams = this.DEFAULT_PARAMS;
this.viewer = viewer;
@ -120,4 +128,4 @@ OpWizard.prototype.dispose = function() {
this.viewer.render();
};
export {OpWizard, IMAGINE_MATERIAL, BASE_MATERIAL}
export {OpWizard, IMAGINE_MATERIAL, IMAGINARY_SURFACE_MATERIAL, BASE_MATERIAL}

View file

@ -344,7 +344,7 @@ function getOutlineByCollision(segments, outerEdges) {
return outline;
}
function findOutline (planePolygons, outer) {
export function findOutline (planePolygons) {
var segmentsByPolygon = polygonsToSegments(planePolygons);
//simplifySegments(segmentsByPolygon);
var planeSegments = cad_utils.arrFlatten1L(segmentsByPolygon);
@ -455,7 +455,7 @@ function deleteRedundantPoints(path) {
return cleanedPath;
}
function segmentsToPaths(segments) {
export function segmentsToPaths(segments) {
var veq = math.strictEqual;
var paths = [];