16 #if ((defined(HAVE_GTS) || defined(HAVE_TRIANGLE)) && defined(SFDP))
34 int *ia = A->
ia, *ja = A->
ja;
36 real expandmax = 1.5, expandmin = 1;
41 for (i = 0; i < A->
m; i++){
42 for (j = ia[i]; j < ia[i+1]; j++){
44 if (jj == i)
continue;
46 dx =
ABS(x[i*dim] - x[jj*dim]);
47 dy =
ABS(x[i*dim+1] - x[jj*dim+1]);
48 wx = width[i*dim]+width[jj*dim];
49 wy = width[i*dim+1]+width[jj*dim+1];
51 ideal_distance[j] = sqrt(wx*wx+wy*wy);
59 t =
MIN(wx/dx, wy/dy);
61 if (t > 1) t =
MAX(t, 1.001);
62 *tmax =
MAX(*tmax, t);
63 *tmin =
MIN(*tmin, t);
64 t =
MIN(expandmax, t);
65 t =
MAX(expandmin, t);
67 ideal_distance[j] = t*
dist;
69 ideal_distance[j] = -t*
dist;
78 #define collide(i,j) ((ABS(x[(i)*dim] - x[(j)*dim]) < width[(i)*dim]+width[(j)*dim]) || (ABS(x[(i)*dim+1] - x[(j)*dim+1]) < width[(i)*dim+1]+width[(j)*dim+1]))
80 enum {INTV_OPEN, INTV_CLOSE};
82 struct scan_point_struct{
88 typedef struct scan_point_struct scan_point;
91 static int comp_scan_points(
const void *p,
const void *q){
92 scan_point *pp = (scan_point *) p;
93 scan_point *qq = (scan_point *) q;
96 }
else if (pp->x < qq->x){
99 if (pp->node > qq->node){
101 }
else if (pp->node < qq->node){
110 void NodeDest(
void* a) {
116 int NodeComp(
const void* a,
const void* b) {
117 return comp_scan_points(a,b);
121 void NodePrint(
const void* a) {
124 aa = (scan_point *) a;
125 fprintf(stderr,
"node {%d, %f, %d}\n", aa->node, aa->x, aa->status);
137 static SparseMatrix get_overlap_graph(
int dim,
int n,
real *x,
real *width,
int check_overlap_only){
139 scan_point *scanpointsx, *scanpointsy;
148 scanpointsx =
N_GNEW(2*n,scan_point);
149 for (i = 0; i < n; i++){
150 scanpointsx[2*i].node = i;
151 scanpointsx[2*i].x = x[i*dim] - width[i*dim];
152 scanpointsx[2*i].status = INTV_OPEN;
153 scanpointsx[2*i+1].node = i+n;
154 scanpointsx[2*i+1].x = x[i*dim] + width[i*dim];
155 scanpointsx[2*i+1].status = INTV_CLOSE;
157 qsort(scanpointsx, 2*n,
sizeof(scan_point), comp_scan_points);
159 scanpointsy =
N_GNEW(2*n,scan_point);
160 for (i = 0; i < n; i++){
161 scanpointsy[i].node = i;
162 scanpointsy[i].x = x[i*dim+1] - width[i*dim+1];
163 scanpointsy[i].status = INTV_OPEN;
164 scanpointsy[i+n].node = i;
165 scanpointsy[i+n].x = x[i*dim+1] + width[i*dim+1];
166 scanpointsy[i+n].status = INTV_CLOSE;
172 for (i = 0; i < 2*n; i++){
174 fprintf(stderr,
" k = %d node = %d x====%f\n",(scanpointsx[i].
node)%n, (scanpointsx[i].node), (scanpointsx[i].x));
177 k = (scanpointsx[i].node)%n;
180 if (scanpointsx[i].status == INTV_OPEN){
182 fprintf(stderr,
"inserting...");
189 fprintf(stderr,
"inserting2...");
190 treey->
PrintKey(&(scanpointsy[k+n]));
195 real bsta, bbsta, bsto, bbsto;
int ii;
197 assert(scanpointsx[i].node >= n);
199 newNode = newNode0 =
RBExactQuery(treey, &(scanpointsy[k + n]));
200 ii = ((scan_point *)newNode->
key)->node;
202 bsta = scanpointsy[ii].x; bsto = scanpointsy[ii+n].x;
205 fprintf(stderr,
"poping..%d....yinterval={%f,%f}\n", scanpointsy[k + n].node, bsta, bsto);
211 neighbor = (((scan_point *)newNode->
key)->node)%n;
212 bbsta = scanpointsy[neighbor].x; bbsto = scanpointsy[neighbor+n].x;
214 fprintf(stderr,
" predecessor is node %d y = %f\n", ((scan_point *)newNode->
key)->node, ((scan_point *)newNode->
key)->x);
217 if (
ABS(0.5*(bsta+bsto) - 0.5*(bbsta+bbsto)) < 0.5*(bsto-bsta) + 0.5*(bbsto-bbsta)){
220 fprintf(stderr,
"====================================== %d %d\n",k,neighbor);
222 if (check_overlap_only)
goto check_overlap_RETURN;
231 fprintf(stderr,
"deleteing...");
235 if (newNode0)
RBDelete(treey,newNode0);
238 if (newNode2 && newNode2 != treey->
nil && newNode2 != newNode0) {
241 fprintf(stderr,
"deleteing2...");
245 if (newNode0)
RBDelete(treey,newNode2);
251 check_overlap_RETURN:
260 if (
Verbose) fprintf(stderr,
"found %d clashes\n", A->
nz);
269 static void relative_position_constraints_delete(
void *d){
295 static void scale_coord(
int dim,
int m,
real *x,
real scale){
297 for (i = 0; i < dim*m; i++) {
313 real scale = -1, scale_best = -1;
315 int check_overlap_only = 1;
322 if (scale_sta <= 0) {
325 scale_coord(dim, m, x, scale_sta);
326 C = get_overlap_graph(dim, m, x, width, check_overlap_only);
327 if (!C || C->
nz == 0) {
328 if (
Verbose) fprintf(stderr,
" shrinking with %f works\n", scale_sta);
332 scale_coord(dim, m, x, 1./scale_sta);
337 if (scale_sta == 0) {
340 scale_sto = scale_sta;
342 scale_coord(dim, m, x, scale_sto);
345 scale_coord(dim, m, x, two);
346 C = get_overlap_graph(dim, m, x, width, check_overlap_only);
347 overlap = (C && C->
nz > 0);
350 scale_coord(dim, m, x, 1/scale_sto);
353 scale_best = scale_sto;
354 while (iter++ < maxiter && scale_sto - scale_sta > epsilon){
356 if (
Verbose) fprintf(stderr,
"in overlap_scaling iter=%d, maxiter=%d, scaling bracket: {%f,%f}\n", iter, maxiter, scale_sta, scale_sto);
358 scale = 0.5*(scale_sta + scale_sto);
359 scale_coord(dim, m, x, scale);
360 C = get_overlap_graph(dim, m, x, width, check_overlap_only);
361 scale_coord(dim, m, x, 1./scale);
362 overlap = (C && C->
nz > 0);
367 scale_best = scale_sto = scale;
372 scale_coord(dim, m, x, scale_best);
377 int dim,
real lambda0,
real *x,
real *width,
int include_original_graph,
int neighborhood_only,
378 real *max_overlap,
real *min_overlap,
379 int edge_labeling_scheme,
int n_constr_nodes,
int *constr_nodes,
SparseMatrix A_constr,
int shrink
382 int i, j, k, *iw, *jw, jdiag;
384 real *lambda, *d, *w, diag_d, diag_w,
dist;
390 if (constr_nodes && n_constr_nodes > 0 && edge_labeling_scheme !=
ELSCHEME_NONE){
392 sm->
data = relative_position_constraints_new(A_constr, edge_labeling_scheme, n_constr_nodes, constr_nodes);
402 for (i = 0; i < m; i++) sm->
lambda[i] = lambda0;
406 if (!neighborhood_only){
408 C = get_overlap_graph(dim, m, x, width, 0);
414 if (include_original_graph){
425 fp = fopen(
"/tmp/111",
"w");
431 if (!(sm->
Lw) || !(sm->
Lwd)) {
438 ideal_distance_avoid_overlap(dim, sm->
Lwd, x, width, (
real*) (sm->
Lwd->
a), max_overlap, min_overlap);
441 if (*max_overlap < 1 && shrink){
442 real scale_sta =
MIN(1, *max_overlap*1.0001), scale_sto = 1;
444 if (
Verbose) fprintf(stderr,
" no overlap (overlap = %f), rescale to shrink\n", *max_overlap - 1);
446 scale_sta =
overlap_scaling(dim, m, x, width, scale_sta, scale_sto, 0.0001, 15);
455 for (i = 0; i < m; i++){
458 for (j = iw[i]; j < iw[i+1]; j++){
465 w[j] = -100/d[j]/d[j];
479 lambda[i] *= (-diag_w);
482 w[jdiag] = -diag_w + lambda[i];
501 fp = fopen(
"/tmp/222",
"w");
516 if (
Verbose) fprintf(stderr,
"avg edge len=%f avg_label-size= %f\n", dist, avg_label_size);
521 for (i = 0; i < dim*A->
m; i++) x[i] *= dist;
524 static void print_bounding_box(
int n,
int dim,
real *x){
531 for (i = 0; i < dim; i++) xmin[i]=xmax[i] = x[i];
533 for (i = 0; i < n; i++){
534 for (k = 0; k < dim; k++){
535 xmin[k] =
MIN(xmin[k],x[i*dim+k]);
536 xmax[k] =
MAX(xmax[k],x[i*dim+k]);
539 fprintf(stderr,
"bounding box = \n");
540 for (i = 0; i < dim; i++) fprintf(stderr,
"{%f,%f}, ",xmin[i], xmax[i]);
541 fprintf(stderr,
"\n");
547 static int check_convergence(
real max_overlap,
real res,
int has_penalty_terms,
real epsilon){
548 if (!has_penalty_terms)
return (max_overlap <= 1);
549 return res < epsilon;
553 int edge_labeling_scheme,
int n_constr_nodes,
int *constr_nodes,
SparseMatrix A_constr,
int do_shrinking,
int *flag){
568 int include_original_graph = 0, i;
570 real avg_label_size, res = LARGE;
571 real max_overlap = 0, min_overlap = 999;
572 int neighborhood_only =
TRUE;
573 int has_penalty_terms =
FALSE;
574 real epsilon = 0.005;
585 if (!label_sizes)
return;
587 if (initial_scaling < 0) {
589 for (i = 0; i < A->
m; i++) avg_label_size += label_sizes[i*dim]+label_sizes[i*dim+1];
591 avg_label_size /= A->
m;
592 scale_to_edge_length(dim, A, x, -initial_scaling*avg_label_size);
593 }
else if (initial_scaling > 0){
594 scale_to_edge_length(dim, A, x, initial_scaling);
602 _statistics[0] = _statistics[1] = 0.;
604 fp = fopen(
"x1",
"w");
605 for (i = 0; i < A->
m; i++){
606 fprintf(fp,
"%f %f\n",x[i*2],x[i*2+1]);
614 fp = fopen(
"/tmp/m",
"wa");
618 has_penalty_terms = (edge_labeling_scheme !=
ELSCHEME_NONE && n_constr_nodes > 0);
619 for (i = 0; i < ntry; i++){
620 if (
Verbose) print_bounding_box(A->
m, dim, x);
621 sm =
OverlapSmoother_new(A, A->
m, dim, lambda, x, label_sizes, include_original_graph, neighborhood_only,
622 &max_overlap, &min_overlap, edge_labeling_scheme, n_constr_nodes, constr_nodes, A_constr, shrink);
623 if (
Verbose) fprintf(stderr,
"overlap removal neighbors only?= %d iter -- %d, overlap factor = %g underlap factor = %g\n", neighborhood_only, i, max_overlap - 1, min_overlap);
624 if (check_convergence(max_overlap, res, has_penalty_terms, epsilon)){
627 if (neighborhood_only ==
FALSE){
631 neighborhood_only =
FALSE;
if (do_shrinking) shrink = 1;
637 if (
Verbose) fprintf(stderr,
"res = %f\n",res);
639 if (i != 0) fprintf(fp,
",");
644 if (
Verbose) fprintf(stderr,
"overlap removal neighbors only?= %d iter -- %d, overlap factor = %g underlap factor = %g\n", neighborhood_only, i, max_overlap - 1, min_overlap);
652 if (has_penalty_terms){
659 fprintf(stderr,
" number of cg iter = %f, number of stress majorization iter = %f number of overlap removal try = %d\n",
660 _statistics[0], _statistics[1], i - 1);
663 fp = fopen(
"x2",
"w");
664 for (i = 0; i < A->
m; i++){
665 fprintf(fp,
"%f %f\n",x[i*2],x[i*2+1]);
673 fp = fopen(
"/tmp/m",
"w");
679 fprintf(stderr,
"post processing %f\n",((
real) (clock() - cpu)) / CLOCKS_PER_SEC);
692 agerr(
AGERR,
"remove_overlap: Graphviz not built with triangulation library\n");
void StressMajorizationSmoother_delete(StressMajorizationSmoother sm)
real OverlapSmoother_smooth(OverlapSmoother sm, int dim, real *x)
SparseMatrix SparseMatrix_copy(SparseMatrix A)
SparseMatrix SparseMatrix_new(int m, int n, int nz, int type, int format)
OverlapSmoother OverlapSmoother_new(SparseMatrix A, int m, int dim, real lambda0, real *x, real *width, int include_original_graph, int neighborhood_only, real *max_overlap, real *min_overlap, int edge_labeling_scheme, int n_constr_nodes, int *constr_nodes, SparseMatrix A_constr, int shrink)
void(* data_deallocator)(void *)
rb_red_blk_node * RBTreeInsert(rb_red_blk_tree *tree, void *key, void *info)
int agerr(agerrlevel_t level, const char *fmt,...)
real distance(real *x, int dim, int i, int j)
int SparseMatrix_is_symmetric(SparseMatrix A, int test_pattern_symmetry_only)
void(* PrintKey)(const void *a)
void OverlapSmoother_delete(OverlapSmoother sm)
void export_embedding(FILE *fp, int dim, SparseMatrix A, real *x, real *width)
void RBDelete(rb_red_blk_tree *tree, rb_red_blk_node *z)
real overlap_scaling(int dim, int m, real *x, real *width, real scale_sta, real scale_sto, real epsilon, int maxiter)
real average_edge_length(SparseMatrix A, int dim, real *coord)
real StressMajorizationSmoother_smooth(StressMajorizationSmoother sm, int dim, real *x, int maxit_sm, real tol)
SparseMatrix SparseMatrix_symmetrize(SparseMatrix A, int pattern_symmetric_only)
void SparseMatrix_delete(SparseMatrix A)
struct relative_position_constraints_struct * relative_position_constraints
SparseMatrix call_tri(int n, int dim, real *x)
EXTERN unsigned char Verbose
rb_red_blk_tree * RBTreeCreate(int(*CompFunc)(const void *, const void *), void(*DestFunc)(void *), void(*InfoDestFunc)(void *), void(*PrintFunc)(const void *), void(*PrintInfo)(void *))
Agnode_t * node(Agraph_t *g, char *name)
rb_red_blk_node * TreePredecessor(rb_red_blk_tree *tree, rb_red_blk_node *x)
void RBTreeDestroy(rb_red_blk_tree *tree)
rb_red_blk_node * RBExactQuery(rb_red_blk_tree *tree, void *q)
double dist(Site *s, Site *t)
#define OverlapSmoother_struct
SparseMatrix SparseMatrix_add(SparseMatrix A, SparseMatrix B)
SparseMatrix SparseMatrix_coordinate_form_add_entries(SparseMatrix A, int nentries, int *irn, int *jcn, void *val)
SparseMatrix SparseMatrix_from_coordinate_format(SparseMatrix A)
void remove_overlap(int dim, SparseMatrix A, int m, real *x, real *label_sizes, int ntry, real initial_scaling, int do_shrinking, int *flag)