struct arc *leaf_to_arc (struct leaf *lf) { int j, k; struct arc *a; struct vertex *v; struct cept *ex; a = allocate_arc (); if (a == NULL) { add_object (tail_cept, ARC, "leaf arc"); add_function (tail_cept, "leaf_to_arc"); add_source (tail_cept, "msrender.c"); return(NULL); } a -> cir = lf -> cir; for (j = 0; j < 2; j++) { v = allocate_vertex (); if (error()) { add_object (tail_cept, VERTEX, "leaf vertex"); add_function (tail_cept, "leaf_to_arc"); add_source (tail_cept, "msrender.c"); return(NULL); } for (k = 0; k < 3; k++) v -> center[k] = lf -> ends[j][k]; a -> vtx[j] = v; } return (a); }
int subdivide_leaf (double fine_pixel, struct leaf *lf, double **pntptr) { int j, k, n; double alpha; struct arc *lfarc; struct vertex *lfvtx[2]; struct cept *ex; if (lf -> cir -> radius <= 0.0) { return (0); } lfarc = allocate_arc (); if (lfarc == NULL) { add_object (tail_cept, ARC, "leaf arc"); add_function (tail_cept, "subdivide_leaf"); add_source (tail_cept, "msrender.c"); return (0); } for (j = 0; j < 2; j++) { lfvtx[j] = allocate_vertex (); if (error()) { add_object (tail_cept, VERTEX, "leaf vertex"); add_function (tail_cept, "subdivide_leaf"); add_source (tail_cept, "msrender.c"); return (0); } } /* convert to arc for subdivision */ lfarc -> cir = lf -> cir; for (k = 0; k < 3; k++) { lfvtx[0] -> center[k] = lf -> ends[0][k]; lfvtx[1] -> center[k] = lf -> ends[1][k]; } for (j = 0; j < 2; j++) lfarc -> vtx[j] = lfvtx[j]; /* angular parameter for subdivision */ alpha = fine_pixel / (lf -> cir -> radius); /* call geometric subdivision function */ n = render_sub_arc (lfarc, pntptr, alpha); if (error()) return(0); free_arc (lfarc); for (j = 0; j < 2; j++) free_vertex (lfvtx[j]); return (n); /* return number of subdivision points */ }
Graph::vertex_t Graph::add_vertex(Point pt) { vertex_t p; auto v = vdesc.find(pt); // check if pt is already present if (v == vdesc.end()) { // point not present vdesc.insert(std::make_pair(pt, nvertex)); // insert in descriptor list vertices.push_back(pt); p = nvertex; allocate_vertex(); // increments nvertex } else { p = v->second; } return p; }
void slice_elbow (struct msscene *ms, struct surface *current_surface, double fine_pixel, struct face *fac) { char message[MAXLINE]; struct leaf *lf; struct circle *lfcir, *torcir; struct arc *torarc; struct vertex *torvtx[2]; int i, j, k, nfocus, atmnum; double anginc, bigrad; double atmcen[3], ccens[2][3], cradii[2]; double focus[3], vect[3], z_axis[3], base[3]; struct variety *vty; double *foci; struct cept *ex; vty = fac -> vty; atmnum = vty -> atmnum[0]; if (debug >= 2) { sprintf (message,"render elbow face for atom %5d", atmnum); inform(message); } for (k = 0; k < 3; k++) atmcen[k] = *(current_surface -> atom_centers + 3 * (atmnum - 1) + k); for (j = 0; j < 2; j++) cradii[j] = vty -> radii[1]; for (j = 0; j < 2; j++) for (k = 0; k < 3; k++) { ccens[j][k] = vty -> ccens[j][k]; } bigrad = distance (atmcen, ccens[0]) + cradii[0]; for (k = 0; k < 3; k++) { if (atmcen[k] + bigrad < ms -> window[0][k]) return; if (atmcen[k] - bigrad > ms -> window[1][k]) return; } /* leaf circle */ lfcir = allocate_circle (); if (lfcir == NULL) { ex = new_cept (MEMORY_ERROR, ALLOCATION, FATAL_SEVERITY); add_object (ex, CIRCLE, "leaf circle"); add_function (ex, "slice_elbow"); add_source (ex, "msrender.c"); return; } /* leaf */ lf = allocate_leaf (); if (lf == NULL) { add_object (tail_cept, LEAF, "leaf"); add_function (tail_cept, "slice_sphere"); return; } /* torus circle radius, center, axis */ torcir = new_circle (vty -> center, vty -> radii[0], vty -> axis); if (torcir == NULL) { add_object (tail_cept, CIRCLE, "torus circle"); add_function (tail_cept, "slice_elbow"); return; } /* torus arc */ torarc = allocate_arc (); if (torarc == NULL) { add_object (tail_cept, ARC, "torus arc"); add_function (tail_cept, "slice_elbow"); add_source (tail_cept, "msrender.c"); return; } for (j = 0; j < 2; j++) { torvtx[j] = allocate_vertex (); if (error()) { add_object (tail_cept, VERTEX, "torus vertex"); add_function (tail_cept, "slice_elbow"); add_source (tail_cept, "msrender.c"); return; } } /* set up leaf fields */ for (k = 0; k < MAXPA; k++) lf -> atmnum[k] = vty -> atmnum[k]; lf -> cir = lfcir; lf -> shape = CONVEX; /* to avoid reversing normal vector */ lf -> type = vty -> type; lf -> fac = fac; lf -> cep = 0; lf -> clip_ep = 0; lf -> side = OUTSIDE; lf -> comp = fac -> comp; lf -> input_hue = fac -> input_hue; for (j = 0; j < 2; j++) lf -> where[j] = ACCESSIBLE; /* setup torus central circle for subdivision */ anginc = fine_pixel / ((vty -> radii[0] + cradii[0])); torcir -> radius = vty -> radii[0]; for (k = 0; k < 3; k++) { torcir -> center[k] = vty -> center[k]; torcir -> axis[k] = vty -> axis[k]; } torarc -> cir = torcir; for (j = 0; j < 2; j++) { torarc -> vtx[j] = torvtx[j]; for (k = 0; k < 3; k++) torvtx[j] -> center[k] = ccens[j][k]; } foci = (double *) NULL; nfocus = render_sub_arc (torarc, &foci, anginc); if (nfocus < 2) { ex = new_cept (LOGIC_ERROR, MSUNDERFLOW, FATAL_SEVERITY); add_function (ex, "slice_elbow"); add_source (ex, "msrender.c"); add_long (ex, "number of foci", (long) nfocus); return; } /* create leaves */ for (i = 0; i < nfocus; i++) { for (k = 0; k < 3; k++) { focus[k] = (*(foci + 3 * i + k)); lfcir -> center[k] = focus[k]; lf -> focus[k] = focus[k]; } lfcir -> radius = cradii[0]; /* compute tangent to torus central circle */ for (k = 0; k < 3; k++) vect[k] = focus[k] - vty -> center[k]; cross (vty -> axis, vect, lfcir -> axis); normalize (lfcir -> axis); for (k = 0; k < 3; k++) z_axis[k] = ((k == 2) ? 1.0 : 0.0); cross (lfcir -> axis, z_axis, base); if (norm (base) <= 0.0) { continue; } normalize (base); for (k = 0; k < 3; k++) { lf -> ends[0][k] = lfcir -> center[k] - lfcir -> radius * base[k]; lf -> ends[1][k] = lfcir -> center[k] + lfcir -> radius * base[k]; } /* clip and render leaf */ clip_leaf (ms, current_surface, fine_pixel, lf); if (error()) return; } free_doubles (foci, 0, VERTS); free_leaf (lf); free_arc (torarc); free_circle (torcir); free_circle (lfcir); for (j = 0; j < 2; j++) free_vertex (torvtx[j]); }
void slice_torus (struct msscene *ms, struct surface *current_surface, double fine_pixel, double probe_radius, struct face *fac) { int k, j, i, nfocus, near1, naif; double anginc, bigrad; double focus[3], vect1[3], vect2[3], vect[3], qvect[3]; double dtq, tcv[3]; double *foci = (double *) NULL; char message[MAXLINE]; struct leaf *lf; struct circle *cir1, *cir2, *cir3; struct circle *lfcir, *torcir; struct variety *vty, *atm1, *atm2; struct arc *a, *nxta; struct arc *torarc; struct vertex *torvtx[2]; struct vertex *qvtx; struct vertex *conevtx; struct cycle *cyc; struct edge *edg; struct cept *ex; vty = fac -> vty; if (vty -> type != TORUS) { ex = new_cept (GEOMETRY_ERROR, INCONSISTENCY, FATAL_SEVERITY); add_function (ex, "slice_torus"); add_source (ex, "msrender.c"); add_long (ex, "variety type", (long) vty -> type); return; } if (vty -> tube) { slice_elbow (ms, current_surface, fine_pixel, fac); return; } if (debug >= 2) { sprintf (message,"render saddle face for atoms %5d %5d", vty -> atmnum[0], vty -> atmnum[1]); inform(message); } /* get pointers to atom varieties */ atm1 = *(current_surface -> variety_handles + fac -> vty -> atmnum[0] - 1); atm2 = *(current_surface -> variety_handles + fac -> vty -> atmnum[1] - 1); /* check versus window */ bigrad = distance (atm1 -> center, atm2 -> center) + atm1 -> radii[0] + atm2 -> radii[0]; for (k = 0; k < 3; k++) { if (vty -> center[k] + bigrad < ms -> window[0][k]) return; if (vty -> center[k] - bigrad > ms -> window[1][k]) return; } /* leaf circle */ lfcir = allocate_circle (); if (error()) { add_object (tail_cept, CIRCLE, "leaf circle"); add_function (tail_cept, "slice_torus"); return; } /* leaf */ lf = allocate_leaf (); if (error()) { add_object (tail_cept, LEAF, "leaf"); add_function (tail_cept, "slice_sphere"); return; } /* torus circle radius, center, axis */ torcir = new_circle (vty -> center, vty -> radii[0], vty -> axis); if (torcir == NULL) { add_object (tail_cept, CIRCLE, "torus circle"); add_function (tail_cept, "slice_circle"); return; } /* torus arc */ torarc = allocate_arc (); if (error()) { add_object (tail_cept, ARC, "torus arc"); add_function (tail_cept, "slice_torus"); add_source (tail_cept, "msrender.c"); return; } for (j = 0; j < 2; j++) { torvtx[j] = allocate_vertex (); if (error()) { add_object (tail_cept, VERTEX, "torus vertex"); add_function (tail_cept, "slice_torus"); add_source (tail_cept, "msrender.c"); return; } } torarc -> cir = torcir; /* copy atom numbers from variety to leaf */ for (k = 0; k < MAXPA; k++) lf -> atmnum[k] = fac -> vty -> atmnum[k]; /* set up leaf fields */ lf -> cir = lfcir; lf -> shape = fac -> shape; lf -> type = fac -> vty -> type; lf -> fac = fac; lf -> cep = 0; lf -> clip_ep = 0; lf -> side = OUTSIDE; lf -> comp = fac -> comp; lf -> input_hue = fac -> input_hue; /* both endpoints of saddle face leaf are always accessible */ for (j = 0; j < 2; j++) lf -> where[j] = ACCESSIBLE; /* angular increment for rotation of leaf about torus axis */ anginc = fine_pixel / (vty -> radii[0]); /* next we need endpoints for torus arc */ /* get them from concave arcs bounding saddle face */ /* intialization */ cir1 = NULL; cir2 = NULL; cir3 = NULL; qvtx = NULL; conevtx = NULL; near1 = 0; /* look for concave arcs */ naif = 0; for (cyc = fac -> first_cycle; cyc != NULL; cyc = cyc -> next) for (edg = cyc -> first_edge; edg != NULL; edg = edg -> next) { naif++; a = edg -> arcptr; if (a -> shape == CONVEX) { cir3 = a -> cir; continue; } if (edg -> next == NULL) nxta = cyc -> first_edge -> arcptr; else nxta = edg -> next -> arcptr; if (along (edg, vty -> axis)) cir2 = a -> cir; else cir1 = a -> cir; /* check for cusp vertex */ if (a -> shape == CONCAVE && nxta -> shape == CONCAVE) { /* cusp point joints two concave arcs */ qvtx = a -> vtx[1-edg->orn]; } } dtq = probe_radius * probe_radius - vty -> radii[0] * vty -> radii[0]; /* later: note: check PI in bubbles */ if (naif == 1) { if (dtq <= 0.0) { ex = new_cept (GEOMETRY_ERROR, INCONSISTENCY, FATAL_SEVERITY); add_function (ex, "slice_torus"); add_source (ex, "msrender.c"); add_message(ex, "toroidal face with only one arc, but not cone"); return; } if (cir3 == NULL) { ex = new_cept (GEOMETRY_ERROR, INCONSISTENCY, FATAL_SEVERITY); add_function (ex, "slice_torus"); add_source (ex, "msrender.c"); add_message(ex, "toroidal face with only one arc, but no contact circle"); return; } /* cone */ qvtx = allocate_vertex (); if (error()) { add_object (tail_cept, VERTEX, "CUSP VERTEX"); add_function (tail_cept, "slice_torus"); add_source (tail_cept, "msrender.c"); return; } conevtx = qvtx; dtq = sqrt (dtq); for (k = 0; k < 3; k++) tcv[k] = cir3 -> center[k] - torcir -> center[k]; normalize (tcv); for (k = 0; k < 3; k++) qvtx -> center[k] = torcir -> center[k] + dtq * tcv[k]; /* hope this is enough */ } if (cir1 == NULL) informd2 ("cir1 null"); if (cir2 == NULL) informd2 ("cir2 null"); if (qvtx != NULL) informd2 ("cusp present"); /* check for cusp vertex */ if (qvtx != NULL) { for (k = 0; k < 3; k++) qvect[k] = qvtx -> center[k] - vty -> center[k]; near1 = (dot_product (qvect, vty -> axis) < 0.0); } /* check for hoop saddle face */ if (cir1 == NULL || cir2 == NULL) { for (j = 0; j < 2; j++) torarc -> vtx[j] = NULL; informd2 ("complete toroidal hoop"); } else { /* concave arc circle centers are endpoints of sphere rolling */ for (k = 0; k < 3; k++) { torvtx[0] -> center[k] = cir1 -> center[k]; torvtx[1] -> center[k] = cir2 -> center[k]; } for (j = 0; j < 2; j++) torarc -> vtx[j] = torvtx[j]; sprintf (message, "saddle rendering (from): %8.3f %8.3f %8.3f", cir1 -> center[0], cir1 -> center[1], cir1 -> center[2]); informd2 (message); sprintf (message, "saddle rendering (to) : %8.3f %8.3f %8.3f", cir2 -> center[0], cir2 -> center[1], cir2 -> center[2]); informd2 (message); } /* the probe sphere centers are the foci of the leaves */ nfocus = render_sub_arc (torarc, &foci, anginc); if (nfocus < 2) { ex = new_cept (LOGIC_ERROR, MSUNDERFLOW, FATAL_SEVERITY); add_function (ex, "slice_torus"); add_source (ex, "msrender.c"); add_long (ex, "number of foci", (long) nfocus); return; } sprintf (message, "nfocus = %d", nfocus); informd2 (message); /* create leaves */ for (i = 0; i < nfocus; i++) { for (k = 0; k < 3; k++) { focus[k] = (*(foci + 3 * i + k)); lfcir -> center[k] = focus[k]; lf -> focus[k] = focus[k]; } /* unit vectors from focus toward atoms */ for (k = 0; k < 3; k++) { vect1[k] = atm1 -> center[k] - focus[k]; vect2[k] = atm2 -> center[k] - focus[k]; } /* correct for cusp vertex */ if (qvtx != NULL) { if (near1) for (k = 0; k < 3; k++) vect2[k] = qvtx -> center[k] - focus[k]; else for (k = 0; k < 3; k++) vect1[k] = qvtx -> center[k] - focus[k]; } /* normalize vectors to unit length */ normalize (vect1); normalize (vect2); /* leaf circle radius is probe radius */ lfcir -> radius = probe_radius; /* set up endpoints of leaf */ for (k = 0; k < 3; k++) { lf -> ends[0][k] = focus[k] + lfcir -> radius * vect1[k]; lf -> ends[1][k] = focus[k] + lfcir -> radius * vect2[k]; } /* compute leaf circle axis */ for (k = 0; k < 3; k++) vect[k] = focus[k] - vty -> center[k]; cross (vty -> axis, vect, lfcir -> axis); normalize (lfcir -> axis); /* clip and render leaf */ clip_leaf (ms, current_surface, fine_pixel, lf); if (error()) return; } /* return temporary memory */ if (!free_doubles (foci, 0, VERTS)) { ex = new_cept (MEMORY_ERROR, FREEING, FATAL_SEVERITY); add_variable (ex, VERTS, "foci"); add_function (ex, "slice_torus"); add_source (ex, "msrender.c"); return; } free_leaf (lf); free_arc (torarc); free_circle (torcir); free_circle (lfcir); for (j = 0; j < 2; j++) free_vertex (torvtx[j]); if (conevtx != NULL) free_vertex (conevtx); return; }
int *kpartition_greedy(int k, com_mat_t *com_mat, int n, int *constraints, int nb_constraints) { int *res = NULL, *best_res=NULL, *size = NULL; int i,j,nb_trials; int max_size, max_val; double cost, best_cost = -1; int start, end; int dumb_id, nb_dumb; for( nb_trials = 0 ; nb_trials < MAX_TRIALS ; nb_trials++ ){ res = (int *)MALLOC(sizeof(int)*n); for ( i = 0 ; i < n ; i ++ ) res[i] = -1; size = (int *)CALLOC(k,sizeof(int)); max_size = n/k; /*printf("Constraints: ");print_1D_tab(constraints,nb_constraints);*/ /* put "dumb" vertices in the correct partition if there are any*/ if (nb_constraints){ start = 0; dumb_id = n-1; for( i = 0 ; i < k ; i ++){ max_val = (i+1)* (n/k); end = start; while( end < nb_constraints){ if(constraints[end] >= max_val) break; end++; } /* now end - start is the number of constarints for the ith subtree hence the number of dumb vertices is the differences between the number of leaves of the subtree (n/k) and the number of constraints */ nb_dumb = n/k - (end-start); /*printf("max_val: %d, nb_dumb=%d, start=%d, end=%d, size=%d\n",max_val, nb_dumb, start, end, n/k);*/ /* dumb vertices are the one with highest indices: put them in the ith partitions*/ for( j = 0; j < nb_dumb; j ++ ){ res[dumb_id] = i; dumb_id--; } /* increase the size of the ith partition accordingly*/ size[i] += nb_dumb; start=end; } } /*printf("After dumb vertices mapping: ");print_1D_tab(res,n);*/ /* choose k initial "true" vertices at random and put them in a different partition */ for ( i = 0 ; i < k ; i ++ ){ /* if the partition is full of dumb vertices go to next partition*/ if(size[i] >= max_size) continue; /* find a vertex not allready partitionned*/ do{ /* call the mersenne twister PRNG of tm_mt.c*/ j = genrand_int32() % n; } while ( res[j] != -1 ); /* allocate and update size of partition*/ res[j] = i; /* printf("random: %d -> %d\n",j,i); */ size[i]++; } /* allocate each unaloacted vertices in the partition that maximize the communication*/ for( i = 0 ; i < n ; i ++) if( res[i] == -1) allocate_vertex(i, res, com_mat, n, size, max_size); cost = eval_cost(res,com_mat); /*print_1D_tab(res,n); printf("cost=%.2f\n",cost);*/ if((cost<best_cost) || (best_cost == -1)){ best_cost=cost; FREE(best_res); best_res=res; }else FREE(res); FREE(size); } /*print_1D_tab(best_res,n); printf("best_cost=%.2f\n",best_cost); */ return best_res; }