Skip to content

Commit a519d8a

Browse files
committed
Allows specification of epsilon while retaining original defaults and API for compatibility (unless a dependency relies on the function.length)
1 parent e0d4222 commit a519d8a

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ Here is how to compute the vertex and face normals for the Stanford bunny:
1414

1515
```js
1616
var bunny = require("bunny");
17-
bunny.vertexNormals = require("normals").vertexNormals(bunny.cells, bunny.positions);
18-
bunny.faceNormals = require("normals").faceNormals(bunny.cells, bunny.positions);
17+
bunny.vertexNormals = require("normals").vertexNormals(bunny.cells, bunny.positions[,epsilon]);
18+
bunny.faceNormals = require("normals").faceNormals(bunny.cells, bunny.positions[,epsilon]);
1919
```
2020

21-
`require("normals").vertexNormals(cells, positions)`
21+
`require("normals").vertexNormals(cells, positions[,epsilon])`
2222
----------------------------------------------------
2323
This estimates the vertex normals for an oriented mesh.
2424

@@ -28,7 +28,7 @@ This estimates the vertex normals for an oriented mesh.
2828
Returns: An array of length = `positions.length` of the per-vertex normals.
2929

3030

31-
`require("normals").faceNormals(cells, positions)`
31+
`require("normals").faceNormals(cells, positions[,epsilon])`
3232
----------------------------------------------------
3333
This estimates the face normals for an oriented mesh.
3434

normals.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
var EPSILON = 1e-6;
1+
var DEFAULT_NORMALS_EPSILON = 1e-6;
2+
var DEFAULT_FACE_EPSILON = 1e-6;
23

34
//Estimate the vertex normals of a mesh
4-
exports.vertexNormals = function(faces, positions) {
5-
5+
exports.vertexNormals = function(faces, positions, specifiedEpsilon) {
6+
67
var N = positions.length;
78
var normals = new Array(N);
8-
9+
var epsilon = specifiedEpsilon === void(0) ? DEFAULT_NORMALS_EPSILON : specifiedEpsilon;
10+
911
//Initialize normal array
1012
for(var i=0; i<N; ++i) {
1113
normals[i] = [0.0, 0.0, 0.0];
1214
}
13-
15+
1416
//Walk over all the faces and add per-vertex contribution to normal weights
1517
for(var i=0; i<faces.length; ++i) {
1618
var f = faces[i];
1719
var p = 0;
1820
var c = f[f.length-1];
1921
var n = f[0];
2022
for(var j=0; j<f.length; ++j) {
21-
23+
2224
//Shift indices back
2325
p = c;
2426
c = n;
2527
n = f[(j+1) % f.length];
26-
28+
2729
var v0 = positions[p];
2830
var v1 = positions[c];
2931
var v2 = positions[n];
30-
32+
3133
//Compute infineteismal arcs
3234
var d01 = new Array(3);
3335
var m01 = 0.0;
@@ -41,7 +43,7 @@ exports.vertexNormals = function(faces, positions) {
4143
}
4244

4345
//Accumulate values in normal
44-
if(m01 * m21 > EPSILON) {
46+
if(m01 * m21 > epsilon) {
4547
var norm = normals[c];
4648
var w = 1.0 / Math.sqrt(m01 * m21);
4749
for(var k=0; k<3; ++k) {
@@ -52,15 +54,15 @@ exports.vertexNormals = function(faces, positions) {
5254
}
5355
}
5456
}
55-
57+
5658
//Scale all normals to unit length
5759
for(var i=0; i<N; ++i) {
5860
var norm = normals[i];
5961
var m = 0.0;
6062
for(var k=0; k<3; ++k) {
6163
m += norm[k] * norm[k];
6264
}
63-
if(m > EPSILON) {
65+
if(m > epsilon) {
6466
var w = 1.0 / Math.sqrt(m);
6567
for(var k=0; k<3; ++k) {
6668
norm[k] *= w;
@@ -77,24 +79,26 @@ exports.vertexNormals = function(faces, positions) {
7779
}
7880

7981
//Compute face normals of a mesh
80-
exports.faceNormals = function(faces, positions) {
82+
exports.faceNormals = function(faces, positions, specifiedEpsilon) {
83+
8184
var N = faces.length;
8285
var normals = new Array(N);
83-
86+
var epsilon = specifiedEpsilon === void(0) ? DEFAULT_FACE_EPSILON : specifiedEpsilon;
87+
8488
for(var i=0; i<N; ++i) {
8589
var f = faces[i];
8690
var pos = new Array(3);
8791
for(var j=0; j<3; ++j) {
8892
pos[j] = positions[f[j]];
8993
}
90-
94+
9195
var d01 = new Array(3);
9296
var d21 = new Array(3);
9397
for(var j=0; j<3; ++j) {
9498
d01[j] = pos[1][j] - pos[0][j];
9599
d21[j] = pos[2][j] - pos[0][j];
96100
}
97-
101+
98102
var n = new Array(3);
99103
var l = 0.0;
100104
for(var j=0; j<3; ++j) {
@@ -103,7 +107,7 @@ exports.faceNormals = function(faces, positions) {
103107
n[j] = d01[u] * d21[v] - d01[v] * d21[u];
104108
l += n[j] * n[j];
105109
}
106-
if(l > EPSILON) {
110+
if(l > epsilon) {
107111
l = 1.0 / Math.sqrt(l);
108112
} else {
109113
l = 0.0;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "normals",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Estimates normals for meshes",
55
"main": "normals.js",
66
"repository": {

0 commit comments

Comments
 (0)