1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include "triangul.h"
#include <math.h>
#include "../../global.h"
//#ifndef HAVE_LOG2
double nlog2( double v )
{
static double tmp_log2 = -1.0;
if (tmp_log2 < 0.0) {
tmp_log2 = 1.0 / log(2.0);
}
return(log(v) * tmp_log2);
}
//#endif
static int choose_idx;
static int permute[SEGSIZE];
/* Generate a random permutation of the segments 1..n */
int generate_random_ordering(n)
int n;
{
int i;
int m, st[SEGSIZE], *p;
choose_idx = 1;
for (i = 0; i <= n; i++)
st[i] = i;
p = st;
for (i = 1; i <= n; i++, p++)
{
m = rand() % (n + 1 - i) + 1;
permute[i] = p[m];
if (m != 1)
p[m] = p[1];
}
return 0;
}
/* Return the next segment in the generated random ordering of all the */
/* segments in S */
int choose_segment()
{
#ifdef DEBUG
fprintf(stderr, "choose_segment: %d\n", permute[choose_idx]);
#endif
return permute[choose_idx++];
}
/* Read in the list of vertices from infile */
int read_segments(filename, genus)
char *filename;
int *genus;
{
FILE *infile;
int ccount;
int i;
int ncontours, npoints, first, last;
if ((infile = fopen(filename, "r")) == NULL)
{
perror(filename);
return -1;
}
fscanf(infile, "%d", &ncontours);
if (ncontours <= 0)
return -1;
/* For every contour, read in all the points for the contour. The */
/* outer-most contour is read in first (points specified in */
/* anti-clockwise order). Next, the inner contours are input in */
/* clockwise order */
ccount = 0;
i = 1;
while (ccount < ncontours)
{
int j;
fscanf(infile, "%d", &npoints);
first = i;
last = first + npoints - 1;
for (j = 0; j < npoints; j++, i++)
{
fscanf(infile, "%lf%lf", &seg[i].v0.x, &seg[i].v0.y);
if (i == last)
{
seg[i].next = first;
seg[i].prev = i-1;
seg[i-1].v1 = seg[i].v0;
}
else if (i == first)
{
seg[i].next = i+1;
seg[i].prev = last;
seg[last].v1 = seg[i].v0;
}
else
{
seg[i].prev = i-1;
seg[i].next = i+1;
seg[i-1].v1 = seg[i].v0;
}
seg[i].is_inserted = FALSE;
}
ccount++;
}
*genus = ncontours - 1;
return i-1;
}
/* Get log*n for given n */
int math_logstar_n(n)
int n;
{
int i;
double v;
for (i = 0, v = (double) n; v >= 1; i++)
v = nlog2(v);
return (i - 1);
}
int math_N(n, h)
int n;
int h;
{
int i;
double v;
for (i = 0, v = (int) n; i < h; i++)
v = nlog2(v);
return (int) ceil((double) 1.0*n/v);
}
|