File: map_2nets.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 (225 lines) | stat: -rw-r--r-- 6,352 bytes parent folder | download
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
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2021 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")
 */

/* 2nets from terminal to terminal, no junctions allowed */

#include "config.h"

#include <stdio.h>

#include "map_2nets.h"
#include "data.h"
#include "find.h"
#include "netlist.h"
#include "search.h"
#include <librnd/core/rnd_printf.h>
#include <librnd/core/plugins.h>
#include "../src_3rd/genprique/fibheap.h"
#include "../src_3rd/genprique/fibheap.c"
#include "../src_3rd/libusearch/a_star_api.h"
#include "../src_3rd/libusearch/a_star_impl.h"

#define OID(o) ((o == NULL) ? 0 : o->ID)

static void list_obj(void *ctx, pcb_board_t *pcb, pcb_layer_t *layer, pcb_any_obj_t *obj)
{
	pcb_2netmap_t *map = ctx;
	pcb_qry_netseg_len_t *seg;
	pcb_any_obj_t **o;
	long n;
	pcb_2netmap_iseg_t *ns;

	if (!map->nonterminals && (obj->term == NULL))
		return;

	if ((layer != NULL) && (pcb_layer_flags_(layer) & PCB_LYT_COPPER) == 0)
		return;

	if ((obj->term == NULL) && (htpp_get(&map->o2n, obj) != NULL)) /* object already found */
		return;

	if (htpp_has(&map->o2n, obj)) /* already mapped */
		return;

	seg = pcb_qry_parent_net_len_mapseg(map->ec, obj, map->find_rats);
	if (seg == NULL)
		return;

	if (seg->junc_at[0] != NULL)
		map->junc_at = seg->junc_at[0];

	ns = calloc(sizeof(pcb_2netmap_iseg_t), 1);
	if (!seg->has_invalid_hub) {
		ns->next = map->isegs;
		map->isegs = ns;
	}
	ns->seg = seg;
	ns->net = NULL;

	if (seg->objs.used > 0) {
		ns->term[0] = (((pcb_any_obj_t *)seg->objs.array[0])->term != NULL);
		ns->term[1] = (((pcb_any_obj_t *)seg->objs.array[seg->objs.used-1])->term != NULL);
	}

	printf("seg=%p %s junc: %ld %ld term: %d %d\n", (void *)seg, (seg->hub ? "HUB" : ""), OID(seg->junction[0]), OID(seg->junction[1]), ns->term[0], ns->term[1]);

	/* insert end terminals if they are junctions */
	if ((seg->junction[1] != NULL) && (seg->junction[1]->term != NULL) && (seg->objs.used > 1)) {
		pcb_any_obj_t *o = seg->objs.array[seg->objs.used-1];
		if (o->term == NULL) /* insert only if the first object is not a terminal already - we want terminal-terminal 2nets */
			vtp0_append(&seg->objs, &seg->junction[1]);
	}
	if ((seg->junction[0] != NULL) && (seg->junction[0]->term != NULL)) {
		pcb_any_obj_t *o = seg->objs.array[0];
		if (o->term == NULL) /* insert only if the first object is not a terminal already - we want terminal-terminal 2nets */
			vtp0_insert_len(&seg->objs, 0, (void **)(&seg->junction[0]), 1);
	}

	/* figure the net by looking at terminals along this segment */
	for(n = 0, o = (pcb_any_obj_t **)seg->objs.array; n < seg->objs.used; n++,o++) {
		if (*o == NULL) {
			printf("  NULL\n");
			continue;
		}
		if (!seg->has_invalid_hub)
			htpp_set(&map->o2n, *o, ns);
		printf("  #%ld\n", (*o)->ID);

		if ((*o)->term != NULL) {
			pcb_net_term_t *t = pcb_net_find_by_obj(&pcb->netlist[PCB_NETLIST_EDITED], *o);
			if ((t != NULL) && (t->parent.net != NULL)) {
				if ((ns->net != NULL) && (ns->net != t->parent.net))
					ns->shorted = 1;
				ns->net = t->parent.net;
			}
		}
	}

	if (seg->has_invalid_hub) {
		rnd_message(RND_MSG_ERROR, "Network %s can not be included in the net map due to invalid junction\n", ns->net->name);
		pcb_qry_lenseg_free_fields(seg);
		free(ns);
		return;
	}
}

static void list_line_cb(void *ctx, pcb_board_t *pcb, pcb_layer_t *layer, pcb_line_t *line)
{
	list_obj(ctx, pcb, layer, (pcb_any_obj_t *)line);
}

static void list_arc_cb(void *ctx, pcb_board_t *pcb, pcb_layer_t *layer, pcb_arc_t *arc)
{
	list_obj(ctx, pcb, layer, (pcb_any_obj_t *)arc);
}

static void list_poly_cb(void *ctx, pcb_board_t *pcb, pcb_layer_t *layer, pcb_poly_t *poly)
{
	list_obj(ctx, pcb, layer, (pcb_any_obj_t *)poly);
}

static void list_pstk_cb(void *ctx, pcb_board_t *pcb, pcb_pstk_t *ps)
{
	list_obj(ctx, pcb, NULL, (pcb_any_obj_t *)ps);
}

static int list_subc_cb(void *ctx, pcb_board_t *pcb, pcb_subc_t *subc, int enter)
{
	pcb_board_t tmp;

	tmp = *pcb;
	tmp.Data = subc->data;
	pcb_loop_all(&tmp, ctx,
		NULL, /* layer */
		list_line_cb,
		list_arc_cb,
		NULL, /* text */
		list_poly_cb,
		NULL, /* gfx */
		list_subc_cb, /* subc */
		list_pstk_cb
	);
	return 0;
}


#include "map_2nets_geo.c"
#include "map_2nets_map.c"


/*** API functions ***/

int pcb_map_2nets_init(pcb_2netmap_t *map, pcb_board_t *pcb)
{
	pcb_qry_exec_t ec;

	pcb_qry_init(&ec, pcb, NULL, 0);

	ec.cfg_prefer_term = 1;

	map->ec = &ec;
	map->junc_at = NULL;

	htpp_init(&map->o2n, ptrhash, ptrkeyeq);

	/* map segments using query's netlen mapper; first from terminals then
	   from non-terminals; this way some corner cases can be avoided by
	   picking out objects forming real nets, starting from terminals */
	for(map->nonterminals = 0; map->nonterminals < 2; map->nonterminals++) {
		pcb_loop_all(PCB, map,
			NULL, /* layer */
			list_line_cb,
			list_arc_cb,
			NULL, /* text */
			list_poly_cb,
			NULL, /* gfx */
			list_subc_cb, /* subc */
			list_pstk_cb
		);
	}

	/* the result is really a graph because of junctions; search random paths
	   from terminal to terminal (junctions resolved into overlaps) */
	map_segs(map);

	pcb_qry_uninit(&ec);

	return 0;
}

int pcb_map_2nets_uninit(pcb_2netmap_t *map)
{
	pcb_2netmap_oseg_t *oseg, *next;
	for(oseg = map->osegs; oseg != NULL; oseg = next) {
		next = oseg->next;
		vtp0_uninit(&oseg->objs);
		free(oseg);
	}
	map->osegs = NULL;
	htpp_uninit(&map->o2n);
	return -1;
}