gs-json
Version:
gs-JSON is a domain agnostic unifying 3D file format for geometric and semantic modelling (hence the 'gs').
1,174 lines (1,074 loc) • 237 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Kernel = undefined;
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _arr2 = require("./libs/arr/arr");
var _enums = require("./enums");
var _geom = require("./geom");
var _topo_trees = require("./libs/topo_trees/topo_trees");
var _three = require("three");
var three = _interopRequireWildcard(_three);
var _threex = require("./libs/threex/threex");
var threex = _interopRequireWildcard(_threex);
var _uuid = require("./libs/uuid/uuid");
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; } }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Kernel Class
* This class controls all access to the data and ensures that the data remains consistent.
* No other class should have any direct access to this data.
*/
var Kernel = exports.Kernel = function () {
/**
* Construct a new kernel. If data is provided, the model will be populated with this data.
* @param
* @return
*/
function Kernel(model, data) {
var _this = this;
_classCallCheck(this, Kernel);
this._model = model;
this._geom = new _geom.Geom(this);
this._attribs = new Map();
this._attribs.set(_enums.EGeomType.points, new Map());
this._attribs.set(_enums.EGeomType.objs, new Map());
this._attribs.set(_enums.EGeomType.vertices, new Map());
this._attribs.set(_enums.EGeomType.edges, new Map());
this._attribs.set(_enums.EGeomType.wires, new Map());
this._attribs.set(_enums.EGeomType.faces, new Map());
this._groups = new Map();
this._topos_trees = new Map();
// Set the data
if (data && data.metadata !== undefined) {
this._metadata = data.metadata;
} else {
this._metadata = { filetype: "gs-json", version: "0.1.8", uuid: (0, _uuid.create_UUID)() };
}
// Geom points
if (data && data.geom !== undefined && data.geom.points !== undefined) {
data.geom.points[0].forEach(function (p, i) {
return p === null && delete data.geom.points[0][i];
});
this._points = data.geom.points;
} else {
this._points = [[], [null]];
}
// Geom objs
if (data && data.geom !== undefined && data.geom.objs !== undefined) {
data.geom.objs.forEach(function (o, i) {
return o === null && delete data.geom.objs[i];
});
this._objs = data.geom.objs;
} else {
this._objs = [];
}
// Attributes
if (data && data.attribs && data.attribs.points !== undefined) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
var _loop = function _loop() {
var attrib_data = _step.value;
attrib_data.values[0].forEach(function (d, i) {
return d === null && delete attrib_data.values[0][i];
});
_this._attribs.get(_enums.EGeomType.points).set(attrib_data.name, attrib_data);
};
for (var _iterator = data.attribs.points[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
_loop();
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
if (data && data.attribs && data.attribs.objs !== undefined) {
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
var _loop2 = function _loop2() {
var attrib_data = _step2.value;
attrib_data.values[0].forEach(function (d, i) {
return d === null && delete attrib_data.values[0][i];
});
_this._attribs.get(_enums.EGeomType.objs).set(attrib_data.name, attrib_data);
};
for (var _iterator2 = data.attribs.objs[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
_loop2();
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
}
if (data && data.attribs && data.attribs.vertices !== undefined) {
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
var _loop3 = function _loop3() {
var attrib_data = _step3.value;
attrib_data.values[0].forEach(function (d, i) {
return d === null && delete attrib_data.values[0][i];
});
_this._attribs.get(_enums.EGeomType.vertices).set(attrib_data.name, attrib_data);
};
for (var _iterator3 = data.attribs.vertices[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
_loop3();
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
if (data && data.attribs && data.attribs.edges !== undefined) {
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
var _loop4 = function _loop4() {
var attrib_data = _step4.value;
attrib_data.values[0].forEach(function (d, i) {
return d === null && delete attrib_data.values[0][i];
});
_this._attribs.get(_enums.EGeomType.edges).set(attrib_data.name, attrib_data);
};
for (var _iterator4 = data.attribs.edges[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
_loop4();
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
}
if (data && data.attribs && data.attribs.wires !== undefined) {
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
var _loop5 = function _loop5() {
var attrib_data = _step5.value;
attrib_data.values[0].forEach(function (d, i) {
return d === null && delete attrib_data.values[0][i];
});
_this._attribs.get(_enums.EGeomType.wires).set(attrib_data.name, attrib_data);
};
for (var _iterator5 = data.attribs.wires[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
_loop5();
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
}
if (data && data.attribs && data.attribs.faces !== undefined) {
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
var _loop6 = function _loop6() {
var attrib_data = _step6.value;
attrib_data.values[0].forEach(function (d, i) {
return d === null && delete attrib_data.values[0][i];
});
_this._attribs.get(_enums.EGeomType.faces).set(attrib_data.name, attrib_data);
};
for (var _iterator6 = data.attribs.faces[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
_loop6();
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
}
}
// Groups
if (data && data.attribs && data.groups !== undefined) {
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator7 = data.groups[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var group_data = _step7.value;
if (group_data.parent === undefined) {
group_data.parent = null;
}
if (group_data.props === undefined) {
group_data.props = [];
}
if (group_data.objs === undefined) {
group_data.objs = [];
}
if (group_data.points === undefined) {
group_data.points = [];
}
this._topos_trees.set(group_data.name, new _topo_trees.TopoTree(group_data.topos));
group_data.topos = undefined;
this._groups.set(group_data.name, group_data);
}
} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError7) {
throw _iteratorError7;
}
}
}
}
}
// Model General ------------------------------------------------------------------------------
/**
* Exports the model as a data object of type IModelData.
* @param
* @return
*/
_createClass(Kernel, [{
key: "modelToModelData",
value: function modelToModelData() {
var model_data = {
metadata: this._metadata,
geom: {
points: this._points,
objs: this._objs
}
};
if (this._attribs !== undefined) {
model_data.attribs = {};
if (this._attribs.get(_enums.EGeomType.points) !== undefined) {
model_data.attribs.points = Array.from(this._attribs.get(_enums.EGeomType.points).values());
}
if (this._attribs.get(_enums.EGeomType.vertices) !== undefined) {
model_data.attribs.vertices = Array.from(this._attribs.get(_enums.EGeomType.vertices).values());
}
if (this._attribs.get(_enums.EGeomType.edges) !== undefined) {
model_data.attribs.edges = Array.from(this._attribs.get(_enums.EGeomType.edges).values());
}
if (this._attribs.get(_enums.EGeomType.wires) !== undefined) {
model_data.attribs.wires = Array.from(this._attribs.get(_enums.EGeomType.wires).values());
}
if (this._attribs.get(_enums.EGeomType.faces) !== undefined) {
model_data.attribs.faces = Array.from(this._attribs.get(_enums.EGeomType.faces).values());
}
if (this._attribs.get(_enums.EGeomType.objs) !== undefined) {
model_data.attribs.objs = Array.from(this._attribs.get(_enums.EGeomType.objs).values());
}
}
//TODO add topos to groups
// In the IGroupData, groups data consists of the following:
// name: string;
// parent?: string;
// objs?: number[];
// topos?: TTreeData; <<< This is an array of 2 x TreeBranch2, and 4 x TreeBranch3
// points?: number[];
// props?: Array<[string, any]>;
// This kernel maintains a this._groups Map of such data, group_name -> group_data
// When saving, the evalues of teh map are saved
if (this._groups !== undefined) {
model_data.groups = Array.from(this._groups.values());
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
try {
for (var _iterator8 = model_data.groups[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var group = _step8.value;
group.topos = []; //TODO add the topo data here
}
} catch (err) {
_didIteratorError8 = true;
_iteratorError8 = err;
} finally {
try {
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
}
} finally {
if (_didIteratorError8) {
throw _iteratorError8;
}
}
}
}
return model_data;
}
/**
* Exports the model as a json string.
* @param
* @return
*/
}, {
key: "modelToJSON",
value: function modelToJSON() {
var jsonData = this.modelToModelData();
return JSON.stringify(jsonData, null, 4);
}
/**
* Cleans up.
* @param
* @return
*/
}, {
key: "modelPurge",
value: function modelPurge() {
this._purgeDelUnusedPoints();
this._purgeDelUnusedPointValues();
}
/**
* Merges the data from another model into this kernel.
* @param data Modle data, as IModelData object.
*/
}, {
key: "modelMerge",
value: function modelMerge(data) {
// Get the number of points
var num_ex_points = this._points[0].length;
var point_id_map = new Map();
var obj_id_map = new Map();
// Merge Geom points
if (data.geom !== undefined && data.geom.points !== undefined) {
// add the points one by one, populating a map as we go
for (var i = 0; i < data.geom.points[0].length; i++) {
if (data.geom.points[0][i] !== undefined && data.geom.points[0][i] !== null) {
// TODO null // check that point has not been deleted
var old_id = i;
var xyz = data.geom.points[1][data.geom.points[0][i]];
var new_id = this.geomAddPoint(xyz);
point_id_map.set(old_id, new_id);
}
}
}
// Merge Geom objs
if (data.geom !== undefined && data.geom.objs !== undefined) {
// add the objs one by one, populating a map as we go
for (var _i = 0; _i < data.geom.objs.length; _i++) {
if (data.geom.objs[_i] !== undefined && data.geom.objs[_i] !== null) {
// TODO null // check that obj has not been deleted
var _old_id = _i;
var old_obj = data.geom.objs[_i];
var new_obj = [[], [], []];
// wires
var _iteratorNormalCompletion9 = true;
var _didIteratorError9 = false;
var _iteratorError9 = undefined;
try {
for (var _iterator9 = old_obj[0][Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
var old_wire = _step9.value;
var new_wire = [];
var _iteratorNormalCompletion11 = true;
var _didIteratorError11 = false;
var _iteratorError11 = undefined;
try {
for (var _iterator11 = old_wire[Symbol.iterator](), _step11; !(_iteratorNormalCompletion11 = (_step11 = _iterator11.next()).done); _iteratorNormalCompletion11 = true) {
var old_point_id = _step11.value;
if (old_point_id !== -1) {
new_wire.push(point_id_map.get(old_point_id));
} else {
new_wire.push(-1);
}
}
} catch (err) {
_didIteratorError11 = true;
_iteratorError11 = err;
} finally {
try {
if (!_iteratorNormalCompletion11 && _iterator11.return) {
_iterator11.return();
}
} finally {
if (_didIteratorError11) {
throw _iteratorError11;
}
}
}
new_obj[0].push(new_wire);
}
// faces
} catch (err) {
_didIteratorError9 = true;
_iteratorError9 = err;
} finally {
try {
if (!_iteratorNormalCompletion9 && _iterator9.return) {
_iterator9.return();
}
} finally {
if (_didIteratorError9) {
throw _iteratorError9;
}
}
}
var _iteratorNormalCompletion10 = true;
var _didIteratorError10 = false;
var _iteratorError10 = undefined;
try {
for (var _iterator10 = old_obj[1][Symbol.iterator](), _step10; !(_iteratorNormalCompletion10 = (_step10 = _iterator10.next()).done); _iteratorNormalCompletion10 = true) {
var old_face = _step10.value;
var new_face = [];
var _iteratorNormalCompletion12 = true;
var _didIteratorError12 = false;
var _iteratorError12 = undefined;
try {
for (var _iterator12 = old_face[Symbol.iterator](), _step12; !(_iteratorNormalCompletion12 = (_step12 = _iterator12.next()).done); _iteratorNormalCompletion12 = true) {
var _old_point_id = _step12.value;
if (_old_point_id !== -1) {
new_face.push(point_id_map.get(_old_point_id));
} else {
new_face.push(-1);
}
}
} catch (err) {
_didIteratorError12 = true;
_iteratorError12 = err;
} finally {
try {
if (!_iteratorNormalCompletion12 && _iterator12.return) {
_iterator12.return();
}
} finally {
if (_didIteratorError12) {
throw _iteratorError12;
}
}
}
new_obj[1].push(new_face);
}
// parameters
} catch (err) {
_didIteratorError10 = true;
_iteratorError10 = err;
} finally {
try {
if (!_iteratorNormalCompletion10 && _iterator10.return) {
_iterator10.return();
}
} finally {
if (_didIteratorError10) {
throw _iteratorError10;
}
}
}
new_obj[2] = _arr2.Arr.deepCopy(old_obj[2]);
// push the new obj
var _new_id = this._objs.push(new_obj) - 1;
obj_id_map.set(_old_id, _new_id);
}
}
}
// Mereg Attributes
// if (data.attribs && data.attribs.points !== undefined) {
// for (const old_attrib_data of data.attribs.points) {
// const new_attrib_data
// if (attrib_data) {
// }
// attrib_data.values[0].forEach((d, i) => (d === null) && delete attrib_data.values[0][i]);
// this._attribs.get(EGeomType.points).set(attrib_data.name, attrib_data);
// }
// }
// if (data.attribs && data.attribs.objs !== undefined) {
// for (const attrib_data of data.attribs.objs) {
// attrib_data.values[0].forEach((d, i) => (d === null) && delete attrib_data.values[0][i]);
// this._attribs.get(EGeomType.objs).set(attrib_data.name, attrib_data);
// }
// }
// if (data.attribs && data.attribs.vertices !== undefined) {
// for (const attrib_data of data.attribs.vertices) {
// attrib_data.values[0].forEach((d, i) => (d === null) && delete attrib_data.values[0][i]);
// this._attribs.get(EGeomType.vertices).set(attrib_data.name, attrib_data);
// }
// }
// if (data.attribs && data.attribs.edges !== undefined) {
// for (const attrib_data of data.attribs.edges) {
// attrib_data.values[0].forEach((d, i) => (d === null) && delete attrib_data.values[0][i]);
// this._attribs.get(EGeomType.edges).set(attrib_data.name, attrib_data);
// }
// }
// if (data.attribs && data.attribs.wires !== undefined) {
// for (const attrib_data of data.attribs.wires) {
// attrib_data.values[0].forEach((d, i) => (d === null) && delete attrib_data.values[0][i]);
// this._attribs.get(EGeomType.wires).set(attrib_data.name, attrib_data);
// }
// }
// if (data.attribs && data.attribs.faces !== undefined) {
// for (const attrib_data of data.attribs.faces) {
// attrib_data.values[0].forEach((d, i) => (d === null) && delete attrib_data.values[0][i]);
// this._attribs.get(EGeomType.faces).set(attrib_data.name, attrib_data);
// }
// }
// Merge Groups
if (data.attribs && data.groups !== undefined) {
var _iteratorNormalCompletion13 = true;
var _didIteratorError13 = false;
var _iteratorError13 = undefined;
try {
for (var _iterator13 = data.groups[Symbol.iterator](), _step13; !(_iteratorNormalCompletion13 = (_step13 = _iterator13.next()).done); _iteratorNormalCompletion13 = true) {
var old_group_data = _step13.value;
var new_group_data = { name: old_group_data.name };
// parent
if (old_group_data.parent !== undefined) {
new_group_data.parent = old_group_data.parent;
} else {
new_group_data.parent = null;
}
// properties
if (old_group_data.props !== undefined) {
new_group_data.props = _arr2.Arr.deepCopy(old_group_data.props);
} else {
new_group_data.props = [];
}
// map point IDs
new_group_data.points = [];
if (old_group_data.points !== undefined) {
var _iteratorNormalCompletion14 = true;
var _didIteratorError14 = false;
var _iteratorError14 = undefined;
try {
for (var _iterator14 = old_group_data.points[Symbol.iterator](), _step14; !(_iteratorNormalCompletion14 = (_step14 = _iterator14.next()).done); _iteratorNormalCompletion14 = true) {
var _old_point_id2 = _step14.value;
new_group_data.points.push(point_id_map.get(_old_point_id2));
}
} catch (err) {
_didIteratorError14 = true;
_iteratorError14 = err;
} finally {
try {
if (!_iteratorNormalCompletion14 && _iterator14.return) {
_iterator14.return();
}
} finally {
if (_didIteratorError14) {
throw _iteratorError14;
}
}
}
}
// map obj IDs
new_group_data.objs = [];
if (old_group_data.objs !== undefined) {
var _iteratorNormalCompletion15 = true;
var _didIteratorError15 = false;
var _iteratorError15 = undefined;
try {
for (var _iterator15 = old_group_data.objs[Symbol.iterator](), _step15; !(_iteratorNormalCompletion15 = (_step15 = _iterator15.next()).done); _iteratorNormalCompletion15 = true) {
var old_obj_id = _step15.value;
new_group_data.objs.push(obj_id_map.get(old_obj_id));
}
} catch (err) {
_didIteratorError15 = true;
_iteratorError15 = err;
} finally {
try {
if (!_iteratorNormalCompletion15 && _iterator15.return) {
_iterator15.return();
}
} finally {
if (_didIteratorError15) {
throw _iteratorError15;
}
}
}
}
// map topo trees
if (old_group_data.topos !== undefined) {
new_group_data.topos = _arr2.Arr.deepCopy(old_group_data.topos);
} else {
new_group_data.topos = [];
}
// check if the name already exists
var existing_group = this._groups.get(new_group_data.name);
if (existing_group !== undefined) {
var _existing_group$props, _existing_group$point, _existing_group$objs;
// add the group data to the existing group
existing_group.parent = new_group_data.parent; // old parent will be overwritten
(_existing_group$props = existing_group.props).push.apply(_existing_group$props, _toConsumableArray(new_group_data.props));
(_existing_group$point = existing_group.points).push.apply(_existing_group$point, _toConsumableArray(new_group_data.points));
(_existing_group$objs = existing_group.objs).push.apply(_existing_group$objs, _toConsumableArray(new_group_data.objs));
// TODO add the topo trees to the existing group
} else {
// add a new group to the model
this._groups.set(new_group_data.name, new_group_data);
// create the topo trees
this._topos_trees.set(new_group_data.name, new _topo_trees.TopoTree(new_group_data.topos));
}
}
} catch (err) {
_didIteratorError13 = true;
_iteratorError13 = err;
} finally {
try {
if (!_iteratorNormalCompletion13 && _iterator13.return) {
_iterator13.return();
}
} finally {
if (_didIteratorError13) {
throw _iteratorError13;
}
}
}
}
}
/**
* Checks the model.
* @param
* @return
*/
}, {
key: "modelValidate",
value: function modelValidate() {
throw new Error("Method not implemented.");
}
// The Model object ---------------------------------------------------------------------------
/**
* Get the Model object
* @return The Model object
*/
}, {
key: "getModel",
value: function getModel() {
return this._model;
}
/**
* Get the Geom object
* @return The Model object
*/
}, {
key: "getGeom",
value: function getGeom() {
return this._geom;
}
// Model attributes ---------------------------------------------------------------------------
/**
* Find attributes in the model of a particular type.
* @param
* @return
*/
}, {
key: "modelFindAttribs",
value: function modelFindAttribs(geom_type) {
switch (geom_type) {
case _enums.EGeomType.points:
return Array.from(this._attribs.get(geom_type).values());
case _enums.EGeomType.objs:
return Array.from(this._attribs.get(geom_type).values());
case _enums.EGeomType.faces:
return Array.from(this._attribs.get(geom_type).values());
case _enums.EGeomType.wires:
return Array.from(this._attribs.get(geom_type).values());
case _enums.EGeomType.edges:
return Array.from(this._attribs.get(geom_type).values());
case _enums.EGeomType.vertices:
return Array.from(this._attribs.get(geom_type).values());
}
}
/**
* Get all the attributes in the model.
* @param
* @return
*/
}, {
key: "modelGetAllAttribs",
value: function modelGetAllAttribs() {
return [].concat(_toConsumableArray(this.modelFindAttribs(_enums.EGeomType.points)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.vertices)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.edges)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.wires)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.faces)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.objs)));
}
/**
* Get all attributes in the model, except point attributes.
* @param
* @return
*/
}, {
key: "modelGetAllAttribsExcPoints",
value: function modelGetAllAttribsExcPoints() {
return [].concat(_toConsumableArray(this.modelFindAttribs(_enums.EGeomType.vertices)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.edges)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.wires)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.faces)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.objs)));
}
/**
* Get all entity attributes in the model.
* @param
* @return
*/
}, {
key: "modelGetAllEntAttribs",
value: function modelGetAllEntAttribs() {
return [].concat(_toConsumableArray(this.modelFindAttribs(_enums.EGeomType.points)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.objs)));
}
/**
* Get all topo attributes in the model.
* @param
* @return
*/
}, {
key: "modelGetAllTopoAttribs",
value: function modelGetAllTopoAttribs() {
return [].concat(_toConsumableArray(this.modelFindAttribs(_enums.EGeomType.vertices)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.edges)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.wires)), _toConsumableArray(this.modelFindAttribs(_enums.EGeomType.faces)));
}
/**
* Get an attribute from the model.
* @param
* @return
*/
}, {
key: "modelGetAttrib",
value: function modelGetAttrib(name, geom_type) {
return this._attribs.get(geom_type).get(name);
}
/**
* Add a new attribute to the model.
* @param
* @return
*/
}, {
key: "modelAddAttrib",
value: function modelAddAttrib(name, geom_type, data_type) {
if (this.modelHasAttrib(name, geom_type)) {
return this.modelGetAttrib(name, geom_type);
}
// name = name.replace(/\s/g, "_");
var data = {
data_type: _enums.mapDataTypeToString.get(data_type),
geom_type: _enums.mapGeomTypeToString.get(geom_type),
name: name,
values: [[], [null]]
};
// save and return data
this._attribs.get(geom_type).set(name, data);
// populate the attribute with indexes all pointing to the null value
this._newAttribAddObjsAndPoints(name, geom_type);
// return the new attribute
return this._attribs.get(geom_type).get(name);
}
/**
* Delete an attribute from the model.
* @param
* @return
*/
}, {
key: "modelDelAttrib",
value: function modelDelAttrib(name, geom_type) {
return this._attribs.get(geom_type).delete(name);
}
/**
* Check is a model has an attribute.
* @param
* @return
*/
}, {
key: "modelHasAttrib",
value: function modelHasAttrib(name, geom_type) {
return this._attribs.get(geom_type).has(name);
}
// Model Groups -------------------------------------------------------------------------------
/**
* Get all the groups in the model.
* @param
* @return
*/
}, {
key: "modelGetAllGroups",
value: function modelGetAllGroups() {
return Array.from(this._groups.values());
}
/**
* Get one group in the model.
* @param
* @return
*/
}, {
key: "modelGetGroup",
value: function modelGetGroup(name) {
return this._groups.get(name);
}
/**
* Add a new group to the model.
* If a group with this name already exists, then that group is returned.
* @param
* @return
*/
}, {
key: "modelAddGroup",
value: function modelAddGroup(name, parent) {
if (this.modelHasGroup(name)) {
return this.modelGetGroup(name);
}
var data = { name: name, parent: null, props: [], points: [], objs: [] };
if (parent !== undefined) {
if (this._groups.has(parent)) {
data.parent = parent;
} else {
throw new Error("Parent group does not exist.");
}
}
this._groups.set(name, data);
this._topos_trees.set(name, new _topo_trees.TopoTree());
return data;
}
/**
* Delete a group from the model.
* @param
* @return
*/
}, {
key: "modelDelGroup",
value: function modelDelGroup(name) {
var group = this._groups.delete(name);
var tree = this._topos_trees.delete(name);
return group && tree;
}
/**
* Check if the group exists in the model.
* @param
* @return
*/
}, {
key: "modelHasGroup",
value: function modelHasGroup(name) {
return this._groups.has(name);
}
// Geom Points --------------------------------------------------------------------------------
/**
* Check if the geometry has this point
* @param
* @return
*/
}, {
key: "geomHasPoint",
value: function geomHasPoint(id) {
return this._points[0][id] !== undefined;
}
/**
* Adds a new point to the model at position xyz.
* @param cartesian xyz coordinates are required to create a point
* @return a instance of type Point is returned
*/
}, {
key: "geomAddPoint",
value: function geomAddPoint(xyz) {
var new_id = this._points[0].length; // next in sparse array
// create the point
this._points[0].push(0); // add a point to the points list
this.pointSetPosition(new_id, xyz);
// update point attributes
this._newPointAddToAttribs(new_id);
return new_id;
}
/**
* Add a set of points to the model based on an array of xyz positions.
* @param
* @return
*/
}, {
key: "geomAddPoints",
value: function geomAddPoints(xyzs) {
var _this2 = this;
return xyzs.map(function (xyz) {
return _this2.geomAddPoint(xyz);
});
}
/**
* Copy a point. The new point will have the same position as the original point.
* If copy_attribs is true, then the copied point will have the same attributes as the original point.
* @param id
* @param copy_attribs
* @return
*/
}, {
key: "geomCopyPoint",
value: function geomCopyPoint(id) {
var copy_attribs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var new_id = this._points[0].length;
// create the ray
this._points[0].push(this._points[0][id]); // add the point, set same position
// update all attributes
this._copiedPointAddToAttribs(new_id, id, copy_attribs);
// return the new id
return new_id;
}
/**
* Copy a set of points. The new points will have the same positions as the original points.
* If copy_attribs is true, then the copied points will have the same attribute values as the original points.
* @param ids
* @param copy_attribs
* @return
*/
}, {
key: "geomCopyPoints",
value: function geomCopyPoints(ids) {
var _this3 = this;
var copy_attribs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return ids.map(function (id) {
return _this3.geomCopyPoint(id, copy_attribs);
});
}
/**
* Delete a point from the model.
* @param
* @return
*/
}, {
key: "geomDelPoint",
value: function geomDelPoint(id) {
// delete the point from the geometry array
if (this._points[0][id] === undefined) {
return false;
}
// delete the point
delete this._points[0][id];
// delete the point from any geometrc objects
this._updateObjsForDelPoint(id);
// delete the point from attribs
this._updateAttribsForDelPoint(id);
// delete the points from groups
this._updateGroupsForDelPoint(id);
// all seem ok
return true;
}
/**
* Delete a list of points from the model.
* @param
* @return
*/
}, {
key: "geomDelPoints",
value: function geomDelPoints(ids) {
var ok = true;
var _iteratorNormalCompletion16 = true;
var _didIteratorError16 = false;
var _iteratorError16 = undefined;
try {
for (var _iterator16 = ids[Symbol.iterator](), _step16; !(_iteratorNormalCompletion16 = (_step16 = _iterator16.next()).done); _iteratorNormalCompletion16 = true) {
var id = _step16.value;
if (this.geomHasPoint(id)) {
if (!this.geomDelPoint(id)) {
ok = false;
}
}
}
} catch (err) {
_didIteratorError16 = true;
_iteratorError16 = err;
} finally {
try {
if (!_iteratorNormalCompletion16 && _iterator16.return) {
_iterator16.return();
}
} finally {
if (_didIteratorError16) {
throw _iteratorError16;
}
}
}
return ok;
}
/**
* Returns the number of points in the model.
* @param
* @return
*/
}, {
key: "geomNumPoints",
value: function geomNumPoints() {
return this._points[0].filter(function (n) {
return n !== undefined;
}).length; // ignores empty slots in spare array
}
/**
* Get the list of all point IDs in the model.
* The list does not include the empty slots.
* @param
* @return
*/
}, {
key: "geomGetPointIDs",
value: function geomGetPointIDs() {
var point_ids = [];
this._points[0].forEach(function (v, i) {
return v !== undefined && point_ids.push(i);
}); // ignores empty slots in spare array
return point_ids;
}
/**
* Calculates the centroid of a set of points, as the average of all point positions.
* @param
* @return
*/
}, {
key: "geomCalcPointsCentroid",
value: function geomCalcPointsCentroid(ids) {
if (ids.length === 1) {
return [this._points[1][this._points[0][ids[0]]][0], this._points[1][this._points[0][ids[0]]][1], this._points[1][this._points[0][ids[0]]][2]];
}
var centroid = [0, 0, 0];
var _iteratorNormalCompletion17 = true;
var _didIteratorError17 = false;
var _iteratorError17 = undefined;
try {
for (var _iterator17 = ids[Symbol.iterator](), _step17; !(_iteratorNormalCompletion17 = (_step17 = _iterator17.next()).done); _iteratorNormalCompletion17 = true) {
var id = _step17.value;
centroi