summaryrefslogtreecommitdiffstats
path: root/lib/math.tcl
blob: 1990a79b80ae4d11dfc8af49812423f4da20ce76 (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
# math.tcl --
#
#     This file provides global math datatypes and utilities.
#

namespace eval ::vec2 {
    proc add {a b} {
        list [+ [lindex $a 0] [lindex $b 0]] [+ [lindex $a 1] [lindex $b 1]]
    }
    proc sub {a b} {
        list [- [lindex $a 0] [lindex $b 0]] [- [lindex $a 1] [lindex $b 1]]        
    }
    proc mult {a b} {
        list [* [lindex $a 0] [lindex $b 0]] [* [lindex $a 1] [lindex $b 1]]        
    }
    proc div {a b} {
        list [/ [lindex $a 0] [lindex $b 0]] [/ [lindex $a 1] [lindex $b 1]]        
    }
    proc scale {a args} {
        if {[llength $args] == 1} {
            set sx [lindex $args 0]; set sy [lindex $args 0]
        } else {
            lassign $args sx sy
        }
        list [* [lindex $a 0] $sx] [* [lindex $a 1] $sy]
    }
    proc rotate {a theta} {
        lassign $a x y
        list [expr {$x*cos($theta) + $y*sin($theta)}] \
             [expr {-$x*sin($theta) + $y*cos($theta)}]
    }
    proc distance {a b} {
        lassign $a ax ay
        lassign $b bx by
        expr {sqrt(pow($ax-$bx, 2) + pow($ay-$by, 2))}
    }
    proc normalize {a} {
        set l2 [vec2 distance $a [list 0 0]]
        vec2 scale $a [/ 1 $l2]
    }
    proc dot {a b} {
        expr {[lindex $a 0]*[lindex $b 0] + [lindex $a 1]*[lindex $b 1]}
    }
    proc distanceToLineSegment {a v w} {
        set l2 [vec2 distance $v $w]
        if {$l2 == 0.0} {
            return [distance $a $v]
        }
        set t [max 0 [min 1 [/ [dot [sub $a $v] [sub $w $v]] $l2]]]
        set proj [add $v [scale [sub $w $v] $t]]
        vec2 distance $a $proj
    }
    proc midpoint {a b} {
        lassign $a x1 y1; lassign $b x2 y2
        list [/ [+ $x1 $x2] 2] [/ [+ $y1 $y2] 2]
    }
    namespace export *
    namespace ensemble create
}

namespace eval ::region {
    # A region is an arbitrary oriented chunk of a plane. The
    # archetypal region is the region of a program/page, which is the
    # quadrilateral area of space that is covered by that page. A
    # region is defined by a set of vertices and a set of edges among
    # those vertices. (TODO: Allow areas to be filled/unfilled.)
    
    set cc [c create]
    $cc include <string.h>

    $cc code {

        typedef struct Point Point;
        struct Point {
            float x;
            float y;
        };

        typedef struct Edge Edge;
        struct Edge {
            int32_t from;
            int32_t to;
        };

        typedef struct Region Region;
        struct Region {

            int32_t n_points;
            Point* points;

            int32_t n_edges;
            Edge* edges;

        };

        float dist2(Point start, Point end) {
            return (
                (start.x - end.x) * (start.x - end.x) +
                (start.y - end.y) * (start.y - end.y)
            );
        }

        float orientation(Point start, Point end, Point candidate) {
            return (
                (end.x - start.x) * (candidate.y - start.y) - 
                (end.y - start.y) * (candidate.x - start.x)
            );
        }
    }

    $cc proc create_convex_region {float[][2] point_list int N} Region* {
        // Takes any number of points
        // Computes the convex-hull
        // Defines the edges such that they are the border of the convex hull

        // Initialize a Region struct on the heap
        size_t r_sz = sizeof(Region);
        Region* ret = (Region *) ckalloc(r_sz);
        memset(ret, 0, r_sz);

        // Construct Points
        size_t p_sz = N * sizeof(Point);
        Point* points = (Point*) ckalloc(p_sz);
        memset(points, 0, p_sz);
        for (int i = 0; i < N; i++) {
            points[i].x = point_list[i][0];
            points[i].y = point_list[i][1];
        }

        // Construct edges
        size_t e_sz = N * sizeof(Edge);
        Edge* edges = (Edge*) ckalloc(e_sz);
        memset(edges, 0, e_sz);
        
        if (N == 0) {
            *ret = (Region) {
                .n_points = 0,
                .points = points,
                .n_edges = 0,
                .edges = edges,
            };
            return ret;
        }

        // Convex-Hull
        // Start with the point with smallest x coordinate.
        // You're guaranteed this point is on the convex-hull.
        int from = 0;
        for (int i = 0; i < N; i++) {
            if (points[i].x < points[from].x) {
                from = i;
            }
        }
        int to = (from + 1) % N;

        // Iteratively, find the following point that's smallest
        // counter-clockwise turn from the current edge.
        // Repeat until you're back where you started.
        int num_edges = 0;
        while (true) {
            for (int cand = 0; cand < N; cand++) {
                if (cand == from) {
                    continue;
                }
                float o = orientation(points[from], points[to], points[cand]);
                if (o > 0) {
                    to = cand;
                } else if (
                    o == 0 && 
                    dist2(points[from], points[cand]) > dist2(points[from], points[to])
                ) {
                    to = cand;
                }
            }
            edges[num_edges].from = from;
            edges[num_edges].to = to;
            num_edges += 1;

            if (num_edges > N) {
                // If all points are on the perimeter, then there should be
                // only N edges. More than N, you have a bug.
                break;
            }

            from = to;
            to = (from + 1) % N;
            if (from == edges[0].from) {
                break;
            }
        }

        *ret = (Region) {
            .n_points = N,
            .points = points,
            .n_edges = num_edges,
            .edges = edges,
        };

        return ret;
    }

    $cc proc free_region {Region* region} void {
        ckfree((char*) region->points);
        ckfree((char*) region->edges);
        ckfree((char*) region);
    }

    $cc proc pprint {Region* region} void {
        for (int i = 0; i < region->n_points; i++) {
            printf("Point %d: [%f, %f]\n", i, region->points[i].x, region->points[i].y);
        }
        for (int i = 0; i < region->n_edges; i++) {
            printf("Edge %d: %d -> %d\n", i, region->edges[i].from, region->edges[i].to);
        }
    }

    $cc proc to_tcl {Region* region} Tcl_Obj* {

        Tcl_Obj* points;
        if (region->n_edges) {
            Tcl_Obj* _points[region->n_points];
            for (int i = 0; i < region->n_points; i++) {
                Tcl_Obj* p[] = {
                    Tcl_NewDoubleObj(region->points[i].x),
                    Tcl_NewDoubleObj(region->points[i].y),
                };
                _points[i] = Tcl_NewListObj(2, p);
            }
            points = Tcl_NewListObj(region->n_points, _points);
        } else {
            points = Tcl_NewListObj(0, NULL);
        }

        Tcl_Obj* edges;
        if (region->n_edges) {
            Tcl_Obj* _edges[region->n_edges];
            for (int i = 0; i < region->n_edges; i++) {
                Tcl_Obj* e[] = {
                    Tcl_NewIntObj(region->edges[i].from),
                    Tcl_NewIntObj(region->edges[i].to),
                };
                _edges[i] = Tcl_NewListObj(2, e);
            }
            edges = Tcl_NewListObj(region->n_edges, _edges);
        } else {
            edges = Tcl_NewListObj(0, NULL);
        }

        Tcl_Obj* _robj[3] = {
            points,
            edges,
            Tcl_NewDoubleObj(0)
        };
        Tcl_Obj* robj;
        robj = Tcl_NewListObj(3, _robj);
        return robj;
    }

    $cc compile

    proc convexHull {points} {
        set ch [region create_convex_region $points [llength $points]]
        set r [region to_tcl $ch]
        region free_region $ch
        return $r
    }

    proc create {vertices edges {angle 0}} {
        list $vertices $edges $angle
    }

    proc fromRect {x y w h {angle 0}} {
	set hw [expr {$w / 2.}]
	set hh [expr {$h / 2.}]
        set vertices [list \
		     [list [expr {$x-$hw}] [expr {$y+$hh}]] \
                     [list [expr {$x+$hw}] [expr {$y+$hh}]] \
                     [list [expr {$x+$hw}] [expr {$y-$hh}]] \
                     [list [expr {$x-$hw}] [expr {$y-$hh}]]]
        set edges [list [list 0 1] [list 1 2] [list 2 3] [list 3 0]]
        region rotate [region create $vertices $edges] $angle
    }

    proc vertices {r} { lindex $r 0 }
    proc edges {r} { lindex $r 1 }
    # Angle the region is rotated above the horizontal, in radians:
    proc angle {r} {
        expr {[llength $r] >= 3 ? [lindex $r 2] : 0}
    }

    proc width {r} {
        set minXp 100000
        set maxXp -100000
        foreach v [vertices [rotate $r [* -1 [angle $r]]]] {
            lassign $v xp yp
            if {$xp < $minXp} { set minXp $xp }
            if {$xp > $maxXp} { set maxXp $xp }
        }
        expr { $maxXp - $minXp }
    }
    proc height {r} {
        set minYp 100000
        set maxYp -100000
        foreach v [vertices [rotate $r [* -1 [angle $r]]]] {
            lassign $v xp yp
            if {$yp < $minYp} { set minYp $yp }
            if {$yp > $maxYp} { set maxYp $yp }
        }
        expr { $maxYp - $minYp }
    }

    proc top {r} {
        # Returns the vec2 point at the top of the region.
        set rp [rotate $r [- [angle $r]]]
        # Reduce all edges to their midpoints
        set edgeMidpoints [lmap e [edges $rp] {
            lassign [edgeToLineSegment $rp $e] p1 p2
            vec2 midpoint $p1 $p2
        }]
        # Which edge has the topmost midpoint y-coordinate?
        set topEdgeIndex [lindex [lsort -indices -real -index 1 $edgeMidpoints] 0]
        vec2 midpoint {*}[edgeToLineSegment $r [lindex [edges $r] $topEdgeIndex]]
    }
    proc left {r} {
        # Returns the vec2 point at the left of the region.
        set rp [rotate $r [- [angle $r]]]
        # Reduce all edges to their midpoints
        set edgeMidpoints [lmap e [edges $rp] {
            lassign [edgeToLineSegment $rp $e] p1 p2
            vec2 midpoint $p1 $p2
        }]
        # Which edge has the leftmost midpoint y-coordinate?
        set leftEdgeIndex [lindex [lsort -indices -real -index 0 $edgeMidpoints] 0]
        vec2 midpoint {*}[edgeToLineSegment $r [lindex [edges $r] $leftEdgeIndex]]
    }
    proc right {r} {
        # Returns the vec2 point at the right of the region.
        set rp [rotate $r [- [angle $r]]]
        # Reduce all edges to their midpoints
        set edgeMidpoints [lmap e [edges $rp] {
            lassign [edgeToLineSegment $rp $e] p1 p2
            vec2 midpoint $p1 $p2
        }]
        # Which edge has the rightmost midpoint y-coordinate?
        set rightEdgeIndex [lindex [lsort -indices -real -index 0 $edgeMidpoints] end]
        vec2 midpoint {*}[edgeToLineSegment $r [lindex [edges $r] $rightEdgeIndex]]
    }
    proc bottom {r} {
        # Returns the vec2 point at the bottom of the region.
        set rp [rotate $r [- [angle $r]]]
        # Reduce all edges to their midpoints
        set edgeMidpoints [lmap e [edges $rp] {
            lassign [edgeToLineSegment $rp $e] p1 p2
            vec2 midpoint $p1 $p2
        }]
        # Which edge has the bottommost midpoint y-coordinate?
        set bottomEdgeIndex [lindex [lsort -indices -real -index 1 $edgeMidpoints] end]
        vec2 midpoint {*}[edgeToLineSegment $r [lindex [edges $r] $bottomEdgeIndex]]
    }
    proc bottomleft {r} {
      lindex [vertices $r] 0
    }
    proc bottomright {r} {
      lindex [vertices $r] 1
    }
    proc topright {r} {
      lindex [vertices $r] 2
    }
    proc topleft {r} {
      lindex [vertices $r] 3
    }

    proc mapVertices {varname r body} {
        lreplace $r 0 0 [uplevel [list lmap $varname [vertices $r] $body]]
    }

    proc edgeToLineSegment {r e} {
        list [lindex [vertices $r] [lindex $e 0]] [lindex [vertices $r] [lindex $e 1]]
    }
    proc distance {r1 r2} {
        set minDist 1e9
        foreach v1 [vertices $r1] e2 [edges $r2] {
            set dist [vec2 distanceToLineSegment $v1 {*}[edgeToLineSegment $r2 $e2]]
            if {$dist < $minDist} { set minDist $dist }
        }
        set minDist
    }

    proc contains {r1 p} {
        lassign $r1 vertices edges
        lassign $vertices a b c d

        set ab [vec2 sub $b $a]
        set ap [vec2 sub $p $a]
        set bc [vec2 sub $c $b]
        set bp [vec2 sub $p $b]
        set dot_abap [vec2 dot $ab $ap]
        set dot_bcbp [vec2 dot $bc $bp]

        expr {0 <= $dot_abap && $dot_abap <= [vec2 dot $ab $ab] && \
                0 <= $dot_bcbp && $dot_bcbp <= [vec2 dot $bc $bc]}
    }
    proc intersects {r1 r2} {
        # Either r1 should contain a vertex of r2 or r2 should contain a vertex of r1
        foreach v1 [vertices $r1] {
            if {[contains $r2 $v1]} { return true }
        }
        foreach v2 [vertices $r2] {
            if {[contains $r1 $v2]} { return true }
        }
        expr false
    }

   proc centroid {r1} {
        # This only works for rectangular regions
        lassign $r1 vertices edges
        lassign $vertices a b c d

        set vecsum [vec2 add [vec2 add [vec2 add $a $b] $c] $d]
        vec2 scale $vecsum 0.25
    }

    proc rotate {r angle} {
        set theta [angle $r]
        set c [centroid $r]
        set r' [mapVertices v $r {
            set v [vec2 sub $v $c]
            set v [vec2 rotate $v $angle]
            set v [vec2 add $v $c]
            set v
        }]
        lset r' 2 [+ $theta $angle]
        set r'
    }

    # Scales about the center of the region, along the x and y axes of
    # the space of the region (not the global x and y).
    proc scale {r args} {
        if {[llength $args] == 1} {
            set args [list width [lindex $args 0] height [lindex $args 0]]
        }
        set sxp 1; set syp 1
        foreach {dim value} $args {
            set theta [angle $r]
            set c [centroid $r]

            if {![regexp {([0-9\.]+)(px|%)?} $value -> value unit]} {
                error "region scale: Invalid scale value $value"
            }

            if {$dim eq "width"} {
                if {$unit eq "px"} {
                    set sxp [* $sxp [/ $value [width $r]]]
                } elseif {$unit eq "%"} {
                    set sxp [* $sxp $value 0.01]
                } elseif {$unit eq ""} {
                    set sxp [* $sxp $value]
                }
            } elseif {$dim eq "height"} {
                if {$unit eq "px"} {
                    set syp [* $syp [/ $value [height $r]]]
                } elseif {$unit eq "%"} {
                    set syp [* $syp [* $value 0.01]]
                } elseif {$unit eq ""} {
                    set syp [* $syp $value]
                }
            } else {
                error "region scale: Invalid dimension $dim"
            }
        }

        # TODO: Optimize
        set r [mapVertices v $r {
            set v [vec2 sub $v $c]
            set v [vec2 rotate $v [* -1 $theta]]
            set v [vec2 scale $v $sxp $syp]
            set v [vec2 rotate $v $theta]
            set v [vec2 add $v $c]
            set v
        }]
        set r
    }

    # Moves the region left/right/up/down along the x and y axes of
    # the space of the region (not the global x and y).
    proc move {r args} {
        foreach {direction distance} $args {
            set theta [angle $r]

            if {![regexp {([0-9\.]+)(px|%)?} $distance -> distance unit]} {
                error "region move: Invalid distance $distance"
            }
            if {$direction ne "left" && $direction ne "right" &&
                $direction ne "up" && $direction ne "down"} {
                error "region move: Invalid direction $direction"
            }

            if {$unit eq "%"} {
                set distance [* $distance 0.01]
                set unit ""
            }
            if {$unit eq ""} {
                # Convert to pixels
                if {$direction eq "left" || $direction eq "right"} {
                    set distance [expr {[width $r] * $distance}]
                } elseif {$direction eq "up" || $direction eq "down"} {
                    set distance [expr {[height $r] * $distance}]
                }
            }
            set dxp [if {$direction eq "left"} {- $distance} \
                     elseif {$direction eq "right"} {+ $distance} \
                     else {+ 0}]
            set dyp [if {$direction eq "up"} {- $distance} \
                     elseif {$direction eq "down"} {+ $distance} \
                     else {+ 0}]
            set dv [vec2 rotate [list $dxp $dyp] $theta]
            set r [mapVertices v $r {vec2 add $v $dv}]
        }
        set r
    }

    namespace export *
    namespace ensemble create
}

proc rectanglesOverlap {P1 P2 Q1 Q2 strict} {
    set b1x1 [lindex $P1 0]
    set b1y1 [lindex $P1 1]
    set b1x2 [lindex $P2 0]
    set b1y2 [lindex $P2 1]
    set b2x1 [lindex $Q1 0]
    set b2y1 [lindex $Q1 1]
    set b2x2 [lindex $Q2 0]
    set b2y2 [lindex $Q2 1]
    # ensure b1x1<=b1x2 etc.
    if {$b1x1 > $b1x2} {
	set temp $b1x1
	set b1x1 $b1x2
	set b1x2 $temp
    }
    if {$b1y1 > $b1y2} {
	set temp $b1y1
	set b1y1 $b1y2
	set b1y2 $temp
    }
    if {$b2x1 > $b2x2} {
	set temp $b2x1
	set b2x1 $b2x2
	set b2x2 $temp
    }
    if {$b2y1 > $b2y2} {
	set temp $b2y1
	set b2y1 $b2y2
	set b2y2 $temp
    }
    # Check if the boxes intersect
    # (From: Cormen, Leiserson, and Rivests' "Algorithms", page 889)
    if {$strict} {
	return [expr {($b1x2>$b2x1) && ($b2x2>$b1x1) \
		&& ($b1y2>$b2y1) && ($b2y2>$b1y1)}]
    } else {
	return [expr {($b1x2>=$b2x1) && ($b2x2>=$b1x1) \
		&& ($b1y2>=$b2y1) && ($b2y2>=$b1y1)}]
    }
}

# TODO: verticesToBbox is better name
# It only really uses the first thing in the region
proc regionToBbox {region} {
    set vertices [lindex $region 0]
    set minX 100000
    set minY 100000
    set maxX -100000
    set maxY -100000
    foreach vertex $vertices {
        set x [lindex $vertex 0]
        set y [lindex $vertex 1]
        if {$x < $minX} {set minX $x}
        if {$y < $minY} {set minY $y}
        if {$x > $maxX} {set maxX $x}
        if {$y > $maxY} {set maxY $y}
    }
    return [list $minX $minY $maxX $maxY]
}
proc boxCentroid {box} {
    # TODO: assert that it's actually a box
    lassign $box minX minY maxX maxY
    set x [expr {($minX + $maxX)/2}]
    set y [expr {($minY + $maxY)/2}]
    return [list $x $y]
}
proc boxWidth {box} {
    lassign $box minX minY maxX maxY
    return [expr {$maxX - $minX}]
}
proc boxHeight {box} {
    lassign $box minX minY maxX maxY
    return [expr {$maxY - $minY}]
}
# TODO: write in C
# TODO: triangulate the region
# TODO: average the centroids of all triangles in the region