File: polycombine.c

package info (click to toggle)
pcb-rnd 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,428 kB
  • sloc: ansic: 249,627; yacc: 5,981; sh: 5,748; makefile: 3,595; awk: 2,704; lex: 1,094; python: 519; lisp: 169; xml: 128; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (353 lines) | stat: -rw-r--r-- 10,325 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * PolyCombine plug-in for PCB.
 *
 * Copyright (C) 2010 Peter Clifton <pcjc2@cam.ac.uk>
 *
 * Licensed under the terms of the GNU General Public
 * License, version 2.
 *
 * Ported to pcb-rnd by Tibor 'Igor2' Palinkas in 2016.
 *
 */

#include <stdio.h>
#include <math.h>

#include "config.h"
#include "board.h"
#include "data.h"
#include "remove.h"
#include <librnd/core/hid.h>
#include <librnd/core/error.h>
#include <librnd/poly/rtree.h>
#include "polygon.h"
#include <librnd/poly/polyarea.h>
#include "assert.h"
#include "flag_str.h"
#include "find.h"
#include "draw.h"
#include "undo.h"
#include <librnd/core/plugins.h>
#include <librnd/core/actions.h>
#include "obj_poly.h"

static rnd_polyarea_t *original_poly(pcb_poly_t *p, rnd_bool *forward)
{
	rnd_pline_t *contour = NULL;
	rnd_polyarea_t *np = NULL;
	rnd_cardinal_t n;
	rnd_vector_t v;
	int hole = 0;

	*forward = rnd_true;

	if ((np = rnd_polyarea_create()) == NULL)
		return NULL;

	/* first make initial polygon contour */
	for (n = 0; n < p->PointN; n++) {
		/* No current contour? Make a new one starting at point */
		/*   (or) Add point to existing contour */

		v[0] = p->Points[n].X;
		v[1] = p->Points[n].Y;
		if (contour == NULL) {
			if ((contour = rnd_poly_contour_new(v)) == NULL)
				return NULL;
		}
		else {
			rnd_poly_vertex_include(contour->head->prev, rnd_poly_node_create(v));
		}

		/* Is current point last in contour? If so process it. */
		if (n == p->PointN - 1 || (hole < p->HoleIndexN && n == p->HoleIndex[hole] - 1)) {
			rnd_poly_contour_pre(contour, rnd_true);

			/* Log the direction in which the outer contour was specified */
			if (hole == 0)
				*forward = (contour->Flags.orient == RND_PLF_DIR);

			/* make sure it is a positive contour (outer) or negative (hole) */
			if (contour->Flags.orient != (hole ? RND_PLF_INV : RND_PLF_DIR))
				rnd_poly_contour_inv(contour);
			assert(contour->Flags.orient == (hole ? RND_PLF_INV : RND_PLF_DIR));

			rnd_polyarea_contour_include(np, contour);
			contour = NULL;
			assert(rnd_poly_valid(np));

			hole++;
		}
	}
	return np;
}

typedef struct poly_tree poly_tree;

struct poly_tree {
	pcb_poly_t *polygon;
	rnd_bool forward;
	rnd_polyarea_t *polyarea;
	poly_tree *parent;
	poly_tree *child;
	poly_tree *prev;
	poly_tree *next;
};

/*
 *                      ______
 *  ___________________|_  P6 |             +P1 ____ +P6
 * | P1                | |    |              |
 * |   _____      ____ |_|____|             -P2 ____ -P4 ____ -P5
 * |  |P2   |    |P5  |  |                   |
 * |  | []  |    |____|  |                  +P3
 * |  |  P3 |            |
 * |  |_____|            |
 * |                     |
 * |          ___        |
 * |         |P4 |       |
 * |         |___|       |
 * |                     |
 * |_____________________|
 *
 * As we encounter each polygon, it gets a record. We need to check
 * whether it contains any of the polygons existing in our tree. If
 * it does, it will become the parent of them. (Check breadth first).
 *
 * When processing, work top down (breadth first), although if the
 * contours can be assumed not to overlap, we can drill down in this
 * order: P1, P2, P3, P4, P5, P6.
 */
static rnd_bool PolygonContainsPolygon(rnd_polyarea_t *outer, rnd_polyarea_t *inner)
{
/*  int contours_isect;*/
	/* Should check outer contours don't intersect? */
/*  contours_isect = rnd_polyarea_touching(outer, inner);*/
	/* Cheat and assume simple single contour polygons for now */
/*  return contours_isect ?
           0 : rnd_poly_contour_in_contour(outer->contours, inner->contours);*/
	return rnd_poly_contour_in_contour(outer->contours, inner->contours);
}


static poly_tree *insert_node_recursive(poly_tree * start_point, poly_tree * to_insert)
{
	poly_tree *cur_node, *next = NULL;
/*  bool to_insert_isects_cur_node;    Intersection */
	rnd_bool to_insert_contains_cur_node;	/* Containment */
	rnd_bool cur_node_contains_to_insert;	/* Containment */
	rnd_bool placed_to_insert = rnd_false;

	poly_tree *return_root = start_point;

	if (start_point == NULL) {
/*      printf ("start_point is NULL, so returning to_insert\n");
		to_insert->parent = !!; UNDEFINED*/
		return to_insert;
	}

	/* Investigate the start point and its peers first */
	for (cur_node = start_point; cur_node != NULL; cur_node = next) {
		next = cur_node->next;

/*      to_insert_isects_cur_node = pcb_is_poly_in_poly(to_insert->polygon, cur_node->polygon);*/
		to_insert_contains_cur_node = PolygonContainsPolygon(to_insert->polyarea, cur_node->polyarea);

#if 0
		printf("Inspecting polygon %ld %s, curnode is %ld %s: to_insert_isects_cur_node = %d, to_insert_contains_cur_node = %i\n",
					 to_insert->polygon->ID,
					 to_insert->forward ? "FWD" : "BWD",
					 cur_node->polygon->ID, cur_node->forward ? "FWD" : "BWD", to_insert_isects_cur_node, to_insert_contains_cur_node);

		if (to_insert_isects_cur_node) {	/* Place as peer of this node? */
		}
#endif

		if (to_insert_contains_cur_node) {	/* Should be a parent of this node */
			/* Remove cur_node from its peers */
			if (cur_node->prev)
				cur_node->prev->next = cur_node->next;
			if (cur_node->next)
				cur_node->next->prev = cur_node->prev;

			/* If we've not yet got a home, insert the to_insert node where cur_node was previously */
			if (!placed_to_insert) {
				to_insert->parent = cur_node->parent;
				to_insert->next = cur_node->next;
				to_insert->prev = cur_node->prev;
				if (to_insert->prev)
					to_insert->prev->next = to_insert;
				if (to_insert->next)
					to_insert->next->prev = to_insert;
				placed_to_insert = rnd_true;

				if (cur_node == start_point)
					return_root = to_insert;
			}

			/* Prepend cur_node to our list of children */
			cur_node->parent = to_insert;

			cur_node->prev = NULL;
			cur_node->next = to_insert->child;
			if (to_insert->child)
				to_insert->child->prev = cur_node;
			to_insert->child = cur_node;

		}
	}

	if (placed_to_insert) {
/*      printf ("Returning new root %ld\n", return_root->polygon->ID);*/
		return return_root;
	}
/*    return (to_insert->parent == NULL) ? to_insert : to_insert->parent;*/

	/* Ok, so we still didn't find anywhere which the to_insert contour contained,
	 * we need to start looking at the children of the start_point and its peers.
	 */
/*  printf ("Looking at child nodes of the start_point\n");*/

	/* Investigate the start point and its peers first */
	for (cur_node = start_point; cur_node != NULL; cur_node = next) {
		next = cur_node->next;

		cur_node_contains_to_insert = PolygonContainsPolygon(cur_node->polyarea, to_insert->polyarea);

#if 0
		printf("Inspecting polygon %ld, curnode is %ld: cur_node_contains_to_insert = %d\n",
					 to_insert->polygon->ID, cur_node->polygon->ID, cur_node_contains_to_insert);
#endif

		/* Need to look at the child, ONLY if we know it engulfs our to_insert polygon */
		if (cur_node_contains_to_insert) {
			/* Can't set the parent within the call, so: */
			to_insert->parent = cur_node;
			cur_node->child = insert_node_recursive(cur_node->child, to_insert);
			return start_point;
			/*return cur_node->parent;*/
		}
	}

/*  if (!placed_to_insert)*/
	/* prepend to_insert polygon to peer of start_point ? */
/*  printf ("Prepending as peer to start_poly\n");*/
	to_insert->parent = start_point->parent;
	to_insert->prev = NULL;
	to_insert->next = start_point;
	to_insert->next->prev = to_insert;

	return to_insert;
}

static rnd_polyarea_t *compute_polygon_recursive(poly_tree * root, rnd_polyarea_t * accumulate)
{
	rnd_polyarea_t *res;
	poly_tree *cur_node;
	for (cur_node = root; cur_node != NULL; cur_node = cur_node->next) {
		/* Process this element */
/*      printf ("Processing node %ld %s\n", cur_node->polygon->ID, cur_node->forward ? "FWD" : "BWD");*/
		rnd_polyarea_boolean_free(accumulate, cur_node->polyarea, &res, cur_node->forward ? RND_PBO_UNITE : RND_PBO_SUB);
		accumulate = res;

		/* And its children if it has them */
		if (cur_node->child) {
/*          printf ("Processing children\n");*/
			accumulate = compute_polygon_recursive(cur_node->child, accumulate);
		}
	}
	return accumulate;
}

/* DOC: polycombine.html */
static fgw_error_t pcb_act_polycombine(fgw_arg_t *res, int argc, fgw_arg_t *argv)
{
	rnd_polyarea_t *rs;
	rnd_bool forward;
	rnd_polyarea_t *np;
/*  bool outer;
    rnd_polyarea_t *pa;
    rnd_pline_t *pline;
    rnd_vnode_t *node;
    pcb_poly_t *Polygon;*/
	pcb_layer_t *Layer = NULL;
	poly_tree *root = NULL;
	poly_tree *this_node;

	/* First pass to combine the forward and backward contours */
	PCB_POLY_VISIBLE_LOOP(PCB->Data);
	{
		if (!PCB_FLAG_TEST(PCB_FLAG_SELECTED, polygon))
			continue;

		/* Pick the layer of the first polygon we find selected */
		if (Layer == NULL)
			Layer = layer;

		/* Only combine polygons on the same layer */
		if (Layer != layer)
			continue;

		np = original_poly(polygon, &forward);

		/* Build a poly_tree record */
		this_node = calloc(1, sizeof(poly_tree));
		this_node->polygon = polygon;
		this_node->forward = forward;
		this_node->polyarea = np;

		/* Check where we should place the node in the tree */
		root = insert_node_recursive(root, this_node);

		/*pcb_poly_remove(layer, polygon);*/
	}
	PCB_ENDALL_LOOP;

	/* Now perform a traversal of the tree, computing a polygon */
	rs = compute_polygon_recursive(root, NULL);

	pcb_undo_save_serial();

	/* Second pass to remove the input polygons */
	PCB_POLY_VISIBLE_LOOP(PCB->Data);
	{
		if (!PCB_FLAG_TEST(PCB_FLAG_SELECTED, polygon))
			continue;

		/* Only combine polygons on the same layer */
		if (Layer != layer)
			continue;

		pcb_poly_remove(layer, polygon);
	}
	PCB_ENDALL_LOOP;

	/* Now de-construct the resulting polygon into raw PCB polygons */
	pcb_poly_to_polygons_on_layer(PCB->Data, Layer, rs, pcb_strflg_board_s2f("clearpoly", NULL));
	pcb_undo_restore_serial();
	pcb_undo_inc_serial();
	pcb_draw();

	RND_ACT_IRES(0);
	return 0;
}

static rnd_action_t polycombine_action_list[] = {
	{"PolyCombine", pcb_act_polycombine, NULL, NULL}
};

char *polycombine_cookie = "polycombine plugin";

int pplg_check_ver_polycombine(int ver_needed) { return 0; }

void pplg_uninit_polycombine(void)
{
	rnd_remove_actions_by_cookie(polycombine_cookie);
}

int pplg_init_polycombine(void)
{
	RND_API_CHK_VER;
	RND_REGISTER_ACTIONS(polycombine_action_list, polycombine_cookie);
	return 0;
}