forked from jscad/OpenJSCAD.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geom.js
412 lines (366 loc) · 12.9 KB
/
geom.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* This is a set of classes that help in the calculation of point coordinates on
* the plane. The core idea is to hide linear algebra and trigonometry in the
* classes and use them like ruler and compasses.
*/
(function(module) {
"use strict";
var Vector2d = function(x, y) {
if (y === undefined && x instanceof Array && x.length == 2)
this.p = x.slice(0);
else if (typeof x === 'number' && typeof y === 'number')
this.p = [x, y];
else
throw new Error("wrong type of input arguments");
};
Vector2d.prototype = {
norm: function() {
return Math.sqrt( this.p[0]*this.p[0] + this.p[1]*this.p[1] );
},
unit: function() {
var n = this.norm();
return new Vector2d(this.p[0]/n, this.p[1]/n);
},
dot: function(v) {
return this.p[0]*v.p[0] + this.p[1]*v.p[1];
},
cross: function(v) {
return this.p[0]*v.p[1] - this.p[1]*v.p[0];
},
add: function(v) {
if (v instanceof Vector2d)
return new Vector2d(this.p[0]+v.p[0], this.p[1]+v.p[1]);
else if (v instanceof Array && v.length == 2)
return new Vector2d(this.p[0]+v[0], this.p[1]+v[1]);
else
throw new Error("input argument is neither Vector2d nor 2D array");
},
sub: function(v) {
if (v instanceof Vector2d)
return new Vector2d(this.p[0]-v.p[0], this.p[1]-v.p[1]);
else if (v instanceof Array && v.length == 2)
return new Vector2d(this.p[0]-v[0], this.p[1]-v[1]);
else
throw new Error("input argument is neither Vector2d nor 2D array");
},
scale: function(c) {
return new Vector2d(c*this.p[0], c*this.p[1]);
},
rotate: function(ang) {
var c = Math.cos(ang);
var s = Math.sin(ang);
return new Vector2d(c*this.p[0]-s*this.p[1], s*this.p[0]+c*this.p[1]);
},
reflect: function(ln) {
if (ln === undefined || ln instanceof Line)
throw new Error("input argument must be a Line");
return null;
},
clone: function() {
return new Vector2d(this.p[0], this.p[1]);
},
toArray: function() {
return this.p.slice(0);
}
};
/**
* Line class helps to find intersection points, projections, distances to the
* geometrical lines.
* Line({p1:[0,1], p2:[1,0]}); // construct from two points
* Line({p:[2,1], n:[0,1]}); // construct from point and normal
*/
var Line = function(prm) {
if (prm === undefined || typeof prm !== 'object')
throw new Error("input argument must be object");
if ('p1' in prm && 'p2' in prm) {
// line from two points
var p1, p2;
if (prm.p1 instanceof Vector2d)
p1 = prm.p1.clone();
else if (prm.p1 instanceof Array && prm.p1.length == 2)
p1 = new Vector2d(prm.p1);
else
throw new Error("'p1' must be Vector2d or array");
if (prm.p2 instanceof Vector2d)
p2 = prm.p2.clone();
else if (prm.p2 instanceof Array && prm.p2.length == 2)
p2 = new Vector2d(prm.p2);
else
throw new Error("'p2' must be Vector2d or array");
this.p = p1.clone();
this.n = p2.sub(p1).unit().rotate(Math.PI/2);
} else if ('p' in prm && 'n' in prm) {
// point and normal
if (prm.p instanceof Vector2d)
this.p = prm.p.clone();
else if (prm.p instanceof Array && prm.p.length == 2)
this.p = new Vector2d(prm.p);
else
throw new Error("'p' must be array or Vector2d");
if (prm.n instanceof Vector2d)
this.n = prm.n.clone();
else if (prm.n instanceof Array && prm.n.length == 2)
this.n = new Vector2d(prm.n);
else
throw new Error("'n' must be array or Vector2d")
} else
throw new Error("wrong fields");
// distance to line
this.d = this.p.dot(this.n)/this.n.norm();
};
Line.prototype = {
intersect: function(ln) {
if (!(ln instanceof Line))
throw new Error("input argument is not an instance of Line class");
// check for parallel
var det = this.n.cross(ln.n);
var norm1 = this.n.norm();
var norm2 = ln.n.norm();
var RelTol = 2e-16;
if (Math.abs(det) < RelTol*norm1*norm2)
throw new Error("linear system is bad conditioned");
var pi2 = Math.PI/2;
return this.n.rotate(pi2).scale(ln.d/det).sub(ln.n.rotate(pi2).scale(this.d/det));
},
reflect: function(ln) {
return null;
},
clone: function() {
return new Line(this.p, this.n);
}
};
/**
* Creates object representing arc properties
*
* Arc({p1:[1,0], p2:[0,1], p3:[-1,0]})
* Arc({p1:[1,0], p2:[-1,0], bulge:1})
* Arc({center: [0,0], radius: 1, start: 0, end: Math.PI/2, isccw: true})
*/
var Arc = function(prm) {
var p1, p2, p3;
var p0, r, phi1, phi2, isccw;
if (Object.keys(prm).length == 3 && 'p1' in prm && 'p2' in prm && 'p3' in prm) {
if (typeof prm.p1 !== 'object' || prm.p1.length != 2)
throw new Error("p1 is not 1x2 array");
if (typeof prm.p2 !== 'object' || prm.p2.length != 2)
throw new Error("p2 is not 1x2 array");
if (typeof prm.p3 !== 'object' || prm.p3.length != 2)
throw new Error("p3 is not 1x2 array");
p1 = prm.p1;
p2 = prm.p2;
p3 = prm.p3;
var det3x3 = function(a,b,c) {
return a[0]*b[1]*c[2] + a[2]*b[0]*c[1] + a[1]*b[2]*c[0] -
a[2]*b[1]*c[0] - a[1]*b[0]*c[2] - a[0]*b[2]*c[1];
};
var tmp1 = [p1[0], p2[0], p3[0]];
var tmp2 = [p1[1], p2[1], p3[1]];
var tmp3 = [p1[0]*p1[0]+p1[1]*p1[1],
p2[0]*p2[0]+p2[1]*p2[1],
p3[0]*p3[0]+p3[1]*p3[1]];
var a = det3x3(tmp1, tmp2, [1,1,1]);
var bx = -det3x3(tmp3, tmp2, [1,1,1]);
var by = det3x3(tmp3, tmp1, [1,1,1]);
var c = -det3x3(tmp3, tmp1, tmp2);
var x0 = -bx/2.0/a;
var y0 = -by/2.0/a;
r = Math.sqrt(bx*bx+by*by-4*a*c)/2.0/Math.abs(a);
isccw = (p2[0]-p1[0])*(p3[1]-p1[1])-(p3[0]-p1[0])*(p2[1]-p1[1]) > 0;
if (isccw) {
phi1 = Math.atan2(p1[1]-y0, p1[0]-x0);
phi2 = Math.atan2(p3[1]-y0, p3[0]-x0);
} else {
phi1 = Math.atan2(p3[1]-y0, p3[0]-x0);
phi2 = Math.atan2(p1[1]-y0, p1[0]-x0);
}
if (phi2<phi1) phi2 += 2.0 * Math.PI;
this.StartPoint = p1.slice(0);
this.EndPoint = p3.slice(0);
this.Center = [x0, y0];
this.Radius = r;
this.isCCW = isccw;
this.StartAngle = phi1;
this.EndAngle = phi2;
this.Bulge = (isccw ? 1.0 : -1.0) * Math.tan((phi2-phi1)/4.0);
} else if (Object.keys(prm).length == 3 && 'p1' in prm && 'p2' in prm && 'bulge' in prm) {
if (typeof prm.p1 !== 'object' || prm.p1.length != 2)
throw new Error("p1 is not 1x2 array");
if (typeof prm.p2 !== 'object' || prm.p2.length != 2)
throw new Error("p2 is not 1x2 array");
if (typeof prm.bulge !== "number")
throw new Error("bulge is not a number");
p1 = prm.p1;
p2 = prm.p2;
var bulge = prm.bulge;
var dpx = p2[0] - p1[0];
var dpy = p2[1] - p1[1];
var h = Math.sqrt(dpx*dpx+dpy*dpy);
var s = 0.5 * bulge * h;
var nx = dpy/h;
var ny = -dpx/h;
return new Arc({
p1: p1,
p2: [0.5*(p1[0]+p2[0]) + s*nx,
0.5*(p1[1]+p2[1]) + s*ny],
p3: p2
});
} else if (Object.keys(prm).length == 5 && 'center' in prm && 'radius' in prm && 'start' in prm && 'end' in prm && 'isccw' in prm) {
if (typeof prm.center !== 'object' || prm.center.length != 2)
throw new Error("center is not 1x2 array");
if (typeof prm.radius !== 'number')
throw new Error("radius is not a number");
if (typeof prm.start !== 'number')
throw new Error("start is not a number");
if (typeof prm.end !== 'number')
throw new Error("end is not a number");
if (typeof prm.isccw !== 'boolean')
throw new Error("end is not a number");
p0 = prm.center;
r = prm.radius;
if (r <= 0)
throw new Error("radius is not a positive number");
isccw = prm.isccw;
phi1 = prm.start;
phi2 = prm.end;
if (phi2<phi1) phi2 += 2.0 * Math.PI;
if (isccw) {
p1 = [p0[0] + r * Math.cos(phi1),
p0[1] + r * Math.sin(phi1)];
p3 = [p0[0] + r * Math.cos(phi2),
p0[1] + r * Math.sin(phi2)];
} else {
p1 = [p0[0] + r * Math.cos(phi2),
p0[1] + r * Math.sin(phi2)];
p3 = [p0[0] + r * Math.cos(phi1),
p0[1] + r * Math.sin(phi1)];
}
this.StartPoint = p1;
this.EndPoint = p3;
this.Center = p0;
this.Radius = r;
this.isCCW = isccw;
this.StartAngle = phi1;
this.EndAngle = phi2;
this.Bulge = (isccw ? 1.0 : -1.0) * Math.tan((phi2-phi1)/4.0);
} else
throw new Error("unsupported list of parameters");
};
Arc.prototype = {
points: function(AbsTol) {
// AbsTol - maximal distance between arc and segments
if (typeof AbsTol === undefined)
AbsTol = 0.1; // [mm], should be enough for 3D printing
if (typeof AbsTol !== 'number')
throw new Error("AbsTol is not a number");
if (AbsTol <= 0)
throw new Error("AbsTol is not a positive number");
var dphi = 2 * Math.acos(1-AbsTol/this.Radius);
var theta = this.EndAngle - this.StartAngle;
var N = Math.ceil(theta/dphi)+1;
var n, pts = [], px, py, phi, phi0;
if (this.isCCW) {
dphi = theta/(N-1);
phi0 = this.StartAngle;
} else {
dphi = -theta/(N-1);
phi0 = this.EndAngle;
}
for (n = 0; n < N; n++) {
phi = phi0 + dphi * n;
px = this.Center[0] + this.Radius*Math.cos(phi);
py = this.Center[1] + this.Radius*Math.sin(phi);
pts.push([px, py]);
}
return pts;
}
};
function createCuttingObject(p) {
/*
Creates printable cylinder without overhanging faces.
Parameters:
style: 'octagon', 'corner', 'flat'
diameter: diameter of the hole
resolution: resolution of the circle
*/
if (typeof p === undefined) p = {};
var d = 'diameter' in p ? p.diameter : 8;
var CutStyle = 'style' in p ? p.style : 'flat';
var Resolution = 'resolution' in p ? p.resolution : 64;
var Points = [];
var phi = 0;
var R = 0;
var n = 0;
switch (CutStyle) {
case 'octagon':
R = 0.5 * d / Math.cos(Math.PI / 8.0);
for (n = 0; n < 8; n++) {
phi = -7.0 * Math.PI / 8.0 + n * Math.PI / 4.0;
Points.push([R * Math.cos(phi), R * Math.sin(phi)]);
}
break;
case 'corner':
Resolution = 3 * Math.ceil(Resolution / 4.0);
R = 0.5 * d / Math.cos(0.75 * Math.PI / Resolution);
for (n = 0; n < Resolution; n++) {
phi = -0.75 * Math.PI + (n + 0.5) * 1.5 * Math.PI / Resolution;
Points.push([R * Math.cos(phi), R * Math.sin(phi)]);
}
Points.push([-R * Math.sqrt(2), 0]);
break;
default:
Resolution = 3 * Math.ceil(Resolution / 4.0);
R = 0.5 * d / Math.cos(0.75 * Math.PI / Resolution);
for (n = 0; n < Resolution; n++) {
phi = -0.75 * Math.PI + (n + 0.5) * 1.5 * Math.PI / Resolution;
Points.push([R * Math.cos(phi), R * Math.sin(phi)]);
}
R = 0.5 * d / Math.cos(Math.PI / 8.0);
phi = 7.0 * Math.PI / 8.0;
Points.push([R * Math.cos(phi), R * Math.sin(phi)]);
phi = -7.0 * Math.PI / 8.0;
Points.push([R * Math.cos(phi), R * Math.sin(phi)]);
break;
}
return Points;
}
module.geom = {
point: function(x, y) {
return new Vector2d(x, y);
},
line: function(prm) {
return new Line(prm);
},
arc: function(prm) {
return new Arc(prm);
},
/*
"octagon", "drop", "flat" are patterns that can be used for cutting printable holes when the axis of the hole
lies in horizontal plane.
d - diameter of the inscribed circle
*/
octagon: function (d) {
var p = {};
p.style = 'octagon';
if (typeof d === undefined)
throw new Error("input argument d is undefined");
p.diameter = d;
return createCuttingObject(p);
},
drop: function (d) {
var p = {};
p.style = 'corner';
if (typeof d === undefined)
throw new Error("input argument d is undefined");
p.diameter = d;
return createCuttingObject(p);
},
flat: function (d) {
var p = {};
p.style = 'flat';
if (typeof d === undefined)
throw new Error("input argument d is undefined");
p.diameter = d;
return createCuttingObject(p);
}
};
}(this));