Graphviz 13.0.0~dev.20250607.1528
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 <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
25static bool Undefined(Rect_t r) { return r.boundary[0] > r.boundary[NUMDIMS]; }
26
27/*-----------------------------------------------------------------------------
28| Initialize a rectangle to have all 0 coordinates.
29-----------------------------------------------------------------------------*/
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-----------------------------------------------------------------------------*/
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-----------------------------------------------------------------------------*/
53void 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
64uint64_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-----------------------------------------------------------------------------*/
84Rect_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-----------------------------------------------------------------------------*/
103bool 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}
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
static const int dim
void InitRect(Rect_t *r)
Definition rectangle.c:30
static bool Undefined(Rect_t r)
Definition rectangle.c:25
Rect_t NullRect(void)
Definition rectangle.c:40
Rect_t CombineRect(const Rect_t r, const Rect_t rr)
Definition rectangle.c:84
uint64_t RectArea(const Rect_t r)
Definition rectangle.c:64
bool Overlap(const Rect_t r, const Rect_t s)
Definition rectangle.c:103
double boundary[NUMSIDES]
Definition rectangle.h:21
Definition grammar.c:89