Repository navigation
Expand file tree
/
Copy patharc.ts
More file actions
405 lines (345 loc) · 13.6 KB
/
Copy patharc.ts
File metadata and controls
405 lines (345 loc) · 13.6 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
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
/*
Copyright (c) 2022 Alethea Katherine Flowers.
Published under the standard MIT License.
Full text available at: https://opensource.org/licenses/MIT
*/
import { Angle } from "./angle";
import { BBox } from "./bbox";
import { Vec2 } from "./vec2";
/**
* Arc direction
*/
export type ArcDirection = "clockwise" | "counter-clockwise";
/**
* A circular arc
*/
export class Arc {
/**
* Create a new Arc
*/
constructor(
public center: Vec2,
public radius: number,
public start_angle: Angle,
public end_angle: Angle,
public width: number,
public direction: ArcDirection = "clockwise",
) {}
/**
* Create an Arc given three points on a circle
*/
static from_three_points(start: Vec2, mid: Vec2, end: Vec2, width = 1) {
const u = 1000000;
const center = arc_center_from_three_points(
new Vec2(start.x * u, start.y * u),
new Vec2(mid.x * u, mid.y * u),
new Vec2(end.x * u, end.y * u),
);
center.x /= u;
center.y /= u;
const radius = center.sub(mid).magnitude;
const start_angle = start.sub(center).angle;
const mid_angle = mid.sub(center).angle;
const end_angle = end.sub(center).angle;
// calculate the arc angle
let arc_angle;
const start_to_mid = mid_angle.sub(start_angle).normalize();
const start_to_end = end_angle.sub(start_angle).normalize();
if (start_to_mid.degrees < start_to_end.degrees) {
// minor arc, angle = start_to_end
arc_angle = start_to_end;
} else {
// major arc, angle = 360 - start_to_end
arc_angle = Angle.from_degrees(360).sub(start_to_end);
}
// although KiCad always creates clockwise arcs, the file may contain
// counter-clockwise arcs through imports from other EDA/CAD programs
let arc_start;
let direction: ArcDirection;
const mid_to_start = mid.sub(start);
const end_to_mid = end.sub(mid);
if (mid_to_start.cross(end_to_mid) < 0) {
arc_start = end_angle.normalize();
direction = "counter-clockwise";
} else {
arc_start = start_angle.normalize();
direction = "clockwise";
}
const arc_end = arc_start.add(arc_angle);
return new Arc(center, radius, arc_start, arc_end, width, direction);
}
static from_center_start_end(
center: Vec2,
start: Vec2,
end: Vec2,
width: number,
) {
// See EDA_SHAPE::CalcArcAngles - normalizes the start and end angle so
// that start < end and their values are between -360 and +360.
const radius = start.sub(center).magnitude;
const start_radial = start.sub(center);
const end_radial = end.sub(center);
let start_angle = start_radial.kicad_angle;
let end_angle = end_radial.kicad_angle;
if (end_angle.degrees == start_angle.degrees) {
// This is a circle, not a zero-length arc.
end_angle.degrees = start_angle.degrees + 360;
}
if (start_angle.degrees > end_angle.degrees) {
if (end_angle.degrees < 0) {
end_angle = end_angle.normalize();
} else {
start_angle = start_angle
.normalize()
.sub(Angle.from_degrees(-360));
}
}
return new Arc(center, radius, start_angle, end_angle, width);
}
get start_radial() {
return this.start_angle.rotate_point(new Vec2(this.radius, 0));
}
get start_point() {
return this.center.add(this.start_radial);
}
get end_radial() {
return this.end_angle.rotate_point(new Vec2(this.radius, 0));
}
get end_point() {
return this.center.add(this.end_radial);
}
get mid_angle() {
return new Angle(
(this.start_angle.radians + this.end_angle.radians) / 2,
);
}
get mid_radial() {
return this.mid_angle.rotate_point(new Vec2(this.radius, 0));
}
get mid_point() {
return this.center.add(this.mid_radial);
}
get arc_angle(): Angle {
return this.end_angle.sub(this.start_angle);
}
/**
* Approximate the Arc using a polyline
*/
to_polyline(): Vec2[] {
const points: Vec2[] = [];
let start = this.start_angle.radians;
let end = this.end_angle.radians;
if (start > end) {
[end, start] = [start, end];
}
// TODO: Pull KiCAD's logic for this, since it adds more segments the
// larger the arc is.
for (let theta = start; theta < end; theta += Math.PI / 32) {
points.push(
new Vec2(
this.center.x + Math.cos(theta) * this.radius,
this.center.y + Math.sin(theta) * this.radius,
),
);
}
let last_angle;
if (this.direction === "counter-clockwise") {
// for a counter-clockwise arc, it was drawn from the endpoint to the start
// so we need reverse the points
points.reverse();
last_angle = start;
} else {
last_angle = end;
}
// Add the last point if needed.
const last_point = new Vec2(
this.center.x + Math.cos(last_angle) * this.radius,
this.center.y + Math.sin(last_angle) * this.radius,
);
if (!last_point.equals(points[points.length - 1])) {
points.push(last_point);
}
return points;
}
/**
* Same as to_polyline, but includes the arc center
*/
to_polygon(): Vec2[] {
const points = this.to_polyline();
points.push(this.center);
return points;
}
/**
* Get a bounding box that encloses the entire arc.
*/
get bbox(): BBox {
// An arc's bbox contains at least three points: the radial for the
// start angle, the radial for the end angle, and the radial inbetween.
// However, that doesn't cover all cases. Whenever the arc crosses an
// axis, the radial at that axis must also be included.
const points = [this.start_point, this.mid_point, this.end_point];
if (this.start_angle.degrees < 0 && this.end_angle.degrees >= 0) {
points.push(this.center.add(new Vec2(this.radius, 0)));
}
if (this.start_angle.degrees < 90 && this.end_angle.degrees >= 90) {
points.push(this.center.add(new Vec2(0, this.radius)));
}
if (this.start_angle.degrees < 180 && this.end_angle.degrees >= 180) {
points.push(this.center.add(new Vec2(-this.radius, 0)));
}
if (this.start_angle.degrees < 270 && this.end_angle.degrees >= 270) {
points.push(this.center.add(new Vec2(0, this.radius)));
}
if (this.start_angle.degrees < 360 && this.end_angle.degrees >= 360) {
points.push(this.center.add(new Vec2(0, this.radius)));
}
return BBox.from_points(points);
}
}
/**
* Figure out the center point of a circular arc given three points along the circle.
*
* Ported from KiCAD's KiMATH trigo
*/
function arc_center_from_three_points(start: Vec2, mid: Vec2, end: Vec2): Vec2 {
const sqrt_1_2 = Math.SQRT1_2;
const center = new Vec2(0, 0);
const y_delta_21 = mid.y - start.y;
let x_delta_21 = mid.x - start.x;
const y_delta_32 = end.y - mid.y;
let x_delta_32 = end.x - mid.x;
// This is a special case for mid as the half-way point when aSlope = 0 and bSlope = inf
// or the other way around. In that case, the center lies in a straight line between
// start and end
if (
(x_delta_21 == 0.0 && y_delta_32 == 0.0) ||
(y_delta_21 == 0.0 && x_delta_32 == 0.0)
) {
center.x = (start.x + end.x) / 2.0;
center.y = (start.y + end.y) / 2.0;
return center;
}
// Prevent div=0 errors
if (x_delta_21 == 0.0) {
x_delta_21 = Number.EPSILON;
}
if (x_delta_32 == 0.0) x_delta_32 = -Number.EPSILON;
let slope_a = y_delta_21 / x_delta_21;
let slope_b = y_delta_32 / x_delta_32;
const d_slope_a =
slope_a * new Vec2(0.5 / y_delta_21, 0.5 / x_delta_21).magnitude;
const d_slope_b =
slope_b * new Vec2(0.5 / y_delta_32, 0.5 / x_delta_32).magnitude;
if (slope_a == slope_b) {
if (start == end) {
// This is a special case for a 360 degrees arc. In this case, the center is halfway between
// the midpoint and either end point
center.x = (start.x + mid.x) / 2.0;
center.y = (start.y + mid.y) / 2.0;
return center;
} else {
// If the points are colinear, the center is at infinity, so offset
// the slope by a minimal amount
// Warning: This will induce a small error in the center location
slope_a += Number.EPSILON;
slope_b -= Number.EPSILON;
}
}
// Prevent divide by zero error
if (slope_a == 0.0) {
slope_a = Number.EPSILON;
}
// What follows is the calculation of the center using the slope of the two lines as well as
// the propagated error that occurs when rounding to the nearest nanometer. The error can be
// ±0.5 units but can add up to multiple nanometers after the full calculation is performed.
// All variables starting with `d` are the delta of that variable. This is approximately equal
// to the standard deviation.
// We ignore the possible covariance between variables. We also truncate our series expansion
// at the first term. These are reasonable assumptions as the worst-case scenario is that we
// underestimate the potential uncertainty, which would potentially put us back at the status quo
const slope_ab_start_end_y = slope_a * slope_b * (start.y - end.y);
const d_slope_ab_start_end_y =
slope_ab_start_end_y *
Math.sqrt(
((d_slope_a / slope_a) * d_slope_a) / slope_a +
((d_slope_b / slope_b) * d_slope_b) / slope_b +
(sqrt_1_2 / (start.y - end.y)) * (sqrt_1_2 / (start.y - end.y)),
);
const slope_b_start_mid_x = slope_b * (start.x + mid.x);
const d_slope_b_start_mid_x =
slope_b_start_mid_x *
Math.sqrt(
((d_slope_b / slope_b) * d_slope_b) / slope_b +
((sqrt_1_2 / (start.x + mid.x)) * sqrt_1_2) / (start.x + mid.x),
);
const slope_a_mid_end_x = slope_a * (mid.x + end.x);
const d_slope_a_mid_end_x =
slope_a_mid_end_x *
Math.sqrt(
((d_slope_a / slope_a) * d_slope_a) / slope_a +
((sqrt_1_2 / (mid.x + end.x)) * sqrt_1_2) / (mid.x + end.x),
);
const twice_b_a_slope_diff = 2 * (slope_b - slope_a);
const d_twice_b_a_slope_diff =
2 * Math.sqrt(d_slope_b * d_slope_b + d_slope_a * d_slope_a);
const center_numerator_x =
slope_ab_start_end_y + slope_b_start_mid_x - slope_a_mid_end_x;
const d_center_numerator_x = Math.sqrt(
d_slope_ab_start_end_y * d_slope_ab_start_end_y +
d_slope_b_start_mid_x * d_slope_b_start_mid_x +
d_slope_a_mid_end_x * d_slope_a_mid_end_x,
);
const center_x =
(slope_ab_start_end_y + slope_b_start_mid_x - slope_a_mid_end_x) /
twice_b_a_slope_diff;
const d_center_x =
center_x *
Math.sqrt(
((d_center_numerator_x / center_numerator_x) *
d_center_numerator_x) /
center_numerator_x +
((d_twice_b_a_slope_diff / twice_b_a_slope_diff) *
d_twice_b_a_slope_diff) /
twice_b_a_slope_diff,
);
const center_numerator_y = (start.x + mid.x) / 2.0 - center_x;
const d_center_numerator_y = Math.sqrt(1.0 / 8.0 + d_center_x * d_center_x);
const center_first_term = center_numerator_y / slope_a;
const d_center_first_term_y =
center_first_term *
Math.sqrt(
((d_center_numerator_y / center_numerator_y) *
d_center_numerator_y) /
center_numerator_y +
((d_slope_a / slope_a) * d_slope_a) / slope_a,
);
const center_y = center_first_term + (start.y + mid.y) / 2.0;
const d_center_y = Math.sqrt(
d_center_first_term_y * d_center_first_term_y + 1.0 / 8.0,
);
const rounded_100_center_x = Math.floor((center_x + 50.0) / 100.0) * 100.0;
const rounded_100_center_y = Math.floor((center_y + 50.0) / 100.0) * 100.0;
const rounded_10_center_x = Math.floor((center_x + 5.0) / 10.0) * 10.0;
const rounded_10_center_y = Math.floor((center_y + 5.0) / 10.0) * 10.0;
// The last step is to find the nice, round numbers near our baseline estimate and see if they are within our uncertainty
// range If they are, then we use this round value as the true value. This is justified because ALL values within the
// uncertainty range are equally true. Using a round number will make sure that we are on a multiple of 1mil or 100nm
// when calculating centers.
if (
Math.abs(rounded_100_center_x - center_x) < d_center_x &&
Math.abs(rounded_100_center_y - center_y) < d_center_y
) {
center.x = rounded_100_center_x;
center.y = rounded_100_center_y;
} else if (
Math.abs(rounded_10_center_x - center_x) < d_center_x &&
Math.abs(rounded_10_center_y - center_y) < d_center_y
) {
center.x = rounded_10_center_x;
center.y = rounded_10_center_y;
} else {
center.x = center_x;
center.y = center_y;
}
return center;
}