Skip to content

Commit

Permalink
simplify projection of vertices to sweep plane
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Oct 28, 2015
1 parent 179a5d4 commit 9c2e13d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 149 deletions.
67 changes: 26 additions & 41 deletions libtess.cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -1173,9 +1173,9 @@ libtess.normal.projectPolygon = function(tess) {
computedNormal = true;
}

var sUnit = tess.sUnit;
var tUnit = tess.tUnit;
var i = libtess.normal.longAxis_(norm);
var vHead = tess.mesh.vHead;
var v;

// NOTE(bckenny): This branch is never taken. See comment on
// libtess.TRUE_PROJECT.
Expand All @@ -1185,6 +1185,9 @@ libtess.normal.projectPolygon = function(tess) {
// to the normal.
libtess.normal.normalize_(norm);

var sUnit = [0, 0, 0];
var tUnit = [0, 0, 0];

sUnit[i] = 0;
sUnit[(i + 1) % 3] = libtess.normal.S_UNIT_X_;
sUnit[(i + 2) % 3] = libtess.normal.S_UNIT_Y_;
Expand All @@ -1202,24 +1205,23 @@ libtess.normal.projectPolygon = function(tess) {
tUnit[2] = norm[0] * sUnit[1] - norm[1] * sUnit[0];
libtess.normal.normalize_(tUnit);

// Project the vertices onto the sweep plane
for (v = vHead.next; v !== vHead; v = v.next) {
v.s = libtess.normal.dot_(v.coords, sUnit);
v.t = libtess.normal.dot_(v.coords, tUnit);
}

} else {
// Project perpendicular to a coordinate axis -- better numerically
sUnit[i] = 0;
sUnit[(i + 1) % 3] = libtess.normal.S_UNIT_X_;
sUnit[(i + 2) % 3] = libtess.normal.S_UNIT_Y_;

tUnit[i] = 0;
tUnit[(i + 1) % 3] = (norm[i] > 0) ?
-libtess.normal.S_UNIT_Y_ : libtess.normal.S_UNIT_Y_;
tUnit[(i + 2) % 3] = (norm[i] > 0) ?
libtess.normal.S_UNIT_X_ : -libtess.normal.S_UNIT_X_;
}

// Project the vertices onto the sweep plane
var vHead = tess.mesh.vHead;
for (var v = vHead.next; v !== vHead; v = v.next) {
v.s = libtess.normal.dot_(v.coords, sUnit);
v.t = libtess.normal.dot_(v.coords, tUnit);
var sAxis = (i + 1) % 3;
var tAxis = (i + 2) % 3;
var tNegate = norm[i] > 0 ? 1 : -1;

// Project the vertices onto the sweep plane
for (v = vHead.next; v !== vHead; v = v.next) {
v.s = v.coords[sAxis];
v.t = tNegate * v.coords[tAxis];
}
}

if (computedNormal) {
Expand All @@ -1230,8 +1232,8 @@ libtess.normal.projectPolygon = function(tess) {
/**
* Computes the dot product of vectors u and v.
* @private
* @param {!Array.<number>} u
* @param {!Array.<number>} v
* @param {!Array<number>} u
* @param {!Array<number>} v
* @return {number}
*/
libtess.normal.dot_ = function(u, v) {
Expand All @@ -1244,7 +1246,7 @@ libtess.normal.dot_ = function(u, v) {
/**
* Normalize vector v.
* @private
* @param {!Array.<number>} v
* @param {!Array<number>} v
*/
libtess.normal.normalize_ = function(v) {
var len = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
Expand All @@ -1258,7 +1260,7 @@ libtess.normal.normalize_ = function(v) {
/**
* Returns the index of the longest component of vector v.
* @private
* @param {!Array.<number>} v
* @param {!Array<number>} v
* @return {number}
*/
libtess.normal.longAxis_ = function(v) {
Expand All @@ -1279,7 +1281,7 @@ libtess.normal.longAxis_ = function(v) {
* Result returned in norm.
* @private
* @param {!libtess.GluTesselator} tess
* @param {!Array.<number>} norm
* @param {!Array<number>} norm
*/
libtess.normal.computeNormal_ = function(tess, norm) {
var maxVal = [
Expand Down Expand Up @@ -1376,9 +1378,6 @@ libtess.normal.checkOrientation_ = function(tess) {
for (var v = vHead.next; v !== vHead; v = v.next) {
v.t = -v.t;
}
tess.tUnit[0] = -tess.tUnit[0];
tess.tUnit[1] = -tess.tUnit[1];
tess.tUnit[2] = -tess.tUnit[2];
}
};

Expand Down Expand Up @@ -3303,25 +3302,11 @@ 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>}
*/
this.sUnit = [0, 0, 0];

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

/*** state needed for the line sweep ***/
// TODO(bckenny): this could be moved to a sweep state object of some sort

/**
* tolerance for merging features
Expand Down
67 changes: 26 additions & 41 deletions libtess.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,9 @@ libtess.normal.projectPolygon = function(tess) {
computedNormal = true;
}

var sUnit = tess.sUnit;
var tUnit = tess.tUnit;
var i = libtess.normal.longAxis_(norm);
var vHead = tess.mesh.vHead;
var v;

// NOTE(bckenny): This branch is never taken. See comment on
// libtess.TRUE_PROJECT.
Expand All @@ -1189,6 +1189,9 @@ libtess.normal.projectPolygon = function(tess) {
// to the normal.
libtess.normal.normalize_(norm);

var sUnit = [0, 0, 0];
var tUnit = [0, 0, 0];

sUnit[i] = 0;
sUnit[(i + 1) % 3] = libtess.normal.S_UNIT_X_;
sUnit[(i + 2) % 3] = libtess.normal.S_UNIT_Y_;
Expand All @@ -1206,24 +1209,23 @@ libtess.normal.projectPolygon = function(tess) {
tUnit[2] = norm[0] * sUnit[1] - norm[1] * sUnit[0];
libtess.normal.normalize_(tUnit);

// Project the vertices onto the sweep plane
for (v = vHead.next; v !== vHead; v = v.next) {
v.s = libtess.normal.dot_(v.coords, sUnit);
v.t = libtess.normal.dot_(v.coords, tUnit);
}

} else {
// Project perpendicular to a coordinate axis -- better numerically
sUnit[i] = 0;
sUnit[(i + 1) % 3] = libtess.normal.S_UNIT_X_;
sUnit[(i + 2) % 3] = libtess.normal.S_UNIT_Y_;

tUnit[i] = 0;
tUnit[(i + 1) % 3] = (norm[i] > 0) ?
-libtess.normal.S_UNIT_Y_ : libtess.normal.S_UNIT_Y_;
tUnit[(i + 2) % 3] = (norm[i] > 0) ?
libtess.normal.S_UNIT_X_ : -libtess.normal.S_UNIT_X_;
}

// Project the vertices onto the sweep plane
var vHead = tess.mesh.vHead;
for (var v = vHead.next; v !== vHead; v = v.next) {
v.s = libtess.normal.dot_(v.coords, sUnit);
v.t = libtess.normal.dot_(v.coords, tUnit);
var sAxis = (i + 1) % 3;
var tAxis = (i + 2) % 3;
var tNegate = norm[i] > 0 ? 1 : -1;

// Project the vertices onto the sweep plane
for (v = vHead.next; v !== vHead; v = v.next) {
v.s = v.coords[sAxis];
v.t = tNegate * v.coords[tAxis];
}
}

if (computedNormal) {
Expand All @@ -1234,8 +1236,8 @@ libtess.normal.projectPolygon = function(tess) {
/**
* Computes the dot product of vectors u and v.
* @private
* @param {!Array.<number>} u
* @param {!Array.<number>} v
* @param {!Array<number>} u
* @param {!Array<number>} v
* @return {number}
*/
libtess.normal.dot_ = function(u, v) {
Expand All @@ -1248,7 +1250,7 @@ libtess.normal.dot_ = function(u, v) {
/**
* Normalize vector v.
* @private
* @param {!Array.<number>} v
* @param {!Array<number>} v
*/
libtess.normal.normalize_ = function(v) {
var len = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
Expand All @@ -1263,7 +1265,7 @@ libtess.normal.normalize_ = function(v) {
/**
* Returns the index of the longest component of vector v.
* @private
* @param {!Array.<number>} v
* @param {!Array<number>} v
* @return {number}
*/
libtess.normal.longAxis_ = function(v) {
Expand All @@ -1284,7 +1286,7 @@ libtess.normal.longAxis_ = function(v) {
* Result returned in norm.
* @private
* @param {!libtess.GluTesselator} tess
* @param {!Array.<number>} norm
* @param {!Array<number>} norm
*/
libtess.normal.computeNormal_ = function(tess, norm) {
var maxVal = [
Expand Down Expand Up @@ -1381,9 +1383,6 @@ libtess.normal.checkOrientation_ = function(tess) {
for (var v = vHead.next; v !== vHead; v = v.next) {
v.t = -v.t;
}
tess.tUnit[0] = -tess.tUnit[0];
tess.tUnit[1] = -tess.tUnit[1];
tess.tUnit[2] = -tess.tUnit[2];
}
};

Expand Down Expand Up @@ -3336,25 +3335,11 @@ 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>}
*/
this.sUnit = [0, 0, 0];

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

/*** state needed for the line sweep ***/
// TODO(bckenny): this could be moved to a sweep state object of some sort

/**
* tolerance for merging features
Expand Down
Loading

0 comments on commit 9c2e13d

Please sign in to comment.