-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpuzzle.js
More file actions
351 lines (276 loc) · 9.46 KB
/
Copy pathpuzzle.js
File metadata and controls
351 lines (276 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var properties = [
{id: "Rows", type: 'range', value: 5, min: 1, max: 10, step: 1},
{id: "Columns", type: 'range', value: 5, min: 1, max: 10, step: 1},
{id: "Separate Pieces", type: "boolean", value: false}
];
var executor = function(args, success, failure) {
var params = args.params;
var rowCount = params.Rows;
var columnCount = params.Columns;
var usingFills = params.Shapes === 'fill';
var separatePieces = params['Separate Pieces'];
var bitWidth, bitUnit;
var getSelectedVolumes = function(volumes, selectedVolumeIds) {
var selectedVolumes = [];
var volume;
for (var i = 0; i < volumes.length; i++) {
volume = volumes[i];
if (selectedVolumeIds.indexOf(volume.id) !== -1) {
selectedVolumes.push(volume);
}
}
return selectedVolumes;
};
var toInches = function(width, unit) {
return unit === "in" ? width : 0.0393701;
};
if (args.bitParams.useDetailBit) {
// Detail bit always cuts outlines if in use (for now)
bitWidth = args.bitParams.detailBit.width;
bitUnit = args.bitParams.detailBit.unit;
} else {
bitWidth = args.bitParams.bit.width;
bitUnit = args.bitParams.bit.unit;
}
var selectedVolumes = getSelectedVolumes(args.volumes, args.selectedVolumeIds);
if (selectedVolumes.length === 0) {
success([]);
}
var firstShapeDepth = selectedVolumes[0].cut.depth;
var right = EASEL.volumeHelper.boundingBoxRight(selectedVolumes);
var left = EASEL.volumeHelper.boundingBoxLeft(selectedVolumes);
var top = EASEL.volumeHelper.boundingBoxTop(selectedVolumes);
var bottom = EASEL.volumeHelper.boundingBoxBottom(selectedVolumes);
var width = right - left;
var height = top - bottom;
// Returns 6 points representing the shape of one edge of a puzzle piece.
// Point coordinates are expressed as percentage distances across the width
// and height of the piece.
var edgeDistributions = (function() {
var randomBetween = function(min, max) {
return Math.random() * (max - min) + min;
};
var baselineOffsets = {
xMin: 51,
xMax: 62,
yMin: -15,
yMax: 5
};
var upperOffsets = {
xMin: 20,
xMax: 30,
yMin: 20,
yMax: 44
};
return function() {
var point1 = [0, 0];
var point2 = [
randomBetween(baselineOffsets.xMin, baselineOffsets.xMax),
randomBetween(baselineOffsets.yMin, baselineOffsets.yMax)
];
var point3 = [
randomBetween(upperOffsets.xMin, upperOffsets.xMax),
randomBetween(upperOffsets.yMin, upperOffsets.yMax)
];
var point4 = [
randomBetween(100-upperOffsets.xMax, 100-upperOffsets.xMin),
randomBetween(upperOffsets.yMin, upperOffsets.yMax)
];
var point5 = [
randomBetween(100-baselineOffsets.xMax, 100-baselineOffsets.xMin),
randomBetween(baselineOffsets.yMin, baselineOffsets.yMax)
];
var point6 = [100, 0];
var sign = Math.random() < 0.5 ? -1 : 1;
return [point1, point2, point3, point4, point5, point6].map(function(p) {
return [p[0] / 100, p[1] * sign / 100];
});
};
})();
// Builds an m + 1 x n matrix of edge shapes. The first and last rows
// are straight edges.
var buildDistributions = function(m, n) {
var lineGroups = [];
var lines = [];
var points, i, j;
for (j = 0; j < n; j++) {
lines.push([[0, 0], [1,0]]);
}
lineGroups.push(lines);
for (i = 1; i < m; i++) {
lines = [];
for (j = 0; j < n; j++) {
lines.push(edgeDistributions());
}
lineGroups.push(lines);
}
lines = [];
for (j = 0; j < n; j++) {
lines.push([[0, 0], [1,0]]);
}
lineGroups.push(lines);
return lineGroups;
};
var transposePoint = function(point) {
return [point[1], point[0]];
};
var offsetPoint = function(point, columnIndex, rowIndex, columnWidth, rowHeight) {
var offsetColumnPosition = function(percent, columnWidth, columnIndex) {
var columnOffset = columnWidth * columnIndex + left;
return percent * columnWidth + columnOffset;
};
var offsetRowPosition = function(percent, rowHeight, rowIndex) {
var rowOffset = rowHeight * rowIndex + bottom;
return percent * rowHeight + rowOffset;
};
var x = offsetColumnPosition(point[0], columnWidth, columnIndex);
var y = offsetRowPosition(point[1], rowHeight, rowIndex);
return [x, y];
};
var offsetPoints = function(lineGroups, offsetter) {
for (var i=0; i<lineGroups.length; i++) {
var lines = lineGroups[i];
for (var j=0; j<lines.length; j++) {
lines[j] = lines[j].map(function(point) {
return offsetter(point, j, i);
});
}
}
};
var buildPieces = function() {
var rowHeight = height / rowCount;
var columnWidth = width / columnCount;
var pieces = [];
var rows = buildDistributions(rowCount, columnCount);
offsetPoints(rows, function(point, j, i) {
return offsetPoint(point, j, i, columnWidth, rowHeight);
});
var columns = buildDistributions(columnCount, rowCount);
offsetPoints(columns, function(point, j, i) {
return offsetPoint(transposePoint(point), i, j, columnWidth, rowHeight);
});
for (var rowIndex = 1; rowIndex <= rowCount; rowIndex++) {
for (var columnIndex = 0; columnIndex < columnCount; columnIndex++) {
var edges = [];
edges.push(rows[rowIndex - 1][columnIndex]);
edges.push(columns[columnIndex + 1][rowIndex - 1]);
edges.push(rows[rowIndex][columnIndex].slice().reverse());
edges.push(columns[columnIndex][rowIndex - 1].slice().reverse());
pieces.push(edges);
}
}
return pieces;
};
var d3CurvedLine = d3_shape.line().curve(d3_shape.curveBasis);
var piecePathData = function(piece) {
return piece.map(function(edge) {
return d3CurvedLine(edge);
}).join(" ");
};
var d3StraightLine = d3_shape.line();
var buildPaths = function(pointArrays, index) {
var path;
var data = pointArrays.map(function(pointArray) {
return d3StraightLine(pointArray);
}).join(" ");
if (pointArrays.length > 0) {
path = svg.path(usingFills, data, "#999");
return spacePath(path, index);
} else {
return "";
}
};
var clippedPieces = function(pieceLines) {
var closeVolume = function(pathVolume) {
var firstPoint, lastPoint, points;
if (pathVolume === null) {
return null;
}
points = pathVolume.shape.points;
if (points.length < 2) {
return pathVolume;
}
firstPoint = points[0];
lastPoint = points[points.length - 1];
if (firstPoint.x !== lastPoint.x || firstPoint.y !== lastPoint.y || firstPoint.lh !== lastPoint.lh || firstPoint.rh !== lastPoint.rh) {
points.push(firstPoint);
}
return pathVolume;
}
var intersect = function(pieceVolumes, selectedVolumes) {
var solutions = [];
var clipVolume;
var clippedVolumes = pieceVolumes.map(function(pieceVolume) {
clipVolume = EASEL.volumeHelper.intersect(selectedVolumes, [pieceVolume]);
if (clipVolume !== null) {
clipVolume.cut = {
type: "fill",
depth: firstShapeDepth
};
}
return clipVolume;
});
return clippedVolumes.map(closeVolume);
}
return intersect(pieceLines, selectedVolumes);
};
var buildPieceVolumes = function(pieces) {
var piecePathDataStrings = pieces.map(piecePathData);
return piecePathDataStrings.map(function(dataString) {
var volume = EASEL.pathUtils.fromSvgPathDataString(dataString);
volume.cut = {
type: "outline",
outlineStyle: "outside",
tabPreference: false,
depth: args.material.dimensions.z
};
var points = [], subpoints;
for (var i = 0; i < volume.shape.points.length; i++) {
subpoints = volume.shape.points[i];
if (i > 0) {
subpoints.shift(); // path begins where previous one ended
}
points = points.concat(subpoints);
}
volume.shape.points = [points];
return volume;
});
};
var spaceOut = function(volumes) {
if (!separatePieces) return;
var horizontalPieceGap = toInches(bitWidth, bitUnit) + (width / columnCount) * 0.5;
var verticalPieceGap = toInches(bitWidth, bitUnit) + (width / columnCount) * 0.5;
var volume, rowIndex, columnIndex;
for (var i = 0; i < volumes.length; i++) {
volume = volumes[i];
if (volume === null) continue;
rowIndex = Math.floor(i / columnCount);
columnIndex = i % columnCount;
volume.shape.center.x += horizontalPieceGap * columnIndex;
volume.shape.center.y += verticalPieceGap * rowIndex;
}
};
var removeSelectedVolumes = function() {
var volume, volumesToRemove = [];
for (var i = 0; i < selectedVolumes.length; i++) {
volume = selectedVolumes[i];
volumesToRemove.push({
id: volume.id
});
}
return volumesToRemove;
}
var generate = function() {
var pieces = buildPieces();
var pieceVolumes = buildPieceVolumes(pieces);
var clippedPieceVolumes = clippedPieces(pieceVolumes);
spaceOut(pieceVolumes);
spaceOut(clippedPieceVolumes);
var nonEmptyClippedPieceVolumes = clippedPieceVolumes.filter(function(volume) {
return volume !== null;
});
var removedVolumes = removeSelectedVolumes();
success(nonEmptyClippedPieceVolumes.concat(pieceVolumes).concat(removedVolumes));
};
generate();
};