gs-json
Version:
gs-JSON is a domain agnostic unifying 3D file format for geometric and semantic modelling (hence the 'gs').
738 lines (661 loc) • 24.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Geom = 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 _enums = require("./enums");
var _entity_point = require("./entity_point");
var _entity_obj_polyline = require("./entity_obj_polyline");
var _entity_obj_circle = require("./entity_obj_circle");
var _entity_obj_ellipse = require("./entity_obj_ellipse");
var _entity_obj_polymesh = require("./entity_obj_polymesh");
var _entity_obj_plane = require("./entity_obj_plane");
var _entity_obj_ray = require("./entity_obj_ray");
var _topo_sub = require("./topo_sub");
var _entity_obj_cast = require("./entity_obj_cast");
var _threex = require("./libs/threex/threex");
var threex = _interopRequireWildcard(_threex);
var _utils = require("./_utils");
var util = _interopRequireWildcard(_utils);
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Class Geom
*/
var Geom = exports.Geom = function () {
/**
* Create a new Geom object from the Kernel.
* @param
* @return
*/
function Geom(kernel) {
_classCallCheck(this, Geom);
this._kernel = kernel;
}
// Copy from model -----------------------------------------------------------------------------------
/**
* Copies a point from another model to thid model.
* @param point The point to copy
* @return A new point.
*/
_createClass(Geom, [{
key: "copyPointFromModel",
value: function copyPointFromModel(point) {
var id = this._kernel.geomAddPoint(point.getPosition());
return new _entity_point.Point(this._kernel, id);
}
/**
* Copies a set of new points to the model, from an array of xyz coordinates.
* @param points An array of points to copy.
* @return An array of new points.
*/
}, {
key: "copyPointsFromModel",
value: function copyPointsFromModel(points) {
var _this = this;
return points.map(function (point) {
return _this.addPoint(point.getPosition());
});
}
/**
* Copies a ray to the model.
* @param ray The ray to copy.
* @param ray_vec A vector defining the direction of the ray.
* @return Object of type Ray
*/
}, {
key: "copyRayFromModel",
value: function copyRayFromModel(ray) {
// get the data
var origin = ray.getOrigin();
var vec = ray.getVector();
// create the points
var origin_id = this._kernel.geomAddPoint(origin.getPosition());
// create the obj
var id = this._kernel.geomAddRay(origin_id, vec);
return new _entity_obj_ray.Ray(this._kernel, id);
}
/**
* Copies a plane to the model.
* @param plane The plane to copy.
* @return Object of type Plane
*/
}, {
key: "copyPlaneFromModel",
value: function copyPlaneFromModel(plane) {
// get the data
var origin = plane.getOrigin();
var axes = plane.getAxes();
// create the points
var origin_id = this._kernel.geomAddPoint(origin.getPosition());
// create the obj
var id = this._kernel.geomAddPlane(origin_id, axes);
return new _entity_obj_plane.Plane(this._kernel, id);
}
/**
* Copies a circle to the model.
* @param circle The circle to copy.
* @return Object of type Circle
*/
}, {
key: "copyCircleFromModel",
value: function copyCircleFromModel(circle) {
// get the data
var origin = circle.getOrigin();
var axes = circle.getAxes();
var angles = circle.getAngles();
// create the points
var origin_id = this._kernel.geomAddPoint(origin.getPosition());
// create the obj
var id = this._kernel.geomAddCircle(origin_id, axes, angles);
return new _entity_obj_circle.Circle(this._kernel, id);
}
/**
* Copies a polyline to the model.
* @param circle The polyline to copy.
* @return Object of type Polyline
*/
}, {
key: "copyPlineFromModel",
value: function copyPlineFromModel(pline) {
//TODO copy polylines
throw new Error("Method not implemented");
}
/**
* Copies a polyline to the model.
* @param circle The polyline to copy.
* @return Object of type Polyline
*/
}, {
key: "copyPmeshFromModel",
value: function copyPmeshFromModel(pline) {
//TODO copy polymeshes
throw new Error("Method not implemented");
}
/**
* Copies an obj to the model.
* @param obj The obj to copy.
* @return Object
*/
}, {
key: "copyObjFromModel",
value: function copyObjFromModel(obj) {
switch (obj.getObjType()) {
case 1 /* ray */:
return this.copyRayFromModel(obj);
case 2 /* plane */:
return this.copyPlaneFromModel(obj);
case 3 /* circle */:
return this.copyCircleFromModel(obj);
case 100 /* polyline */:
return this.copyPlineFromModel(obj);
case 200 /* polymesh */:
return this.copyPmeshFromModel(obj);
default:
throw new Error("Object type not found:" + obj.getObjType());
}
}
/**
* Copies an array of objs to the model.
* @param objs The objs to copy.
* @return Object
*/
}, {
key: "copyObjsFromModel",
value: function copyObjsFromModel(objs) {
//TODO copy objects with the shared points
// The rtick here is tomake sure that points are still shared between the objects
throw new Error("Method not implemented");
}
// Creation -----------------------------------------------------------------------------------
/**
* Adds a new point to the model at position xyz.
* @param xyz xyz coordinates are required to create a point
* @return A point instance.
*/
}, {
key: "addPoint",
value: function addPoint(xyz) {
var id = this._kernel.geomAddPoint(xyz);
return new _entity_point.Point(this._kernel, id);
}
/**
* Adds a set of new points to the model, from an array of xyz coordinates.
* @param xyz An array of xyz coordinates.
* @return An array Point instances.
*/
}, {
key: "addPoints",
value: function addPoints(xyz_arr) {
var _this2 = this;
return xyz_arr.map(function (xyz) {
return _this2.addPoint(xyz);
});
}
/**
* Adds a new ray to the model.
* @param origin_point The ray origin point.
* @param ray_vec A vector defining the direction of the ray.
* @return Object of type Ray
*/
}, {
key: "addRay",
value: function addRay(origin_point, ray_vec) {
var id = this._kernel.geomAddRay(origin_point.getID(), ray_vec);
return new _entity_obj_ray.Ray(this._kernel, id);
}
/**
* Adds a new plane to the model.
* @param origin_point The plane origin point.
* @param x_vec A vector defining the x axis.
* @param vec A vector in the plane.
* @return Object of type Plane
*/
}, {
key: "addPlane",
value: function addPlane(origin_point, x_vec, vec) {
// make three ortho vectors
var axes = threex.makeXYZOrthogonal(x_vec, vec, false);
if (axes === null) {
throw new Error("Vectors cannot be parallel.");
}
// make the circle
var id = this._kernel.geomAddPlane(origin_point.getID(), axes);
return new _entity_obj_plane.Plane(this._kernel, id);
}
/**
* Adds a new circle to the model.
* @param Origin The origin point.
* @param x_vec A vector in the local x direction, also defines the raidus.
* @param vec A vector in the plane
* @param angles The angles, can be undefined, in which case a closed conic is generated.
* @return Object of type Circle
*/
}, {
key: "addCircle",
value: function addCircle(origin_point, x_vec, vec, angles) {
// make the angles correct
angles = util.checkCircleAngles(angles);
// make three ortho vectors
var axes = threex.makeXYZOrthogonal(x_vec, vec, false);
if (axes === null) {
throw new Error("Vectors cannot be parallel.");
}
// make the circle
var id = this._kernel.geomAddCircle(origin_point.getID(), axes, angles);
return new _entity_obj_circle.Circle(this._kernel, id);
}
/**
* Adds a new ellipse to the model.
* @param Origin The origin point.
* @param x_vec A vector defining the radius in the local x direction.
* @param y_vec A vector defining the radius in the local y direction, must be orthogonal to x.
* @param angles The angles, can be undefined, in which case a closed conic is generated.
* @return Object of type Ellipse
*/
}, {
key: "addEllipse",
value: function addEllipse(origin_point, x_vec, vec, angles) {
// make the angles correct
angles = util.checkCircleAngles(angles);
// make three ortho vectors
var axes = threex.makeXYZOrthogonal(x_vec, vec, false);
if (axes === null) {
throw new Error("Vectors cannot be parallel.");
}
// make the circle
var id = this._kernel.geomAddEllipse(origin_point.getID(), axes, angles);
return new _entity_obj_ellipse.Ellipse(this._kernel, id);
}
/**
* Adds a new polyline to the model.
* @param points A collection of Points.
* @param is_closed True if the polyline is closed.
* @return Object of type Polyline
*/
}, {
key: "addPolyline",
value: function addPolyline(points, is_closed) {
var point_ids = points.map(function (p) {
return p.getID();
});
var id = this._kernel.geomAddPolyline(point_ids, is_closed);
return new _entity_obj_polyline.Polyline(this._kernel, id);
}
/**
* Adds a new polymesh to the model.
* @param face_points An array of arrays of points.
* @return Object of type Polymesh
*/
}, {
key: "addPolymesh",
value: function addPolymesh(face_points) {
var point_ids = face_points.map(function (f) {
return f.map(function (p) {
return p.getID();
});
});
var id = this._kernel.geomAddPolymesh(point_ids);
return new _entity_obj_polymesh.Polymesh(this._kernel, id);
}
// Points -------------------------------------------------------------------------------------
/**
* Get all the points in this model.
* @param
* @return
*/
}, {
key: "getAllPoints",
value: function getAllPoints() {
var _this3 = this;
var ids = this._kernel.geomGetPointIDs();
return ids.map(function (id) {
return new _entity_point.Point(_this3._kernel, id);
});
}
/**
* Get a set of points from an array of IDs.
* @param
* @return
*/
}, {
key: "getPoints",
value: function getPoints(ids) {
var _this4 = this;
return ids.map(function (id) {
return new _entity_point.Point(_this4._kernel, id);
});
}
/**
* Get a single point from an ID.
* @param
* @return
*/
}, {
key: "getPoint",
value: function getPoint(id) {
return new _entity_point.Point(this._kernel, id);
}
/**
* Delete a set of points.
* @param
* @return
*/
}, {
key: "delPoints",
value: function delPoints(points) {
return this._kernel.geomDelPoints(points.map(function (point) {
return point.getID();
}));
}
/**
* Delete a single point.
* @param
* @return
*/
}, {
key: "delPoint",
value: function delPoint(point) {
return this._kernel.geomDelPoint(point.getID());
}
/**
* Get the number of points in the model.
* @param
* @return
*/
}, {
key: "numPoints",
value: function numPoints() {
return this._kernel.geomNumPoints();
}
/**
* Merge points.
* @param
* @return
*/
}, {
key: "mergePoints",
value: function mergePoints(points, tolerance) {
var _this5 = this;
if (tolerance === undefined) {
var id = this._kernel.geomMergePoints(points.map(function (point) {
return point.getID();
}));
return [new _entity_point.Point(this._kernel, id)];
}
return this._kernel.geomMergePointsByTol(points.map(function (point) {
return point.getID();
}), tolerance).map(function (id) {
return new _entity_point.Point(_this5._kernel, id);
});
}
/**
* Merge all points.
* @param
* @return
*/
}, {
key: "mergeAllPoints",
value: function mergeAllPoints(tolerance) {
var _this6 = this;
return this._kernel.geomMergeAllPoints(tolerance).map(function (id) {
return new _entity_point.Point(_this6._kernel, id);
});
}
/**
* Copy the points.
*/
}, {
key: "copyPoints",
value: function copyPoints(points) {
var _this7 = this;
var copy_attribs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return this._kernel.geomCopyPoints(points.map(function (point) {
return point.getID();
}), copy_attribs).map(function (id) {
return new _entity_point.Point(_this7._kernel, id);
});
}
/**
* Transform the points.
*/
}, {
key: "xformPoints",
value: function xformPoints(points, matrix) {
return this._kernel.geomXformPoints(points.map(function (point) {
return point.getID();
}), matrix);
}
// Objects ------------------------------------------------------------------------------------
/**
* Find certain types of objects in the model.
* @param
* @return
*/
}, {
key: "findObjs",
value: function findObjs(obj_type) {
throw new Error("Method not implemented");
}
/**
* Get all the object in the model.
* @param
* @return
*/
}, {
key: "getAllObjs",
value: function getAllObjs() {
var _this8 = this;
return this._kernel.geomGetObjIDs().map(function (id) {
return (0, _entity_obj_cast._castToObjType)(_this8._kernel, id);
});
}
/**
* Get an array of objects from an array of IDs.
* @param
* @return
*/
}, {
key: "getObjs",
value: function getObjs(ids) {
var _this9 = this;
return ids.map(function (id) {
return (0, _entity_obj_cast._castToObjType)(_this9._kernel, id);
});
}
/**
* Get a single object from an ID.
* @param
* @return
*/
}, {
key: "getObj",
value: function getObj(id) {
return (0, _entity_obj_cast._castToObjType)(this._kernel, id);
}
/**
* Delete an array of objects.
* @param
* @return
*/
}, {
key: "delObjs",
value: function delObjs(objs) {
var keep_points = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return this._kernel.geomDelObjs(objs.map(function (point) {
return point.getID();
}), keep_points);
}
/**
* Delete a single object.
* @param
* @return
*/
}, {
key: "delObj",
value: function delObj(obj) {
var keep_points = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return this._kernel.geomDelObj(obj.getID(), keep_points);
}
/**
* Get the total number of objects in the model.
* @param
* @return
*/
}, {
key: "numObjs",
value: function numObjs() {
return this._kernel.geomNumObjs();
}
/**
* Copy the array of object.
*/
}, {
key: "copyObjs",
value: function copyObjs(objs) {
var _this10 = this;
var copy_attribs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return this._kernel.geomCopyObjs(objs.map(function (obj) {
return obj.getID();
}), copy_attribs).map(function (id) {
return (0, _entity_obj_cast._castToObjType)(_this10._kernel, id);
});
}
/**
* Transform all the points for this array of object.
*/
}, {
key: "xformObjs",
value: function xformObjs(objs, matrix) {
return this._kernel.geomXformObjs(objs.map(function (point) {
return point.getID();
}), matrix);
}
// Topos --------------------------------------------------------------------------------------
/**
* Get all the topos in the model for a specific geom type. (Vertices, Edges, Wires, Faces.)
* @param
* @return
*/
// public getTopos(geom_type: EGeomType): ITopo[] {
}, {
key: "getTopos",
value: function getTopos(geom_type) {
var _this11 = this;
var paths = this._kernel.geomGetTopoPaths(geom_type);
switch (geom_type) {
case _enums.EGeomType.vertices:
return paths.map(function (p) {
return new _topo_sub.Vertex(_this11._kernel, p);
});
case _enums.EGeomType.edges:
return paths.map(function (p) {
return new _topo_sub.Edge(_this11._kernel, p);
});
case _enums.EGeomType.wires:
return paths.map(function (p) {
return new _topo_sub.Wire(_this11._kernel, p);
});
case _enums.EGeomType.faces:
return paths.map(function (p) {
return new _topo_sub.Face(_this11._kernel, p);
});
}
}
/**
* Get a topo from a topo path. If the topo does not exist, then null is returned.
* @param
* @return
*/
}, {
key: "getTopo",
value: function getTopo(path) {
if (!this._kernel.geomHasTopo(path)) {
return null;
}
if (path.st === undefined) {
if (path.tt === 0) {
return new _topo_sub.Wire(this._kernel, path);
}
if (path.tt === 1) {
return new _topo_sub.Face(this._kernel, path);
}
} else {
if (path.st === 0) {
return new _topo_sub.Vertex(this._kernel, path);
}
if (path.st === 1) {
return new _topo_sub.Edge(this._kernel, path);
}
}
}
/**
* Get a topo from a topo label.
* @param
* @return
*/
}, {
key: "getTopoFromLabel",
value: function getTopoFromLabel(path_str) {
// o2:f3:e1
var parts = path_str.split(":").map(function (v) {
return [v.slice(0, 1), Number.parseInt(v.slice(1))];
});
if (parts.length !== 2 && parts.length !== 3) {
return null;
}
if (parts[0][0] !== "o") {
return null;
}
if (parts[1][0] !== "w" && parts[1][0] !== "f") {
return null;
}
if (Number.isNaN(parts[0][1])) {
return null;
}
if (Number.isNaN(parts[1][1])) {
return null;
}
var id = parts[0][1];
var tt = void 0;
if (parts[1][0] === "w") {
tt = 0;
} else {
tt = 1;
}
var ti = parts[1][1];
var path = { id: id, tt: tt, ti: ti };
if (parts.length === 3) {
if (parts[2][0] !== "v" && parts[2][0] !== "e") {
return null;
}
if (Number.isNaN(parts[2][1])) {
return null;
}
var st = void 0;
if (parts[2][0] === "v") {
st = 0;
} else {
st = 1;
}
var si = parts[2][1];
path.st = st;
path.si = si;
}
return this.getTopo(path);
}
/**
* Get the number of topos in the model for a specific geom type. (Vertices, Edges, Wires, Faces.)
* @param
* @return
*/
}, {
key: "numTopos",
value: function numTopos(geom_type) {
return this._kernel.geomNumTopos(geom_type);
}
}]);
return Geom;
}();
//# sourceMappingURL=geom.js.map