Debian Codegraph @

lib/label/rectangle.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12
13 #include <label/index.h>
14 #include <math.h>
15 #include <stdbool.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <assert.h>
19 #include <stdlib.h>
20 #include <common/arith.h>
21 #include <label/rectangle.h>
22 #include <cgraph/cgraph.h>
23 #include <util/exit.h>
24
25 static bool Undefined(Rect_t r) { return r.boundary[0] > r.boundary[NUMDIMS]; }
26
27 /*-----------------------------------------------------------------------------
28 | Initialize a rectangle to have all 0 coordinates.
29 -----------------------------------------------------------------------------*/
30 void InitRect(Rect_t * r)
31 {
32 for (size_t i = 0; i < NUMSIDES; i++)
33 r->boundary[i] = 0;
34 }
35
36 /*-----------------------------------------------------------------------------
37 | Return a rect whose first low side is higher than its opposite side -
38 | interpreted as an undefined rect.
39 -----------------------------------------------------------------------------*/
40 Rect_t NullRect(void)
41 {
42 Rect_t r = {{0}};
43
44 r.boundary[0] = 1;
45 r.boundary[NUMDIMS] = -1;
46 return r;
47 }
48
49 #ifdef RTDEBUG
50 /*-----------------------------------------------------------------------------
51 | Print rectangle lower upper bounds by dimension
52 -----------------------------------------------------------------------------*/
53 void PrintRect(Rect_t r) {
54 fprintf(stderr, "rect:");
55 for (size_t i = 0; i < NUMDIMS; i++)
56 fprintf(stderr, "\t%.0f\t%.0f\n", r.boundary[i], r.boundary[i + NUMDIMS]);
57 }
58 #endif
59
60 /*-----------------------------------------------------------------------------
61 | Calculate the n-dimensional area of a rectangle
62 -----------------------------------------------------------------------------*/
63
64 uint64_t RectArea(const Rect_t r) {
65 if (Undefined(r))
66 return 0;
67
68 uint64_t area = 1;
69 for (size_t i = 0; i < NUMDIMS; i++) {
70 const uint64_t dim = (uint64_t)(r.boundary[i + NUMDIMS] - r.boundary[i]);
71 if (dim == 0) return 0;
72 if (UINT64_MAX / dim < area) {
73 agerrorf("label: area too large for rtree\n");
74 graphviz_exit(EXIT_FAILURE);
75 }
76 area *= dim;
77 }
78 return area;
79 }
80
81 /*-----------------------------------------------------------------------------
82 | Combine two rectangles, make one that includes both.
83 -----------------------------------------------------------------------------*/
84 Rect_t CombineRect(const Rect_t r, const Rect_t rr) {
85 Rect_t new;
86
87 if (Undefined(r))
88 return rr;
89 if (Undefined(rr))
90 return r;
91
92 for (size_t i = 0; i < NUMDIMS; i++) {
93 new.boundary[i] = fmin(r.boundary[i], rr.boundary[i]);
94 size_t j = i + NUMDIMS;
95 new.boundary[j] = fmin(r.boundary[j], rr.boundary[j]);
96 }
97 return new;
98 }
99
100 /*-----------------------------------------------------------------------------
101 | Decide whether two rectangles overlap.
102 -----------------------------------------------------------------------------*/
103 bool Overlap(const Rect_t r, const Rect_t s) {
104 for (size_t i = 0; i < NUMDIMS; i++) {
105 size_t j = i + NUMDIMS; /* index for high sides */
106 if (r.boundary[i] > s.boundary[j] || s.boundary[i] > r.boundary[j])
107 return false;
108 }
109 return true;
110 }