Skip to content

Commit 57e8f5d

Browse files
authored
fix(CAG): added back missing CAG.fromCompactBinary (#91)
* fix(CAG): added back missing CAG.fromCompactBinary !! * added correct exports * upgrading tests so this does not happen again * test(cag-conversions): CAG to/from compact binary are now working * added clearTags helper to be able to compare objects while disregarding (irrelevant tags)
1 parent 5cedebc commit 57e8f5d

File tree

4 files changed

+79
-33
lines changed

4 files changed

+79
-33
lines changed

csg.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ CSG.fromPolygons = fromPolygons
162162

163163
CSG.toPointCloud = require('./src/api/debugHelpers').toPointCloud
164164

165-
const CAGMakers = require('./src/core/CAGFactories')
166-
CAG.fromSides = CAGMakers.fromSides
167-
CAG.fromObject = CAGMakers.fromObject
168-
CAG.fromPoints = CAGMakers.fromPoints
169-
CAG.fromPointsNoCheck = CAGMakers.fromPointsNoCheck
170-
CAG.fromPath2 = CAGMakers.fromPath2
171-
CAG.fromFakeCSG = CAGMakers.fromFakeCSG
165+
const CAGFactories = require('./src/core/CAGFactories')
166+
CAG.fromSides = CAGFactories.fromSides
167+
CAG.fromObject = CAGFactories.fromObject
168+
CAG.fromPoints = CAGFactories.fromPoints
169+
CAG.fromPointsNoCheck = CAGFactories.fromPointsNoCheck
170+
CAG.fromPath2 = CAGFactories.fromPath2
171+
CAG.fromFakeCSG = CAGFactories.fromFakeCSG
172+
CAG.fromCompactBinary = CAGFactories.fromCompactBinary
172173

173174
/// ////////////////////////////////////
174175
// option parsers

src/core/CAGFactories.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Side = require('./math/Side')
22
const Vector2D = require('./math/Vector2')
3-
const Vertex = require('./math/Vertex2')
3+
const Vertex2 = require('./math/Vertex2')
44
const {areaEPS} = require('./constants')
55
const {isSelfIntersecting} = require('./utils/cagValidation')
66

@@ -39,10 +39,10 @@ const fromPoints = function (points) {
3939
if (numpoints < 3) throw new Error('CAG shape needs at least 3 points')
4040
let sides = []
4141
let prevpoint = new Vector2D(points[numpoints - 1])
42-
let prevvertex = new Vertex(prevpoint)
42+
let prevvertex = new Vertex2(prevpoint)
4343
points.map(function (p) {
4444
let point = new Vector2D(p)
45-
let vertex = new Vertex(point)
45+
let vertex = new Vertex2(point)
4646
let side = new Side(prevvertex, vertex)
4747
sides.push(side)
4848
prevvertex = vertex
@@ -86,10 +86,10 @@ const fromObject = function (obj) {
8686
const fromPointsNoCheck = function (points) {
8787
let sides = []
8888
let prevpoint = new Vector2D(points[points.length - 1])
89-
let prevvertex = new Vertex(prevpoint)
89+
let prevvertex = new Vertex2(prevpoint)
9090
points.map(function (p) {
9191
let point = new Vector2D(p)
92-
let vertex = new Vertex(point)
92+
let vertex = new Vertex2(point)
9393
let side = new Side(prevvertex, vertex)
9494
sides.push(side)
9595
prevvertex = vertex
@@ -107,11 +107,43 @@ const fromPath2 = function (path) {
107107
return fromPoints(path.getPoints())
108108
}
109109

110+
/** Reconstruct a CAG from the output of toCompactBinary().
111+
* @param {CompactBinary} bin - see toCompactBinary()
112+
* @returns {CAG} new CAG object
113+
*/
114+
const fromCompactBinary = function (bin) {
115+
if (bin['class'] !== 'CAG') throw new Error('Not a CAG')
116+
let vertices = []
117+
let vertexData = bin.vertexData
118+
let numvertices = vertexData.length / 2
119+
let arrayindex = 0
120+
for (let vertexindex = 0; vertexindex < numvertices; vertexindex++) {
121+
let x = vertexData[arrayindex++]
122+
let y = vertexData[arrayindex++]
123+
let pos = new Vector2D(x, y)
124+
let vertex = new Vertex2(pos)
125+
vertices.push(vertex)
126+
}
127+
let sides = []
128+
let numsides = bin.sideVertexIndices.length / 2
129+
arrayindex = 0
130+
for (let sideindex = 0; sideindex < numsides; sideindex++) {
131+
let vertexindex0 = bin.sideVertexIndices[arrayindex++]
132+
let vertexindex1 = bin.sideVertexIndices[arrayindex++]
133+
let side = new Side(vertices[vertexindex0], vertices[vertexindex1])
134+
sides.push(side)
135+
}
136+
let cag = fromSides(sides)
137+
cag.isCanonicalized = true
138+
return cag
139+
}
140+
110141
module.exports = {
111142
fromSides,
112143
fromObject,
113144
fromPoints,
114145
fromPointsNoCheck,
115146
fromPath2,
116-
fromFakeCSG
147+
fromFakeCSG,
148+
fromCompactBinary
117149
}

test/cag-conversions.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
import test from 'ava'
2-
import {CSG} from '../csg'
3-
import {CAG} from '../csg'
1+
const test = require('ava')
2+
const {CAG} = require('../csg')
3+
const clearTags = require('./helpers/clearTags')
44

55
//
66
// Test suite for CAG Conversions
77
//
88

9-
test.failing('CAG should convert to and from binary', t => {
9+
test('CAG should convert to and from binary', t => {
1010
// test using simple default shapes
1111
// In the current form this test cannot be working, something comparing
1212
// sides one by one should be written as objects differs
1313

14-
var c1 = CAG.circle()
15-
var c2 = CAG.ellipse()
16-
var c3 = CAG.rectangle()
17-
var c4 = CAG.roundedRectangle()
14+
const c1 = CAG.circle()
15+
const c2 = CAG.ellipse()
16+
const c3 = CAG.rectangle()
17+
const c4 = CAG.roundedRectangle()
1818

19-
var b1 = c1.toCompactBinary()
20-
var r1 = CAG.fromCompactBinary(b1)
21-
t.deepEqual(c1, r1)
22-
var b2 = c2.toCompactBinary()
23-
var r2 = CAG.fromCompactBinary(b2)
24-
t.deepEqual(c2, r2)
25-
var b3 = c3.toCompactBinary()
26-
var r3 = CAG.fromCompactBinary(b3)
27-
t.deepEqual(c3, r3)
28-
var b4 = c4.toCompactBinary()
29-
var r4 = CAG.fromCompactBinary(b4)
30-
t.deepEqual(c4, r4)
19+
const b1 = c1.toCompactBinary()
20+
const r1 = CAG.fromCompactBinary(b1)
21+
t.deepEqual(clearTags(c1), r1)
22+
const b2 = c2.toCompactBinary()
23+
const r2 = CAG.fromCompactBinary(b2)
24+
t.deepEqual(clearTags(c2), r2)
25+
const b3 = c3.toCompactBinary()
26+
const r3 = CAG.fromCompactBinary(b3)
27+
t.deepEqual(clearTags(c3), r3)
28+
const b4 = c4.toCompactBinary()
29+
const r4 = CAG.fromCompactBinary(b4)
30+
t.deepEqual(clearTags(c4), r4)
3131
})
3232

3333
test('CAG should convert to and from anonymous object', t => {

test/helpers/clearTags.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** helper function to remove the very variable 'tags' fields from a CAG object
2+
* @param {CAG} input CAG
3+
* @returns {CAG} the MUTATED CAG
4+
**/
5+
const clearTags = inputCAG => {
6+
inputCAG.sides = inputCAG.sides.map(function (side) {
7+
delete side.vertex0.tag
8+
delete side.vertex1.tag
9+
return side
10+
})
11+
return inputCAG
12+
}
13+
module.exports = clearTags

0 commit comments

Comments
 (0)