Graphviz 16.1.3~dev.20261128.0221
Loading...
Searching...
No Matches
rescale_layout.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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
12// //
13// This file contains the functions //
14// for distorting the layout. //
15// //
16// Four methods are available: //
17// 1) Uniform denisity - rectilinear //
18// 2) Uniform denisity - polar //
19// 3) Fisheye - rectilinear //
20// 4) Fisheye - Ploar //
21// //
23
24#include "config.h"
25
26#include <assert.h>
27#include <limits.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <math.h>
32#include <time.h>
33#include <neatogen/matrix_ops.h>
34#include <neatogen/delaunay.h>
35#include <common/arith.h>
36#include <topfish/hierarchy.h>
37#include <util/alloc.h>
38#include <util/sort.h>
39
40static double *compute_densities(v_data *graph, size_t n, double *x, double *y)
41{
42// compute density of every node by calculating the average edge length in a 2-D layout
43 int j, neighbor;
44 double sum;
45 double *densities = gv_calloc(n, sizeof(double));
46
47 for (size_t i = 0; i < n; i++) {
48 sum = 0;
49 for (j = 1; j < graph[i].nedges; j++) {
50 neighbor = graph[i].edges[j];
51 sum += hypot(x[i] - x[neighbor], y[i] - y[neighbor]);
52 }
53 densities[i] = sum / graph[i].nedges;
54 }
55 return densities;
56}
57
58static double *smooth_vec(double *vec, int *ordering, size_t n, int interval) {
59// smooth 'vec' by setting each components to the average of is 'interval'-neighborhood in 'ordering'
60 assert(interval >= 0);
61 double sum;
62 double *smoothed_vec = gv_calloc(n, sizeof(double));
63 size_t n1 = MIN(1 + (size_t)interval, n);
64 sum = 0;
65 for (size_t i = 0; i < n1; i++) {
66 sum += vec[ordering[i]];
67 }
68
69 size_t len = n1;
70 for (size_t i = 0; i < MIN(n, (size_t)interval); i++) {
71 smoothed_vec[ordering[i]] = sum / (double)len;
72 if (len < n) {
73 sum += vec[ordering[len++]];
74 }
75 }
76 if (n <= (size_t)interval) {
77 return smoothed_vec;
78 }
79
80 for (size_t i = (size_t)interval; i < n - (size_t)interval - 1; i++) {
81 smoothed_vec[ordering[i]] = sum / (double)len;
82 sum +=
83 vec[ordering[i + (size_t)interval + 1]] - vec[ordering[i - (size_t)interval]];
84 }
85 for (size_t i = MAX(n - (size_t)interval - 1, (size_t)interval); i < n; i++) {
86 smoothed_vec[ordering[i]] = sum / (double)len;
87 sum -= vec[ordering[i - (size_t)interval]];
88 len--;
89 }
90 return smoothed_vec;
91}
92
93#define DIST(x1, y1, x2, y2) hypot((x1) - (x2), (y1) - (y2))
94
95static void rescale_layout_polarFocus(v_data *graph, size_t n, double *x_coords,
96 double *y_coords, double x_focus,
97 double y_focus, int interval,
98 double distortion) {
99 // Polar distortion - auxiliary function
100 double *densities = NULL;
101 double *distances = gv_calloc(n, sizeof(double));
102 double *orig_distances = gv_calloc(n, sizeof(double));
103
104 for (size_t i = 0; i < n; i++)
105 {
106 distances[i] = DIST(x_coords[i], y_coords[i], x_focus, y_focus);
107 }
108 assert(n <= INT_MAX);
109 copy_vector((int)n, distances, orig_distances);
110
111 int *ordering = gv_calloc(n, sizeof(int));
112 for (size_t i = 0; i < n; i++)
113 {
114 ordering[i] = (int)i;
115 }
116 sort_place(distances, ordering, n);
117
118 densities = compute_densities(graph, n, x_coords, y_coords);
119 double *smoothed_densities = smooth_vec(densities, ordering, n, interval);
120
121 // rescale distances
122 if (distortion < 1.01 && distortion > 0.99)
123 {
124 for (size_t i = 1; i < n; i++)
125 {
126 distances[ordering[i]] = distances[ordering[i - 1]] + (orig_distances[ordering[i]] -
127 orig_distances[ordering
128 [i -
129 1]]) / smoothed_densities[ordering[i]];
130 }
131 } else
132 {
133 // just to make milder behavior:
134 const double factor = (signbit(distortion) ? -1 : 1) * sqrt(fabs(distortion));
135 for (size_t i = 1; i < n; i++)
136 {
137 distances[ordering[i]] =
138 distances[ordering[i - 1]] + (orig_distances[ordering[i]] -
139 orig_distances[ordering
140 [i -
141 1]]) /
142 pow(smoothed_densities[ordering[i]], factor);
143 }
144 }
145
146 // compute new coordinate:
147 for (size_t i = 0; i < n; i++)
148 {
149 const double ratio =
150 orig_distances[i] > 0 ? distances[i] / orig_distances[i] : 0;
151 x_coords[i] = x_focus + (x_coords[i] - x_focus) * ratio;
152 y_coords[i] = y_focus + (y_coords[i] - y_focus) * ratio;
153 }
154
155 free(densities);
156 free(smoothed_densities);
157 free(distances);
158 free(orig_distances);
159 free(ordering);
160}
161
162void
163rescale_layout_polar(double *x_coords, double *y_coords,
164 double *x_foci, double *y_foci, int num_foci,
165 size_t n, int interval, double width,
166 double height, double distortion) {
167 // Polar distortion - main function
168 double minX, maxX, minY, maxY;
169 double aspect_ratio;
170 v_data *graph;
171 double scaleX;
172 double scale_ratio;
173
174 // compute original aspect ratio
175 minX = maxX = x_coords[0];
176 minY = maxY = y_coords[0];
177 for (size_t i = 1; i < n; i++)
178 {
179 minX = fmin(minX, x_coords[i]);
180 minY = fmin(minY, y_coords[i]);
181 maxX = fmax(maxX, x_coords[i]);
182 maxY = fmax(maxY, y_coords[i]);
183 }
184 aspect_ratio = (maxX - minX) / (maxY - minY);
185
186 // construct mutual neighborhood graph
187 assert(n <= INT_MAX);
188 graph = UG_graph(x_coords, y_coords, (int)n);
189
190 if (num_foci == 1)
191 { // accelerate execution of most common case
192 rescale_layout_polarFocus(graph, n, x_coords, y_coords, x_foci[0],
193 y_foci[0], interval, distortion);
194 } else
195 {
196 // average-based rescale
197 double *final_x_coords = gv_calloc(n, sizeof(double));
198 double *final_y_coords = gv_calloc(n, sizeof(double));
199 double *cp_x_coords = gv_calloc(n, sizeof(double));
200 double *cp_y_coords = gv_calloc(n, sizeof(double));
201 assert(n <= INT_MAX);
202 for (int i = 0; i < num_foci; i++) {
203 copy_vector((int)n, x_coords, cp_x_coords);
204 copy_vector((int)n, y_coords, cp_y_coords);
205 rescale_layout_polarFocus(graph, n, cp_x_coords, cp_y_coords,
206 x_foci[i], y_foci[i], interval, distortion);
207 scadd(final_x_coords, (int)n - 1, 1.0 / num_foci, cp_x_coords);
208 scadd(final_y_coords, (int)n - 1, 1.0 / num_foci, cp_y_coords);
209 }
210 copy_vector((int)n, final_x_coords, x_coords);
211 copy_vector((int)n, final_y_coords, y_coords);
212 free(final_x_coords);
213 free(final_y_coords);
214 free(cp_x_coords);
215 free(cp_y_coords);
216 }
217 free(graph[0].edges);
218 free(graph);
219
220 minX = maxX = x_coords[0];
221 minY = maxY = y_coords[0];
222 for (size_t i = 1; i < n; i++) {
223 minX = fmin(minX, x_coords[i]);
224 minY = fmin(minY, y_coords[i]);
225 maxX = fmax(maxX, x_coords[i]);
226 maxY = fmax(maxY, y_coords[i]);
227 }
228
229 // shift points:
230 for (size_t i = 0; i < n; i++) {
231 x_coords[i] -= minX;
232 y_coords[i] -= minY;
233 }
234
235 // rescale x_coords to maintain aspect ratio:
236 scaleX = aspect_ratio * (maxY - minY) / (maxX - minX);
237 for (size_t i = 0; i < n; i++) {
238 x_coords[i] *= scaleX;
239 }
240
241
242 // scale the layout to fit full drawing area:
243 scale_ratio =
244 MIN((width) / (aspect_ratio * (maxY - minY)),
245 (height) / (maxY - minY));
246 for (size_t i = 0; i < n; i++) {
247 x_coords[i] *= scale_ratio;
248 y_coords[i] *= scale_ratio;
249 }
250}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define MIN(a, b)
Definition arith.h:28
#define MAX(a, b)
Definition arith.h:33
v_data * UG_graph(double *x, double *y, int n)
Definition delaunay.c:578
static double len(glCompPoint p)
Definition glutils.c:138
void free(void *)
node NULL
Definition grammar.y:181
Agraph_t * graph(char *name)
Definition gv.cpp:34
#define neighbor(t, i, edim, elist)
Definition make_map.h:41
void copy_vector(int n, const double *restrict source, double *restrict dest)
Definition matrix_ops.c:264
void scadd(double *vec1, int end, double fac, double *vec2)
Definition matrix_ops.c:188
static double * compute_densities(v_data *graph, size_t n, double *x, double *y)
static void rescale_layout_polarFocus(v_data *graph, size_t n, double *x_coords, double *y_coords, double x_focus, double y_focus, int interval, double distortion)
void rescale_layout_polar(double *x_coords, double *y_coords, double *x_foci, double *y_foci, int num_foci, size_t n, int interval, double width, double height, double distortion)
static double * smooth_vec(double *vec, int *ordering, size_t n, int interval)
#define DIST(x1, y1, x2, y2)
qsort with carried along context
static void sort_place(double *place, int *ordering, size_t size)
sort an array of doubles based on an list of their desired order
Definition sort.h:57