Source

Triangulator / query.ml

Full commit
  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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
open Arithmetic;;
open Geometry;;


type query = Sink of trapezoid
	     | Source of query ref ref
	     | XNode of query ref * segment * query ref
	     | YNode of query ref * point2 * query ref  (* below, p, above *)
;;


let rec num_nodes (q: query) =
  match q with 
      Sink _ -> 1
    | XNode (left, _, right) -> 1+ (num_nodes !left) + (num_nodes !right)
    | YNode (left, _, right) -> 1+ (num_nodes !left) + (num_nodes !right)
    | Source nodeRefRef -> num_nodes !(!nodeRefRef)
;;


let (@@) (p: point2) (tr: trapezoid) =  
  (* read as 'p strictly belongs to tr' *)
  match tr with
      Plane -> true
    | HalfPlane (y, Up) -> y<p.y
    | HalfPlane (y, Down) -> y>p.y
    | Stripe (yt, yb, NoSide, NoSide) -> p.y > yb && p.y < yt
    | Stripe (yt, yb, NoSide, Side ln) -> p.y > yb && p.y < yt &&
	(left_or_right ln p = Left)
    | Stripe (yt, yb, Side ln, NoSide) -> p.y > yb && p.y < yt &&
	(left_or_right ln p = Right)
    | Stripe (yt, yb, Side lnl, Side lnr) -> p.y > yb && p.y < yt &&
	(left_or_right lnl p = Right) && (left_or_right lnr p = Left)
;;

type horizontally_divided_trapezoid = {lower: trapezoid; upper: trapezoid};;
(* assumes that p @@ tr *)
let divide_by_point (tr:trapezoid) (p:point2) =
  match tr with
      Plane -> 
	let upper = HalfPlane (p.y, Up) in 
	let lower = HalfPlane (p.y, Down) in 
	  if upper = lower then failwith "p divided trapezoids cannot be equal!\n"
	  else
	    {lower=lower; upper=upper}
    | HalfPlane (y, Up) ->
	let upper = HalfPlane (p.y, Up) in 
	let lower = Stripe (p.y, y, NoSide, NoSide) in 
	  if upper = lower then failwith "p divided trapezoids cannot be equal!\n"
	  else
	    {lower=lower; upper=upper}
    | HalfPlane (y, Down) ->
	let upper = Stripe (y, p.y, NoSide, NoSide) in 
	let lower = HalfPlane (p.y, Down) in
	  if upper = lower then failwith "p divided trapezoids cannot be equal!\n"
	  else
	    {lower=lower; upper=upper}
    | Stripe (yt, yb, s1, s2) ->
	let upper = Stripe (yt, p.y, s1, s2) in 
	let lower = Stripe (p.y, yb, s1, s2) in 
	  if upper = lower then failwith "p divided trapezoids cannot be equal!\n"
	  else
	    {lower=lower; upper=upper}
;;

let rec add_point (q: query) (p: point2) =
  match q with
      Source nodeRefRef ->
	begin
	  match !(!nodeRefRef) with
	      Sink tr -> if p @@ tr then
		let trs = divide_by_point tr p in
		  (!nodeRefRef) := YNode (ref (Sink trs.lower),
					  p,
					  ref (Sink trs.upper))
	      else () (* do nothing if not the proper tr *)
	    | YNode _ | XNode _ -> add_point !(!nodeRefRef) p
	    | _ -> failwith "source cannot point to source\n"
	end 
	  (* cannot be a sink, only YNode or XNode *)
    | YNode (below, _, above) ->
	begin
	  match !below with
	      Sink tr ->
		if p @@ tr then
		    let trs = divide_by_point tr p in
		      below := YNode (ref (Sink trs.lower),
				      p,
				      ref (Sink trs.upper))
		else () 
	    | _ -> add_point !below p
	end;
	begin
	  match !above with
	      Sink tr ->
		if p @@ tr then
		  let trs = divide_by_point tr p in
		    above := YNode (ref (Sink trs.lower),
				    p,
				    ref (Sink trs.upper))
		else ()
	    | _ -> add_point !above p
	end
    | XNode (left, _, right) -> 
	begin
	  match !left with
	      Sink tr ->
		if p @@ tr then
		  let trs = divide_by_point tr p in
		    left := YNode (ref (Sink trs.lower),
				   p,
				   ref (Sink trs.upper))
		else () 
	    | _ -> add_point !left p
	end;
	begin	  
	  match !right with
	      Sink tr ->
		if p @@ tr then
		  let trs = divide_by_point tr p in
		    right := YNode (ref (Sink trs.lower),
				    p,
				    ref (Sink trs.upper))
		else () 
	    | _ -> add_point !right p
	end
    | _ -> failwith "sink is not expected\n"
;;


let ($) seg tr =
  match tr with
      Plane | HalfPlane _ -> false
    | Stripe (yt, yb, s1, s2) ->
	let high_enough = seg.e1.y >= yt && seg.e2.y <= yb in
	let sline = line_of_segment seg in
	let xxt = find_x yt sline in (* line intersects top side *)
	let xxb = find_x yb sline in (* line intersects bottom side *)
	  begin
	    match s1, s2 with
		NoSide, NoSide -> high_enough
	      | NoSide, Side ln ->
		  let rcxt = find_x yt ln in
		  let rcxb = find_x yb ln in
		    (xxt<rcxt && xxb <= rcxb) || (xxt<=rcxt && xxb < rcxb)
	      | Side ln, NoSide ->
		  let lcxt = find_x yt ln in
		  let lcxb = find_x yb ln in
		    (xxt>lcxt && xxb>=lcxb) || (xxt>=lcxt && xxb>lcxb)
	      | Side lnl, Side lnr ->
		  let lcxt = find_x yt lnl in
		  let lcxb = find_x yb lnl in
		  let rcxt = find_x yt lnr in
		  let rcxb = find_x yb lnr in
		    (xxt > lcxt && xxt <= rcxt && xxb >= lcxb && xxb < rcxb) ||
		      (xxt >= lcxt && xxt < rcxt && xxb > lcxb && xxb <= rcxb)
	  end
;;



type seg_to_line_orientation = SLeft | SRight | SOn | SIntersects;;

(* Left | Right | On, fail if intersects *)
let segment_to_line seg ln =
  let lr1 = left_or_right ln seg.e1 in
  let lr2 = left_or_right ln seg.e2 in
    match lr1, lr2 with
	Left, Right | Right, Left -> SIntersects
      | Left, On | On, Left -> SLeft
      | Right, On | On, Right -> SRight
      | Right, Right -> SRight
      | Left, Left -> SLeft
      | On, On -> SOn
;;


(* this predicate checks if the trapezoid is valid *)
let proper_tr tr =
  match tr with
      Plane | HalfPlane _ -> true
    | Stripe (yt, yb, left, right) ->
	if yt<=yb then false
	else
	  match left, right with
	      NoSide, NoSide -> true
	    | Side ln, NoSide | NoSide, Side ln -> not (ln.k=0.0)
	    | Side lnl, Side lnr ->
		let plt = {x = find_x yt lnl; y=yt} in
		let plb = {x = find_x yb lnl; y=yb} in
		let prt = {x = find_x yt lnr; y=yt} in
		let prb = {x = find_x yb lnr; y=yb} in
		  ((not (plt=prt)) || (not (plb=prb))) &&
		    plt.x<=prt.x && plb.x<=prb.x
;;


let rec check_all_trapezoids q =
  match q with
      Source nodeRefRef ->
	begin
	  match !(!nodeRefRef) with
	      Sink tr ->
		if not (proper_tr tr) then
		  failwith "improper trapezoid\n"
		else ()
	    | _ -> check_all_trapezoids !(!nodeRefRef)
	end
    | XNode (left, _, right) ->
	begin
	  match !left with
	      Sink tr ->
		if not (proper_tr tr) then
		  failwith "improper trapezoid\n"
		else ()
	    | _ -> check_all_trapezoids !left
	end;
	begin
	  match !right with
	      Sink tr ->
		if not (proper_tr tr) then
		  failwith "improper trapezoid\n"
		else ()
	    | _ -> check_all_trapezoids !right
	end
   | YNode (below, _, above) ->
	begin
	  match !below with
	      Sink tr ->
		if not (proper_tr tr) then
		  failwith "improper trapezoid\n"
		else ()
	    | _ -> check_all_trapezoids !below
	end;
	begin
	  match !above with
	      Sink tr ->
		if not (proper_tr tr) then
		  failwith "improper trapezoid\n"
		else ()
	    | _ -> check_all_trapezoids !above
	end
    | _ -> failwith "source after source is not expected\n"
;;

(* don't know which of the two is used *)
let ($) (s:segment) (tr: trapezoid) = 
  match tr with
      Plane | HalfPlane _ -> false
    | Stripe (yt, yb, left, right) ->
	let high_enough = s.e1.y >= yt && s.e2.y <= yb in
	  if not high_enough then false
	  else
	    begin
	      match left, right with
		  NoSide, NoSide -> true
		| NoSide, Side ln ->
		    begin
		      match segment_to_line s ln with
			  SLeft -> true
			| _ -> false
		    end
		| Side ln, NoSide ->
		    begin
		      match segment_to_line s ln with
			  SRight -> true
			| _ -> false
		    end
		| Side lnl, Side lnr ->
		    begin
		      match (segment_to_line s lnl), (segment_to_line s lnr) with
			  SRight, SLeft -> true
			| _ -> false
		    end
	    end
;;





type non_horizontally_divided_trapezoid = {left: trapezoid; right: trapezoid};;

let divide_by_edge (tr:trapezoid) (s: segment) =
  let edge = line_of_segment s in
    match tr with
	Stripe (yt, yb, s1, s2) -> 
	  let left = Stripe (yt, yb, s1, Side edge) in
	  let right = Stripe (yt, yb, Side edge, s2) in
	    if left = right then failwith "trapezoids cannot be equal!\n"
	    else
	      {left = left; right = right}
      | _ -> failwith "Panic: non-stripe trapezoid is not expected!\n"
;;

(* non-triangular *)
let divide_by_diagonal tr =
  match tr with
      Stripe (yt, yb, Side ln1, Side ln2) ->
	let plt = {x=(find_x yt ln1); y=yt} in
	let plb = {x=(find_x yb ln1); y=yb} in
	let prt = {x=(find_x yt ln2); y=yt} in
	let prb = {x=(find_x yb ln2); y=yb} in
	let edge = {e1=plt; e2 = prb} in
	  (divide_by_edge tr edge, edge)
    | _ -> failwith "only bound trapezoids are allowed\n"
;;





let rec add_edge (q: query) (s: segment) =
  match q with
      Source nodeRefRef ->
	begin
	  match !(!nodeRefRef) with
	      Sink tr ->
		if s $ tr then
		  let trs = divide_by_edge tr s in
		    nodeRefRef := ref (XNode (ref(Sink trs.left), s, ref (Sink trs.right)))
		else ()
	    | _ -> add_edge !(!nodeRefRef) s
	end
    | XNode (left, _, right) ->
	begin
	  match !left with
	      Sink tr -> 
		if s $ tr then
		  let trs = divide_by_edge tr s in
		    left := XNode (ref(Sink trs.left), s, ref(Sink trs.right))
		else ()
	    | _ -> add_edge !left s
	end;
	begin
	  match !right with
	      Sink tr -> 
		if s $ tr then
		  let trs = divide_by_edge tr s in
		    right := XNode (ref(Sink trs.left), s, ref(Sink trs.right))
		else ()
	    | _ -> add_edge !right s
	end
	    | YNode (below, _, above) -> 
	begin
	  match !below with
	      Sink tr ->
		if s $ tr then
		  let trs = divide_by_edge tr s in
		    below:= XNode (ref(Sink trs.left), s, ref(Sink trs.right))
		else ()
	    | _ -> add_edge !below s
	end;
	begin
	  match !above with
	      Sink tr ->
		if s $ tr then
		  let trs = divide_by_edge tr s in
		    above:= XNode (ref(Sink trs.left), s, ref(Sink trs.right))
		else ()
	    | _ -> add_edge !above s
	end
    | _ -> failwith "query structure cannot start from sink!\n"
;;




let rec triangulate (q: query) = 
  match q with
      Source nodeRefRef ->
	begin
	  match !(!nodeRefRef) with
	      Sink tr ->
		begin
		  match triangle_stripe tr with
		      true, _ -> ()
		    | false, _ ->
			let divi = (divide_by_diagonal tr) in
			  begin
			    match divi with
				trs, s ->
				  nodeRefRef := ref (XNode (ref (Sink trs.left),
							    s,
							    ref (Sink trs.right)))
			  end
		end
	    | XNode _ | YNode _ -> triangulate !(!nodeRefRef)
	    | _ -> failwith "source after source is not expected\n"
	end
    | XNode (left, _, right) ->
	begin
	  match !left with
	      Sink tr ->
		begin
		  match triangle_stripe tr with
		      true, _ -> ()
		    | false, _ ->
			let divi = (divide_by_diagonal tr) in
			  begin
			    match divi with
				trs, s -> 
				  left := XNode (ref (Sink trs.left),
						 s,
						 ref (Sink trs.right))
			  end
		end
	end;
	begin
	  match !right with
	      Sink tr ->
		begin
		  match triangle_stripe tr with
		      true, _ -> ()
		    | false, _ ->
			let divi = (divide_by_diagonal tr) in
			  begin
			    match divi with
				trs, s -> 
				  right := XNode (ref (Sink trs.left),
						  s,
						  ref (Sink trs.right))
			  end
		end
	end
    | YNode (below, _, above) -> 
	begin
	  match !below with
	      Sink tr ->
		begin
		  match triangle_stripe tr with
		      true, _ -> ()
		    | false, _ ->
			let divi = (divide_by_diagonal tr) in
			  begin
			    match divi with
				trs, s -> 
				  below := XNode (ref (Sink trs.left),
						  s,
						  ref (Sink trs.right))
			  end
		end
		  
	end;
	begin
	  match !above with
	      Sink tr ->
		begin
		  match triangle_stripe tr with
		      true, _ -> ()
		    | false, _ ->
			let divi = (divide_by_diagonal tr) in
			  begin
			    match divi with
				trs, s -> 
				  above := XNode (ref (Sink trs.left),
						  s,
						  ref (Sink trs.right))
			  end
		end

	end
    | _ -> failwith "lalala\n"
;;


(* pretty-printer for my structure *)
let pretty_print (q: query) = 
  let rec pretty_print_helper (q: query) (offset: int) = 
    let off_string n = String.make n ' ' in
      match q with
	  Source nodeRefRef -> print_string "#Source\n"; 
	    pretty_print_helper !(!nodeRefRef) (offset+1)
	| Sink tr -> print_string ((off_string offset)^"#Sink "^(string_of_trapezoid tr)^"\n")
	| YNode (a,p,b) -> print_string ((off_string offset)^"YNode "^(string_of_point p)^"\n");
	    pretty_print_helper !a (offset+1);
	    pretty_print_helper !b (offset+1)
	| XNode (l, s, r) -> print_string ((off_string offset)^"XNode "^(string_of_segment s)^"\n");
	  pretty_print_helper !l (offset+1);
	  pretty_print_helper !r (offset+1)
  in pretty_print_helper q 0
;;





(* assumes that the trapezoid is bound *)
let patch_trapezoid tr = 
  match tr with
      Stripe (yt, yb, Side lnl, Side lnr) ->
	let lb = {x = find_x yb lnl; y=yb} in
	let lt = {x = find_x yt lnl; y=yt} in
	let rt = {x = find_x yt lnr; y=yt} in
	let rb = {x = find_x yb lnr; y=yb} in
	  if not (lt=rt || lb=rb) then
	    let verts = [lb;lt;rt;rb] in
	    let v_x = String.concat "," (List.map (fun v -> string_of_float v.x) verts) in
	    let v_y = String.concat "," (List.map (fun v -> string_of_float v.y) verts) in
	    let v_z = String.concat "," (List.map (fun v -> "0.0") verts) in
	    let verts = "["^v_x^";"^v_y^";"^v_z^"]'" in
	      "patch ('Faces', [1,2,3,4], 'Vertices',"^verts^", 'FaceColor', 'r');"
	  else
	    if lt = rt then
	      let verts = [lb;lt;rb] in
	      let v_x = String.concat "," (List.map (fun v -> string_of_float v.x) verts) in
	      let v_y = String.concat "," (List.map (fun v -> string_of_float v.y) verts) in
	      let v_z = String.concat "," (List.map (fun v -> "0.0") verts) in
	      let verts = "["^v_x^";"^v_y^";"^v_z^"]'" in
		"patch ('Faces', [1,2,3], 'Vertices',"^verts^", 'FaceColor', 'r');"
	    else
	      let verts = [lb;lt;rt] in
	      let v_x = String.concat "," (List.map (fun v -> string_of_float v.x) verts) in
	      let v_y = String.concat "," (List.map (fun v -> string_of_float v.y) verts) in
	      let v_z = String.concat "," (List.map (fun v -> "0.0") verts) in
	      let verts = "["^v_x^";"^v_y^";"^v_z^"]'" in
		"patch ('Faces', [1,2,3], 'Vertices',"^verts^", 'FaceColor', 'r'); hold on; "
	
    | _ -> failwith "only bounded trapezoid can be represented as patches\n"
;;





let rec generate_patches (q: query) =
  let rec generate_patches_helper (q: query) (mcode: string) = 
    match q with
	Sink tr ->
	  if bound_tr tr then
	    mcode^(patch_trapezoid tr)
	  else
	    mcode
      | XNode (left, _, right) ->
	  generate_patches_helper !right (generate_patches_helper !left mcode)
      | YNode (below, _, above) -> 
	  generate_patches_helper !below (generate_patches_helper !above mcode)
      | Source nodeRefRef ->
	  generate_patches_helper !(!nodeRefRef) mcode
  in generate_patches_helper q ""
;;


(* unbound, triangle, polygon *)
type classification = Unbound | Triangle | Polygon
let tr_classify tr = 
match tr with
    Plane | HalfPlane _ -> Unbound
  | Stripe (_, _, _, NoSide) | Stripe (_, _, NoSide, _) -> Unbound
  | Stripe (yt, yb, Side lnl, Side lnr) ->
      let xtl = find_x yt lnl in
      let xbl = find_x yb lnl in
      let xtr = find_x yt lnr in
      let xbr = find_x yb lnr in
	if xtl === xtr then 
	  Triangle
	else if xbl === xbr then
	  Triangle
	else
	  Polygon
;;


(* assume that tr is a polygon 
 * the order of vertices is bl, tl, tr, br
 *)
let tr_verts tr = 
  match tr with
      Stripe (yt, yb, Side lnl, Side lnr) -> 
	let xbl = find_x yb lnl in
	let xtl = find_x yt lnl in
	let xtr = find_x yt lnr in
	let xbr = find_x yb lnr in
	({x=xbl;y=yb}, {x=xtl;y=yt}, {x=xtr; y=yt}, {x=xbr; y=yb})
    | _ -> failwith "unbound trapezoid\n"
;;

let short_diag tr =
  let dist = fun p1 -> fun p2 -> sqrt ((p1.x-.p2.x)**2.0+.(p1.y-.p2.y)**2.0) in
  let (e1,e2,e3,e4) = tr_verts tr in
  if (dist e1 e3)<(dist e2 e4) then {e1=e3;e2=e1}
  else {e1=e2;e2=e4};;


(***************************************)


let rec triangulate q =
  match q with
      Source nodeRefRef ->
	begin
	  match !(!nodeRefRef) with
	      Sink tr ->
		begin
		  match tr_classify tr with
		      Unbound -> ()
		    | Triangle -> ()
		    | Polygon ->
			match tr_classify tr with
			    Unbound | Triangle -> ()
			  | Polygon -> 
			      let diag = (short_diag tr) in
			      let divtr = divide_by_edge tr diag in
				nodeRefRef:=ref (XNode (ref (Sink divtr.left),
							diag,
							ref (Sink divtr.right)))

		end
	    | XNode _ | YNode _ -> triangulate !(!nodeRefRef)
	    | _ -> failwith "source cannot point to source\n"
	end
	      | XNode (left, _, right) ->
	begin
	  match !left with
	      Sink tr ->
		begin
		  match tr_classify tr with
		      Unbound | Triangle -> ()
		    | Polygon ->
			let diag = (short_diag tr) in
			let divtr = divide_by_edge tr diag in
			  left:= XNode (ref (Sink divtr.left),
					 diag,
					 ref (Sink divtr.right))
		end
	    | XNode _ | YNode _ -> triangulate !left
	    | _ -> failwith "source can be only in the root\n"
	end;
	begin
	  match !right with
	      Sink tr ->
		begin
		  match tr_classify tr with
		      Unbound | Triangle -> ()
		    | Polygon -> 
			let diag = (short_diag tr) in
			let divtr = divide_by_edge tr diag in
			  right:= XNode (ref (Sink divtr.left),
					 diag,
					 ref (Sink divtr.right))
		end
	    | XNode _ | YNode _ -> triangulate !right
	    | _ -> failwith "source can be only in the root\n"
	end
    | YNode (below, _, above) ->
	begin
	  match !below with
	      Sink tr ->
		begin
		  match tr_classify tr with
		      Unbound | Triangle -> ()
		    | Polygon ->
			match tr_classify tr with
			    Unbound | Triangle -> ()
			  | Polygon -> 
			      let diag = (short_diag tr) in
			      let divtr = divide_by_edge tr diag in
				below:= XNode (ref (Sink divtr.left),
					       diag,
					       ref (Sink divtr.right))
				  
		end
	    | XNode _ | YNode _ -> triangulate !below
	    | _ -> failwith "source can be only in the root\n"
	end;
	begin
	  match !above with
	      Sink tr ->
		begin
		  match tr_classify tr with
		      Unbound | Triangle -> ()
		    | Polygon ->
			match tr_classify tr with
			    Unbound | Triangle -> ()
			  | Polygon -> 
			      let diag = (short_diag tr) in
			      let divtr = divide_by_edge tr diag in
				above:= XNode (ref (Sink divtr.left),
					       diag,
					       ref (Sink divtr.right))
		end
	    | XNode _ | YNode _ -> triangulate !above
	    | _ -> failwith "source can be only in the root\n"
	end
    | _ -> failwith "sink is not expected here\n"
;;
(***************************************)




(* assume at least one edge *)
exception NotHere;;
let rec find_outer_edge q =
  match q with
      Source nodeRefRef ->
	begin
	  match !(!nodeRefRef) with
	      XNode _ | YNode _ -> find_outer_edge !(!nodeRefRef)
	    | _ -> raise NotHere
	end
    | XNode (left, s, right) ->
	begin
	  match !left with
	      Sink tr ->
		if (tr_classify tr) = Unbound
		then s
		else
		  raise NotHere
	    | _ -> 
		try find_outer_edge !left
		with NotHere -> find_outer_edge !right
	end
    | YNode (below, _, above) ->
	begin
	  try
	    find_outer_edge !below
	  with NotHere ->
	    find_outer_edge !above
	end
    | _ -> raise NotHere
	    
;;






let rec get_all_trapezoids q =
  match q with
      Source nodeRefRef ->
	get_all_trapezoids !(!nodeRefRef)
    | Sink tr -> [tr]
    | XNode (left, _, right) ->
	(get_all_trapezoids !left)@(get_all_trapezoids !right)
    | YNode (below, _, above) ->
	(get_all_trapezoids !below)@(get_all_trapezoids !above)
;;