29static double norm(
int n,
const double *x) {
30 return sqrt(std::inner_product(x, x + n, x, 0.0));
33static double sqr_dist(
int dim,
const double *x,
const double *y) {
36 for (i = 0; i <
dim; i++) res += (x[i] - y[i])*(x[i] - y[i]);
40static double dist(
int dim,
const double *x,
const double *y) {
66 e.
wgts = std::vector<double>(np - 1, wgt);
76 double dist1,
dist2, len1, len2;
80 const double *u1 = e1.
x.data();
82 const double *u2 = e2.
x.data();
93 dist1 = std::max(0.1, dist1 / (len1 + len2 + 0.0001 * dist1));
106 double dist1,
dist2, len1, len2,
len;
107 double tmp, ca, cp, cs;
109 bool flipped =
false;
111 const double *u1 = e1.
x.data();
113 const double *u2 = e2.
x.data();
123 len = 0.5*(len1+len2);
127 for (i = 0; i <
dim; i++)
128 ca += (v1[i]-u1[i])*(v2[i]-u2[i]);
129 ca = fabs(ca/(len1*len2));
133 cs = 2 / (std::max(len1, len2) /
len +
len / std::min(len1, len2));
134 assert(cs > -0.001 && cs < 1.001);
138 for (i = 0; i <
dim; i++) {
139 tmp = .5*(v1[i]+u1[i])-.5*(v2[i]+u2[i]);
144 assert(cp > -0.001 && cp < 1.001);
157 fprintf(fp,
"#%02x%02x%02x%02x", r, g, b,
alpha);
163 fprintf(fp,
"strict graph{\n");
165 for (
int i = 0; i < ne; i++){
167 const std::vector<double> &x =
edge.
x;
170 const int sto =
edge.npoints - 1;
172 fprintf(fp,
"%d [pos=\"", i);
173 for (
int k = 0; k <
dim; k++) {
174 if (k != 0) fprintf(fp,
",");
175 fprintf(fp,
"%f", x[sta*
dim+k]);
177 fprintf(fp,
"\"];\n");
179 fprintf(fp,
"%d [pos=\"", i + ne);
180 for (
int k = 0; k <
dim; k++) {
181 if (k != 0) fprintf(fp,
",");
182 fprintf(fp,
"%f", x[sto*
dim+k]);
184 fprintf(fp,
"\"];\n");
189 for (
int i = 0; i < ne; i++){
191 if (!
edge.wgts.empty()) {
192 for (
int j = 0; j <
edge.npoints - 1; j++) {
193 maxwgt = std::max(maxwgt,
edge.wgts[j]);
199 for (
int i = 0; i < ne; i++){
200 fprintf(fp,
"%d -- %d [pos=\"", i, i + ne);
202 const std::vector<double> &x =
edge.
x;
205 for (
int j = 0; j <
edge.npoints; j++) {
212 const double tt1[] = {0.15, 0.5, 0.85};
213 const double tt2[] = {0.15, 0.4, 0.6, 0.85};
214 if (j == 1 || j ==
edge.npoints - 1) {
221 for (
int kk = 1; kk <= mm; kk++){
222 const double t = tt[kk - 1];
223 for (
int k = 0; k <
dim; k++) {
224 if (k != 0) fprintf(fp,
",");
225 fprintf(fp,
"%f", x[(j - 1) *
dim + k] * (1 - t) + x[j *
dim + k] * t);
230 if (j == 0 || j ==
edge.npoints - 1){
231 for (
int k = 0; k <
dim; k++) {
232 if (k != 0) fprintf(fp,
",");
233 fprintf(fp,
"%f", x[j*
dim+k]);
238 if (!
edge.wgts.empty()) {
239 fprintf(fp,
"\", wgts=\"");
240 for (
int j = 0; j <
edge.npoints - 1; j++){
241 if (j != 0) fprintf(fp,
",");
242 fprintf(fp,
"%f",
edge.wgts[j]);
245 double len_total0 = 0;
246 fprintf(fp,
"\", color=\"");
247 for (
int j = 0; j <
edge.npoints - 1; j++){
250 for (k = 0; k <
dim; k++){
256 for (
int j = 0; j <
edge.npoints - 1; j++) {
259 for (k = 0; k <
dim; k++){
263 const double t =
edge.wgts[j] / maxwgt;
265 const int r = 255 * t;
267 const int b = 255 * (1 - t);
268 if (j != 0) fprintf(fp,
":");
270 if (j <
edge.npoints - 2) fprintf(fp,
";%f",
len / len_total0);
274 fprintf(fp,
"\"];\n");
281static void pedge_print(
char *comments,
const pedge &e) {
284 fprintf(stderr,
"%s", comments);
285 for (i = 0; i < e.
npoints; i++){
286 if (i > 0) fprintf(stderr,
",");
288 for (j = 0; j <
dim; j++){
289 if (j > 0) fprintf(stderr,
",");
290 fprintf(stderr,
"%f", e.
x[
dim * i + j]);
294 fprintf(stderr,
"\n");
302 e.
x.resize(e.
dim * n, 0);
303 if (e.
wgts.empty()) {
304 e.
wgts.resize(n - 1);
305 for (i = 0; i < e.
npoints; i++)
308 e.
wgts.resize(n - 1);
318 assert(npoints >= 2);
319 if (npoints*2-1 >
len){
323 std::vector<double> &x = e.
x;
325 for (i = npoints - 1; i >= 0; i--){
327 for (j = 0; j <
dim; j++){
328 x[
dim*ii + j] = x[
dim*i + j];
332 for (i = 0; i < npoints - 1; i++){
335 for (j = 0; j <
dim; j++){
336 x[
dim*(2*i + 1) + j] = 0.5*(x[
dim*ii + j] + x[
dim*ii2 + j]);
345 const std::vector<double> &x = e.
x;
354 for (i = 1; i <= np - 2; i++){
357 for (j = 0; j <
dim; j++) force[i*
dim + j] +=
s*(x[
left*
dim + j] - x[i*
dim + j]);
363 const pedge &e2, std::vector<double> &force) {
365 const std::vector<double> &x1 = e1.
x, &x2 = e2.
x;
376 const double s = similarity * edge_length;
377 for (
int i = 1; i <= np - 2; i++){
380 const double ss =
s / (
dist + 0.1 * edge_length * sqrt(
dist));
381 for (
int j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[i*
dim + j] - x1[i*
dim + j]);
384 const double s = -similarity * edge_length;
385 for (
int i = 1; i <= np - 2; i++){
388 const double ss =
s / (
dist + 0.1 * edge_length * sqrt(
dist));
389 for (
int j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[(np - 1 - i)*
dim + j] - x1[i*
dim + j]);
396 std::vector<pedge> &edges,
int maxit,
397 double step0,
double K) {
398 int i, j, ne =
A->
n, k;
399 int *ia =
A->ia, *ja =
A->ja, iter = 0;
400 double *a = (
double*)
A->a;
401 const int np = edges[0].npoints,
dim = edges[0].dim;
403 double fnorm_a, fnorm_t, edge_length, start;
406 fprintf(stderr,
"total interaction pairs = %" PRISIZE_T
407 " out of %d, avg neighbors per edge = %f\n",
A->nz,
A->m *
A->m,
408 (
double)
A->nz /
A->m);
410 std::vector<double> force_t(
dim * np);
411 std::vector<double> force_a(
dim * np);
412 while (step > 0.001 && iter <
maxit){
415 for (i = 0; i < ne; i++){
416 for (j = 0; j <
dim*np; j++) {
420 pedge &e1 = edges[i];
421 std::vector<double> &x = e1.
x;
423 for (j = ia[i]; j < ia[i+1]; j++){
424 const pedge &e2 = edges[ja[j]];
431 for (j = 1; j <= np - 2; j++){
432 for (k = 0; k <
dim; k++) {
433 x[j *
dim + k] += step * edge_length
434 * (force_t[j *
dim + k] + K * force_a[j *
dim+k])
435 / hypot(fnorm_t, K * fnorm_a);
442 fprintf(stderr,
"iter ==== %d cpu = %f npoints = %d\n", iter, (
double)(clock() - start) / CLOCKS_PER_SEC, np - 2);
447 std::vector<pedge> &edges,
448 double angle_param,
double angle) {
449 int *assignment =
NULL, nclusters;
451 int *clusterp, *clusters;
465 if (
Verbose > 1) fprintf(stderr,
"there are %d clusters, modularity = %f\n",nclusters, modularity);
469 for (i = 0; i < ne; i++){
478 for (i = 0; i < nclusters; i++) {
479 ink1 =
ink(edges, clusterp[i + 1] - clusterp[i], &clusters[clusterp[i]],
480 &ink0, &meet1, &meet2, angle_param, angle);
482 fprintf(stderr,
"nedges = %d ink0 = %f, ink1 = %f\n",clusterp[i+1] - clusterp[i], ink0,
ink1);
484 for (j = clusterp[i]; j < clusterp[i+1]; j++){
488 pedge &e = edges[clusters[j]];
489 e.
x[1 *
dim] = meet1.
x;
490 e.
x[1 *
dim + 1] = meet1.
y;
491 e.
x[2 *
dim] = meet2.
x;
492 e.
x[2 *
dim + 1] = meet2.
y;
494 e.
x[3 *
dim + 1] = e.
x[4 *
dim + 1];
503 const std::vector<pedge> &edges,
504 int compatibility_method,
double tol) {
507 int *ia, *ja, i, j, jj;
512 ia =
A->ia; ja =
A->ja;
514 for (i = 0; i < ne; i++){
515 for (j = ia[i]; j < ia[i+1]; j++){
517 if (i == jj)
continue;
534 fprintf(stderr,
"edge compatibilitu time = %f\n",((
double) (clock() - start))/CLOCKS_PER_SEC);
539 const std::vector<double> &x,
int maxit_outer,
540 double K,
int method,
int nneighbor,
541 int compatibility_method,
int max_recursion,
542 double angle_param,
double angle) {
561 double step0 = 0.1, start = 0.0;
565 std::vector<pedge> edges;
568 for (i = 0; i < ne; i++){
594 for (k = 0; k < maxit_outer; k++){
595 for (i = 0; i < ne; i++){
608 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