2 * Utility to convert overlapping Excellon drill hits into drill slots.
3 * Copyright © 2018, 2021 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 #include <CNearTree.h>
32 #include <localcharset.h>
39 #define _(x) (gettext(x))
41 static unsigned verbose;
43 static const char *progname = "slotifier";
44 static const char sopts[] = "o:vVH";
45 static const struct option lopts[] = {
46 { "output", 1, NULL, 'o' },
47 { "verbose", 0, NULL, 'v' },
48 { "version", 0, NULL, 'V' },
49 { "help", 0, NULL, 'H' },
53 static void print_version(void)
55 const char *copysign = "(C)";
56 void *convsign = NULL;
61 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
66 printf("Copyright %s 2021 Nick Bowler.\n", copysign);
67 puts("License GPLv3+: GNU GPL version 3 or any later version.");
68 puts("This is free software: you are free to change and redistribute it.");
69 puts("There is NO WARRANTY, to the extent permitted by law.");
74 static void print_usage(FILE *f)
76 fprintf(f, _("Usage: %s [options] [-o filename] filename\n"), progname);
78 fprintf(f, _("Try %s --help for more information.\n"),
82 static void print_help(void)
84 const struct option *opt;
87 puts(_("This is \"slotifier\": a tool to convert overlapping drill hits in Excellon\n"
88 "drill files to G85 drill slots."));
92 for (opt = lopts; opt->val; opt++) {
93 const char *line, *text = "ARG";
98 case 'o': text = _("FILE");
101 w += printf(_(" -%c, --%s=%s"),
102 opt->val, opt->name, text);
104 w += printf(_(" -%c, --%s"), opt->val, opt->name);
114 text = _("Output to FILE, instead of standard output.");
117 text = _("Increase verbosity (can be specified more than once).");
120 text = _("Print a version message and then exit.");
123 text = _("Print this message and then exit.");
131 for (line = text; line[0];) {
132 const char *nl = strchr(line, '\n');
135 printf("%*s%s\n", 20-w, "", line);
139 printf("%*s%.*s\n", 20-w, "", (int)(nl-line), line);
145 puts(_("For more information, see the slotifier(1) man page."));
148 printf(_("Report bugs to <%s>."), PACKAGE_BUGREPORT);
152 static void init_i18n(void)
154 setlocale(LC_ALL, "");
155 bindtextdomain(PACKAGE, LOCALEDIR);
159 static CNearTreeHandle build_search_tree(gerbv_image_t *drill)
164 if (CNearTreeCreate(&t, 2, CNEARTREE_TYPE_DOUBLE|CNEARTREE_NORM_L2))
167 /* Build a search tree from all the holes. */
168 for (x = drill->netlist; x; x = x->next) {
169 double xy[2] = { x->start_x, x->start_y };
171 assert(x->aperture >= 0 && x->aperture < APERTURE_MAX);
173 /* Skip things that aren't drill hits */
174 if (!drill->aperture[x->aperture])
177 /* Holes are marked as "flashing"; otherwise it's an existing
179 if (x->aperture_state != GERBV_APERTURE_STATE_FLASH)
182 if (CNearTreeInsert(t, xy, x)) {
188 if (CNearTreeCompleteDelayedInsert(t)) {
196 static gerbv_aperture_type_t tool_type(gerbv_image_t *drill, int aperture)
198 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
203 static double tool_radius(gerbv_image_t *drill, int aperture)
205 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
207 /* Half a mil slop to decisively include points on boundary. */
208 return tool->parameter[0] / 2.0 + 0.0005;
211 static int holes_overlap(gerbv_image_t *drill, gerbv_net_t *a, gerbv_net_t *b)
213 double d = hypot(a->start_x - b->start_x, a->start_y - b->start_y);
215 return tool_radius(drill, a->aperture) >= d
216 || tool_radius(drill, b->aperture) >= d;
219 static int combine_holes(gerbv_image_t *drill, gerbv_net_t *hole,
222 CVectorHandle group, tmp;
223 int biggest_tool, ret = -1;
228 * Since we consider holes in order of decreasing size, the initial hole
229 * considered is by definition the biggest one we will find in a group.
231 biggest_r = tool_radius(drill, (biggest_tool = hole->aperture));
233 if (CVectorCreate(&group, sizeof (gerbv_net_t *), 10)) {
234 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
238 if (CVectorCreate(&tmp, sizeof (void *), 10)) {
239 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
245 * Breadth-first nearest neighbour search of holes. We negate the
246 * aperture to indicate which holes have been previously visited.
248 CVectorAddElement(group, &hole);
249 hole->aperture = -hole->aperture;
251 for (i = 0; i < CVectorSize(group); i++) {
254 CVectorGetElement(group, &hole, i);
256 assert(tool_type(drill, hole->aperture) == GERBV_APTYPE_CIRCLE);
257 assert(tool_radius(drill, hole->aperture) <= biggest_r);
259 xy[0] = hole->start_x; xy[1] = hole->start_y;
260 if (CNearTreeFindInSphere(t, biggest_r, 0, tmp, xy, 1) != 0) {
261 /* We should always should find at least one hole! */
262 fprintf(stderr, _("%s: fatal error searching holes\n"),
267 for (j = 0; j < CVectorSize(tmp); j++) {
268 /* I don't know why, but CNearTree returns a list
269 * of pointers to its internal copies of pointers
270 * to the objects in the tree. So we need this
271 * double indirection to get the actual hole. */
272 gerbv_net_t *newhole;
275 CVectorGetElement(tmp, &p, j);
276 newhole = *(void **)p;
278 if (newhole->aperture < 0)
279 continue; /* already visited */
281 if (holes_overlap(drill, hole, newhole)) {
282 CVectorAddElement(group, &newhole);
283 newhole->aperture = -newhole->aperture;
288 /* Compare each pair of matched points to find the longest slot. */
289 if (CVectorSize(group) > 1) {
290 int biggest_slot = 0;
293 for (i = 0; i < CVectorSize(group) - 1; i++) {
294 CVectorGetElement(group, &hole, i);
295 for (j = i+1; j < CVectorSize(group); j++) {
299 CVectorGetElement(group, &h2, j);
300 newlen = hypot(hole->start_x - h2->start_x,
301 hole->start_y - h2->start_y);
303 if (newlen > bestlen) {
304 hole->stop_x = h2->start_x;
305 hole->stop_y = h2->stop_y;
312 CVectorGetElement(group, &hole, biggest_slot);
313 hole->aperture = biggest_tool;
314 hole->aperture_state = GERBV_APERTURE_STATE_ON;
317 const char *fmt = ngettext(
318 "%s: merged %zu hole into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
319 "%s: merged %zu holes into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
322 fprintf(stderr, fmt, progname, CVectorSize(group),
323 hole->start_x, hole->start_y,
324 hole->stop_x, hole->stop_y);
327 /* The only hole we found was our original one, restore
329 CVectorGetElement(group, &hole, 0);
331 assert(hole->aperture < 0);
332 hole->aperture = -hole->aperture;
335 fprintf(stderr, _("%s: hole at (%.4f,%.4f) not merged\n"),
336 progname, hole->start_x, hole->start_y);
348 * Order two holes by hole diameter.
350 static gerbv_image_t *hsc_drill_data;
351 static int hole_size_cmp(const void *a_, const void *b_)
353 gerbv_net_t * const *a = a_, * const *b = b_;
354 gerbv_aperture_t *ta, *tb;
356 ta = hsc_drill_data->aperture[abs(a[0]->aperture)];
357 assert(ta->type == GERBV_APTYPE_CIRCLE);
359 tb = hsc_drill_data->aperture[abs(b[0]->aperture)];
360 assert(tb->type == GERBV_APTYPE_CIRCLE);
362 if (ta->parameter[0] > tb->parameter[0])
364 if (ta->parameter[0] < tb->parameter[0])
369 static void sort_holes_by_size(gerbv_image_t *drill, CVectorHandle work)
371 hsc_drill_data = drill;
372 qsort(work->array, work->size, work->elementsize, hole_size_cmp);
375 static int slotify(gerbv_image_t *drill)
377 CVectorHandle holes, work;
382 t = build_search_tree(drill);
384 fprintf(stderr, _("%s: failed to build search tree\n"),
389 CNearTreeObjects(t, &holes);
393 if (CVectorCreate(&work, sizeof (gerbv_net_t *), CVectorSize(holes))) {
394 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
398 memcpy(work->array, holes->array, holes->size * holes->elementsize);
399 work->size = holes->size;
400 sort_holes_by_size(drill, work);
402 for (i = 0; i < CVectorSize(work); i++) {
405 CVectorGetElement(work, &hole, i);
406 /* Skip holes we've already looked at */
407 if (hole->aperture < 0)
409 if (hole->aperture_state == GERBV_APERTURE_STATE_ON)
413 fprintf(stderr, _("%s: checking hole at (%.4f,%.4f) for overlaps\n"),
414 progname, hole->start_x, hole->start_y);
417 if (combine_holes(drill, hole, t) < 0) {
423 /* Clean out any holes we don't need anymore */
424 for (i = 0; i < CVectorSize(holes); i++) {
427 CVectorGetElement(holes, &hole, i);
428 if (hole->aperture < 0)
429 gerbv_image_delete_net(hole);
438 int main(int argc, char **argv)
440 const char *outfile = "/dev/stdout";
442 gerbv_image_t *drill;
450 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
471 fprintf(stderr, _("%s: error: must specify a filename\n"),
477 if (optind + 1 < argc) {
478 fprintf(stderr, _("%s: error: excess command-line arguments\n"),
484 gp = gerbv_create_project();
488 gerbv_open_layer_from_filename(gp, argv[optind]);
494 drill = gp->file[0]->image;
495 if (drill->layertype != GERBV_LAYERTYPE_DRILL) {
496 fprintf(stderr, _("%s: %s: error: not a drill file\n"),
497 progname, argv[optind]);
502 if (slotify(drill) != 0) {
507 if (!gerbv_export_drill_file_from_image(outfile, drill, NULL))
510 gerbv_destroy_project(gp);