File: compound.c

package info (click to toggle)
graphviz 14.1.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,476 kB
  • sloc: ansic: 142,288; cpp: 11,975; python: 7,883; makefile: 4,044; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,391; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (457 lines) | stat: -rw-r--r-- 13,275 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Details at https://graphviz.org
 *************************************************************************/


/* Module for clipping splines to cluster boxes.
 */

#include "config.h"

#include	<dotgen/dot.h>
#include	<math.h>
#include	<stdbool.h>
#include	<stddef.h>
#include	<util/agxbuf.h>
#include	<util/alloc.h>
#include	<util/gv_math.h>

/* Return point where line segment [pp,cp] intersects
 * the box bp. Assume cp is outside the box, and pp is
 * on or in the box. 
 */
static pointf boxIntersectf(pointf pp, pointf cp, boxf * bp)
{
    pointf ipp;
    double ppx = pp.x;
    double ppy = pp.y;
    double cpx = cp.x;
    double cpy = cp.y;

    const pointf ll = bp->LL;
    const pointf ur = bp->UR;
    if (cp.x < ll.x) {
	ipp.x = ll.x;
	ipp.y = pp.y + round((ipp.x - ppx) * (ppy - cpy) / (ppx - cpx));
	if (ipp.y >= ll.y && ipp.y <= ur.y)
	    return ipp;
    }
    if (cp.x > ur.x) {
	ipp.x = ur.x;
	ipp.y = pp.y + round((ipp.x - ppx) * (ppy - cpy) / (ppx - cpx));
	if (ipp.y >= ll.y && ipp.y <= ur.y)
	    return ipp;
    }
    if (cp.y < ll.y) {
	ipp.y = ll.y;
	ipp.x = pp.x + round((ipp.y - ppy) * (ppx - cpx) / (ppy - cpy));
	if (ipp.x >= ll.x && ipp.x <= ur.x)
	    return ipp;
    }
    if (cp.y > ur.y) {
	ipp.y = ur.y;
	ipp.x = pp.x + round((ipp.y - ppy) * (ppx - cpx) / (ppy - cpy));
	if (ipp.x >= ll.x && ipp.x <= ur.x)
	    return ipp;
    }

    /* failure */
    agerrorf(
          "segment [(%.5g, %.5g),(%.5g,%.5g)] does not intersect box "
          "ll=(%.5g,%.5g),ur=(%.5g,%.5g)\n", pp.x, pp.y, cp.x, cp.y, ll.x, ll.y,
          ur.x, ur.y);
    assert(0);

    return ipp;
}

/// returns true if p is on or in box bb
static bool inBoxf(pointf p, boxf *bb) {
    return INSIDE(p, *bb);
}

/* Returns subgraph with given name.
 * Returns NULL if no name is given, or subgraph of
 * that name does not exist.
 */
static graph_t *getCluster(char *cluster_name, Dt_t *map) {
    Agraph_t* sg;

    if (!cluster_name || *cluster_name == '\0')
	return NULL;
    sg = findCluster (map, cluster_name);
    if (sg == NULL) {
	agwarningf("cluster named %s not found\n", cluster_name);
    }
    return sg;
}

/* The following functions are derived from pp. 411-415 (pp. 791-795)
 * of Graphics Gems. In the code there, they use a SGN function to
 * count crossings. This doesn't seem to handle certain special cases,
 * as when the last point is on the line. It certainly didn't work
 * for us when we used int values; see bug 145. We needed to use `fcmp` instead.
 *
 * Possibly unnecessary with double values, but harmless.
 */

/* Return the number of times the Bézier control polygon crosses
 * the vertical line x = xcoord.
 */
static int countVertCross(pointf * pts, double xcoord)
{
    int i;
    int sign, old_sign;
    int num_crossings = 0;

    sign = fcmp(pts[0].x, xcoord);
    if (sign == 0)
	num_crossings++;
    for (i = 1; i <= 3; i++) {
	old_sign = sign;
	sign = fcmp(pts[i].x, xcoord);
	if (sign != old_sign && old_sign != 0)
	    num_crossings++;
    }
    return num_crossings;
}

/* Return the number of times the Bézier control polygon crosses
 * the horizontal line y = ycoord.
 */
static int countHorzCross(pointf * pts, double ycoord)
{
    int i;
    int sign, old_sign;
    int num_crossings = 0;

    sign = fcmp(pts[0].y, ycoord);
    if (sign == 0)
	num_crossings++;
    for (i = 1; i <= 3; i++) {
	old_sign = sign;
	sign = fcmp(pts[i].y, ycoord);
	if (sign != old_sign && old_sign != 0)
	    num_crossings++;
    }
    return num_crossings;
}

/* Given 4 Bézier control points pts, corresponding to the portion
 * of an initial spline with path parameter in the range
 * 0.0 <= tmin <= t <= tmax <= 1.0, return t where the spline 
 * first crosses a vertical line segment
 * [(xcoord,ymin),(xcoord,ymax)]. Return -1 if not found.
 * This is done by binary subdivision. 
 */
static double
findVertical(pointf * pts, double tmin, double tmax,
	     double xcoord, double ymin, double ymax)
{
    pointf Left[4];
    pointf Right[4];
    double t;
    int no_cross;

    if (tmin == tmax)
	return tmin;

    no_cross = countVertCross(pts, xcoord);
    if (no_cross == 0)
	return -1.0;

    /* if 1 crossing and on the line x == xcoord (within 0.005 point) */
    if (no_cross == 1 && fabs(pts[3].x - xcoord) <= 0.005) {
	if (ymin <= pts[3].y && pts[3].y <= ymax) {
	    return tmax;
	} else
	    return -1.0;
    }

    /* split the Bézier into halves, trying the first half first. */
    Bezier(pts, 0.5, Left, Right);
    t = findVertical(Left, tmin, (tmin + tmax) / 2.0, xcoord, ymin, ymax);
    if (t >= 0.0)
	return t;
    return findVertical(Right, (tmin + tmax) / 2.0, tmax, xcoord, ymin,
			ymax);

}

/* Given 4 Bézier control points pts, corresponding to the portion
 * of an initial spline with path parameter in the range
 * 0.0 <= tmin <= t <= tmax <= 1.0, return t where the spline 
 * first crosses a horizontal line segment
 * [(xmin,ycoord),(xmax,ycoord)]. Return -1 if not found.
 * This is done by binary subdivision. 
 */
static double
findHorizontal(pointf * pts, double tmin, double tmax,
	       double ycoord, double xmin, double xmax)
{
    pointf Left[4];
    pointf Right[4];
    double t;
    int no_cross;

    if (tmin == tmax)
	return tmin;

    no_cross = countHorzCross(pts, ycoord);
    if (no_cross == 0)
	return -1.0;

    /* if 1 crossing and on the line y == ycoord (within 0.005 point) */
    if (no_cross == 1 && fabs(pts[3].y - ycoord) <= 0.005) {
	if (xmin <= pts[3].x && pts[3].x <= xmax) {
	    return tmax;
	} else
	    return -1.0;
    }

    /* split the Bézier into halves, trying the first half first. */
    Bezier(pts, 0.5, Left, Right);
    t = findHorizontal(Left, tmin, (tmin + tmax) / 2.0, ycoord, xmin,
		       xmax);
    if (t >= 0.0)
	return t;
    return findHorizontal(Right, (tmin + tmax) / 2.0, tmax, ycoord, xmin,
			  xmax);
}

/* Given four spline control points and a box,
 * find the shortest portion of the spline from
 * pts[0] to the intersection with the box, if any.
 * If an intersection is found, the four points are stored in pts[0..3]
 * with pts[3] being on the box, and 1 is returned. Otherwise, pts
 * is left unchanged and 0 is returned.
 */
static int splineIntersectf(pointf * pts, boxf * bb)
{
    double tmin = 2.0;
    double t;
    pointf origpts[4];
    int i;

    for (i = 0; i < 4; i++) {
	origpts[i] = pts[i];
    }

    t = findVertical(pts, 0.0, 1.0, bb->LL.x, bb->LL.y, bb->UR.y);
    if (t >= 0 && t < tmin) {
	Bezier(origpts, t, pts, NULL);
	tmin = t;
    }
    t = findVertical(pts, 0.0, MIN(1.0, tmin), bb->UR.x, bb->LL.y,
		     bb->UR.y);
    if (t >= 0 && t < tmin) {
	Bezier(origpts, t, pts, NULL);
	tmin = t;
    }
    t = findHorizontal(pts, 0.0, MIN(1.0, tmin), bb->LL.y, bb->LL.x,
		       bb->UR.x);
    if (t >= 0 && t < tmin) {
	Bezier(origpts, t, pts, NULL);
	tmin = t;
    }
    t = findHorizontal(pts, 0.0, MIN(1.0, tmin), bb->UR.y, bb->LL.x,
		       bb->UR.x);
    if (t >= 0 && t < tmin) {
	Bezier(origpts, t, pts, NULL);
	tmin = t;
    }

    if (tmin < 2.0) {
	return 1;
    } else
	return 0;
}

/* If edge e has a cluster head and/or cluster tail,
 * clip spline to outside of cluster. 
 * Requirement: spline is composed of only one part, 
 * with n control points where n >= 4 and n (mod 3) = 1.
 * If edge has arrowheads, reposition them.
 */
static void makeCompoundEdge(edge_t *e, Dt_t *clustMap) {
    size_t starti = 0, endi = 0; // index of first and last control point

    /* find head and tail target clusters, if defined */
    graph_t *lh = getCluster(agget(e, "lhead"), clustMap); // cluster containing head
    graph_t *lt = getCluster(agget(e, "ltail"), clustMap); // cluster containing tail
    if (!lt && !lh)
	return;
    if (!ED_spl(e)) return;

    /* at present, we only handle single spline case */
    if (ED_spl(e)->size > 1) {
	agwarningf("%s -> %s: spline size > 1 not supported\n",
	      agnameof(agtail(e)), agnameof(aghead(e)));
	return;
    }
    bezier *bez = ED_spl(e)->list; // original Bézier for e
    const size_t size = bez->size;

    node_t *head = aghead(e);
    node_t *tail = agtail(e);

    /* allocate new Bézier */
    bezier nbez = {0}; // new Bézier for `e`
    nbez.eflag = bez->eflag;
    nbez.sflag = bez->sflag;

    /* if Bézier has four points, almost collinear,
     * make line - unimplemented optimization?
     */

    /* If head cluster defined, find first Bézier
     * crossing head cluster, and truncate spline to
     * box edge.
     * Otherwise, leave end alone.
     */
    bool fixed = false;
    if (lh) {
	boxf *bb = &GD_bb(lh);
	if (!inBoxf(ND_coord(head), bb)) {
	    agwarningf("%s -> %s: head not inside head cluster %s\n",
		  agnameof(agtail(e)), agnameof(aghead(e)), agget(e, "lhead"));
	} else {
	    /* If first control point is in bb, degenerate case. Spline
	     * reduces to four points between the arrow head and the point 
	     * where the segment between the first control point and arrow head 
	     * crosses box.
	     */
	    if (inBoxf(bez->list[0], bb)) {
		if (inBoxf(ND_coord(tail), bb)) {
		    agwarningf(
			  "%s -> %s: tail is inside head cluster %s\n",
			  agnameof(agtail(e)), agnameof(aghead(e)), agget(e, "lhead"));
		} else if (!inBoxf(bez->sp, bb)) {
		    assert(bez->sflag);	/* must be arrowhead on tail */
		    pointf p = boxIntersectf(bez->list[0], bez->sp, bb);
		    bez->list[3] = p;
		    bez->list[1] = mid_pointf(p, bez->sp);
		    bez->list[0] = mid_pointf(bez->list[1], bez->sp);
		    bez->list[2] = mid_pointf(bez->list[1], p);
		    if (bez->eflag)
			endi = arrowEndClip(e, bez->list,
					 starti, 0, &nbez, bez->eflag);
		    endi += 3;
		    fixed = true;
		}
	    } else {
		for (endi = 0; endi < size - 1; endi += 3) {
		    if (splineIntersectf(&bez->list[endi], bb))
			break;
		}
		if (endi == size - 1) {	/* no intersection */
		    assert(bez->eflag);
		    nbez.ep = boxIntersectf(bez->ep, bez->list[endi], bb);
		} else {
		    if (bez->eflag)
			endi =
			    arrowEndClip(e, bez->list,
					 starti, endi, &nbez, bez->eflag);
		    endi += 3;
		}
		fixed = true;
	    }
	}
    }
    if (!fixed) { // if no lh, or something went wrong, use original head
	endi = size - 1;
	if (bez->eflag)
	    nbez.ep = bez->ep;
    }

    /* If tail cluster defined, find last Bézier
     * crossing tail cluster, and truncate spline to
     * box edge.
     * Otherwise, leave end alone.
     */
    fixed = false;
    if (lt) {
	boxf *bb = &GD_bb(lt);
	if (!inBoxf(ND_coord(tail), bb)) {
	    agwarningf("%s -> %s: tail not inside tail cluster %s\n",
		agnameof(agtail(e)), agnameof(aghead(e)), agget(e, "ltail"));
	} else {
	    /* If last control point is in bb, degenerate case. Spline
	     * reduces to four points between arrow head, and the point
	     * where the segment between the last control point and the 
	     * arrow head crosses box.
	     */
	    if (inBoxf(bez->list[endi], bb)) {
		if (inBoxf(ND_coord(head), bb)) {
		    agwarningf(
			"%s -> %s: head is inside tail cluster %s\n",
		  	agnameof(agtail(e)), agnameof(aghead(e)), agget(e, "ltail"));
		} else if (bez->eflag && !inBoxf(nbez.ep, bb)) {
		    pointf p = boxIntersectf(bez->list[endi], nbez.ep, bb);
		    starti = endi - 3;
		    bez->list[starti] = p;
		    bez->list[starti + 2] = mid_pointf(p, nbez.ep);
		    bez->list[starti + 3] = mid_pointf(bez->list[starti + 2], nbez.ep);
		    bez->list[starti + 1] = mid_pointf(bez->list[starti + 2], p);
		    if (bez->sflag)
			starti = arrowStartClip(e, bez->list, starti,
				endi - 3, &nbez, bez->sflag);
		    fixed = true;
		}
	    } else {
		for (starti = endi; starti > 0; starti -= 3) {
		    pointf pts[4];
		    for (size_t i = 0; i < 4; i++)
			pts[i] = bez->list[starti - i];
		    if (splineIntersectf(pts, bb)) {
			for (size_t i = 0; i < 4; i++)
			    bez->list[starti - i] = pts[i];
			break;
		    }
		}
		if (starti == 0 && bez->sflag) {
		    nbez.sp = boxIntersectf(bez->sp, bez->list[starti], bb);
		} else if (starti != 0) {
		    starti -= 3;
		    if (bez->sflag)
			starti = arrowStartClip(e, bez->list, starti,
				endi - 3, &nbez, bez->sflag);
		}
		fixed = true;
	    }
	}
    }
    if (!fixed) { // if no lt, or something went wrong, use original tail
	/* Note: starti == 0 */
	if (bez->sflag)
	    nbez.sp = bez->sp;
    }

    /* complete Bézier, free garbage and attach new Bézier to edge 
     */
    nbez.size = endi - starti + 1;
    nbez.list = gv_calloc(nbez.size, sizeof(pointf));
    for (size_t i = 0, j = starti; i < nbez.size; i++, j++)
	nbez.list[i] = bez->list[j];
    free(bez->list);
    *ED_spl(e)->list = nbez;
}

void dot_compoundEdges(graph_t * g)
{
    edge_t *e;
    node_t *n;
    Dt_t* clustMap = mkClustMap (g);
    for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
	for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
	    makeCompoundEdge(e, clustMap);
	}
    }
    dtclose(clustMap);
}