gs-json
Version:
gs-JSON is a domain agnostic unifying 3D file format for geometric and semantic modelling (hence the 'gs').
597 lines (524 loc) • 21.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Obj = undefined;
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 _arr = require("./libs/arr/arr");
var _enums = require("./enums");
var _topo_sub = require("./topo_sub");
var _entity = require("./entity");
var _entity_point = require("./entity_point");
var _groups = require("./groups");
var _entity_obj_cast = require("./entity_obj_cast");
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"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
/**
* Abstract class Obj.
* The superclass for all geometric objects,
* including Polyline and Polymesh.
*/
var Obj = exports.Obj = function (_Ent) {
_inherits(Obj, _Ent);
function Obj() {
_classCallCheck(this, Obj);
return _possibleConstructorReturn(this, (Obj.__proto__ || Object.getPrototypeOf(Obj)).apply(this, arguments));
}
_createClass(Obj, [{
key: "getGeomType",
/**
* Get the geometry type.
* This method overrides the method in the Ent class.
* @return The geometry type.
*/
value: function getGeomType() {
return _enums.EGeomType.objs;
}
/**
* Get the object type.
* This method will be overridden by subclasses.
* @return The object type.
*/
}, {
key: "getObjType",
value: function getObjType() {
throw new Error("Method to be overridden by subclass.");
}
/**
* Check if this entity exists in the model. (i.e has it been deleted?)
* @return The entity ID number.
*/
}, {
key: "exists",
value: function exists() {
return this._kernel.geomHasObj(this._id);
}
/**
* Get the label for this object.
* @return The xyz of the centroid.
*/
}, {
key: "getLabel",
value: function getLabel() {
return "o" + this._id;
}
/**
* Get the label centroid for this object.
* @return The xyz of the label.
*/
}, {
key: "getLabelCentroid",
value: function getLabelCentroid() {
var xyzs = this.getPointsSet().map(function (v) {
return v.getPosition();
});
var centroid = [0, 0, 0];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = xyzs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var xyz = _step.value;
centroid[0] += xyz[0];
centroid[1] += xyz[1];
centroid[2] += xyz[2];
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
var num_vertices = xyzs.length;
return [centroid[0] / num_vertices, centroid[1] / num_vertices, centroid[2] / num_vertices];
}
/**
* Make a copy of this entity.
* This method must be overridden by the sub-classes.
* @return The geometry type.
*/
}, {
key: "copy",
value: function copy(copy_attribs) {
return (0, _entity_obj_cast._castToObjType)(this._kernel, this._kernel.geomCopyObj(this._id, copy_attribs));
}
/**
* Transform the points for this object.
* @param matrix The xform matrix.
*/
}, {
key: "xform",
value: function xform(matrix) {
return this._kernel.objXform(this._id, matrix);
}
// Points -------------------------------------------------------------------------------------
/**
* Get the points for this object. If the point_type is not specified, then
* points for both wires and faces are returned.
* @return A nested array of points, with sub-arrays for wires and faces.
*/
}, {
key: "getPoints",
value: function getPoints(point_type) {
var _this2 = this;
var ids = this._kernel.objGetPointIDs(this._id, point_type);
switch (point_type) {
case _enums.EGeomType.wires:
return [ids[0].map(function (w) {
return w.map(function (id) {
return new _entity_point.Point(_this2._kernel, id);
});
}), []];
case _enums.EGeomType.faces:
return [[], ids[1].map(function (f) {
return f.map(function (id) {
return new _entity_point.Point(_this2._kernel, id);
});
})];
default:
return [ids[0].map(function (w) {
return w.map(function (id) {
return new _entity_point.Point(_this2._kernel, id);
});
}), ids[1].map(function (f) {
return f.map(function (id) {
return new _entity_point.Point(_this2._kernel, id);
});
})];
}
}
/**
* Get the points for this object. If the point_type is not specified, then
* points for both wires and faces are returned.
* @return A flat array of points.
*/
}, {
key: "getPointsArr",
value: function getPointsArr() {
return _arr.Arr.flatten(this.getPoints());
}
/**
* Get the set of unique points for this object.
* @return The array of point IDs.
*/
}, {
key: "getPointsSet",
value: function getPointsSet() {
var exclude = [];
var unique_points = [];
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = this.getPointsArr()[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var point = _step2.value;
var id = point.getID();
if (exclude.indexOf(id) === -1) {
exclude.push(id);
unique_points.push(point);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return unique_points;
}
// Topos --------------------------------------------------------------------------------------
/**
* Get the vertices for this object. If the vertex_type is not specified, then
* vertices for both wires and faces are returned.
* @return The array of vertices.
*/
}, {
key: "getVertices",
value: function getVertices(vertex_type) {
var _this3 = this;
var paths = this._kernel.objGetVertices(this._id, vertex_type);
return [paths[0].map(function (w) {
return w.map(function (path) {
return new _topo_sub.Vertex(_this3._kernel, path);
});
}), paths[1].map(function (f) {
return f.map(function (path) {
return new _topo_sub.Vertex(_this3._kernel, path);
});
})];
}
/**
* Get the edges for this object. If the edge_type is not specified, then
* edges for both wires and faces are returned.
* @return The array of edges.
*/
}, {
key: "getEdges",
value: function getEdges(edge_type) {
var _this4 = this;
var paths = this._kernel.objGetEdges(this._id, edge_type);
return [paths[0].map(function (w) {
return w.map(function (path) {
return new _topo_sub.Edge(_this4._kernel, path);
});
}), paths[1].map(function (f) {
return f.map(function (path) {
return new _topo_sub.Edge(_this4._kernel, path);
});
})];
}
/**
* Get the wires for this object.
* @return The array of wires.
*/
}, {
key: "getWires",
value: function getWires() {
var _this5 = this;
var paths = this._kernel.objGetWires(this._id);
return paths.map(function (path) {
return new _topo_sub.Wire(_this5._kernel, path);
});
}
/**
* Get the faces for this object.
* @return The array of faces.
*/
}, {
key: "getFaces",
value: function getFaces() {
var _this6 = this;
var paths = this._kernel.objGetFaces(this._id);
return paths.map(function (path) {
return new _topo_sub.Face(_this6._kernel, path);
});
}
/**
* Get the number of wires for this object.
* @return The number of wires.
*/
}, {
key: "numWires",
value: function numWires() {
return this._kernel.objNumWires(this._id);
}
/**
* Get the number of faces for this object.
* @return The number of faces.
*/
}, {
key: "numFaces",
value: function numFaces() {
return this._kernel.objNumFaces(this._id);
}
/**
* Returns the number of vertices in this polymesh wires.
* @return Return the number of vertices.
*/
}, {
key: "numWireVertices",
value: function numWireVertices() {
var count = 0;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = this.getWires()[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var wire = _step3.value;
count += wire.numVertices();
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return count;
}
/**
* Returns the number of vertices in this polymesh faces.
* @return Return the number of vertices.
*/
}, {
key: "numFaceVertices",
value: function numFaceVertices() {
var count = 0;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = this.getFaces()[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var face = _step4.value;
count += face.numVertices();
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
return count;
}
/**
* Returns the number of edges in this polymesh wires.
* @return Return the number of edges.
*/
}, {
key: "numWireEdges",
value: function numWireEdges() {
var count = 0;
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = this.getWires()[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var wire = _step5.value;
count += wire.numEdges();
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
return count;
}
/**
* Returns the number of edges in this polymesh faces.
* @return Return the number of edges.
*/
}, {
key: "numFaceEdges",
value: function numFaceEdges() {
var count = 0;
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = this.getFaces()[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var face = _step6.value;
count += face.numEdges();
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
}
return count;
}
/**
*
* @return
*/
}, {
key: "getWirePoints",
value: function getWirePoints() {
var points = [];
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator7 = this.getWires()[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
var wire = _step7.value;
points.push.apply(points, _toConsumableArray(wire.getVertices().map(function (v) {
return v.getPoint();
})));
}
} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError7) {
throw _iteratorError7;
}
}
}
return points;
}
/**
*
* @return
*/
}, {
key: "getFacePoints",
value: function getFacePoints() {
var points = [];
var _iteratorNormalCompletion8 = true;
var _didIteratorError8 = false;
var _iteratorError8 = undefined;
try {
for (var _iterator8 = this.getFaces()[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
var face = _step8.value;
points.push.apply(points, _toConsumableArray(face.getVertices().map(function (v) {
return v.getPoint();
})));
}
} catch (err) {
_didIteratorError8 = true;
_iteratorError8 = err;
} finally {
try {
if (!_iteratorNormalCompletion8 && _iterator8.return) {
_iterator8.return();
}
} finally {
if (_didIteratorError8) {
throw _iteratorError8;
}
}
}
return points;
}
// Groups -------------------------------------------------------------------------------------
/**
* Get the group names for all the groups for which this entity is a member.
* @return The array of group names.
*/
}, {
key: "getGroups",
value: function getGroups() {
var _this7 = this;
return this._kernel.objGetGroups(this._id).map(function (v) {
return new _groups.Group(_this7._kernel, v);
});
}
/**
* Add this object to a group.
* @param group The group.
* @return True if the entity was added, False is the object was already in the group.
*/
}, {
key: "addToGroup",
value: function addToGroup(group) {
return this._kernel.groupAddObj(group.getName(), this._id);
}
// toString -------------------------------------------------------------------------------------
/**
* Create s string representation of this object.
* @return Strig
*/
}, {
key: "toString",
value: function toString() {
return "Obj('" + this.getLabel() + "', " + _enums.mapObjTypeToString.get(this.getObjType()) + ", faces:" + this.numFaces() + ", wires:" + this.numWires() + ", edges:" + (this.numFaceEdges() + this.numWireEdges()) + ", vertices:" + (this.numFaceVertices() + this.numWireVertices()) + ")";
}
}]);
return Obj;
}(_entity.Ent);
//# sourceMappingURL=entity_obj.js.map