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/>.
28 #include <localcharset.h>
32 #include <CNearTree.h>
42 #define _(x) (gettext(x))
44 static const char *progname = "slotifier";
45 static unsigned verbose;
48 static const char sopts[] = SOPT_STRING;
49 static const struct option lopts[] = {
54 static void print_version(void)
56 const char *copysign = "(C)";
57 void *convsign = NULL;
62 convsign = str_iconv("\xc2\xa9", "UTF-8", locale_charset());
67 printf("Copyright %s 2021 Nick Bowler.\n", copysign);
68 puts("License GPLv3+: GNU GPL version 3 or any later version.");
69 puts("This is free software: you are free to change and redistribute it.");
70 puts("There is NO WARRANTY, to the extent permitted by law.");
75 static void print_usage(FILE *f)
77 fprintf(f, _("Usage: %s [options] [-o filename] filename\n"), progname);
79 fprintf(f, _("Try %s --help for more information.\n"),
83 static void print_help(void)
85 const struct option *opt;
89 puts(_("This is \"slotifier\": a tool to convert overlapping drill hits in Excellon\n"
90 "drill files to G85 drill slots."));
94 for (opt = lopts; opt->val; opt++) {
95 struct lopt_help help;
97 if (!lopt_get_help(opt, &help))
100 help_print_option(opt, help.arg, help.desc, 20);
104 puts(_("For more information, see the slotifier(1) man page."));
108 * TRANSLATORS: Please add *another line* indicating where users should
109 * report translation bugs.
111 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
114 static void init_i18n(void)
116 setlocale(LC_ALL, "");
117 bindtextdomain(PACKAGE, LOCALEDIR);
121 static CNearTreeHandle build_search_tree(gerbv_image_t *drill)
126 if (CNearTreeCreate(&t, 2, CNEARTREE_TYPE_DOUBLE|CNEARTREE_NORM_L2))
129 /* Build a search tree from all the holes. */
130 for (x = drill->netlist; x; x = x->next) {
131 double xy[2] = { x->start_x, x->start_y };
133 assert(x->aperture >= 0 && x->aperture < APERTURE_MAX);
135 /* Skip things that aren't drill hits */
136 if (!drill->aperture[x->aperture])
139 /* Holes are marked as "flashing"; otherwise it's an existing
141 if (x->aperture_state != GERBV_APERTURE_STATE_FLASH)
144 if (CNearTreeInsert(t, xy, x)) {
150 if (CNearTreeCompleteDelayedInsert(t)) {
158 static gerbv_aperture_type_t tool_type(gerbv_image_t *drill, int aperture)
160 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
165 static double tool_radius(gerbv_image_t *drill, int aperture)
167 gerbv_aperture_t *tool = drill->aperture[abs(aperture)];
169 /* Half a mil slop to decisively include points on boundary. */
170 return tool->parameter[0] / 2.0 + 0.0005;
173 static int holes_overlap(gerbv_image_t *drill, gerbv_net_t *a, gerbv_net_t *b)
175 double d = hypot(a->start_x - b->start_x, a->start_y - b->start_y);
177 return tool_radius(drill, a->aperture) >= d
178 || tool_radius(drill, b->aperture) >= d;
181 static int combine_holes(gerbv_image_t *drill, gerbv_net_t *hole,
184 CVectorHandle group, tmp;
185 int biggest_tool, ret = -1;
190 * Since we consider holes in order of decreasing size, the initial hole
191 * considered is by definition the biggest one we will find in a group.
193 biggest_r = tool_radius(drill, (biggest_tool = hole->aperture));
195 if (CVectorCreate(&group, sizeof (gerbv_net_t *), 10)) {
196 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
200 if (CVectorCreate(&tmp, sizeof (void *), 10)) {
201 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
207 * Breadth-first nearest neighbour search of holes. We negate the
208 * aperture to indicate which holes have been previously visited.
210 CVectorAddElement(group, &hole);
211 hole->aperture = -hole->aperture;
213 for (i = 0; i < CVectorSize(group); i++) {
216 CVectorGetElement(group, &hole, i);
218 assert(tool_type(drill, hole->aperture) == GERBV_APTYPE_CIRCLE);
219 assert(tool_radius(drill, hole->aperture) <= biggest_r);
221 xy[0] = hole->start_x; xy[1] = hole->start_y;
222 if (CNearTreeFindInSphere(t, biggest_r, 0, tmp, xy, 1) != 0) {
223 /* We should always should find at least one hole! */
224 fprintf(stderr, _("%s: fatal error searching holes\n"),
229 for (j = 0; j < CVectorSize(tmp); j++) {
230 /* I don't know why, but CNearTree returns a list
231 * of pointers to its internal copies of pointers
232 * to the objects in the tree. So we need this
233 * double indirection to get the actual hole. */
234 gerbv_net_t *newhole;
237 CVectorGetElement(tmp, &p, j);
238 newhole = *(void **)p;
240 if (newhole->aperture < 0)
241 continue; /* already visited */
243 if (holes_overlap(drill, hole, newhole)) {
244 CVectorAddElement(group, &newhole);
245 newhole->aperture = -newhole->aperture;
250 /* Compare each pair of matched points to find the longest slot. */
251 if (CVectorSize(group) > 1) {
252 int biggest_slot = 0;
255 for (i = 0; i < CVectorSize(group) - 1; i++) {
256 CVectorGetElement(group, &hole, i);
257 for (j = i+1; j < CVectorSize(group); j++) {
261 CVectorGetElement(group, &h2, j);
262 newlen = hypot(hole->start_x - h2->start_x,
263 hole->start_y - h2->start_y);
265 if (newlen > bestlen) {
266 hole->stop_x = h2->start_x;
267 hole->stop_y = h2->stop_y;
274 CVectorGetElement(group, &hole, biggest_slot);
275 hole->aperture = biggest_tool;
276 hole->aperture_state = GERBV_APERTURE_STATE_ON;
279 const char *fmt = ngettext(
280 "%s: merged %zu hole into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
281 "%s: merged %zu holes into slot (%.4f,%.4f)-(%.4f,%.4f)\n",
284 fprintf(stderr, fmt, progname, CVectorSize(group),
285 hole->start_x, hole->start_y,
286 hole->stop_x, hole->stop_y);
289 /* The only hole we found was our original one, restore
291 CVectorGetElement(group, &hole, 0);
293 assert(hole->aperture < 0);
294 hole->aperture = -hole->aperture;
297 fprintf(stderr, _("%s: hole at (%.4f,%.4f) not merged\n"),
298 progname, hole->start_x, hole->start_y);
310 * Order two holes by hole diameter.
312 static gerbv_image_t *hsc_drill_data;
313 static int hole_size_cmp(const void *a_, const void *b_)
315 gerbv_net_t * const *a = a_, * const *b = b_;
316 gerbv_aperture_t *ta, *tb;
318 ta = hsc_drill_data->aperture[abs(a[0]->aperture)];
319 assert(ta->type == GERBV_APTYPE_CIRCLE);
321 tb = hsc_drill_data->aperture[abs(b[0]->aperture)];
322 assert(tb->type == GERBV_APTYPE_CIRCLE);
324 if (ta->parameter[0] > tb->parameter[0])
326 if (ta->parameter[0] < tb->parameter[0])
331 static void sort_holes_by_size(gerbv_image_t *drill, CVectorHandle work)
333 hsc_drill_data = drill;
334 qsort(work->array, work->size, work->elementsize, hole_size_cmp);
337 static int slotify(gerbv_image_t *drill)
339 CVectorHandle holes, work;
344 t = build_search_tree(drill);
346 fprintf(stderr, _("%s: failed to build search tree\n"),
351 CNearTreeObjects(t, &holes);
355 if (CVectorCreate(&work, sizeof (gerbv_net_t *), CVectorSize(holes))) {
356 fprintf(stderr, _("%s: failed to allocate memory\n"), progname);
360 memcpy(work->array, holes->array, holes->size * holes->elementsize);
361 work->size = holes->size;
362 sort_holes_by_size(drill, work);
364 for (i = 0; i < CVectorSize(work); i++) {
367 CVectorGetElement(work, &hole, i);
368 /* Skip holes we've already looked at */
369 if (hole->aperture < 0)
371 if (hole->aperture_state == GERBV_APERTURE_STATE_ON)
375 fprintf(stderr, _("%s: checking hole at (%.4f,%.4f) for overlaps\n"),
376 progname, hole->start_x, hole->start_y);
379 if (combine_holes(drill, hole, t) < 0) {
385 /* Clean out any holes we don't need anymore */
386 for (i = 0; i < CVectorSize(holes); i++) {
389 CVectorGetElement(holes, &hole, i);
390 if (hole->aperture < 0)
391 gerbv_image_delete_net(hole);
400 int main(int argc, char **argv)
402 const char *outfile = "/dev/stdout";
404 gerbv_image_t *drill;
412 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
433 fprintf(stderr, _("%s: error: must specify a filename\n"),
439 if (optind + 1 < argc) {
440 fprintf(stderr, _("%s: error: excess command-line arguments\n"),
446 gp = gerbv_create_project();
450 gerbv_open_layer_from_filename(gp, argv[optind]);
456 drill = gp->file[0]->image;
457 if (drill->layertype != GERBV_LAYERTYPE_DRILL) {
458 fprintf(stderr, _("%s: %s: error: not a drill file\n"),
459 progname, argv[optind]);
464 if (slotify(drill) != 0) {
469 if (!gerbv_export_drill_file_from_image(outfile, drill, NULL))
472 gerbv_destroy_project(gp);