-
Notifications
You must be signed in to change notification settings - Fork 0
/
lace-maker2-lib.mjs
313 lines (276 loc) · 8.29 KB
/
lace-maker2-lib.mjs
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
import d3Delaunay from 'd3-delaunay';
const Delaunay = d3Delaunay.Delaunay;
import _ from 'lodash';
import * as utils from './utils.mjs';
const {
showCut,
show,
approxShape,
bufferPath,
bufferPoints,
generatePointsInPath,
roundCorners
} = utils;
let paper = null;
async function waitForPaper() {
console.error('waiitng for paper in lace maker')
paper = await utils.waitForPaper();
}
export class LaceMaker {
constructor({
debug,
holeSize,
inchInPoints,
rounded,
subtract,
subtractBuffer,
numPoints,
numExtraPoints,
voronoi,
addHole,
butt,
maxWidth,
maxHeight,
safeBorder,
outlineSize
}) {
this.debug = debug;
this.rounded = rounded;
this.subtract = subtract;
this.inchInPoints = inchInPoints;
this.numPoints = numPoints;
this.numExtraPoints = numExtraPoints;
this.voronoi = voronoi;
this.addHole = addHole;
this.butt = butt;
this.maxWidth = maxWidth;
this.maxHeight = maxHeight;
this.safeBorder = safeBorder;
this.subtractBuffer = subtractBuffer;
this.outlineSize = outlineSize;
this.threadHoleSize = this.pointInches(holeSize);
this.threadHoleBuffer = this.pointInches(0.04);
this.threadHoleTotalSize = this.threadHoleSize + this.threadHoleBuffer;
if (!this.debug) {
console.log = s => {};
}
}
pointInches(n) {
return n * this.inchInPoints;
}
inchPoints(n) {
return n / this.inchInPoints;
}
loadAndResizeSvg({ svgData, yOffset = 0, xOffset = 0 }) {
var svgItem = paper.project.importSVG(svgData); //, {insert: false})
// show(svgItem, 'black');
const path = svgItem.children[1];
console.log('num children', svgItem.children.length);
let actualPath = path;
if (path.children) {
actualPath = _.sortBy(path.children, path => -path.length)[0];
console.log('num children children', path.children.length);
// actualPath = path.children[0];
}
const maxWidthPixels = this.pointInches(this.maxWidth);
const maxHeightPixels = this.pointInches(this.maxHeight);
console.log(
`current size: ${this.inchPoints(
actualPath.bounds.width
)} x ${this.inchPoints(actualPath.bounds.height)}`
);
const scale = Math.min(
maxWidthPixels / actualPath.bounds.width,
maxHeightPixels / actualPath.bounds.height
);
svgItem = svgItem.scale(scale);
svgItem.translate(
new paper.Point(
-actualPath.bounds.x + xOffset,
-actualPath.bounds.y + yOffset
)
);
console.log(
`new size: ${this.inchPoints(
actualPath.bounds.width
)} x ${this.inchPoints(actualPath.bounds.height)}`
);
console.log(
`new size: ${this.inchPoints(svgItem.bounds.width)} x ${this.inchPoints(
svgItem.bounds.height
)}`
);
svgItem.remove();
// console.log(svgItem.bounds.width, svgItem.bounds.height);
return actualPath;
}
doAddHole({ path, size, buffer }) {
if (!this.addHole) {
showCut(path);
return;
}
let movement = new paper.Point(0, 1);
let initial = new paper.Point(path.bounds.width / 2, 0);
// super wide
if (this.butt && path.bounds.width / path.bounds.height > 3) {
movement = new paper.Point(-1, 0);
initial = new paper.Point(
path.bounds.x + path.bounds.width + (size + buffer) * 2,
path.bounds.y + path.bounds.height / 2
);
}
// add hole for string
let threadHoleOuter = new paper.Path.Circle(initial, size + buffer);
function touchingEnough(path1, path2) {
console.log('length of path1', path1.length);
console.log('length of path1', path1.area);
const intersection = path1.intersect(path2, {
insert: false,
trace: false
});
console.log('length of path1 after subtract path2', intersection.length);
console.log('length of path1 after subtract path2', intersection.area);
// showCut(intersection, 'blue')
if (intersection) {
console.log(intersection.length);
return intersection.length < path1.length * 0.5;
}
return false;
}
while (
!threadHoleOuter.intersects(path) ||
!touchingEnough(threadHoleOuter, path)
) {
threadHoleOuter = threadHoleOuter.translate(movement);
}
// showCut(threadHoleOuter);
threadHoleOuter.scale(1.2);
for (let i = 0; i < 10; i++) {
threadHoleOuter.translate(movement);
}
let pathToUnite = path;
if (pathToUnite instanceof paper.CompoundPath) {
pathToUnite = pathToUnite.children[0];
}
showCut(threadHoleOuter.unite(pathToUnite));
const threadHole = new paper.Path.Circle(
threadHoleOuter.bounds.center,
size
);
showCut(threadHole);
}
buildTriangles({ path }) {
// let outerShape = bufferPath(1, path);
// outerShape.closePath();
// show(outerShape, 'brown');
// outerShape = outerShape.unite(path);
const outerShape = path;
show(new paper.Path(approxShape(path)), 'brown');
let innerShapes = bufferPath({
buffer: -this.pointInches(this.safeBorder),
path
});
show(innerShapes, 'brown');
innerShapes.forEach(innerShape =>
this.processInnerShape({ innerShape, outerShape })
);
}
processInnerShape({ outerShape, innerShape }) {
let veryInnerShapes = bufferPath({
buffer: -this.pointInches(this.subtractBuffer),
path: outerShape
});
show(veryInnerShapes, 'brown');
const numPointsToGet = this.numPoints;
const points = this.pointsToArray(approxShape(innerShape, numPointsToGet));
points.forEach(p => show(new paper.Path.Circle(p, 1), 'blue'));
const extraPoints = generatePointsInPath({
path: innerShape,
numExtraPoints: this.numExtraPoints
});
const allPoints = points.concat(extraPoints);
console.log('triangulating');
const delaunay = Delaunay.from(allPoints);
console.log('done');
let polygonArray = Array.from(delaunay.trianglePolygons());
if (this.voronoi) {
const voronoi = delaunay.voronoi([
outerShape.bounds.x,
outerShape.bounds.y,
outerShape.bounds.x + outerShape.bounds.width,
outerShape.bounds.y + outerShape.bounds.height
]);
polygonArray = Array.from(voronoi.cellPolygons());
}
console.log(`have ${polygonArray.length} triangles`);
polygonArray.forEach((polygon, index) => {
// console.log(`triangle ${index} of ${numTriangles.length}`);
const points = polygon.map(p => new paper.Point(p[0], p[1]));
// const tri = new paper.Path(points);
// showCut(tri);
let smallShape = bufferPoints(
-this.pointInches(this.outlineSize),
points
);
if (smallShape) {
smallShape = smallShape[0];
smallShape.closePath();
if (this.rounded) {
smallShape = roundCorners(smallShape, 2);
}
smallShape.closePath();
let cutOffShape = smallShape.intersect(innerShape);
if (this.subtract && veryInnerShapes) {
veryInnerShapes.forEach(
veryInnerShape =>
(cutOffShape = cutOffShape.subtract(veryInnerShape))
);
}
// cutOffShape.translate(new paper.Point(0, threadHoleTotalSize * 2));
showCut(cutOffShape);
} else {
console.log(`could not shrink :-( triagnel ${index}`);
}
});
}
pointsToArray(points) {
const ret = [];
points.forEach(p => {
ret.push([p.x, p.y]);
});
return ret;
}
async loadAndProcessSvgData({ svgData, paperModule }) {
if (paperModule) {
paper = paperModule;
utils.setPaper(paper);
}
if (!paper) {
await waitForPaper();
paper.setup([10, 10]);
}
paper.settings.insertItems = false;
let actualPath = this.loadAndResizeSvg({
svgData,
xOffset: this.threadHoleTotalSize * 2,
yOffset: this.threadHoleTotalSize * 2
});
if (!actualPath.closed) {
console.log('not closed outer ring');
actualPath.closePath();
//process.exit(1);
}
this.buildTriangles({ path: actualPath }); //, points: allPoints });
this.doAddHole({
path: actualPath,
size: this.threadHoleSize,
buffer: this.threadHoleBuffer
});
}
exportSVGString() {
return paper.project.exportSVG({
asString: true,
bounds: 'content'
});
}
}