File: ptcloud.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 (330 lines) | stat: -rw-r--r-- 8,552 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
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2024 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")
 */

/* Fill up a polyarea with more or less evenly spaced points */

#include "config.h"
#include <assert.h>
#include <librnd/core/error.h>

#include "ptcloud.h"

#include "ptcloud_debug.c"

/* unit is ctx->target_dist; so value 7 means 7 * ctx->target_dist, making
   the area of a single grid tile (7*ctx->target_dist)^2 */
#define GRID_SIZE 10
#define PROXIMITY 1.42


#define WEIGHT_CONTOUR 300
#define WEIGHT_INTERNAL 100

#define TRACE_ANNEAL 0

/* returns grid idx for coords x;y */
RND_INLINE long xy2gidx(pcb_ptcloud_ctx_t *ctx, rnd_coord_t x, rnd_coord_t y)
{
	long gx = (x - ctx->pl->xmin) / (GRID_SIZE * ctx->target_dist) + 1; /* +1 for the border */
	long gy = (y - ctx->pl->ymin) / (GRID_SIZE * ctx->target_dist) + 1; /* +1 for the border */
	return gy * ctx->gsx + gx;
}

/* returns grid idx where the point is linked in */
RND_INLINE long pt2gidx(pcb_ptcloud_ctx_t *ctx, pcb_ptcloud_pt_t *pt)
{
	assert(pt->link.parent != NULL);
	return pt->link.parent - ctx->grid;
}

RND_INLINE void pt_move(pcb_ptcloud_ctx_t *ctx, pcb_ptcloud_pt_t *pt)
{
	long curr_gidx, new_gidx;

	if ((pt->dx == 0) && (pt->dy == 0))
		return;

	curr_gidx = pt2gidx(ctx, pt);
	pt->x += pt->dx;
	pt->y += pt->dy;
	pt->dx = pt->dy = 0;
	new_gidx = xy2gidx(ctx, pt->x, pt->y);

	if (curr_gidx != new_gidx) {
		gdl_remove(&ctx->grid[curr_gidx], pt, link);
		gdl_append(&ctx->grid[new_gidx], pt, link);
	}
}

RND_INLINE void pt_create(pcb_ptcloud_ctx_t *ctx, rnd_coord_t x, rnd_coord_t y, int weight, int fixed)
{
	pcb_ptcloud_pt_t *pt = calloc(sizeof(pcb_ptcloud_pt_t), 1);
	long gidx = xy2gidx(ctx, x, y);
	assert(gidx >= 0);
	assert(gidx < ctx->glen);

	pt->x = x;
	pt->y = y;
	pt->weight = weight;
	pt->fixed = fixed;

	gdl_append(&ctx->grid[gidx], pt, link);
	gdl_append(&ctx->points, pt, all);
}

RND_INLINE void grid_alloc(pcb_ptcloud_ctx_t *ctx)
{
	ctx->gsx = (ctx->pl->xmax - ctx->pl->xmin) / (GRID_SIZE * ctx->target_dist) + 3;
	ctx->gsy = (ctx->pl->ymax - ctx->pl->ymin) / (GRID_SIZE * ctx->target_dist) + 3;
	ctx->glen = ctx->gsx * ctx->gsy;
	ctx->grid = calloc(sizeof(gdl_list_t), ctx->glen);
}

RND_INLINE rnd_coord_t edge_x_for_y(rnd_coord_t lx1, rnd_coord_t ly1, rnd_coord_t lx2, rnd_coord_t ly2, rnd_coord_t y)
{
	double dx = (double)(lx2 - lx1) / (double)(ly2 - ly1);
	return rnd_round((double)lx1 + (double)(y - ly1) * dx);
}

RND_INLINE void ptcloud_pline_create_points(pcb_ptcloud_ctx_t *ctx, rnd_pline_t *pl)
{
	rnd_vnode_t *vn = pl->head;

	do {
		double vx, vy, len, x, y, l, n, step;

		vx = vn->next->point[0] - vn->point[0];
		vy = vn->next->point[1] - vn->point[1];
		len = sqrt(vx*vx + vy*vy);
		if (len == 0)
			continue;

		n = ceil(len / ctx->target_dist);
		step = len / n;

		vx = vx/len * step;
		vy = vy/len * step;

		pt_create(ctx, vn->point[0], vn->point[1], WEIGHT_CONTOUR, 1);
		x = vn->point[0];
		y = vn->point[1];
		for(l = step; l < len; l += step) {
			x += vx;
			y += vy;
			pt_create(ctx, rnd_round(x), rnd_round(y), WEIGHT_CONTOUR, 1);
		}
	} while((vn = vn->next) != pl->head);
}

RND_INLINE void ptcloud_contour_create_points(pcb_ptcloud_ctx_t *ctx)
{
	rnd_pline_t *pl;
	for(pl = ctx->pl; pl != NULL; pl = pl->next)
		ptcloud_pline_create_points(ctx, pl);
}

static rnd_rtree_dir_t ptcloud_ray_cb(void *udata, void *obj, const rnd_rtree_box_t *box)
{
	pcb_ptcloud_ctx_t *ctx = udata;
	rnd_vnode_t *vn = rnd_pline_seg2vnode(obj);
	rnd_coord_t x;

	/* ignore edges with endpoint hit if the edge is not going upward;
	   emulate that the ray is a tiny bit above the integer y coordinate) */
	if ((ctx->ray_y == vn->next->point[1]) && (vn->next->point[1] >= vn->point[1]))
		return 0;
	if ((ctx->ray_y == vn->point[1]) && (vn->next->point[1] <= vn->point[1]))
		return 0;

	/* add x coord of the crossing to the (yet unodreded) edge vector */
	x = edge_x_for_y(vn->point[0], vn->point[1], vn->next->point[0], vn->next->point[1], ctx->ray_y);
	vtc0_append(&ctx->edges, x);

	return 0;
}

static int cmp_crd(const void *A, const void *B)
{
	const rnd_coord_t *a = A, *b = B;
	return (*a < *b) ? -1 : +1;
}

static void ptcloud_ray_create_points(pcb_ptcloud_ctx_t *ctx)
{
	long n;
	for(n = 0; n+1 < ctx->edges.used; n+=2) {
		rnd_coord_t x, x1 = ctx->edges.array[n], x2 = ctx->edges.array[n+1];

		TODO("verify there's no horizontal line overlapping");
		for(x = x1 + ctx->target_dist; x <= x2 - ctx->target_dist; x += ctx->target_dist)
			pt_create(ctx, x, ctx->ray_y, WEIGHT_INTERNAL, 0);
	}
}

RND_INLINE void ptcloud_anneal_compute_pt(pcb_ptcloud_ctx_t *ctx, long gidx, pcb_ptcloud_pt_t *pt0)
{
	pcb_ptcloud_pt_t *pt;

	for(pt = gdl_first(&ctx->points); pt != NULL; pt = pt->all.next) {
		rnd_coord_t dx, dy;
		double dx2, dy2, d2, err, px, py;

	}
}


RND_INLINE void ptcloud_anneal_compute(pcb_ptcloud_ctx_t *ctx)
{
	pcb_ptcloud_pt_t *pt;

	for(pt = gdl_first(&ctx->points); pt != NULL; pt = pt->all.next) {
		long gidx;

		if (pt->fixed)
			continue;

		if (TRACE_ANNEAL)
			rnd_trace("ANN: %.06mm %.06mm\n", pt->x, pt->y);

		pt->dx = pt->dy = 0;
		gidx = pt2gidx(ctx, pt);

return;
	}
}

RND_INLINE double ptcloud_anneal_execute(pcb_ptcloud_ctx_t *ctx)
{
	pcb_ptcloud_pt_t *pt;
	double err = 0;

	for(pt = gdl_first(&ctx->points); pt != NULL; pt = pt->all.next) {

		if (pt->fixed || ((pt->dx == 0) && (pt->dy == 0)))
			continue;

		err += fabs(pt->dx) + fabs(pt->dy);
		pt_move(ctx, pt);
	}
	return err;
}

RND_INLINE int ptcloud_triangulate(pcb_ptcloud_ctx_t *ctx)
{
	size_t mem_req;
	long n, num_pt = gdl_length(&ctx->points);
	pcb_ptcloud_pt_t *pt;

	mem_req = fp2t_memory_required(num_pt);
	ctx->tri_mem = calloc(mem_req, 1);

	if (!fp2t_init(&ctx->tri, ctx->tri_mem, num_pt)) {
		free(ctx->tri_mem);
		return -1;
	}

	for(pt = gdl_first(&ctx->points), n = 0; pt != NULL; pt = pt->all.next, n++) {
		fp2t_point_t *fpt = fp2t_push_point(&ctx->tri);
		fpt->X = pt->x;
		fpt->Y = pt->y;
		if (n == ctx->num_pt_edge)
			fp2t_add_edge(&ctx->tri);

		assert(num_pt-- > 0);
	}

	fp2t_triangulate(&ctx->tri);

	return 0;
}

void pcb_ptcloud(pcb_ptcloud_ctx_t *ctx)
{
	rnd_coord_t y, half = ctx->target_dist/2;
	double err, min_err;
	long n;

	grid_alloc(ctx);
	ctx->closed = ceil(PROXIMITY * ctx->target_dist);
	ctx->closed2 = (double)ctx->closed * (double)ctx->closed;
	ctx->target2 = (double)ctx->target_dist * (double)ctx->target_dist;
	min_err = (double)ctx->target_dist / 20.0; /* 5% */

	ptcloud_contour_create_points(ctx);

	ctx->num_pt_edge = gdl_length(&ctx->points);

	/* horizontal rays */
	for(y = ctx->pl->ymin + ctx->target_dist; y <= ctx->pl->ymax - ctx->target_dist; y += ctx->target_dist) {
		rnd_rtree_box_t sb;

		sb.x1 = ctx->pl->xmin - half; sb.y1 = y;
		sb.x2 = RND_COORD_MAX; sb.y2 = y+1;
		ctx->edges.used = 0;
		ctx->ray_y = y;
		rnd_rtree_search_obj(ctx->pl->tree, &sb, ptcloud_ray_cb, ctx);

rnd_trace(" y: %06mm hits: %d\n", y, ctx->edges.used);

		if (ctx->edges.used == 0)
			continue;

		assert((ctx->edges.used % 2) == 0);

		qsort(ctx->edges.array, ctx->edges.used, sizeof(rnd_coord_t), cmp_crd);
/*		ptcloud_ray_create_points(ctx);*/
	}


	ptcloud_triangulate(ctx);
	ptcloud_debug_draw(ctx, "PTcloud.svg");

/*
	for(n = 0; n < 1; n++) {
		char fn[128];
		
		ptcloud_anneal_compute(ctx);

		if ((n % 10) == 0) {
			sprintf(fn, "PTcloud%04ld.svg", n);
			ptcloud_debug_draw(ctx, fn);
		}

		err = ptcloud_anneal_execute(ctx);
		rnd_trace("[%ld] err=%f\n", n, err);
	}
	*/


	vtc0_uninit(&ctx->edges);
}

void pcb_ptcloud_free(pcb_ptcloud_ctx_t *ctx)
{
	free(ctx->tri_mem);
	TODO("free points and grid");
}