Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
rectangle.c
Go to the documentation of this file.
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 <stdbool.h>
15#include <stdint.h>
16#include <stdio.h>
17#include <assert.h>
18#include <stdlib.h>
19#include <common/arith.h>
20#include <label/rectangle.h>
21#include <cgraph/cgraph.h>
22#include <cgraph/exit.h>
23
24#define Undefined(x) ((x)->boundary[0] > (x)->boundary[NUMDIMS])
25
26/*-----------------------------------------------------------------------------
27| Initialize a rectangle to have all 0 coordinates.
28-----------------------------------------------------------------------------*/
30{
31 for (size_t i = 0; i < NUMSIDES; i++)
32 r->boundary[i] = 0;
33}
34
35/*-----------------------------------------------------------------------------
36| Return a rect whose first low side is higher than its opposite side -
37| interpreted as an undefined rect.
38-----------------------------------------------------------------------------*/
40{
41 Rect_t r = {{0}};
42
43 r.boundary[0] = 1;
44 r.boundary[NUMDIMS] = -1;
45 return r;
46}
47
48#ifdef RTDEBUG
49/*-----------------------------------------------------------------------------
50| Print rectangle lower upper bounds by dimension
51-----------------------------------------------------------------------------*/
52void PrintRect(Rect_t * r)
53{
54 assert(r);
55 fprintf(stderr, "rect:");
56 for (size_t i = 0; i < NUMDIMS; i++)
57 fprintf(stderr, "\t%d\t%d\n", r->boundary[i],
58 r->boundary[i + NUMDIMS]);
59}
60#endif
61
62/*-----------------------------------------------------------------------------
63| Calculate the n-dimensional area of a rectangle
64-----------------------------------------------------------------------------*/
65
66uint64_t RectArea(const Rect_t *r) {
67 assert(r);
68
69 if (Undefined(r))
70 return 0;
71
72 uint64_t area = 1;
73 for (size_t i = 0; i < NUMDIMS; i++) {
74 unsigned int dim = r->boundary[i + NUMDIMS] - r->boundary[i];
75 if (dim == 0) return 0;
76 if (UINT64_MAX / dim < area) {
77 agerrorf("label: area too large for rtree\n");
78 graphviz_exit(EXIT_FAILURE);
79 }
80 area *= dim;
81 }
82 return area;
83}
84
85/*-----------------------------------------------------------------------------
86| Combine two rectangles, make one that includes both.
87-----------------------------------------------------------------------------*/
88Rect_t CombineRect(const Rect_t *r, const Rect_t *rr) {
89 Rect_t new;
90 assert(r && rr);
91
92 if (Undefined(r))
93 return *rr;
94 if (Undefined(rr))
95 return *r;
96
97 for (size_t i = 0; i < NUMDIMS; i++) {
98 new.boundary[i] = MIN(r->boundary[i], rr->boundary[i]);
99 size_t j = i + NUMDIMS;
100 new.boundary[j] = MAX(r->boundary[j], rr->boundary[j]);
101 }
102 return new;
103}
104
105/*-----------------------------------------------------------------------------
106| Decide whether two rectangles overlap.
107-----------------------------------------------------------------------------*/
108bool Overlap(const Rect_t *r, const Rect_t *s) {
109 assert(r && s);
110
111 for (size_t i = 0; i < NUMDIMS; i++) {
112 size_t j = i + NUMDIMS; /* index for high sides */
113 if (r->boundary[i] > s->boundary[j] || s->boundary[i] > r->boundary[j])
114 return false;
115 }
116 return true;
117}
#define MIN(a, b)
Definition arith.h:28
abstract graph C library, Cgraph API
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
void agerrorf(const char *fmt,...)
Definition agerror.c:165
#define NUMDIMS
Definition index.h:37
#define NUMSIDES
Definition index.h:40
void InitRect(Rect_t *r)
Definition rectangle.c:29
Rect_t NullRect(void)
Definition rectangle.c:39
bool Overlap(const Rect_t *r, const Rect_t *s)
Definition rectangle.c:108
Rect_t CombineRect(const Rect_t *r, const Rect_t *rr)
Definition rectangle.c:88
uint64_t RectArea(const Rect_t *r)
Definition rectangle.c:66
#define Undefined(x)
Definition rectangle.c:24
int boundary[NUMSIDES]
Definition rectangle.h:21
Definition grammar.c:93
#define MAX(a, b)
Definition write.c:31