aboutsummaryrefslogtreecommitdiffstats
path: root/test/ccured_olden/voronoi/newvor.c
blob: 9b5a440851a91eddb018ff5eb8a231f8bf398cb9 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* For copyright information, see olden_v1.0/COPYRIGHT */

#ifdef SS_PLAIN
#include "ssplain.h"
#else SS_PLAIN
#include <cm/cmmd.h>
#include <fcntl.h>
#endif SS_PLAIN

#define DEFINE_GLOBALS
#include "defines.h"
/* WARNING!  Don't use LOCAL on QUAD_EDGE as cache addresses are not aligned */

int flag;

QUAD_EDGE connect_left(a, b)
QUAD_EDGE a,b;
{
  VERTEX_PTR t1,t2;
  QUAD_EDGE ans,lnexta;

  t1=dest(a);
  lnexta=lnext(a); 
  t2=orig(b);
  ans = makeedge(t1,t2/*dest(a), orig(b)*/);
  splice(ans, lnexta);
  splice(sym(ans), b);
  return(ans);
}

QUAD_EDGE connect_right(a, b)
     QUAD_EDGE a,b;
{
  VERTEX_PTR t1,t2;
  QUAD_EDGE ans, oprevb;

  t1=dest(a);
  t2=orig(b);

  oprevb=oprev(b); 
  ans = makeedge(t1,t2);
  splice(ans, sym(a));
  splice(sym(ans), oprevb);
  return(ans);
}

/****************************************************************/
/*	Top-level Delaunay Triangulation Procedure              */
/****************************************************************/

QUAD_EDGE build_delaunay_triangulation(tree,extra)
    /* builds delaunay triangulation.
       va is an array of vertices, from 0 to size.  Each vertex consists of
       a vector and a data pointer.   edge is a pointer to an
       edge on the convex hull of the constructed delaunay triangulation. */

     VERTEX_PTR tree,extra;
{
    EDGE_PAIR retval;

    retval=build_delaunay(tree,extra);
    return retval.left;
}

VERTEX_PTR get_low(tree)
     VERTEX_PTR tree;
{
  VERTEX_PTR temp;
  while((temp=tree->left)) tree=temp;
  return tree;
}

/****************************************************************/
/*	Recursive Delaunay Triangulation Procedure              */
/*	Contains modifications for axis-switching division.     */
/****************************************************************/

EDGE_PAIR build_delaunay(tree,extra)
     VERTEX_PTR tree,extra;
{
    QUAD_EDGE a,b,c,ldo,rdi,ldi,rdo;
    EDGE_PAIR retval;
    VERTEX_PTR maxx, minx;
    VERTEX_PTR s1, s2, s3;

    EDGE_PAIR delleft, delright;

    if (tree && tree->right && tree->left) 
      {
        /* more than three elements; do recursion */
	minx = get_low(tree); maxx = extra;

	delright = build_delaunay(tree->right,extra);
	delleft = build_delaunay(tree->left, tree);
	ldo = delleft.left; ldi=delleft.right;
	rdi=delright.left; rdo=delright.right;

	retval=do_merge(ldo, ldi, rdi, rdo);
	ldo = retval.left;
	rdo = retval.right;
	while (orig(ldo) != minx) { ldo = rprev(ldo); }
	while (orig(rdo) != maxx) { rdo = lprev(rdo); }
	retval.left = ldo;
	retval.right = rdo;
    }
    else if (!tree)
      {
	printf("ERROR: Only 1 point!\n"); 
	exit(-1);
      }
    else if (!tree->left) 
      {	
	/* two points */
	a = makeedge(tree, extra);
	retval.left =  a;
	retval.right = sym(a);
      }
    else 
      { /*  tree->left, !tree->right  */	/* three points */
	/* 3 cases: triangles of 2 orientations, and 3 points on a line. */
	s1 = tree->left;
	s2 = tree;
	s3 = extra;
	a = makeedge(s1, s2);
	b = makeedge(s2, s3);
	splice(sym(a), b);
	c = connect_left(b, a);
	if (ccw(s1, s3, s2)) 
	  {
	    retval.left = sym(c);
	    retval.right = c;
	  }
	else 
	  {
	    retval.left =  a;
	    retval.right = sym(b);
	    if (!ccw(s1, s2, s3)) deleteedge(c);    /* colinear */
	  }
      }
    return retval;
}

/****************************************************************/
/*	Quad-edge storage allocation                            */
/****************************************************************/
QUAD_EDGE next_edge, avail_edge;
#ifndef NEW
#define NYL -1
#else
#define NYL NULL
#endif

void
free_edge(e)
     QUAD_EDGE e;
{
  e = (QUAD_EDGE) ((int) e ^ ((int) e & ANDF));
  onext(e) = avail_edge;
  avail_edge = e;
}

void
deleteedge(e)
     /*disconnects e from the rest of the structure and destroys it. */
     QUAD_EDGE e;
{
  QUAD_EDGE f;
  f=oprev(e);
  splice(e, f);
  f=oprev(sym(e));
  splice(sym(e),f);
  free_edge(e);
}

void
delete_all_edges() 
{ 
  next_edge= 0; 
  avail_edge = NYL;
}

QUAD_EDGE alloc_edge()
{
  QUAD_EDGE ans;

  if (avail_edge == NYL) 
    {
      ans = (QUAD_EDGE) malloc(4*ALLOC_SIZE);
#ifdef OUT
      if ((int) ans & ANDF) 
#else !OUT
      if (!(int) ans & 0xF)
#endif OUT
	{
	  printf("Aborting in alloc_edge, ans = 0x%x\n",(unsigned)ans);
	  exit(-1);
	}
    }
  else ans = (QUAD_EDGE) avail_edge, avail_edge = onext(avail_edge);
  return ans;
}

/****************************************************************/
/*	Geometric primitives                                    */
/****************************************************************/

BOOLEAN incircle(a,b,c,d)
     /* incircle, as in the Guibas-Stolfi paper. */
     VERTEX_PTR a,b,c,d;
{
  double adx, ady, bdx, bdy, cdx, cdy, dx, dy, anorm, bnorm, cnorm, dnorm;
  double dret ;

  dx = X(d); dy = Y(d); dnorm = NORM(d); 
  adx = X(a) - dx; ady = Y(a) - dy; anorm = NORM(a); 
  bdx = X(b) - dx; bdy = Y(b) - dy; bnorm = NORM(b);
  cdx = X(c) - dx; cdy = Y(c) - dy; cnorm = NORM(c); 
  dret =  (anorm - dnorm) * (bdx * cdy - bdy * cdx);
  dret += (bnorm - dnorm) * (cdx * ady - cdy * adx);
  dret += (cnorm - dnorm) * (adx * bdy - ady * bdx);
  return( (0.0 < dret) ? TRUE : FALSE );
}

BOOLEAN ccw(a,b,c)
     /* TRUE iff A, B, C form a counterclockwise oriented triangle */
     VERTEX_PTR a,b,c;
{
  double dret ;
  double xa,ya,xb,yb,xc,yc;
	
  xa=X(a); ya=Y(a);
  xb=X(b); yb=Y(b);
  xc=X(c); yc=Y(c);

  dret = (xa-xc)*(yb-yc) - (xb-xc)*(ya-yc);
  return( (dret  > 0.0)? TRUE : FALSE);
}

/****************************************************************/
/*	Quad-edge manipulation primitives                       */
/****************************************************************/
QUAD_EDGE makeedge(origin, destination)
     VERTEX_PTR origin, destination;
{
  QUAD_EDGE temp, ans;
  temp =  alloc_edge();
  ans = temp;
  
  onext(temp) = ans;
  orig(temp) = origin;
  temp = (QUAD_EDGE) ((int) temp+SIZE);
  onext(temp) = (QUAD_EDGE) ((int) ans + 3*SIZE);
  temp = (QUAD_EDGE) ((int) temp+SIZE);
  onext(temp) = (QUAD_EDGE) ((int) ans + 2*SIZE);
  orig(temp) = destination;
  temp = (QUAD_EDGE) ((int) temp+SIZE);
  onext(temp) = (QUAD_EDGE) ((int) ans + 1*SIZE);
  return(ans);
}

void
splice(a,b)
     QUAD_EDGE a, b;
{
  QUAD_EDGE alpha, beta, temp;
  QUAD_EDGE t1;
  
  alpha = rot(onext(a));
  beta = rot(onext(b));

  t1 = onext(beta);
  temp = onext(alpha);

  onext(alpha) = t1;  
  onext(beta) = temp;

  temp = onext(a); 
  t1 = onext(b); 
  onext(b) = temp; 
  onext(a) = t1;
}

void
swapedge(e)
     QUAD_EDGE e;
{
  QUAD_EDGE a,b,syme,lnexttmp;
  VERTEX_PTR a1,b1;
  
  a = oprev(e);
  syme = sym(e);
  b = oprev(syme); 
  splice(e, a);
  splice(syme, b);
  lnexttmp = lnext(a);
  splice(e, lnexttmp);
  lnexttmp = lnext(b);
  splice(syme, lnexttmp);
  a1 = dest(a);
  b1 = dest(b);
  orig(e) = a1;
  dest(e) = b1; 
}

/****************************************************************/
/*	The Merge Procedure.                                    */
/****************************************************************/

int valid(l,basel)
     QUAD_EDGE l,basel;
{
  VERTEX_PTR t1,t2,t3;

  t1=orig(basel);
  t3=dest(basel);

  t2=dest(l);
  return ccw(t1,t2,t3);
}

void dump_quad(ptr)
     QUAD_EDGE ptr;
{
  int i;
  QUAD_EDGE j;
  VERTEX_PTR v;

  ptr = (QUAD_EDGE) ((int) ptr & ~ANDF);
  chatting("Entered DUMP_QUAD: ptr=0x%x\n",ptr);
  for (i=0; i<4; i++)
   {
    j=onext(((QUAD_EDGE) (ptr+i)));
    v = orig(j);
    chatting("DUMP_QUAD: ptr=0x%x onext=0x%x,v=0x%x\n",ptr+i,j,v);
   }
}


EDGE_PAIR do_merge(ldo, ldi, rdi, rdo)
     QUAD_EDGE ldi, rdi, ldo, rdo;
{
  int rvalid, lvalid;
  QUAD_EDGE basel,lcand,rcand,t;
  VERTEX_PTR t1,t2,t3;
  
  while (TRUE) 
    {
      t3=orig(rdi);

      t1=orig(ldi);
      t2=dest(ldi);

      while (ccw(t1,t2,t3/*orig(ldi), dest(ldi), orig(rdi)*/)) 
	{
	  ldi = lnext(ldi);

	  t1=orig(ldi);
	  t2=dest(ldi);
	}

      t2=dest(rdi);

      if (ccw(t2,t3,t1/*dest(rdi), orig(rdi), orig(ldi)*/)) 
	{  
	  rdi = rprev(rdi); 
	}
      else 
	{ 
	  break; 
	}
    }
  
  basel = connect_left(sym(rdi), ldi);

  lcand = rprev(basel);
  rcand = oprev(basel);
  t1 = orig(basel);
  t2 = dest(basel);

  if (t1/*orig(basel)*/ == orig(rdo)) rdo = basel;
  if (t2/*dest(basel)*/ == orig(ldo)) ldo = sym(basel);
  
  while (TRUE) 
    {
      VERTEX_PTR v1,v2,v3,v4;

      /*chatting("valid site 1,lcand=0x%x,basel=0x%x\n",lcand,basel);*/
      /*dump_quad(lcand);*/
      t=onext(lcand);
      if (valid(t,basel)) 
	{
	  v4=orig(basel);

	  v1=dest(lcand);
	  v3=orig(lcand);

	  v2=dest(t);
	  while (incircle(v1,v2,v3,v4
			  /*dest(lcand), dest(t), orig(lcand), orig(basel)*/))
	    {
	      deleteedge(lcand);
	      lcand = t;

	      t = onext(lcand);
	      v1=dest(lcand);
	      v3=orig(lcand);

	      v2=dest(t);
	    }
	}
      
      /*chatting("valid site 2\n");*/
      t=oprev(rcand);
      if (valid(t,basel)) {
	v4=dest(basel);
	v1=dest(t);
	v2=dest(rcand);
	v3=orig(rcand);
	while (incircle(v1,v2,v3,v4
			/*dest(t), dest(rcand), orig(rcand), dest(basel)*/))
	  {
	    deleteedge(rcand);
	    rcand = t;
	    t = oprev(rcand);
	    v2=dest(rcand);
	    v3=orig(rcand);
	    v1=dest(t);
	  }
      }
      /*chatting("Valid sites 3,4\n");*/
      lvalid = valid(lcand,basel);
      /*chatting("Valid sites 3,4\n");*/
      rvalid = valid(rcand,basel);
      if ((! lvalid) && (! rvalid))
	{
	  EDGE_PAIR retval; 
	  retval.left = ldo; retval.right = rdo; return retval;
	}
      v1=dest(lcand);
      v2=orig(lcand);
      v3=orig(rcand);
      v4=dest(rcand);
      if (!lvalid ||
	  (rvalid && incircle(v1,v2,v3,v4
			      /*dest(lcand), orig(lcand),
				orig(rcand), dest(rcand)*/)))
	{
	  basel = connect_left(rcand, sym(basel));
	  rcand = lnext(sym(basel));
	}
      else
	{
	  basel = sym(connect_right(lcand, basel));
	  lcand = rprev(basel);
	}
    }
}



#define DEFINE_GLOBALS
#define CONST_m1 10000
#define CONST_b 31415821
#define RANGE 100

struct EDGE_STACK *allocate_stack();
struct get_point get_points();
int loop = 0, randum = 1, filein = 0 , fileout = 1, statistics = 0; 

void in_order(tree)
     VERTEX_PTR tree;
{
  VERTEX_PTR tleft, tright;
  double x, y;

  if (!tree) {
    return;
  }

  x = X(tree);
  y = Y(tree);
  chatting("X=%f, Y=%f\n",x, y);
  tleft = tree->left;
  in_order(tleft);
  tright = tree->right;
  in_order(tright);
}

int mult(int p, int q)
{
	int p1, p0, q1, q0;
	
	p1=p/CONST_m1; p0=p%CONST_m1;
	q1=q/CONST_m1; q0=q%CONST_m1;
	return (((p0*q1+p1*q0) % CONST_m1)*CONST_m1+p0*q0);
}

int skiprand(int seed, int n)
/* Generate the nth random # */
{
  for (; n; n--) 
#ifdef SS_PLAIN
    seed = olden_random(seed);
#else SS_PLAIN
    seed=random(seed);
#endif SS_PLAIN
  return seed;
}


#ifdef SS_PLAIN
int olden_random(int seed)
#else SS_PLAIN
int random(int seed)
#endif SS_PLAIN
{
  seed = (mult(seed,CONST_b)+1);
  return seed;
}


void
print_extra(extra)
     VERTEX_PTR extra;
{
  
  double x, y;
  x = X(extra);
  y = Y(extra);
  chatting("X=%f, Y=%f\n",x, y);
}


void
main(argc,argv) 
     int argc;
     char *argv[];
{
  struct EDGE_STACK *my_stack;
  struct get_point point, extra;
  QUAD_EDGE edge;
  int n, retained;
  to_lincoln = to_off = to_3d_out = to_color = 0;
  voronoi = delaunay = 1; interactive = ahost = 0 ;
  retained = 0;
  
  filestuff();
  chatting("argc = %d\n",argc);
  n = dealwithargs(argc, argv);

  chatting("getting %d points\n", n);
  extra=get_points(1,1.0,n,1023);
  point=get_points(n-1,extra.curmax,n-1,extra.seed);
  chatting("Done getting points\n");
  num_vertices = n + 1;
  my_stack=allocate_stack(num_vertices);

//#ifdef SS_PLAIN
//  ssplain_alloc_stats();
//#endif SS_PLAIN

  if (flag) in_order(point.v);
  if (flag) print_extra(extra.v);
  chatting("Doing voronoi on %d nodes\n", n); 

  edge=build_delaunay_triangulation(point.v,extra.v);

  chatting("Elapsed time %f\n", CMMD_node_timer_elapsed(0));
  if (flag) output_voronoi_diagram(edge,n,my_stack);

  exit(0);
}

struct EDGE_STACK *allocate_stack(num_vertices)
     int num_vertices;
{
  struct EDGE_STACK *my_stack;

  num_edgeparts = 12*num_vertices;
  my_alloc(my_stack, struct EDGE_STACK, 1);
  my_alloc(my_stack->elts, QUAD_EDGE , num_edgeparts);
  my_stack->stack_size = num_edgeparts/2;
  return my_stack;
}

void
free_all(cont,my_stack)
     int cont;
     struct EDGE_STACK *my_stack;
{
  free(my_stack->elts);
  free(my_stack);
}

struct get_point get_points(n,curmax,i,seed)
     int n;
     double curmax;
     int i,seed;
{
  VERTEX_PTR node;

  struct get_point point;

  if (n<1 || i<=n/2) {
    point.v = NULL;
    point.curmax=curmax;
	 point.seed = seed;
    return point;
  }
#ifdef OUT
  chatting("Get points: %d, %f, %d\n",n,curmax,i);
#endif OUT

  point = get_points(n/2,curmax,i,seed);
  /*chatting("rec call n=%d\n",n);*/
  i -= n/2;

  node = (VERTEX_PTR) mymalloc(sizeof(struct VERTEX));

  /*chatting("Get points past alloc,n=%d\n",n);*/
  X(node) = point.curmax * exp(log(drand(point.seed))/i);
  Y(node) = drand(point.seed);
  NORM(node) = X(node)*X(node) + Y(node)*Y(node);
  node->right = point.v;
  /*chatting("node = 0x%x\n",node);*/
  point = get_points(n/2,X(node),i-1,point.seed);

  node->left = point.v;
  point.v = node;
  return point;
}


/****************************************************************/
/*	Voronoi Diagram Routines.                               */
/****************************************************************/

/****************************************************************/
/*	Graph Traversal Routines                                */
/****************************************************************/

QUAD_EDGE pop_edge(x)
     struct EDGE_STACK *x;
{
  int a=x->ptr--;
  return (x)->elts[a];
}

void
push_edge(stack,edge)
     struct EDGE_STACK *stack;
     QUAD_EDGE edge;
{
  int a;
  /*chatting("pushing edge \n");*/
  if (stack->ptr == stack->stack_size) {
    printf("cannot push onto stack: stack is too large\n");
  }
  else {
    a = stack->ptr;
    a++;
    stack->ptr = a;
    stack->elts[a] = edge;
  }
}

void
push_ring(stack, edge)
     struct EDGE_STACK *stack;
     QUAD_EDGE edge;
{
  QUAD_EDGE nex;
  nex = onext(edge);
  while (nex != edge) {
    if (seen(nex) == 0) {
      seen(nex) = 1;
      push_edge(stack, nex);
    }
    nex = onext(nex);
  }
}

void
push_nonzero_ring(stack, edge)
     struct EDGE_STACK *stack;
     QUAD_EDGE edge;
{
  QUAD_EDGE nex;
  nex = onext(edge);
  while (nex != edge) {
    if (seen(nex) != 0) {
      seen(nex) = 0;
      push_edge(stack, nex);
    }
    nex = onext(nex);
  }
}

void
zero_seen(my_stack,edge)
     QUAD_EDGE edge;
     struct EDGE_STACK *my_stack;
{
  my_stack->ptr = 0;
  push_nonzero_ring(my_stack, edge);
  while (my_stack->ptr != 0) {
    edge = pop_edge(my_stack);
    push_nonzero_ring(my_stack, sym(edge));
  }
}