Skip to content

Commit

Permalink
Adding tests and code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
fstoner committed Apr 25, 2014
1 parent 5cc5511 commit 45c19b4
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 162 deletions.
3 changes: 0 additions & 3 deletions Apps/Sandcastle/gallery/Sensors.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@

function addFootprint(ellipsoid) {
var foot = new Cesium.CustomSensorVolume();

var numberOfVertices = 180;
var azimuthDivision = 360.0 / numberOfVertices;

var directions = [];
directions.push({ clock : Cesium.Math.toRadians(0.0), cone : Cesium.Math.toRadians(14.03624347)});
Expand Down
164 changes: 7 additions & 157 deletions Source/Core/SphericalPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,158 +199,6 @@ define([
}
};

function kDopFacetNormalName(i, j) {
return 'u_kDopFacetNormal_' + i + '_' + j;
}

function convexHullImplicitSurfaceFunction(vertices, name, sign) {
var length = vertices.length;
var glsl = '';
var result = '';
var oppositeSign = (sign === '+') ? '-' : '+';
for (var i = 0; i < length; ++i) {
var j = (i + 1 === length) ? 0 : i + 1;
var initialIndex = vertices[i];
var finalIndex = vertices[j];
var uniform = (initialIndex < finalIndex) ? sign + kDopFacetNormalName(initialIndex, finalIndex) : oppositeSign + kDopFacetNormalName(finalIndex, initialIndex);
if (i === 0) {
result += '\tfloat value = dot(displacement, ' + uniform + ');\n';
} else {
result += '\tvalue = max(value, dot(displacement, ' + uniform + '));\n';
}
}
glsl += '\nfloat ' + name + '(vec3 displacement)\n{\n' + result + '\treturn value;\n}\n';
return glsl;
}

function sphericalPolygonImplicitSurfaceFunction(hull, name, sign) {
var result = '';
if (defined(hull.holes)) {
var oppositeSign = (sign === '+') ? '-' : '+';
for (var h = 0; h < hull.holes.length; ++h) {
var functionName = name + '_' + h;
result += sphericalPolygonImplicitSurfaceFunction(hull.holes[h], functionName, oppositeSign);
}
}
result += convexHullImplicitSurfaceFunction(hull, name, sign);
return result;
}

function sphericalPolygonImplicitSurfaceFunctionUniforms(hull, uniforms) {
var result = '';
if (defined(hull.holes)) {
for (var h = 0; h < hull.holes.length; ++h) {
result += sphericalPolygonImplicitSurfaceFunctionUniforms(hull.holes[h], uniforms);
}
}
var length = hull.length;
for (var i = 0; i < length; ++i) {
var j = (i + 1 === length) ? 0 : i + 1;
var initialIndex = hull[i];
var finalIndex = hull[j];
var name = (initialIndex < finalIndex) ? kDopFacetNormalName(initialIndex, finalIndex) : kDopFacetNormalName(finalIndex, initialIndex);

if (!defined(uniforms[name])) {
uniforms[name] = true;
result += 'uniform vec3 ' + name + ';\n';
}
}
return result;
}

function aggregateFunction(hull, functionName, variableName) {
var result = '\tfloat ' + variableName + ' = ' + functionName + '(displacement);\n';
if (defined(hull.holes)) {
for (var i = 0; i < hull.holes.length; ++i) {
var name = functionName + '_' + i;
result += '\t' + variableName + ' = max(' + variableName + ', -' + name + '(displacement));\n';
var hole = hull.holes[i];
if (defined(hole.holes)) {
var variable = variableName + '_' + i;
for (var j = 0; j < hole.holes.length; ++j) {
var v = variable + '_' + j;
result += aggregateFunction(hole.holes[j], name + '_' + j, v);
result += '\t' + variableName + ' = min(' + variableName + ', ' + v + ');\n';
}
}
}
}
return result;
}

function returnNormal(normal) {
return function() {
return normal;
};
}

function addUniforms(floats, hull, uniforms) {
var numberOfVertices = floats.length / stride;

if (defined(hull.holes)) {
for (var h = 0; h < hull.holes.length; ++h) {
addUniforms(floats, hull.holes[h], uniforms);
}
}
var length = hull.length;
for (var i = 0; i < length; ++i) {
var j = (i + 1 === length) ? 0 : i + 1;
var initialIndex = hull[i];
var finalIndex = hull[j];

var offset = initialIndex * stride + directionsOffset;
lastDirection = Cartesian3.fromArray(floats, offset, lastDirection);
offset = finalIndex * stride + directionsOffset;
direction = Cartesian3.fromArray(floats, offset, direction);
var name = (initialIndex < finalIndex) ? kDopFacetNormalName(initialIndex, finalIndex) : kDopFacetNormalName(finalIndex, initialIndex);

if (!defined(uniforms[name])) {
var difference = (initialIndex < finalIndex) ? finalIndex - initialIndex : finalIndex + numberOfVertices - initialIndex;
if (difference === 1) {
uniforms[name] = (initialIndex < finalIndex) ? returnNormal(Cartesian3.fromArray(floats, finalIndex * stride + normalsOffset)) : returnNormal(Cartesian3.negate(Cartesian3.fromArray(floats, finalIndex * stride + normalsOffset)));
} else {
uniforms[name] = (initialIndex < finalIndex) ? returnNormal(Cartesian3.cross(direction, lastDirection)) : returnNormal(Cartesian3.cross(lastDirection, direction));
}
}
}
}

/**
* DOC_TBA
*
* @memberof SphericalPolygon.prototype
*
* @private
*/
SphericalPolygon.prototype.uniforms = function() {
var uniforms = {};
addUniforms(this._directionsNormalsAndBisectorsWithMagnitudeSquared, this.convexHull, uniforms);
return uniforms;
};

/**
* DOC_TBA
*
* @memberof SphericalPolygon.prototype
*
* @private
*/
SphericalPolygon.prototype.implicitSurfaceFunction = function() {
var convexHull = this.convexHull;
var glsl = '\n';
// Emit uniforms.
var uniforms = {};
glsl += sphericalPolygonImplicitSurfaceFunctionUniforms(convexHull, uniforms);
// Emit convex hull funtions.
var functionName = 'convexHull';
var variableName = 'value';
glsl += sphericalPolygonImplicitSurfaceFunction(convexHull, functionName, '+');
// Emit implicit surface function.
var result = aggregateFunction(convexHull, functionName, variableName);
glsl += '\nfloat sensorSurfaceFunction(vec3 displacement)\n{\n' + result + '\treturn ' + variableName + ';\n}\n';
return glsl;
};

var first = new Cartesian3();
var second = new Cartesian3();
var third = new Cartesian3();
Expand Down Expand Up @@ -471,8 +319,10 @@ define([

/**
* Gets and sets the vertices which define the spherical polygon. The list of vertices should conform to the following restrictions:
* - Duplicate vertices are not allowed.
* - Consecutive vertices should be less than 180 degrees apart.
* <ul>
* <li>Duplicate vertices are not allowed.</li>
* <li>Consecutive vertices should be less than 180 degrees apart.</li>
* </ul>
*
* @memberof SphericalPolygon.prototype
* @type {Array}
Expand Down Expand Up @@ -542,7 +392,7 @@ define([
*/
convexHull : {
get: function() {
if (this._convexHull.length === 0) {
if (defined(this._vertices) && this._convexHull.length === 0) {
SphericalPolygon.findConvexHull(this._directionsNormalsAndBisectorsWithMagnitudeSquared, stride, directionsOffset, 1.0, 0, this._vertices.length - 1, this._convexHull);
}
return this._convexHull;
Expand All @@ -560,7 +410,7 @@ define([
*/
referenceAxis : {
get: function() {
if (!defined(this._referenceAxis)) {
if (!defined(this._referenceAxis) && this.convexHull.length > 0) {
this.computeBoundingCone2();
}
return this._referenceAxis;
Expand All @@ -578,7 +428,7 @@ define([
*/
referenceDistance : {
get: function() {
if (!defined(this._referenceDistance)) {
if (!defined(this._referenceDistance) && this.convexHull.length > 0) {
this.computeBoundingCone2();
}
return this._referenceDistance;
Expand Down
6 changes: 4 additions & 2 deletions Source/Scene/CustomSensorVolume.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,10 @@ define([
/**
* Sets the directions which define the vertices of the custom sensor volume.
* The list of vertices should conform to the following restrictions:
* - Duplicate vertices are not allowed.
* - Consecutive vertices should be less than 180 degrees apart.
* <ul>
* <li>Duplicate vertices are not allowed.</li>
* <li>Consecutive vertices should be less than 180 degrees apart.</li>
* </ul>
*
* @memberof CustomSensorVolume
*
Expand Down
Loading

0 comments on commit 45c19b4

Please sign in to comment.