Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
node_distinct_coloring.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 <sparse/general.h>
12#include <sparse/SparseMatrix.h>
13#include <sparse/QuadTree.h>
15#include <edgepaint/lab.h>
18#include <stdbool.h>
19#include <string.h>
20#include <util/alloc.h>
21
22static void node_distinct_coloring_internal2(int scheme, QuadTree qt,
23 bool weightedQ, SparseMatrix A,
24 int cdim, double accuracy,
25 int seed, double *colors,
26 double *color_diff0,
27 double *color_diff_sum0) {
28 /* here we assume the graph is connected. And that the matrix is symmetric */
29 int i, j, *ia, *ja, n, k = 0;
30 int max_level;
31 double center[3];
32 double width;
33 double *a = NULL;
34 double dist_max;
35 double color_diff = 0, color_diff_old;
36 double color_diff_sum = 0, color_diff_sum_old, *cc;
37 int iter = 0;
38 static const int iter_max = 100;
39 double cspace_size = 0.7;
40 double red[3], black[3], min;
41 int imin;
42
43 assert(accuracy > 0);
44 max_level = MAX(1, -log(accuracy)/log(2.));
45 max_level = MIN(30, max_level);
46
47 {
48 color_rgb rgb = { .r = 255*0.5, .g = 0, .b = 0 };
49 color_lab lab = RGB2LAB(rgb);
50 red[0] = lab.l; red[1] = lab.a; red[2] = lab.b;
51 }
52
53 n = A->m;
54 if (n == 1){
55 if (scheme == COLOR_LAB){
56 assert(qt);
57 QuadTree_get_nearest(qt, black, colors, &imin, &min);
58 LAB2RGB_real_01(colors);
59 *color_diff0 = 1000; *color_diff_sum0 = 1000;
60 } else {
61 for (i = 0; i < cdim; i++) colors[i] = 0;
62 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
63 }
64 return;
65 } else if (n == 2){
66 if (scheme == COLOR_LAB){
67 assert(qt);
68 QuadTree_get_nearest(qt, black, colors, &imin, &min);
69 LAB2RGB_real_01(colors);
70
71 QuadTree_get_nearest(qt, red, colors+cdim, &imin, &min);
72 LAB2RGB_real_01(colors+cdim);
73 *color_diff0 = 1000; *color_diff_sum0 = 1000;
74
75 } else {
76 for (i = 0; i < cdim; i++) colors[i] = 0;
77 for (i = 0; i < cdim; i++) colors[cdim+i] = 0;
78 colors[cdim] = 0.5;
79 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
80 }
81 return;
82 }
83 assert(n == A->m);
84 ia = A->ia;
85 ja = A->ja;
86 if (A->type == MATRIX_TYPE_REAL && A->a){
87 a = A->a;
88 }
89
90 /* cube [0, cspace_size]^3: only uised if not LAB */
91 center[0] = center[1] = center[2] = cspace_size*0.5;
92 width = cspace_size*0.5;
93
94 /* randomly assign colors first */
95 srand(seed);
96 for (i = 0; i < n*cdim; i++) colors[i] = cspace_size*drand();
97
98 double *x = gv_calloc(cdim * n, sizeof(double));
99 double *wgt = weightedQ ? gv_calloc(n, sizeof(double)) : NULL;
100
101 color_diff = 0; color_diff_old = -1;
102 color_diff_sum = 0; color_diff_sum_old = -1;
103
104 while (iter++ < iter_max && (color_diff > color_diff_old || (color_diff == color_diff_old && color_diff_sum > color_diff_sum_old))){
105 color_diff_old = color_diff;
106 color_diff_sum_old = color_diff_sum;
107 for (i = 0; i < n; i++){
108 k = 0;
109 for (j = ia[i]; j < ia[i+1]; j++){
110 if (ja[j] == i) continue;
111 memcpy(&(x[k*cdim]), &(colors[ja[j]*cdim]), sizeof(double)*cdim);
112 if (wgt && a) wgt[k] = a[j];
113 k++;
114 }
115 cc = &(colors[i*cdim]);
116 if (scheme == COLOR_LAB){
117 furtherest_point_in_list(k, cdim, wgt, x, qt, max_level, &dist_max, &cc);
118 } else if (scheme == COLOR_RGB || scheme == COLOR_GRAY){
119 furtherest_point(k, cdim, wgt, x, center, width, max_level, &dist_max, &cc);
120 } else {
121 assert(0);
122 }
123 if (i == 0){
124 color_diff = dist_max;
125 color_diff_sum = dist_max;
126 } else {
127 color_diff = MIN(dist_max, color_diff);
128 color_diff_sum += dist_max;
129 }
130 }
131
132 if (Verbose) fprintf(stderr,"iter ---- %d ---, color_diff = %f, color_diff_sum = %f\n", iter, color_diff, color_diff_sum);
133 }
134
135 if (scheme == COLOR_LAB){
136 /* convert from LAB to RGB */
137 color_rgb rgb;
138 color_lab lab;
139 for (i = 0; i < n; i++){
140 lab = color_lab_init(colors[i*cdim], colors[i*cdim+1], colors[i*cdim+2]);
141 rgb = LAB2RGB(lab);
142 colors[i*cdim] = (rgb.r)/255;
143 colors[i*cdim+1] = (rgb.g)/255;
144 colors[i*cdim+2] = (rgb.b)/255;
145 }
146 }
147 *color_diff0 = color_diff;
148 *color_diff_sum0 = color_diff_sum;
149 free(x);
150 free(wgt);
151}
152
153static void node_distinct_coloring_internal(int scheme, QuadTree qt,
154 bool weightedQ, SparseMatrix A,
155 int cdim, double accuracy,
156 int seed, double *colors) {
157 int i;
158 double color_diff;
159 double color_diff_sum;
160 if (seed < 0) {
161 /* do multiple iterations and pick the best */
162 int iter, seed_max = -1;
163 double color_diff_max = -1;
164 srand(123);
165 iter = -seed;
166 for (i = 0; i < iter; i++){
167 seed = irand(100000);
168 node_distinct_coloring_internal2(scheme, qt, weightedQ, A, cdim, accuracy, seed, colors, &color_diff, &color_diff_sum);
169 if (color_diff_max < color_diff){
170 seed_max = seed; color_diff_max = color_diff;
171 }
172 }
173 seed = seed_max;
174 }
175 node_distinct_coloring_internal2(scheme, qt, weightedQ, A, cdim, accuracy, seed, colors, &color_diff, &color_diff_sum);
176
177}
178
179int node_distinct_coloring(const char *color_scheme, int *lightness,
180 bool weightedQ, SparseMatrix A0, double accuracy,
181 int seed, int *cdim0, double **colors) {
182 SparseMatrix B, A = A0;
183 int ncomps, *comps = NULL;
184 int nn, n;
185 int i, j, jj;
186 QuadTree qt = NULL;
187 int cdim;
188 int scheme = COLOR_LAB;
189 int maxcolors = 10000, max_qtree_level = 10, r, g, b;
190 const char *color_list = color_palettes_get(color_scheme);
191 if (color_list) color_scheme = color_list;
192
193 cdim = *cdim0 = 3;
194 if (strcmp(color_scheme, "lab") == 0){
195 if (Verbose) fprintf(stderr,"lab\n");
196 scheme = COLOR_LAB;
197 qt = lab_gamut_quadtree(lightness, max_qtree_level);
198 if (!qt){
199 fprintf(stderr, "out of memory\n");
200 return -1;
201 }
202 } else if (strcmp(color_scheme, "rgb") == 0){
203 if (Verbose) fprintf(stderr,"rgb\n");
204 scheme = COLOR_RGB;
205 } else if (strcmp(color_scheme, "gray") == 0){
206 scheme = COLOR_GRAY;
207 cdim = *cdim0 = 1;
208 } else if (sscanf(color_scheme,"#%02X%02X%02X", &r, &g, &b) == 3 ){
209 scheme = COLOR_LAB;
210 double *color_points = color_blend_rgb2lab(color_scheme, maxcolors);
211 assert(color_points);
212 qt = QuadTree_new_from_point_list(cdim, maxcolors, max_qtree_level,
213 color_points);
214 free(color_points);
215 assert(qt);
216 } else {
218 }
219
220
221 if (accuracy <= 0) accuracy = 0.0001;
222
223 n = A->m;
224 if (n != A->n) {
225 QuadTree_delete(qt);
226 return -1;
227 }
228
229 *colors = gv_calloc(cdim * n, sizeof(double));
230 double *ctmp = gv_calloc(cdim * n, sizeof(double));
231
232 B = SparseMatrix_symmetrize(A, false);
233 A = B;
234
235 int *comps_ptr = SparseMatrix_weakly_connected_components(A, &ncomps, &comps);
236
237 for (i = 0; i < ncomps; i++){
238 nn = comps_ptr[i+1] - comps_ptr[i];
239 B = SparseMatrix_get_submatrix(A, nn, nn, &(comps[comps_ptr[i]]), &(comps[comps_ptr[i]]));
240 node_distinct_coloring_internal(scheme, qt, weightedQ, B, cdim, accuracy, seed, ctmp);
241
242 for (j = comps_ptr[i]; j < comps_ptr[i+1]; j++){
243 jj = j - comps_ptr[i];
244 memcpy(&((*colors)[comps[j]*cdim]), &(ctmp[jj*cdim]), cdim*sizeof(double));
245 }
247 }
248 free(comps_ptr);
249 free(ctmp);
250 QuadTree_delete(qt);
251
252 if (A != A0) SparseMatrix_delete(A);
253 free(comps);
254 return 0;
255}
void QuadTree_get_nearest(QuadTree qt, double *x, double *ymin, int *imin, double *min)
Definition QuadTree.c:682
QuadTree QuadTree_new_from_point_list(int dim, int n, int max_level, double *coord)
Definition QuadTree.c:309
void QuadTree_delete(QuadTree q)
Definition QuadTree.c:375
SparseMatrix SparseMatrix_get_submatrix(SparseMatrix A, int nrow, int ncol, int *rindices, int *cindices)
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, bool pattern_symmetric_only)
void SparseMatrix_delete(SparseMatrix A)
int * SparseMatrix_weakly_connected_components(SparseMatrix A0, int *ncomp, int **comps)
@ MATRIX_TYPE_REAL
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
const char * color_palettes_get(const char *color_palette_name)
static long seed
Definition exeval.c:1035
#define A(n, t)
Definition expr.h:76
void furtherest_point_in_list(int k, int dim, double *wgt, double *pts, QuadTree qt, int max_level, double *dist_max, double **argmax)
void furtherest_point(int k, int dim, double *wgt, double *pts, double *center, double width, int max_level, double *dist_max, double **argmax)
int irand(int n)
Definition general.c:26
static bool Verbose
Definition gml2gv.c:23
void free(void *)
node NULL
Definition grammar.y:163
static double drand(void)
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:27
static const char black[]
#define B
Definition hierarchy.c:117
QuadTree lab_gamut_quadtree(const int *lightness, int max_qtree_level)
construct a quadtree of the LAB gamut points
Definition lab.c:186
void LAB2RGB_real_01(double *color)
Definition lab.c:74
color_lab color_lab_init(double l, double a, double b)
Definition lab.c:36
color_lab RGB2LAB(color_rgb color)
Definition lab.c:62
double * color_blend_rgb2lab(const char *color_list, const int maxpoints)
Definition lab.c:212
color_rgb LAB2RGB(color_lab color)
Definition lab.c:87
static void node_distinct_coloring_internal(int scheme, QuadTree qt, bool weightedQ, SparseMatrix A, int cdim, double accuracy, int seed, double *colors)
int node_distinct_coloring(const char *color_scheme, int *lightness, bool weightedQ, SparseMatrix A0, double accuracy, int seed, int *cdim0, double **colors)
static void node_distinct_coloring_internal2(int scheme, QuadTree qt, bool weightedQ, SparseMatrix A, int cdim, double accuracy, int seed, double *colors, double *color_diff0, double *color_diff_sum0)
@ ERROR_BAD_COLOR_SCHEME
double b
l: 0 to 100, a,b: -128 to 128
Definition lab.h:24
double a
Definition lab.h:24
double l
Definition lab.h:24
double r
Definition lab.h:14
double g
Definition lab.h:14
double b
Definition lab.h:14
static point center(point vertex[], size_t n)
#define MAX(a, b)
Definition write.c:31