Skip to content

Commit

Permalink
docs and (minor) source clean up of libtess.normal
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Nov 22, 2014
1 parent 78ed75d commit 281ed0f
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 177 deletions.
139 changes: 65 additions & 74 deletions libtess.cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,57 +1202,53 @@ libtess.mesh.killFace_ = function(fDel, newLFace) {
};



/* global libtess */

/** @const */
libtess.normal = {};


// TODO(bckenny): NOTE:
/* The "feature merging" is not intended to be complete. There are
// TODO(bckenny): Integrate SLANTED_SWEEP somehow?
/* The "feature merging" is not intended to be complete. There are
* special cases where edges are nearly parallel to the sweep line
* which are not implemented. The algorithm should still behave
* which are not implemented. The algorithm should still behave
* robustly (ie. produce a reasonable tesselation) in the presence
* of such edges, however it may miss features which could have been
* merged. We could minimize this effect by choosing the sweep line
* merged. We could minimize this effect by choosing the sweep line
* direction to be something unusual (ie. not parallel to one of the
* coordinate axes).
* #if defined(SLANTED_SWEEP)
* #define S_UNIT_X 0.50941539564955385 // Pre-normalized
* #define S_UNIT_Y 0.86052074622010633
* #endif
*/
/*#if defined(SLANTED_SWEEP)
#define S_UNIT_X 0.50941539564955385 // Pre-normalized
#define S_UNIT_Y 0.86052074622010633
#endif
*/

/**
* @type {number}
* @private
* X coordinate of local basis for polygon projection.
* @private {number}
* @const
*/
libtess.normal.S_UNIT_X_ = 1.0;


/**
* @type {number}
* @private
* Y coordinate of local basis for polygon projection.
* @private {number}
* @const
*/
libtess.normal.S_UNIT_Y_ = 0.0;


/**
* projectPolygon determines the polygon normal
* and projects vertices onto the plane of the polygon.
*
* @param {libtess.GluTesselator} tess [description].
* Determines a polygon normal and projects vertices onto the plane of the
* polygon.
* @param {!libtess.GluTesselator} tess
*/
libtess.normal.projectPolygon = function(tess) {
var computedNormal = false;

var norm = [0, 0, 0];
norm[0] = tess.normal[0]; // TODO(bckenny): better way to init these?
norm[1] = tess.normal[1];
norm[2] = tess.normal[2];
var norm = [
tess.normal[0],
tess.normal[1],
tess.normal[2]
];
if (norm[0] === 0 && norm[1] === 0 && norm[2] === 0) {
libtess.normal.computeNormal_(tess, norm);
computedNormal = true;
Expand Down Expand Up @@ -1312,19 +1308,17 @@ libtess.normal.projectPolygon = function(tess) {
}
};


/**
* Dot product.
* Computes the dot product of vectors u and v.
* @private
* @param {Array.<number>} u [description].
* @param {Array.<number>} v [description].
* @return {number} [description].
* @param {!Array.<number>} u
* @param {!Array.<number>} v
* @return {number}
*/
libtess.normal.dot_ = function(u, v) {
return u[0] * v[0] + u[1] * v[1] + u[2] * v[2];
};


// NOTE(bckenny): only called from within libtess.normal.projectPolygon's
// TRUE_PROJECT branch, so ignoring for code coverage.
/* istanbul ignore next */
Expand All @@ -1343,12 +1337,11 @@ libtess.normal.normalize_ = function(v) {
v[2] /= len;
};


/**
* Returns the index of the longest component of vector v.
* @private
* @param {Array.<number>} v [description].
* @return {number} The index of the longest component.
* @param {!Array.<number>} v
* @return {number}
*/
libtess.normal.longAxis_ = function(v) {
var i = 0;
Expand All @@ -1363,35 +1356,31 @@ libtess.normal.longAxis_ = function(v) {
return i;
};


/**
* [computeNormal description]
*
* Compute an approximate normal of the polygon from the vertices themselves.
* Result returned in norm.
* @private
* @param {libtess.GluTesselator} tess [description].
* @param {Array.<number>} norm [description].
* @param {!libtess.GluTesselator} tess
* @param {!Array.<number>} norm
*/
libtess.normal.computeNormal_ = function(tess, norm) {
// TODO(bckenny): better way to init these
// TODO(bckenny): can pool these, but only called once per poly
var maxVal = [0, 0, 0];
var minVal = [0, 0, 0];
var d1 = [0, 0, 0];
var d2 = [0, 0, 0];
var tNorm = [0, 0, 0];

maxVal[0] = maxVal[1] = maxVal[2] = -2 * libtess.GLU_TESS_MAX_COORD;
minVal[0] = minVal[1] = minVal[2] = 2 * libtess.GLU_TESS_MAX_COORD;

// TODO(bckenny): better way to init these
var maxVert = new Array(3);
var minVert = new Array(3);
var maxVal = [
-2 * libtess.GLU_TESS_MAX_COORD,
-2 * libtess.GLU_TESS_MAX_COORD,
-2 * libtess.GLU_TESS_MAX_COORD
];
var minVal = [
2 * libtess.GLU_TESS_MAX_COORD,
2 * libtess.GLU_TESS_MAX_COORD,
2 * libtess.GLU_TESS_MAX_COORD
];
var maxVert = [];
var minVert = [];

var i;
var v;
var vHead = tess.mesh.vHead;
for (v = vHead.next; v !== vHead; v = v.next) {
for (i = 0; i < 3; ++i) {
for (var i = 0; i < 3; ++i) {
var c = v.coords[i];
if (c < minVal[i]) { minVal[i] = c; minVert[i] = v; }
if (c > maxVal[i]) { maxVal[i] = c; maxVert[i] = v; }
Expand All @@ -1400,10 +1389,10 @@ libtess.normal.computeNormal_ = function(tess, norm) {

// Find two vertices separated by at least 1/sqrt(3) of the maximum
// distance between any two vertices
i = 0;
if (maxVal[1] - minVal[1] > maxVal[0] - minVal[0]) { i = 1; }
if (maxVal[2] - minVal[2] > maxVal[i] - minVal[i]) { i = 2; }
if (minVal[i] >= maxVal[i]) {
var index = 0;
if (maxVal[1] - minVal[1] > maxVal[0] - minVal[0]) { index = 1; }
if (maxVal[2] - minVal[2] > maxVal[index] - minVal[index]) { index = 2; }
if (minVal[index] >= maxVal[index]) {
// All vertices are the same -- normal doesn't matter
norm[0] = 0; norm[1] = 0; norm[2] = 1;
return;
Expand All @@ -1412,11 +1401,15 @@ libtess.normal.computeNormal_ = function(tess, norm) {
// Look for a third vertex which forms the triangle with maximum area
// (Length of normal == twice the triangle area)
var maxLen2 = 0;
var v1 = minVert[i];
var v2 = maxVert[i];
d1[0] = v1.coords[0] - v2.coords[0];
d1[1] = v1.coords[1] - v2.coords[1];
d1[2] = v1.coords[2] - v2.coords[2];
var v1 = minVert[index];
var v2 = maxVert[index];
var tNorm = [0, 0, 0];
var d1 = [
v1.coords[0] - v2.coords[0],
v1.coords[1] - v2.coords[1],
v1.coords[2] - v2.coords[2]
];
var d2 = [0, 0, 0];
for (v = vHead.next; v !== vHead; v = v.next) {
d2[0] = v.coords[0] - v2.coords[0];
d2[1] = v.coords[1] - v2.coords[1];
Expand All @@ -1440,16 +1433,14 @@ libtess.normal.computeNormal_ = function(tess, norm) {
}
};


/**
* [checkOrientation description]
*
* Check that the sum of the signed area of all projected contours is
* non-negative. If not, negate the t-coordinates to reverse the orientation and
* make it so.
* @private
* @param {libtess.GluTesselator} tess [description].
* @param {!libtess.GluTesselator} tess
*/
libtess.normal.checkOrientation_ = function(tess) {
// When we compute the normal automatically, we choose the orientation
// so that the the sum of the signed areas of all contours is non-negative.
var area = 0;
var fHead = tess.mesh.fHead;
for (var f = fHead.next; f !== fHead; f = f.next) {
Expand Down Expand Up @@ -3472,20 +3463,20 @@ libtess.GluTesselator = function() {

/**
* user-specified normal (if provided)
* @type {Array.<number>}
* @type {!Array.<number>}
*/
this.normal = [0, 0, 0];
// TODO(bckenny): better way to init these arrays?

/**
* unit vector in s-direction (debugging)
* @type {Array.<number>}
* @type {!Array.<number>}
*/
this.sUnit = [0, 0, 0];

/**
* unit vector in t-direction (debugging)
* @type {Array.<number>}
* @type {!Array.<number>}
*/
this.tUnit = [0, 0, 0];

Expand Down Expand Up @@ -4626,7 +4617,7 @@ libtess.GluVertex = function(opt_nextVertex, opt_prevVertex) {

/**
* The vertex location in 3D.
* @type {Array.<number>}
* @type {!Array.<number>}
*/
this.coords = [0, 0, 0];
// TODO(bckenny): we may want to rethink coords, either eliminate (using s
Expand Down
Loading

0 comments on commit 281ed0f

Please sign in to comment.