27static double norm(
int n,
const double *x) {
31 for (i = 0; i < n; i++) res += x[i]*x[i];
36static double sqr_dist(
int dim,
const double *x,
const double *y) {
39 for (i = 0; i <
dim; i++) res += (x[i] - y[i])*(x[i] - y[i]);
43static double dist(
int dim,
const double *x,
const double *y) {
69 e.
wgts = std::vector<double>(np - 1, wgt);
79 double dist1,
dist2, len1, len2;
83 const double *u1 = e1.
x.data();
85 const double *u2 = e2.
x.data();
96 dist1 = std::max(0.1, dist1 / (len1 + len2 + 0.0001 * dist1));
109 double dist1,
dist2, len1, len2,
len;
110 double tmp, ca, cp, cs;
112 bool flipped =
false;
114 const double *u1 = e1.
x.data();
116 const double *u2 = e2.
x.data();
127 len = 0.5*(len1+len2);
131 for (i = 0; i <
dim; i++)
132 ca += (v1[i]-u1[i])*(v2[i]-u2[i]);
133 ca = fabs(ca/(len1*len2));
137 cs = 2 / (std::max(len1, len2) /
len +
len / std::min(len1, len2));
138 assert(cs > -0.001 && cs < 1.001);
142 for (i = 0; i <
dim; i++) {
143 tmp = .5*(v1[i]+u1[i])-.5*(v2[i]+u2[i]);
148 assert(cp > -0.001 && cp < 1.001);
161 fprintf(fp,
"#%02x%02x%02x%02x", r, g, b,
alpha);
166 int i, j, k, kk,
dim, sta, sto;
167 double maxwgt = 0,
len, len_total0;
169 double tt1[3]={0.15,0.5,0.85};
170 double tt2[4]={0.15,0.4,0.6,0.85};
173 fprintf(fp,
"strict graph{\n");
175 for (i = 0; i < ne; i++){
177 const std::vector<double> &x =
edge.
x;
180 sto =
edge.npoints - 1;
182 fprintf(fp,
"%d [pos=\"", i);
183 for (k = 0; k <
dim; k++) {
184 if (k != 0) fprintf(fp,
",");
185 fprintf(fp,
"%f", x[sta*
dim+k]);
187 fprintf(fp,
"\"];\n");
189 fprintf(fp,
"%d [pos=\"", i + ne);
190 for (k = 0; k <
dim; k++) {
191 if (k != 0) fprintf(fp,
",");
192 fprintf(fp,
"%f", x[sto*
dim+k]);
194 fprintf(fp,
"\"];\n");
199 for (i = 0; i < ne; i++){
201 if (!
edge.wgts.empty()) {
202 for (j = 0; j <
edge.npoints - 1; j++) {
203 maxwgt = std::max(maxwgt,
edge.wgts[j]);
209 for (i = 0; i < ne; i++){
210 fprintf(fp,
"%d -- %d [pos=\"", i, i + ne);
212 const std::vector<double> &x =
edge.
x;
215 for (j = 0; j <
edge.npoints; j++) {
221 if (j == 1 || j ==
edge.npoints - 1) {
228 for (kk = 1; kk <= mm; kk++){
230 for (k = 0; k <
dim; k++) {
231 if (k != 0) fprintf(fp,
",");
232 fprintf(fp,
"%f", (x[(j-1)*
dim+k]*(1-t)+x[j*
dim+k]*(t)));
237 if (j == 0 || j ==
edge.npoints - 1){
238 for (k = 0; k <
dim; k++) {
239 if (k != 0) fprintf(fp,
",");
240 fprintf(fp,
"%f", x[j*
dim+k]);
245 if (!
edge.wgts.empty()) {
246 fprintf(fp,
"\", wgts=\"");
247 for (j = 0; j <
edge.npoints - 1; j++){
248 if (j != 0) fprintf(fp,
",");
249 fprintf(fp,
"%f",
edge.wgts[j]);
253 fprintf(fp,
"\", color=\"");
254 for (j = 0; j <
edge.npoints - 1; j++){
256 for (k = 0; k <
dim; k++){
262 for (j = 0; j <
edge.npoints - 1; j++) {
264 for (k = 0; k <
dim; k++){
268 t =
edge.wgts[j] / maxwgt;
270 r = 255*t; g = 0; b = 255*(1-t); b = 255*(1-t);
271 if (j != 0) fprintf(fp,
":");
273 if (j <
edge.npoints - 2) fprintf(fp,
";%f",
len / len_total0);
277 fprintf(fp,
"\"];\n");
284static void pedge_print(
char *comments,
const pedge &e) {
287 fprintf(stderr,
"%s", comments);
288 for (i = 0; i < e.
npoints; i++){
289 if (i > 0) fprintf(stderr,
",");
291 for (j = 0; j <
dim; j++){
292 if (j > 0) fprintf(stderr,
",");
293 fprintf(stderr,
"%f", e.
x[
dim * i + j]);
297 fprintf(stderr,
"\n");
305 e.
x.resize(e.
dim * n, 0);
306 if (e.
wgts.empty()) {
307 e.
wgts.resize(n - 1);
308 for (i = 0; i < e.
npoints; i++)
311 e.
wgts.resize(n - 1);
321 assert(npoints >= 2);
322 if (npoints*2-1 >
len){
326 std::vector<double> &x = e.
x;
328 for (i = npoints - 1; i >= 0; i--){
330 for (j = 0; j <
dim; j++){
331 x[
dim*ii + j] = x[
dim*i + j];
335 for (i = 0; i < npoints - 1; i++){
338 for (j = 0; j <
dim; j++){
339 x[
dim*(2*i + 1) + j] = 0.5*(x[
dim*ii + j] + x[
dim*ii2 + j]);
348 const std::vector<double> &x = e.
x;
357 for (i = 1; i <= np - 2; i++){
360 for (j = 0; j <
dim; j++) force[i*
dim + j] +=
s*(x[
left*
dim + j] - x[i*
dim + j]);
366 const pedge &e2, std::vector<double> &force) {
368 const std::vector<double> &x1 = e1.
x, &x2 = e2.
x;
382 s = similarity*edge_length;
383 for (i = 1; i <= np - 2; i++){
386 ss =
s/(
dist+0.1*edge_length*sqrt(
dist));
387 for (j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[i*
dim + j] - x1[i*
dim + j]);
391 s = -similarity*edge_length;
392 for (i = 1; i <= np - 2; i++){
395 ss =
s/(
dist+0.1*edge_length*sqrt(
dist));
396 for (j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[(np - 1 - i)*
dim + j] - x1[i*
dim + j]);
403 std::vector<pedge> &edges,
int maxit,
404 double step0,
double K) {
405 int i, j, ne =
A->
n, k;
406 int *ia =
A->ia, *ja =
A->ja, iter = 0;
407 double *a = (
double*)
A->a;
408 const int np = edges[0].npoints,
dim = edges[0].dim;
410 double fnorm_a, fnorm_t, edge_length, start;
413 fprintf(stderr,
"total interaction pairs = %d out of %d, avg neighbors per edge = %f\n",
A->nz,
A->m*
A->m,
A->nz/(
double)
A->m);
415 std::vector<double> force_t(
dim * np);
416 std::vector<double> force_a(
dim * np);
417 while (step > 0.001 && iter <
maxit){
420 for (i = 0; i < ne; i++){
421 for (j = 0; j <
dim*np; j++) {
425 pedge &e1 = edges[i];
426 std::vector<double> &x = e1.
x;
428 for (j = ia[i]; j < ia[i+1]; j++){
429 const pedge &e2 = edges[ja[j]];
436 for (j = 1; j <= np - 2; j++){
437 for (k = 0; k <
dim; k++) {
438 x[j *
dim + k] += step * edge_length
439 * (force_t[j *
dim + k] + K * force_a[j *
dim+k])
440 / hypot(fnorm_t, K * fnorm_a);
447 fprintf(stderr,
"iter ==== %d cpu = %f npoints = %d\n",iter, ((
double) (clock() - start))/CLOCKS_PER_SEC, np - 2);
452 std::vector<pedge> &edges,
453 double angle_param,
double angle) {
454 int *assignment =
NULL, nclusters;
456 int *clusterp, *clusters;
470 if (
Verbose > 1) fprintf(stderr,
"there are %d clusters, modularity = %f\n",nclusters, modularity);
474 for (i = 0; i < ne; i++){
483 for (i = 0; i < nclusters; i++) {
484 ink1 =
ink(edges, clusterp[i + 1] - clusterp[i], &clusters[clusterp[i]],
485 &ink0, &meet1, &meet2, angle_param, angle);
487 fprintf(stderr,
"nedges = %d ink0 = %f, ink1 = %f\n",clusterp[i+1] - clusterp[i], ink0,
ink1);
489 for (j = clusterp[i]; j < clusterp[i+1]; j++){
493 pedge &e = edges[clusters[j]];
494 e.
x[1 *
dim] = meet1.
x;
495 e.
x[1 *
dim + 1] = meet1.
y;
496 e.
x[2 *
dim] = meet2.
x;
497 e.
x[2 *
dim + 1] = meet2.
y;
499 e.
x[3 *
dim + 1] = e.
x[4 *
dim + 1];
508 const std::vector<pedge> &edges,
509 int compatibility_method,
double tol) {
512 int *ia, *ja, i, j, jj;
517 ia =
A->ia; ja =
A->ja;
519 for (i = 0; i < ne; i++){
520 for (j = ia[i]; j < ia[i+1]; j++){
522 if (i == jj)
continue;
539 fprintf(stderr,
"edge compatibilitu time = %f\n",((
double) (clock() - start))/CLOCKS_PER_SEC);
544 const std::vector<double> &x,
int maxit_outer,
545 double K,
int method,
int nneighbor,
546 int compatibility_method,
int max_recursion,
547 double angle_param,
double angle) {
566 double step0 = 0.1, start = 0.0;
570 std::vector<pedge> edges;
573 for (i = 0; i < ne; i++){
599 for (k = 0; k < maxit_outer; k++){
600 for (i = 0; i < ne; i++){
613 fprintf(stderr,
"total edge bundling cpu = %f\n",((
double) (clock() - start))/CLOCKS_PER_SEC);
SparseMatrix SparseMatrix_from_coordinate_format(SparseMatrix A)
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, bool pattern_symmetric_only)
SparseMatrix SparseMatrix_coordinate_form_add_entry(SparseMatrix A, int irn, int jcn, const void *val)
void SparseMatrix_delete(SparseMatrix A)
SparseMatrix SparseMatrix_copy(SparseMatrix A)
SparseMatrix SparseMatrix_apply_fun(SparseMatrix A, double(*fun)(double x))
SparseMatrix SparseMatrix_new(int m, int n, int nz, int type, int format)
void agglomerative_ink_bundling(int dim, SparseMatrix A, std::vector< pedge > &edges, int nneighbor, int MAX_RECURSE_LEVEL, double angle_param, double angle)
void modularity_clustering(SparseMatrix A, bool inplace, int ncluster_target, int *nclusters, int **assignment, double *modularity)
static SparseMatrix check_compatibility(SparseMatrix A, int ne, const std::vector< pedge > &edges, int compatibility_method, double tol)
pedge pedge_wgt_new(int np, int dim, double *x, double wgt)
static void force_directed_edge_bundling(SparseMatrix A, std::vector< pedge > &edges, int maxit, double step0, double K)
void pedge_wgts_realloc(pedge &e, int n)
static void edge_attraction_force(double similarity, const pedge &e1, const pedge &e2, std::vector< double > &force)
static void fprint_rgb(FILE *fp, int r, int g, int b, int alpha)
static void modularity_ink_bundling(int dim, int ne, SparseMatrix B, std::vector< pedge > &edges, double angle_param, double angle)
std::vector< pedge > edge_bundling(SparseMatrix A0, int dim, const std::vector< double > &x, int maxit_outer, double K, int method, int nneighbor, int compatibility_method, int max_recursion, double angle_param, double angle)
static double norm(int n, const double *x)
void pedge_export_gv(FILE *fp, int ne, const std::vector< pedge > &edges)
static void edge_tension_force(std::vector< double > &force, const pedge &e)
static double edge_compatibility(const pedge &e1, const pedge &e2)
void pedge_double(pedge &e)
static double edge_compatibility_full(const pedge &e1, const pedge &e2)
static double sqr_dist(int dim, const double *x, const double *y)
static double dist(int dim, const double *x, const double *y)
void pedge_delete(pedge &)
static pedge pedge_new(int np, int dim, const double *x)
static double len(glCompPoint p)
double ink(const std::vector< pedge > &edges, int numEdges, int *pick, double *ink0, point_t *meet1, point_t *meet2, double angle_param, double angle)
double ink1(const pedge &e)
PATHUTIL_API COORD dist2(Ppoint_t, Ppoint_t)
std::vector< double > x
coordinates of the npoints poly points. Dimension dim*npoints
std::vector< double > wgts
graphs, nodes and edges info: Agraphinfo_t, Agnodeinfo_t and Agedgeinfo_t