forked from StandardCyborg/mesh-fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh-fixer.js
60 lines (47 loc) · 1.35 KB
/
mesh-fixer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var rasterize = require('haar-3d/rasterize-cells')
var contour = require('haar-3d/contour')
var calcNormals = require('angle-normals')
var refine = require('refine-mesh')
var createGrid = require('./lib/mesh-grid')
var projectVerts = require('./lib/project-verts')
function calcTolerance (cells, positions) {
var avgEdge = 0
for (var i = 0; i < cells.length; ++i) {
var c = cells[i]
for (var j = 0; j < 3; ++j) {
var a = positions[c[j]]
var b = positions[c[(j + 1) % 3]]
var d = Math.sqrt(
Math.pow(a[0] - b[0], 2) +
Math.pow(a[1] - b[1], 2) +
Math.pow(a[2] - b[2], 2))
avgEdge += d
}
}
return 2 * avgEdge / (3 * cells.length)
}
module.exports = function (cells, positions, _options) {
var options = _options || {}
var tolerance
if ('tolerance' in options) {
tolerance = options.tolerance
} else {
tolerance = calcTolerance(cells, positions)
}
var GRID_SIZE = 2 * tolerance
var mesh = contour(rasterize(cells, positions, {
resolution: 0.5 * tolerance
}))
var grid = createGrid(cells, positions, GRID_SIZE)
mesh.normals = calcNormals(mesh.cells, mesh.positions)
mesh = refine(mesh.cells, mesh.positions, mesh.normals, {
edgeLength: 0.5 * tolerance
})
projectVerts(
grid,
positions,
mesh.positions,
mesh.normals,
GRID_SIZE)
return mesh
}