gs-json
Version:
gs-JSON is a domain agnostic unifying 3D file format for geometric and semantic modelling (hence the 'gs').
103 lines (90 loc) • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.makeVertices2D = makeVertices2D;
exports.triangulate2D = triangulate2D;
var _three = require("three");
var three = _interopRequireWildcard(_three);
var _threex = require("../threex/threex");
var threex = _interopRequireWildcard(_threex);
var _arr = require("../arr/arr");
var _earcut = require("./earcut");
var earcut = _interopRequireWildcard(_earcut);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
// 3D to 2D ======================================================================================================
/**
* Function to transform a set of vertices in 3d space onto the xy plane. This function assumes that the vertices
* are co-planar. Returns a set of three Vectors that represent points on the xy plane.
*/
function makeVertices2D(vertices) {
var points = threex.vectorsFromVertices(vertices);
var o = new three.Vector3();
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = points[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var v = _step.value;
o.add(v);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
o.divideScalar(points.length);
var vx = void 0;
var vz = void 0;
var got_vx = false;
for (var i = 0; i < vertices.length; i++) {
if (!got_vx) {
vx = threex.subVectors(points[i], o).normalize();
if (vx.lengthSq() !== 0) {
got_vx = true;
}
} else {
vz = threex.crossVectors(vx, threex.subVectors(points[i], o).normalize()).normalize();
if (vz.lengthSq() !== 0) {
break;
}
}
if (i === vertices.length - 1) {
return null;
}
}
var vy = threex.crossVectors(vz, vx);
var m = threex.xformMatrix(o, vx, vy, vz);
var points_2d = points.map(function (v) {
return threex.multVectorMatrix(v, m);
});
// console.log(o, vx, vy, vz);
// console.log(points_2d);
return points_2d;
}
// Triangulation =================================================================================================
function triangulate2D(vertices, verts_indexes) {
var points_2d = makeVertices2D(vertices);
if (points_2d === null) {
return [0, 1, 2].map(function (v) {
return verts_indexes[v];
});
}
var flat_vert_xyzs = _arr.Arr.flatten(points_2d.map(function (v) {
return [v.x, v.y];
}));
var tri_indexes = earcut.Earcut.triangulate(flat_vert_xyzs);
return tri_indexes.map(function (v) {
return verts_indexes[v];
});
}
//# sourceMappingURL=triangulate.js.map