Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Here is how to compute the vertex and face normals for the Stanford bunny:

```js
var bunny = require("bunny");
bunny.vertexNormals = require("normals").vertexNormals(bunny.cells, bunny.positions);
bunny.faceNormals = require("normals").faceNormals(bunny.cells, bunny.positions);
bunny.vertexNormals = require("normals").vertexNormals(bunny.cells, bunny.positions[,epsilon]);
bunny.faceNormals = require("normals").faceNormals(bunny.cells, bunny.positions[,epsilon]);
```

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

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


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

Expand Down
36 changes: 20 additions & 16 deletions normals.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
var EPSILON = 1e-6;
var DEFAULT_NORMALS_EPSILON = 1e-6;
var DEFAULT_FACE_EPSILON = 1e-6;

//Estimate the vertex normals of a mesh
exports.vertexNormals = function(faces, positions) {
exports.vertexNormals = function(faces, positions, specifiedEpsilon) {

var N = positions.length;
var normals = new Array(N);

var epsilon = specifiedEpsilon === void(0) ? DEFAULT_NORMALS_EPSILON : specifiedEpsilon;

//Initialize normal array
for(var i=0; i<N; ++i) {
normals[i] = [0.0, 0.0, 0.0];
}

//Walk over all the faces and add per-vertex contribution to normal weights
for(var i=0; i<faces.length; ++i) {
var f = faces[i];
var p = 0;
var c = f[f.length-1];
var n = f[0];
for(var j=0; j<f.length; ++j) {

//Shift indices back
p = c;
c = n;
n = f[(j+1) % f.length];

var v0 = positions[p];
var v1 = positions[c];
var v2 = positions[n];

//Compute infineteismal arcs
var d01 = new Array(3);
var m01 = 0.0;
Expand All @@ -41,7 +43,7 @@ exports.vertexNormals = function(faces, positions) {
}

//Accumulate values in normal
if(m01 * m21 > EPSILON) {
if(m01 * m21 > epsilon) {
var norm = normals[c];
var w = 1.0 / Math.sqrt(m01 * m21);
for(var k=0; k<3; ++k) {
Expand All @@ -52,15 +54,15 @@ exports.vertexNormals = function(faces, positions) {
}
}
}

//Scale all normals to unit length
for(var i=0; i<N; ++i) {
var norm = normals[i];
var m = 0.0;
for(var k=0; k<3; ++k) {
m += norm[k] * norm[k];
}
if(m > EPSILON) {
if(m > epsilon) {
var w = 1.0 / Math.sqrt(m);
for(var k=0; k<3; ++k) {
norm[k] *= w;
Expand All @@ -77,24 +79,26 @@ exports.vertexNormals = function(faces, positions) {
}

//Compute face normals of a mesh
exports.faceNormals = function(faces, positions) {
exports.faceNormals = function(faces, positions, specifiedEpsilon) {

var N = faces.length;
var normals = new Array(N);

var epsilon = specifiedEpsilon === void(0) ? DEFAULT_FACE_EPSILON : specifiedEpsilon;

for(var i=0; i<N; ++i) {
var f = faces[i];
var pos = new Array(3);
for(var j=0; j<3; ++j) {
pos[j] = positions[f[j]];
}

var d01 = new Array(3);
var d21 = new Array(3);
for(var j=0; j<3; ++j) {
d01[j] = pos[1][j] - pos[0][j];
d21[j] = pos[2][j] - pos[0][j];
}

var n = new Array(3);
var l = 0.0;
for(var j=0; j<3; ++j) {
Expand All @@ -103,7 +107,7 @@ exports.faceNormals = function(faces, positions) {
n[j] = d01[u] * d21[v] - d01[v] * d21[u];
l += n[j] * n[j];
}
if(l > EPSILON) {
if(l > epsilon) {
l = 1.0 / Math.sqrt(l);
} else {
l = 0.0;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "normals",
"version": "1.0.0",
"version": "1.0.1",
"description": "Estimates normals for meshes",
"main": "normals.js",
"repository": {
Expand Down