28static double norm(
int n,
const double *x) {
29 return sqrt(std::inner_product(x, x + n, x, 0.0));
32static double sqr_dist(
int dim,
const double *x,
const double *y) {
35 for (i = 0; i <
dim; i++) res += (x[i] - y[i])*(x[i] - y[i]);
39static double dist(
int dim,
const double *x,
const double *y) {
65 e.
wgts = std::vector<double>(np - 1, wgt);
75 double dist1,
dist2, len1, len2;
79 const double *u1 = e1.
x.data();
81 const double *u2 = e2.
x.data();
92 dist1 = std::max(0.1, dist1 / (len1 + len2 + 0.0001 * dist1));
105 double dist1,
dist2, len1, len2,
len;
106 double tmp, ca, cp, cs;
108 bool flipped =
false;
110 const double *u1 = e1.
x.data();
112 const double *u2 = e2.
x.data();
122 len = 0.5*(len1+len2);
126 for (i = 0; i <
dim; i++)
127 ca += (v1[i]-u1[i])*(v2[i]-u2[i]);
128 ca = fabs(ca/(len1*len2));
132 cs = 2 / (std::max(len1, len2) /
len +
len / std::min(len1, len2));
133 assert(cs > -0.001 && cs < 1.001);
137 for (i = 0; i <
dim; i++) {
138 tmp = .5*(v1[i]+u1[i])-.5*(v2[i]+u2[i]);
143 assert(cp > -0.001 && cp < 1.001);
156 fprintf(fp,
"#%02x%02x%02x%02x", r, g, b,
alpha);
162 fprintf(fp,
"strict graph{\n");
164 for (
int i = 0; i < ne; i++){
166 const std::vector<double> &x =
edge.
x;
169 const int sto =
edge.npoints - 1;
171 fprintf(fp,
"%d [pos=\"", i);
172 for (
int k = 0; k <
dim; k++) {
173 if (k != 0) fprintf(fp,
",");
174 fprintf(fp,
"%f", x[sta*
dim+k]);
176 fprintf(fp,
"\"];\n");
178 fprintf(fp,
"%d [pos=\"", i + ne);
179 for (
int k = 0; k <
dim; k++) {
180 if (k != 0) fprintf(fp,
",");
181 fprintf(fp,
"%f", x[sto*
dim+k]);
183 fprintf(fp,
"\"];\n");
188 for (
int i = 0; i < ne; i++){
190 if (!
edge.wgts.empty()) {
191 for (
int j = 0; j <
edge.npoints - 1; j++) {
192 maxwgt = std::max(maxwgt,
edge.wgts[j]);
198 for (
int i = 0; i < ne; i++){
199 fprintf(fp,
"%d -- %d [pos=\"", i, i + ne);
201 const std::vector<double> &x =
edge.
x;
204 for (
int j = 0; j <
edge.npoints; j++) {
211 const double tt1[] = {0.15, 0.5, 0.85};
212 const double tt2[] = {0.15, 0.4, 0.6, 0.85};
213 if (j == 1 || j ==
edge.npoints - 1) {
220 for (
int kk = 1; kk <= mm; kk++){
221 const double t = tt[kk - 1];
222 for (
int k = 0; k <
dim; k++) {
223 if (k != 0) fprintf(fp,
",");
224 fprintf(fp,
"%f", x[(j - 1) *
dim + k] * (1 - t) + x[j *
dim + k] * t);
229 if (j == 0 || j ==
edge.npoints - 1){
230 for (
int k = 0; k <
dim; k++) {
231 if (k != 0) fprintf(fp,
",");
232 fprintf(fp,
"%f", x[j*
dim+k]);
237 if (!
edge.wgts.empty()) {
238 fprintf(fp,
"\", wgts=\"");
239 for (
int j = 0; j <
edge.npoints - 1; j++){
240 if (j != 0) fprintf(fp,
",");
241 fprintf(fp,
"%f",
edge.wgts[j]);
244 double len_total0 = 0;
245 fprintf(fp,
"\", color=\"");
246 for (
int j = 0; j <
edge.npoints - 1; j++){
249 for (k = 0; k <
dim; k++){
255 for (
int j = 0; j <
edge.npoints - 1; j++) {
258 for (k = 0; k <
dim; k++){
262 const double t =
edge.wgts[j] / maxwgt;
264 const int r = 255 * t;
266 const int b = 255 * (1 - t);
267 if (j != 0) fprintf(fp,
":");
269 if (j <
edge.npoints - 2) fprintf(fp,
";%f",
len / len_total0);
273 fprintf(fp,
"\"];\n");
280static void pedge_print(
char *comments,
const pedge &e) {
283 fprintf(stderr,
"%s", comments);
284 for (i = 0; i < e.
npoints; i++){
285 if (i > 0) fprintf(stderr,
",");
287 for (j = 0; j <
dim; j++){
288 if (j > 0) fprintf(stderr,
",");
289 fprintf(stderr,
"%f", e.
x[
dim * i + j]);
293 fprintf(stderr,
"\n");
301 e.
x.resize(e.
dim * n, 0);
302 if (e.
wgts.empty()) {
303 e.
wgts.resize(n - 1);
304 for (i = 0; i < e.
npoints; i++)
307 e.
wgts.resize(n - 1);
317 assert(npoints >= 2);
318 if (npoints*2-1 >
len){
322 std::vector<double> &x = e.
x;
324 for (i = npoints - 1; i >= 0; i--){
326 for (j = 0; j <
dim; j++){
327 x[
dim*ii + j] = x[
dim*i + j];
331 for (i = 0; i < npoints - 1; i++){
334 for (j = 0; j <
dim; j++){
335 x[
dim*(2*i + 1) + j] = 0.5*(x[
dim*ii + j] + x[
dim*ii2 + j]);
344 const std::vector<double> &x = e.
x;
353 for (i = 1; i <= np - 2; i++){
356 for (j = 0; j <
dim; j++) force[i*
dim + j] +=
s*(x[
left*
dim + j] - x[i*
dim + j]);
362 const pedge &e2, std::vector<double> &force) {
364 const std::vector<double> &x1 = e1.
x, &x2 = e2.
x;
375 const double s = similarity * edge_length;
376 for (
int i = 1; i <= np - 2; i++){
379 const double ss =
s / (
dist + 0.1 * edge_length * sqrt(
dist));
380 for (
int j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[i*
dim + j] - x1[i*
dim + j]);
383 const double s = -similarity * edge_length;
384 for (
int i = 1; i <= np - 2; i++){
387 const double ss =
s / (
dist + 0.1 * edge_length * sqrt(
dist));
388 for (
int j = 0; j <
dim; j++) force[i*
dim + j] += ss*(x2[(np - 1 - i)*
dim + j] - x1[i*
dim + j]);
395 std::vector<pedge> &edges,
int maxit,
396 double step0,
double K) {
397 int i, j, ne =
A->
n, k;
398 int *ia =
A->ia, *ja =
A->ja, iter = 0;
399 double *a = (
double*)
A->a;
400 const int np = edges[0].npoints,
dim = edges[0].dim;
402 double fnorm_a, fnorm_t, edge_length, start;
405 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);
407 std::vector<double> force_t(
dim * np);
408 std::vector<double> force_a(
dim * np);
409 while (step > 0.001 && iter <
maxit){
412 for (i = 0; i < ne; i++){
413 for (j = 0; j <
dim*np; j++) {
417 pedge &e1 = edges[i];
418 std::vector<double> &x = e1.
x;
420 for (j = ia[i]; j < ia[i+1]; j++){
421 const pedge &e2 = edges[ja[j]];
428 for (j = 1; j <= np - 2; j++){
429 for (k = 0; k <
dim; k++) {
430 x[j *
dim + k] += step * edge_length
431 * (force_t[j *
dim + k] + K * force_a[j *
dim+k])
432 / hypot(fnorm_t, K * fnorm_a);
439 fprintf(stderr,
"iter ==== %d cpu = %f npoints = %d\n", iter, (
double)(clock() - start) / CLOCKS_PER_SEC, np - 2);
444 std::vector<pedge> &edges,
445 double angle_param,
double angle) {
446 int *assignment =
NULL, nclusters;
448 int *clusterp, *clusters;
462 if (
Verbose > 1) fprintf(stderr,
"there are %d clusters, modularity = %f\n",nclusters, modularity);
466 for (i = 0; i < ne; i++){
475 for (i = 0; i < nclusters; i++) {
476 ink1 =
ink(edges, clusterp[i + 1] - clusterp[i], &clusters[clusterp[i]],
477 &ink0, &meet1, &meet2, angle_param, angle);
479 fprintf(stderr,
"nedges = %d ink0 = %f, ink1 = %f\n",clusterp[i+1] - clusterp[i], ink0,
ink1);
481 for (j = clusterp[i]; j < clusterp[i+1]; j++){
485 pedge &e = edges[clusters[j]];
486 e.
x[1 *
dim] = meet1.
x;
487 e.
x[1 *
dim + 1] = meet1.
y;
488 e.
x[2 *
dim] = meet2.
x;
489 e.
x[2 *
dim + 1] = meet2.
y;
491 e.
x[3 *
dim + 1] = e.
x[4 *
dim + 1];
500 const std::vector<pedge> &edges,
501 int compatibility_method,
double tol) {
504 int *ia, *ja, i, j, jj;
509 ia =
A->ia; ja =
A->ja;
511 for (i = 0; i < ne; i++){
512 for (j = ia[i]; j < ia[i+1]; j++){
514 if (i == jj)
continue;
531 fprintf(stderr,
"edge compatibilitu time = %f\n",((
double) (clock() - start))/CLOCKS_PER_SEC);
536 const std::vector<double> &x,
int maxit_outer,
537 double K,
int method,
int nneighbor,
538 int compatibility_method,
int max_recursion,
539 double angle_param,
double angle) {
558 double step0 = 0.1, start = 0.0;
562 std::vector<pedge> edges;
565 for (i = 0; i < ne; i++){
591 for (k = 0; k < maxit_outer; k++){
592 for (i = 0; i < ne; i++){
605 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