File: net_int.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 (203 lines) | stat: -rw-r--r-- 6,367 bytes parent folder | download | duplicates (4)
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
/*
 *
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  (this file is based on PCB, interactive printed circuit board design)
 *  Copyright (C) 2018,2020 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 St, 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 <genht/htpp.h>
#include <genht/hash.h>

#include "board.h"
#include "find.h"
#include "netlist.h"
#include "net_int.h"

#define PCB dontuse

/* evaluates to true if obj was marked on list (fa or fb) */
#define IS_FOUND(obj, list) PCB_FIND_IS_MARKED(&ctx->list, obj)

static int pcb_int_broken_cb(pcb_find_t *fctx, pcb_any_obj_t *new_obj, pcb_any_obj_t *arrived_from, pcb_found_conn_type_t ctype)
{
	pcb_net_int_t *ctx = fctx->user_data;

	if (arrived_from == NULL) /* ingore the starting object - it must be marked as we started from the same object in the first search */
		return 0;

	/* broken if new object is not marked in the shrunk search (fa) but
	   the arrived_from object was marked (so we notify direct breaks only) */
	if (!IS_FOUND(new_obj, fa) && IS_FOUND(arrived_from, fa)) {
		int r = ctx->broken_cb(ctx, new_obj, arrived_from, ctype);
		if (r != 0) return r;
		return ctx->fast; /* if fast is 1, we are aborting the search, returning the first hit only */
	}
	return 0;
}

rnd_bool pcb_net_integrity(pcb_board_t *pcb, pcb_any_obj_t *from, rnd_coord_t shrink, rnd_coord_t bloat, pcb_int_broken_cb_t *cb, void *cb_data)
{
	pcb_net_int_t ctx;

	ctx.pcb = pcb;
	ctx.fast = 1;
	ctx.data = pcb->Data;
	ctx.bloat = bloat;
	ctx.shrink = shrink;
	ctx.broken_cb = cb;
	ctx.cb_data = cb_data;

	memset(&ctx.fa, 0, sizeof(ctx.fa));
	memset(&ctx.fb, 0, sizeof(ctx.fb));
	ctx.fa.user_data = ctx.fb.user_data = &ctx;
	ctx.fb.found_cb = pcb_int_broken_cb;

	/* Check for minimal overlap: shrink mark all objects on the net in fa;
	   restart the search without shrink in fb: if anything new is found, it did
	   not have enough overlap and shrink disconnected it. */
	if (shrink != 0) {
		ctx.shrunk = 1;
		ctx.fa.bloat = -shrink;
		pcb_find_from_obj(&ctx.fa, pcb->Data, from);
		ctx.fa.bloat = 0;
		pcb_find_from_obj(&ctx.fb, pcb->Data, from);
		pcb_find_free(&ctx.fa);
		pcb_find_free(&ctx.fb);
	}

	/* Check for minimal distance: bloat mark all objects on the net in fa;
	   restart the search without bloat in fb: if anything new is found, it did
	   not have a connection without the bloat. */
	if (bloat != 0) {
		ctx.shrunk = 0;
		ctx.fa.bloat = 0;
		pcb_find_from_obj(&ctx.fa, pcb->Data, from);
		ctx.fb.bloat = bloat;
		pcb_find_from_obj(&ctx.fb, pcb->Data, from);
		pcb_find_free(&ctx.fa);
		pcb_find_free(&ctx.fb);
	}

	return rnd_false;
}


/*** cached object -> network-terminal ***/

/* Whenever a net tag of an object is queried:
   1. look at the cache - if the object is already mapped, return result from
      cache (ec->obj2netterm)
   2. else map the whole network segment starting from obj, using galvanic
      connections only; remember every object being in the segment
      (using ec->tmplst)
   3. at the end of the mapping, select the best terminal object for the
      segment: terminal with the lowest ID (when segment is part of a netlisted
      net) or random object with the lowest ID (when segment is floating with
      no connection to terminal)
   4. assign the resulting terminal object to all objects on the net in
      the cache (ec->obj2netterm) so subsequent calls on any segment object
      will bail out in step 1
*/

typedef struct {
	pcb_qry_exec_t *ec;
	pcb_any_obj_t *best_term;
	pcb_any_obj_t *best_nonterm;
} parent_net_term_t;

static int parent_net_term_found_cb(pcb_find_t *fctx, pcb_any_obj_t *new_obj, pcb_any_obj_t *arrived_from, pcb_found_conn_type_t ctype)
{
	parent_net_term_t *ctx = fctx->user_data;

	vtp0_append(&ctx->ec->tmplst, new_obj);

	if (new_obj->term != NULL) {
		if ((ctx->best_term == NULL) || (new_obj->ID < ctx->best_term->ID))
			ctx->best_term = new_obj;
	}
	else {
		if ((ctx->best_nonterm == NULL) || (new_obj->ID < ctx->best_nonterm->ID))
			ctx->best_nonterm = new_obj;
	}
	return 0;
}


RND_INLINE pcb_any_obj_t *pcb_qry_parent_net_term_(pcb_qry_exec_t *ec, pcb_any_obj_t *from)
{
	pcb_find_t fctx;
	parent_net_term_t ctx;
	pcb_any_obj_t *res = NULL;
	long n;

	assert(ec->tmplst.used == 0); /* temp list must be empty so we are nto getting void * ptrs to anything else than we find */

	ctx.ec = ec;
	ctx.best_term = NULL;
	ctx.best_nonterm = from; /* worst case: floating segment with only this object */

	memset(&fctx, 0, sizeof(fctx));
	fctx.user_data = &ctx;
	fctx.found_cb = parent_net_term_found_cb;
	pcb_find_from_obj(&fctx, ec->pcb->Data, from);
	pcb_find_free(&fctx);

	/* for terminals do the expensive lookup and return the PCB_OBJ_NET_TERM object */
	if (ctx.best_term != NULL) {
		pcb_net_term_t *t;
		t = pcb_net_find_by_obj(&ec->pcb->netlist[PCB_NETLIST_EDITED], ctx.best_term);
		res = (t == NULL) ? ctx.best_term : (pcb_any_obj_t *)t;
	}
	else
		res = ctx.best_nonterm;

	for(n = 0; n < ec->tmplst.used; n++)
		htpp_set(&ec->obj2netterm, ec->tmplst.array[n], res);

	ec->tmplst.used = 0;

	return res;
}


pcb_any_obj_t *pcb_qry_parent_net_term(pcb_qry_exec_t *ec, pcb_any_obj_t *from)
{
	pcb_any_obj_t *res;
	if (!ec->obj2netterm_inited) {
		htpp_init(&ec->obj2netterm, ptrhash, ptrkeyeq);
		ec->obj2netterm_inited = 1;
		res = NULL;
	}
	else
		res = htpp_get(&ec->obj2netterm, from);

	if (res == NULL)
		res = pcb_qry_parent_net_term_(ec, from);

	return res;
}