const CAG = require('./CAG') const Side = require('./math/Side') const Vector2D = require('./math/Vector2') const Vertex = require('./math/Vertex2') const Path2 = require('./math/Path2') /** Reconstruct a CAG from an object with identical property names. * @param {Object} obj - anonymous object, typically from JSON * @returns {CAG} new CAG object */ const fromObject = function (obj) { let sides = obj.sides.map(function (s) { return Side.fromObject(s) }) let cag = CAG.fromSides(sides) cag.isCanonicalized = obj.isCanonicalized return cag } /** Construct a CAG from a list of points (a polygon). * Like fromPoints() but does not check if the result is a valid polygon. * The points MUST rotate counter clockwise. * The points can define a convex or a concave polygon. * The polygon must not self intersect. * @param {points[]} points - list of points in 2D space * @returns {CAG} new CAG object */ const fromPointsNoCheck = function (points) { let sides = [] let prevpoint = new Vector2D(points[points.length - 1]) let prevvertex = new Vertex(prevpoint) points.map(function (p) { let point = new Vector2D(p) let vertex = new Vertex(point) let side = new Side(prevvertex, vertex) sides.push(side) prevvertex = vertex }) return CAG.fromSides(sides) } /** Construct a CAG from a 2d-path (a closed sequence of points). * Like fromPoints() but does not check if the result is a valid polygon. * @param {path} Path2 - a Path2 path * @returns {CAG} new CAG object */ const fromPath2 = function (path) { if (!path.isClosed()) throw new Error('The path should be closed!') return CAG.fromPoints(path.getPoints()) } module.exports = { fromObject, fromPointsNoCheck, fromPath2 //fromFakeCSG }