File: acompnet.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (242 lines) | stat: -rw-r--r-- 7,196 bytes parent folder | download | duplicates (2)
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
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2016,2019 Tibor 'Igor2' Palinkas
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 */

#include "config.h"
#include "board.h"
#include "data.h"
#include "layer.h"
#include "layer_ui.h"
/*#include "acompnet_conf.h"*/
#include <librnd/core/actions.h>
#include <librnd/core/plugins.h>
#include "search.h"
#include "polygon.h"
#include <librnd/core/conf.h>
#include "conf_core.h"
#include <librnd/core/compat_misc.h>
#include <librnd/core/error.h>
#include "meshgraph.h"

static const char *acompnet_cookie = "acompnet plugin";

static pcb_layer_t *ly = NULL;
static rnd_color_t ly_clr;

typedef struct {
	rnd_coord_t x, y;
	rnd_coord_t r;
} overlap_t;

static rnd_rtree_dir_t overlap(void *cl, void *obj_, const rnd_rtree_box_t *box)
{
	pcb_any_obj_t *obj = (pcb_any_obj_t *)obj_;
	overlap_t *ovl = (overlap_t *)cl;
	switch(obj->type) {
		case PCB_OBJ_LINE:
			if (pcb_is_point_in_line(ovl->x, ovl->y, ovl->r, (pcb_any_line_t *)obj))
				return rnd_RTREE_DIR_FOUND_STOP;
			break;
		case PCB_OBJ_ARC:
			if (pcb_is_point_on_arc(ovl->x, ovl->y, ovl->r, (pcb_arc_t *)obj))
				return rnd_RTREE_DIR_FOUND_STOP;
			break;
		case PCB_OBJ_TEXT:
			if (pcb_is_point_in_box(ovl->x, ovl->y, &obj->bbox_naked, ovl->r))
				return rnd_RTREE_DIR_FOUND_STOP;
			break;
		case PCB_OBJ_POLY:
			if (pcb_poly_is_point_in_p(ovl->x, ovl->y, ovl->r, (pcb_poly_t *)obj))
				return rnd_RTREE_DIR_FOUND_STOP;
			break;
		default: break;
	}
	return rnd_RTREE_DIR_NOT_FOUND_CONT;
}

TODO("move this to search.[ch]")
/* Search for object(s) on a specific layer */
static rnd_rtree_dir_t pcb_search_on_layer(pcb_layer_t *layer, const rnd_rtree_box_t *bbox, rnd_rtree_dir_t (*cb)(void *cl, void *obj, const rnd_rtree_box_t *box), void *closure)
{
	rnd_rtree_dir_t res, fin = 0;

	if ((res = rnd_rtree_search_any(layer->line_tree, bbox, NULL, cb, closure, NULL)) & rnd_RTREE_DIR_FOUND)
		return res;
	fin |= res;

	if ((res = rnd_rtree_search_any(layer->arc_tree, bbox, NULL, cb, closure, NULL))  & rnd_RTREE_DIR_FOUND)
		return res;
	fin |= res;

	if ((res = rnd_rtree_search_any(layer->polygon_tree, bbox, NULL, cb, closure, NULL)) & rnd_RTREE_DIR_FOUND)
		return res;
	fin |= res;

	if ((res = rnd_rtree_search_any(layer->text_tree, bbox, NULL, cb, closure, NULL)) & rnd_RTREE_DIR_FOUND)
		return res;
	fin |= res;

TODO("padstack too");

	return res;
}


pcb_flag_t flg_mesh_pt;
static void acompnet_mesh_addpt(pcb_meshgraph_t *gr, pcb_layer_t *layer, double x, double y, int score, double sep)
{
	overlap_t ovl;
	rnd_box_t bbox;

	x = rnd_round(x);
	y = rnd_round(y);

	ovl.x = x;
	ovl.y = y;
	ovl.r = rnd_round(sep/2-1);
	bbox.X1 = x - ovl.r;
	bbox.X2 = x + ovl.r;
	bbox.Y1 = y - ovl.r;
	bbox.Y2 = y + ovl.r;

	if (!(pcb_search_on_layer(layer, (rnd_rtree_box_t *)&bbox, overlap, &ovl) & rnd_RTREE_DIR_FOUND)) {
		bbox.X1 = x;
		bbox.X2 = x+1;
		bbox.Y1 = y;
		bbox.Y2 = y+1;
		pcb_msgr_add_node(gr, &bbox, score);
		pcb_line_new(ly, x, y, x, y, conf_core.design.line_thickness, conf_core.design.bloat, flg_mesh_pt);
	}
}

static void acompnet_mesh(pcb_meshgraph_t *gr, pcb_layer_t *layer)
{
	double sep = conf_core.design.line_thickness + conf_core.design.bloat;
	int n;

	PCB_LINE_LOOP(PCB_CURRLAYER(PCB)) {
		double i, len, vx, vy, x1, y1, x2, y2, nx, ny;
		x1 = line->Point1.X;
		x2 = line->Point2.X;
		y1 = line->Point1.Y;
		y2 = line->Point2.Y;
		vx = x2 - x1;
		vy = y2 - y1;
		len = sqrt(vx*vx + vy*vy);
		vx = vx/len;
		vy = vy/len;
		nx = vy;
		ny = -vx;

		/* straight line extension points */
		acompnet_mesh_addpt(gr, layer, x1 - vx*sep, y1 - vy*sep, 0, sep);
		acompnet_mesh_addpt(gr, layer, x2 + vx*sep, y2 + vy*sep, 0, sep);

		/* side and extended points; n is in-line offset from endpoint */
		for(n = 0; n <= 1; n++) {
			acompnet_mesh_addpt(gr, layer, x1 - n*vx*sep + nx*sep, y1 - n*vy*sep + ny*sep, 1, sep);
			acompnet_mesh_addpt(gr, layer, x1 - n*vx*sep - nx*sep, y1 - n*vy*sep - ny*sep, 1, sep);

			acompnet_mesh_addpt(gr, layer, x2 + n*vx*sep + nx*sep, y2 + n*vy*sep + ny*sep, 1, sep);
			acompnet_mesh_addpt(gr, layer, x2 + n*vx*sep - nx*sep, y2 + n*vy*sep - ny*sep, 1, sep);
		}

		for(i = 2*sep; i <= len - 2*sep; i += sep*3) {
			acompnet_mesh_addpt(gr, layer, x1 + i*vx + nx*sep, y1 + i*vy + ny*sep, 2, sep);
			acompnet_mesh_addpt(gr, layer, x1 + i*vx - nx*sep, y1 + i*vy - ny*sep, 2, sep);
		}
	}
	PCB_END_LOOP;
}


static const char pcb_acts_acompnet[] = "acompnet()\n" ;
static const char pcb_acth_acompnet[] = "Attempt to auto-complete the current network";

static fgw_error_t pcb_act_acompnet(fgw_arg_t *res, int argc, fgw_arg_t *argv)
{
	pcb_meshgraph_t gr;
	long int is, ie;

	if (ly == NULL)
		ly = pcb_uilayer_alloc(PCB_ACT_BOARD, acompnet_cookie, "autocomp-net", &ly_clr);

	pcb_msgr_init(&gr);
	acompnet_mesh(&gr, PCB_CURRLAYER(PCB));

	{ /* temporary hack for testing: fixed, off-mesh start/end */
		rnd_box_t bbox;
		bbox.X1 = RND_MM_TO_COORD(6.35); bbox.X2 = bbox.X1+1;
		bbox.Y1 = RND_MM_TO_COORD(21.5); bbox.Y2 = bbox.Y1+1;
		is = pcb_msgr_add_node(&gr, &bbox, 0);
		bbox.X1 = RND_MM_TO_COORD(12); bbox.X2 = bbox.X1+1;
		bbox.Y1 = RND_MM_TO_COORD(3.8); bbox.Y2 = bbox.Y1+1;
		ie = pcb_msgr_add_node(&gr, &bbox, 0);
	}

	pcb_msgr_astar(&gr, is, ie);

	{ /* temporary visualisation code */
		long int id = ie;
		for(;;) {
			pcb_meshnode_t *curr, *prev;
			curr = htip_get(&gr.id2node, id);
			id = curr->came_from;
			if (id == 0)
				break;
			prev = htip_get(&gr.id2node, id);
			pcb_line_new(ly, curr->bbox.X1, curr->bbox.Y1, prev->bbox.X1, prev->bbox.Y1, conf_core.design.line_thickness/2, conf_core.design.bloat/2, flg_mesh_pt);
TODO("use pcb_drc_lines(), probably during A*, saving mid-point, and draw pairs of lines here");
		}
	}
	RND_ACT_IRES(0);
	return 0;
}

rnd_action_t acompnet_action_list[] = {
	{"acompnet", pcb_act_acompnet, pcb_acth_acompnet, pcb_acts_acompnet},
};


int pplg_check_ver_acompnet(int ver_needed) { return 0; }

void pplg_uninit_acompnet(void)
{
	rnd_remove_actions_by_cookie(acompnet_cookie);
	pcb_uilayer_free_all_cookie(acompnet_cookie);
}

int pplg_init_acompnet(void)
{

	RND_API_CHK_VER;

	if (ly_clr.str[0] != '#')
		rnd_color_load_str(&ly_clr, "#c09920");

	RND_REGISTER_ACTIONS(acompnet_action_list, acompnet_cookie)

	return 0;
}