31static double norm(
int n,
const double *x) {
32 return sqrt(std::inner_product(x, x + n, x, 0.0));
35static double sqr_dist(
int dim,
const double *x,
const double *y) {
38 for (i = 0; i <
dim; i++) res += (x[i] - y[i])*(x[i] - y[i]);
42static double dist(
int dim,
const double *x,
const double *y) {
68 e.
wgts = std::vector<double>(np - 1, wgt);
78 double dist1,
dist2, len1, len2;
82 const double *u1 = e1.
x.data();
84 const double *u2 = e2.
x.data();
95 dist1 = std::max(0.1, dist1 / (len1 + len2 + 0.0001 * dist1));
108 double dist1,
dist2, len1, len2,
len;
109 double tmp, ca, cp, cs;
111 bool flipped =
false;
113 const double *u1 = e1.
x.data();
115 const double *u2 = e2.
x.data();
125 len = 0.5*(len1+len2);
129 for (i = 0; i <
dim; i++)
130 ca += (v1[i]-u1[i])*(v2[i]-u2[i]);
131 ca = fabs(ca/(len1*len2));
135 cs = 2 / (std::max(len1, len2) /
len +
len / std::min(len1, len2));
136 assert(cs > -0.001 && cs < 1.001);
140 for (i = 0; i <
dim; i++) {
141 tmp = .5*(v1[i]+u1[i])-.5*(v2[i]+u2[i]);
146 assert(cp > -0.001 && cp < 1.001);
159 fprintf(fp,
"#%02x%02x%02x%02x", r, g, b,
alpha);
165 fprintf(fp,
"strict graph{\n");
167 for (
int i = 0; i < ne; i++){
169 const std::vector<double> &x =
edge.
x;
172 const int sto =
edge.npoints - 1;
174 fprintf(fp,
"%d [pos=\"", i);
175 for (
int k = 0; k <
dim; k++) {
176 if (k != 0) fprintf(fp,
",");
177 fprintf(fp,
"%f", x[sta*
dim+k]);
179 fprintf(fp,
"\"];\n");
181 fprintf(fp,
"%d [pos=\"", i + ne);
182 for (
int k = 0; k <
dim; k++) {
183 if (k != 0) fprintf(fp,
",");
184 fprintf(fp,
"%f", x[sto*
dim+k]);
186 fprintf(fp,
"\"];\n");
191 for (
int i = 0; i < ne; i++){
193 if (!
edge.wgts.empty()) {
194 for (
int j = 0; j <
edge.npoints - 1; j++) {
195 maxwgt = std::max(maxwgt,
edge.wgts[j]);
201 for (
int i = 0; i < ne; i++){
202 fprintf(fp,
"%d -- %d [pos=\"", i, i + ne);
204 const std::vector<double> &x =
edge.
x;
207 for (
int j = 0; j <
edge.npoints; j++) {
214 const double tt1[] = {0.15, 0.5, 0.85};
215 const double tt2[] = {0.15, 0.4, 0.6, 0.85};
216 if (j == 1 || j ==
edge.npoints - 1) {
223 for (
int kk = 1; kk <= mm; kk++){
224 const double t = tt[kk - 1];
225 for (
int k = 0; k <
dim; k++) {
226 if (k != 0) fprintf(fp,
",");
227 fprintf(fp,
"%f", x[(j - 1) *
dim + k] * (1 - t) + x[j *
dim + k] * t);
232 if (j == 0 || j ==
edge.npoints - 1){
233 for (
int k = 0; k <
dim; k++) {
234 if (k != 0) fprintf(fp,
",");
235 fprintf(fp,
"%f", x[j*
dim+k]);
240 if (!
edge.wgts.empty()) {
241 fprintf(fp,
"\", wgts=\"");
242 for (
int j = 0; j <
edge.npoints - 1; j++){
243 if (j != 0) fprintf(fp,
",");
244 fprintf(fp,
"%f",
edge.wgts[j]);
247 double len_total0 = 0;
248 fprintf(fp,
"\", color=\"");
249 for (
int j = 0; j <
edge.npoints - 1; j++){
252 for (k = 0; k <
dim; k++){
258 for (
int j = 0; j <
edge.npoints - 1; j++) {
261 for (k = 0; k <
dim; k++){
265 const double t =
edge.wgts[j] / maxwgt;
267 const int r = 255 * t;
269 const int b = 255 * (1 - t);
270 if (j != 0) fprintf(fp,
":");
272 if (j <
edge.npoints - 2) fprintf(fp,
";%f",
len / len_total0);
276 fprintf(fp,
"\"];\n");
283static void pedge_print(
char *comments,
const pedge &e) {
286 fprintf(stderr,
"%s", comments);
287 for (i = 0; i < e.
npoints; i++){
288 if (i > 0) fprintf(stderr,
",");
290 for (j = 0; j <
dim; j++){
291 if (j > 0) fprintf(stderr,
",");
292 fprintf(stderr,
"%f", e.
x[
dim * i + j]);
296 fprintf(stderr,
"\n");
304 e.
x.resize(e.
dim * n, 0);
305 if (e.
wgts.empty()) {
306 e.
wgts.resize(n - 1);
307 for (i = 0; i < e.
npoints; i++)
310 e.
wgts.resize(n - 1);
320 assert(npoints >= 2);
321 if (npoints*2-1 >
len){
325 std::vector<double> &x = e.
x;
327 for (i = npoints - 1; i >= 0; i--){
329 for (j = 0; j <
dim; j++){
330 x[
dim*ii + j] = x[
dim*i + j];
334 for (i = 0; i < npoints - 1; i++){
337 for (j = 0; j <
dim; j++){
338 x[
dim*(2*i + 1) + j] = 0.5*(x[
dim*ii + j] + x[
dim*ii2 + j]);
347 const std::vector<double> &x = e.
x;
356 for (i = 1; i <= np - 2; i++){
359 for (j = 0; j <
dim; j++) force[i*
dim + j] +=
s*(x[
left*
dim + j] - x[i*
dim + j]);
365 const pedge &e2, std::vector<double> &force) {
367 const std::vector<double> &x1 = e1.
x, &x2 = e2.
x;
378 const double s = similarity * edge_length;
379 for (
int i = 1; i <= np - 2; i++){
382 const double ss =
s / (
dist + 0.1 * edge_length * sqrt(
dist));
383 for (
int j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[i*
dim + j] - x1[i*
dim + j]);
386 const double s = -similarity * edge_length;
387 for (
int i = 1; i <= np - 2; i++){
390 const double ss =
s / (
dist + 0.1 * edge_length * sqrt(
dist));
391 for (
int j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[(np - 1 - i)*
dim + j] - x1[i*
dim + j]);
398 std::vector<pedge> &edges,
int maxit,
399 double step0,
double K) {
400 int i, j, ne =
A->
n, k;
401 int *ia =
A->ia, *ja =
A->ja, iter = 0;
402 double *a = (
double*)
A->a;
403 const int np = edges[0].npoints,
dim = edges[0].dim;
405 double fnorm_a, fnorm_t, edge_length, start;
408 fprintf(stderr,
"total interaction pairs = %" PRISIZE_T
409 " out of %d, avg neighbors per edge = %f\n",
A->nz,
A->m *
A->m,
410 (
double)
A->nz /
A->m);
412 std::vector<double> force_t(
dim * np);
413 std::vector<double> force_a(
dim * np);
414 while (step > 0.001 && iter <
maxit){
417 for (i = 0; i < ne; i++){
418 for (j = 0; j <
dim*np; j++) {
422 pedge &e1 = edges[i];
423 std::vector<double> &x = e1.
x;
425 for (j = ia[i]; j < ia[i+1]; j++){
426 const pedge &e2 = edges[ja[j]];
433 for (j = 1; j <= np - 2; j++){
434 for (k = 0; k <
dim; k++) {
435 x[j *
dim + k] += step * edge_length
436 * (force_t[j *
dim + k] + K * force_a[j *
dim+k])
437 / hypot(fnorm_t, K * fnorm_a);
444 fprintf(stderr,
"iter ==== %d cpu = %f npoints = %d\n", iter, (
double)(clock() - start) / CLOCKS_PER_SEC, np - 2);
449 std::vector<pedge> &edges,
450 double angle_param,
double angle) {
451 int *assignment =
NULL, nclusters;
453 int *clusterp, *clusters;
467 if (
Verbose > 1) fprintf(stderr,
"there are %d clusters, modularity = %f\n",nclusters, modularity);
471 for (i = 0; i < ne; i++){
480 for (i = 0; i < nclusters; i++) {
481 ink1 =
ink(edges, clusterp[i + 1] - clusterp[i], &clusters[clusterp[i]],
482 &ink0, &meet1, &meet2, angle_param, angle);
484 fprintf(stderr,
"nedges = %d ink0 = %f, ink1 = %f\n",clusterp[i+1] - clusterp[i], ink0,
ink1);
486 for (j = clusterp[i]; j < clusterp[i+1]; j++){
490 pedge &e = edges[clusters[j]];
491 e.
x[1 *
dim] = meet1.
x;
492 e.
x[1 *
dim + 1] = meet1.
y;
493 e.
x[2 *
dim] = meet2.
x;
494 e.
x[2 *
dim + 1] = meet2.
y;
496 e.
x[3 *
dim + 1] = e.
x[4 *
dim + 1];
505 const std::vector<pedge> &edges,
506 int compatibility_method,
double tol) {
509 int *ia, *ja, i, j, jj;
514 ia =
A->ia; ja =
A->ja;
516 for (i = 0; i < ne; i++){
517 for (j = ia[i]; j < ia[i+1]; j++){
519 if (i == jj)
continue;
536 fprintf(stderr,
"edge compatibilitu time = %f\n",((
double) (clock() - start))/CLOCKS_PER_SEC);
541 const std::vector<double> &x,
int maxit_outer,
542 double K,
int method,
int nneighbor,
543 int compatibility_method,
int max_recursion,
544 double angle_param,
double angle) {
563 double step0 = 0.1, start = 0.0;
567 std::vector<pedge> edges;
570 for (i = 0; i < ne; i++){
596 for (k = 0; k < maxit_outer; k++){
597 for (i = 0; i < ne; i++){
610 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, size_t 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