Graphviz 14.1.2~dev.20260120.0924
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 "config.h"
12
13#include <sparse/general.h>
14#include <sparse/SparseMatrix.h>
15#include <sparse/QuadTree.h>
17#include <edgepaint/lab.h>
19#include <math.h>
21#include <stdbool.h>
22#include <string.h>
23#include <util/alloc.h>
24#include <util/debug.h>
25#include <util/gv_math.h>
26#include <util/random.h>
27
28static void node_distinct_coloring_internal2(int scheme, QuadTree qt,
29 bool weightedQ, SparseMatrix A,
30 int cdim, double accuracy,
31 int seed, double *colors,
32 double *color_diff0,
33 double *color_diff_sum0) {
34 /* here we assume the graph is connected. And that the matrix is symmetric */
35 double dist_max;
36 double *cc;
37 int iter = 0;
38 static const int iter_max = 100;
39 double red[3];
40 double black[] = {0, 0, 0};
41
42 assert(accuracy > 0);
43 const int max_level = d2i(fmin(30, fmax(1, -log(accuracy) / log(2.))));
44
45 {
46 color_rgb rgb = { .r = 255*0.5, .g = 0, .b = 0 };
47 color_lab lab = RGB2LAB(rgb);
48 red[0] = lab.l; red[1] = lab.a; red[2] = lab.b;
49 }
50
51 const int n = A->m;
52 if (n == 1){
53 if (scheme == COLOR_LAB){
54 assert(qt);
55 QuadTree_get_nearest(qt, black, colors, &(int){0}, &(double){0});
56 LAB2RGB_real_01(colors);
57 *color_diff0 = 1000; *color_diff_sum0 = 1000;
58 } else {
59 for (int i = 0; i < cdim; i++) colors[i] = 0;
60 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
61 }
62 return;
63 } else if (n == 2){
64 if (scheme == COLOR_LAB){
65 assert(qt);
66 QuadTree_get_nearest(qt, black, colors, &(int){0}, &(double){0});
67 LAB2RGB_real_01(colors);
68
69 QuadTree_get_nearest(qt, red, colors+cdim, &(int){0}, &(double){0});
70 LAB2RGB_real_01(colors+cdim);
71 *color_diff0 = 1000; *color_diff_sum0 = 1000;
72
73 } else {
74 for (int i = 0; i < cdim; i++) colors[i] = 0;
75 for (int i = 0; i < cdim; i++) colors[cdim+i] = 0;
76 colors[cdim] = 0.5;
77 *color_diff0 = sqrt(cdim); *color_diff_sum0 = sqrt(cdim);
78 }
79 return;
80 }
81 assert(n == A->m);
82 const int *ia = A->ia;
83 const int *ja = A->ja;
84 const double *const a = A->type == MATRIX_TYPE_REAL ? A->a : NULL;
85
86 /* cube [0, cspace_size]^3: only uised if not LAB */
87 const double cspace_size = 0.7;
88 double center[] = {cspace_size * 0.5, cspace_size * 0.5, cspace_size * 0.5};
89 const double width = cspace_size * 0.5;
90
91 /* randomly assign colors first */
92 srand(seed);
93 for (int i = 0; i < n*cdim; i++) colors[i] = cspace_size*drand();
94
95 double *x = gv_calloc(cdim * n, sizeof(double));
96 double *wgt = weightedQ ? gv_calloc(n, sizeof(double)) : NULL;
97
98 double color_diff = 0;
99 double color_diff_old = -1;
100 double color_diff_sum = 0;
101 double color_diff_sum_old = -1;
102
103 while (iter++ < iter_max && (color_diff > color_diff_old || (color_diff == color_diff_old && color_diff_sum > color_diff_sum_old))){
104 color_diff_old = color_diff;
105 color_diff_sum_old = color_diff_sum;
106 for (int i = 0; i < n; i++){
107 int k = 0;
108 for (int j = ia[i]; j < ia[i+1]; j++){
109 if (ja[j] == i) continue;
110 memcpy(&(x[k*cdim]), &(colors[ja[j]*cdim]), sizeof(double)*cdim);
111 if (wgt && a) wgt[k] = a[j];
112 k++;
113 }
114 cc = &(colors[i*cdim]);
115 if (scheme == COLOR_LAB){
116 furtherest_point_in_list(k, cdim, wgt, x, qt, max_level, &dist_max, &cc);
117 } else if (scheme == COLOR_RGB || scheme == COLOR_GRAY){
118 furtherest_point(k, cdim, wgt, x, center, width, max_level, &dist_max, &cc);
119 } else {
120 assert(0);
121 }
122 if (i == 0){
123 color_diff = dist_max;
124 color_diff_sum = dist_max;
125 } else {
126 color_diff = MIN(dist_max, color_diff);
127 color_diff_sum += dist_max;
128 }
129 }
130
131 GV_DEBUG("iter ---- %d ---, color_diff = %f, color_diff_sum = %f", iter,
132 color_diff, color_diff_sum);
133 }
134
135 if (scheme == COLOR_LAB){
136 /* convert from LAB to RGB */
137 for (int i = 0; i < n; i++){
138 const color_lab lab = color_lab_init(colors[i * cdim],
139 colors[i * cdim + 1],
140 colors[i * cdim + 2]);
141 const color_rgb 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 = gv_random(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 GV_DEBUG("lab");
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 GV_DEBUG("rgb");
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:684
QuadTree QuadTree_new_from_point_list(int dim, int n, int max_level, double *coord)
Definition QuadTree.c:311
void QuadTree_delete(QuadTree q)
Definition QuadTree.c:377
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)
helpers for verbose/debug printing
#define GV_DEBUG(...)
Definition debug.h:39
static long seed
Definition exeval.c:1008
#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)
double drand(void)
Definition general.c:24
void free(void *)
node NULL
Definition grammar.y:181
Arithmetic helper functions.
static int d2i(double v)
Definition gv_math.h:154
static const char black[]
#define B
Definition hierarchy.c:120
QuadTree lab_gamut_quadtree(const int *lightness, int max_qtree_level)
construct a quadtree of the LAB gamut points
Definition lab.c:183
void LAB2RGB_real_01(double *color)
Definition lab.c:76
color_lab color_lab_init(double l, double a, double b)
Definition lab.c:38
color_lab RGB2LAB(color_rgb color)
Definition lab.c:64
double * color_blend_rgb2lab(const char *color_list, const int maxpoints)
Definition lab.c:209
color_rgb LAB2RGB(color_lab color)
Definition lab.c:84
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
int gv_random(int bound)
Definition random.c:85
random number generation
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)