UNPKG

1.76 kBJavaScriptView Raw
1const CAG = require('./CAG')
2const Side = require('./math/Side')
3const Vector2D = require('./math/Vector2')
4const Vertex = require('./math/Vertex2')
5const Path2 = require('./math/Path2')
6
7/** Reconstruct a CAG from an object with identical property names.
8 * @param {Object} obj - anonymous object, typically from JSON
9 * @returns {CAG} new CAG object
10 */
11const fromObject = function (obj) {
12 let sides = obj.sides.map(function (s) {
13 return Side.fromObject(s)
14 })
15 let cag = CAG.fromSides(sides)
16 cag.isCanonicalized = obj.isCanonicalized
17 return cag
18}
19
20/** Construct a CAG from a list of points (a polygon).
21 * Like fromPoints() but does not check if the result is a valid polygon.
22 * The points MUST rotate counter clockwise.
23 * The points can define a convex or a concave polygon.
24 * The polygon must not self intersect.
25 * @param {points[]} points - list of points in 2D space
26 * @returns {CAG} new CAG object
27 */
28const fromPointsNoCheck = function (points) {
29 let sides = []
30 let prevpoint = new Vector2D(points[points.length - 1])
31 let prevvertex = new Vertex(prevpoint)
32 points.map(function (p) {
33 let point = new Vector2D(p)
34 let vertex = new Vertex(point)
35 let side = new Side(prevvertex, vertex)
36 sides.push(side)
37 prevvertex = vertex
38 })
39 return CAG.fromSides(sides)
40}
41
42/** Construct a CAG from a 2d-path (a closed sequence of points).
43 * Like fromPoints() but does not check if the result is a valid polygon.
44 * @param {path} Path2 - a Path2 path
45 * @returns {CAG} new CAG object
46 */
47const fromPath2 = function (path) {
48 if (!path.isClosed()) throw new Error('The path should be closed!')
49 return CAG.fromPoints(path.getPoints())
50}
51
52
53module.exports = {
54 fromObject,
55 fromPointsNoCheck,
56 fromPath2
57 //fromFakeCSG
58}