Skip to content

Commit

Permalink
Merge pull request #2334 from AnalyticalGraphicsInc/geometry-perf
Browse files Browse the repository at this point in the history
Geometry performance
  • Loading branch information
pjcozzi committed Dec 18, 2014
2 parents a8ff6c8 + 1ca5627 commit 904b940
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 144 deletions.
25 changes: 5 additions & 20 deletions Source/Core/EllipseGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,18 +467,14 @@ define([
}

function computeWallIndices(positions) {
var UL;
var UR;
var LL;
var LR;
var length = positions.length / 3;
var indices = IndexDatatype.createTypedArray(length, length * 6);
var index = 0;
for ( var i = 0; i < length - 1; i++) {
UL = i;
LL = i + length;
UR = UL + 1;
LR = UR + length;
for (var i = 0; i < length; i++) {
var UL = i;
var LL = i + length;
var UR = (UL + 1) % length;
var LR = UR + length;
indices[index++] = UL;
indices[index++] = LL;
indices[index++] = UR;
Expand All @@ -487,17 +483,6 @@ define([
indices[index++] = LR;
}

UL = length - 1;
LL = i + length;
UR = 0;
LR = UR + length;
indices[index++] = UL;
indices[index++] = LL;
indices[index++] = UR;
indices[index++] = UR;
indices[index++] = LL;
indices[index++] = LR;

return indices;
}

Expand Down
58 changes: 31 additions & 27 deletions Source/Core/EllipseGeometryLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,24 @@ define([
var size = (extrude) ? positions.length / 3 * 2 : positions.length / 3;

var finalPositions = new Float64Array(size * 3);
var normal = scratchNormal;

var length = positions.length;
var bottomOffset = (extrude) ? length : 0;
for (var i = 0; i < length; i += 3) {
var i1 = i + 1;
var i2 = i + 2;

var position = Cartesian3.fromArray(positions, i, scratchCartesian1);
var extrudedPosition;
ellipsoid.scaleToGeodeticSurface(position, position);

position = ellipsoid.scaleToGeodeticSurface(position, position);
extrudedPosition = Cartesian3.clone(position, scratchCartesian2);
normal = ellipsoid.geodeticSurfaceNormal(position, normal);
var extrudedPosition = Cartesian3.clone(position, scratchCartesian2);
var normal = ellipsoid.geodeticSurfaceNormal(position, scratchNormal);
var scaledNormal = Cartesian3.multiplyByScalar(normal, height, scratchCartesian3);
position = Cartesian3.add(position, scaledNormal, position);
Cartesian3.add(position, scaledNormal, position);

if (extrude) {
scaledNormal = Cartesian3.multiplyByScalar(normal, extrudedHeight, scaledNormal);
extrudedPosition = Cartesian3.add(extrudedPosition, scaledNormal, extrudedPosition);
Cartesian3.multiplyByScalar(normal, extrudedHeight, scaledNormal);
Cartesian3.add(extrudedPosition, scaledNormal, extrudedPosition);

finalPositions[i + bottomOffset] = extrudedPosition.x;
finalPositions[i1 + bottomOffset] = extrudedPosition.y;
Expand Down Expand Up @@ -125,6 +124,10 @@ define([
// The number of points in the first quadrant
var numPts = 1 + Math.ceil(CesiumMath.PI_OVER_TWO / granularity);
var deltaTheta = CesiumMath.PI_OVER_TWO / (numPts - 1);
var theta = CesiumMath.PI_OVER_TWO - numPts * deltaTheta;
if (theta < 0.0) {
numPts -= Math.ceil(Math.abs(theta) / deltaTheta);
}

// If the number of points were three, the ellipse
// would be tessellated like below:
Expand All @@ -142,16 +145,17 @@ define([
// *---*
// Notice each vertical column contains an even number of positions.
// The sum of the first n even numbers is n * (n + 1). Double it for the number of points
// for the whole ellipse. Note: this is just an estimate and may actually be less depending
// on the number of iterations before the angle reaches pi/2.
// for the whole ellipse.
var size = 2 * numPts * (numPts + 1);
var positions = (addFillPositions) ? new Array(size * 3) : undefined;
var positionIndex = 0;
var position = scratchCartesian1;
var reflectedPosition = scratchCartesian2;

var outerLeft = (addEdgePositions) ? [] : undefined;
var outerRight = (addEdgePositions) ? [] : undefined;
var outerPositionsLength = (2 * numPts + 2 * (numPts - 1)) * 3;
var outerRightIndex = outerPositionsLength - 1;
var outerLeftIndex = 0;
var outerPositions = (addEdgePositions) ? new Array(outerPositionsLength) : undefined;

var i;
var j;
Expand All @@ -160,8 +164,8 @@ define([
var interiorPosition;

// Compute points in the 'northern' half of the ellipse
var theta = CesiumMath.PI_OVER_TWO;
for (i = 0; i < numPts && theta > 0; ++i) {
theta = CesiumMath.PI_OVER_TWO;
for (i = 0; i < numPts; ++i) {
position = pointOnEllipsoid(theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, position);
reflectedPosition = pointOnEllipsoid(Math.PI - theta, rotation, northVec, eastVec, aSqr, ab, bSqr, mag, unitPos, reflectedPosition);

Expand All @@ -185,18 +189,19 @@ define([
}

if (addEdgePositions) {
outerRight.unshift(position.x, position.y, position.z);
outerPositions[outerRightIndex--] = position.z;
outerPositions[outerRightIndex--] = position.y;
outerPositions[outerRightIndex--] = position.x;
if (i !== 0) {
outerLeft.push(reflectedPosition.x, reflectedPosition.y, reflectedPosition.z);
outerPositions[outerLeftIndex++] = reflectedPosition.x;
outerPositions[outerLeftIndex++] = reflectedPosition.y;
outerPositions[outerLeftIndex++] = reflectedPosition.z;
}
}

theta = CesiumMath.PI_OVER_TWO - (i + 1) * deltaTheta;
}

// Set numPts if theta reached zero
numPts = i;

// Compute points in the 'southern' half of the ellipse
for (i = numPts; i > 0; --i) {
theta = CesiumMath.PI_OVER_TWO - (i - 1) * deltaTheta;
Expand Down Expand Up @@ -224,26 +229,25 @@ define([
}

if (addEdgePositions) {
outerRight.unshift(position.x, position.y, position.z);
outerPositions[outerRightIndex--] = position.z;
outerPositions[outerRightIndex--] = position.y;
outerPositions[outerRightIndex--] = position.x;
if (i !== 1) {
outerLeft.push(reflectedPosition.x, reflectedPosition.y, reflectedPosition.z);
outerPositions[outerLeftIndex++] = reflectedPosition.x;
outerPositions[outerLeftIndex++] = reflectedPosition.y;
outerPositions[outerLeftIndex++] = reflectedPosition.z;
}
}
}

var r = {};
if (addFillPositions) {
// The original length may have been an over-estimate
if (positions.length !== positionIndex) {
size = positionIndex / 3;
positions.length = positionIndex;
}
r.positions = positions;
r.numPts = numPts;
}

if (addEdgePositions) {
r.outerPositions = outerRight.concat(outerLeft);
r.outerPositions = outerPositions;
}

return r;
Expand Down
20 changes: 7 additions & 13 deletions Source/Core/EllipseOutlineGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ define([
var length = positions.length / 3;
var indices = IndexDatatype.createTypedArray(length, length * 2);
var index = 0;
for ( var i = 0; i < length - 1; i++) {
for ( var i = 0; i < length; ++i) {
indices[index++] = i;
indices[index++] = i + 1;
indices[index++] = (i + 1) % length;
}
indices[index++] = length - 1;
indices[index++] = 0;

return {
boundingSphere : boundingSphere,
Expand Down Expand Up @@ -99,22 +97,18 @@ define([

length /= 2;
var index = 0;
for (var i = 0; i < length - 1; i++) {
var i;
for (i = 0; i < length; ++i) {
indices[index++] = i;
indices[index++] = i + 1;
indices[index++] = (i + 1) % length;
indices[index++] = i + length;
indices[index++] = i + length + 1;
indices[index++] = ((i + 1) % length) + length;
}

indices[index++] = length - 1;
indices[index++] = 0;
indices[index++] = length + length - 1;
indices[index++] = length;

var numSide;
if (numberOfVerticalLines > 0) {
var numSideLines = Math.min(numberOfVerticalLines, length);
numSide = Math.round(length/numSideLines);
numSide = Math.round(length / numSideLines);
}


Expand Down
65 changes: 39 additions & 26 deletions Source/Core/PolylineGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ define([
VertexFormat) {
"use strict";

function interpolateColors(p0, p1, color0, color1, minDistance) {
var numPoints = PolylinePipeline.numberOfPoints(p0, p1, minDistance);
var colors = new Array(numPoints);
var scratchInterpolateColorsArray = [];

function interpolateColors(p0, p1, color0, color1, numPoints) {
var colors = scratchInterpolateColorsArray;
colors.length = numPoints;
var i;

var r0 = color0.red;
Expand Down Expand Up @@ -142,6 +144,7 @@ define([
var scratchPosition = new Cartesian3();
var scratchPrevPosition = new Cartesian3();
var scratchNextPosition = new Cartesian3();

/**
* Computes the geometric representation of a polyline, including its vertices, indices, and a bounding sphere.
*
Expand All @@ -166,44 +169,54 @@ define([
var j;
var k;

var p0;
var p1;
var c0;
var c1;

var positions = PolylinePipeline.removeDuplicates(polylineGeometry._positions);
if (!defined(positions)) {
positions = polylineGeometry._positions;
}
var positionsLength = positions.length;

//>>includeStart('debug', pragmas.debug);
if (positions.length < 2) {
if (positionsLength < 2) {
throw new DeveloperError('At least two unique positions are required.');
}
//>>includeEnd('debug');

if (followSurface) {
var heights = PolylinePipeline.extractHeights(positions, ellipsoid);
var newColors = defined(colors) ? [] : undefined;

if (defined(colors)) {
for (i = 0; i < positions.length-1; i++) {
p0 = positions[i];
p1 = positions[i+1];
c0 = colors[i];

if (perVertex && i < colors.length) {
c1 = colors[i+1];
newColors = newColors.concat(interpolateColors(p0, p1, c0, c1, minDistance));
var colorLength = 1;
for (i = 0; i < positionsLength - 1; ++i) {
colorLength += PolylinePipeline.numberOfPoints(positions[i], positions[i+1], minDistance);
}

var newColors = new Array(colorLength);
var newColorIndex = 0;

for (i = 0; i < positionsLength - 1; ++i) {
var p0 = positions[i];
var p1 = positions[i+1];
var c0 = colors[i];

var numColors = PolylinePipeline.numberOfPoints(p0, p1, minDistance);
if (perVertex && i < colorLength) {
var c1 = colors[i+1];
var interpolatedColors = interpolateColors(p0, p1, c0, c1, numColors);
var interpolatedColorsLength = interpolatedColors.length;
for (j = 0; j < interpolatedColorsLength; ++j) {
newColors[newColorIndex++] = interpolatedColors[j];
}
} else {
var l = PolylinePipeline.numberOfPoints(p0, p1, minDistance);
for (j = 0; j < l; j++) {
newColors.push(Color.clone(c0));
for (j = 0; j < numColors; ++j) {
newColors[newColorIndex++] = Color.clone(c0);
}
}
}
newColors.push(Color.clone(colors[colors.length-1]));

newColors[newColorIndex] = Color.clone(colors[colors.length-1]);
colors = newColors;

scratchInterpolateColorsArray.length = 0;
}

positions = PolylinePipeline.generateCartesianArc({
Expand All @@ -214,7 +227,8 @@ define([
});
}

var size = positions.length * 4.0 - 4.0;
positionsLength = positions.length;
var size = positionsLength * 4.0 - 4.0;

var finalPositions = new Float64Array(size * 3);
var prevPositions = new Float64Array(size * 3);
Expand All @@ -233,7 +247,6 @@ define([
var count = 0;
var position;

var positionsLength = positions.length;
for (j = 0; j < positionsLength; ++j) {
if (j === 0) {
position = scratchCartesian3;
Expand Down Expand Up @@ -341,10 +354,10 @@ define([
});
}

var indices = IndexDatatype.createTypedArray(size, positions.length * 6 - 6);
var indices = IndexDatatype.createTypedArray(size, positionsLength * 6 - 6);
var index = 0;
var indicesIndex = 0;
var length = positions.length - 1.0;
var length = positionsLength - 1.0;
for (j = 0; j < length; ++j) {
indices[indicesIndex++] = index;
indices[indicesIndex++] = index + 2;
Expand Down
Loading

0 comments on commit 904b940

Please sign in to comment.