~ubuntu-branches/ubuntu/precise/graphviz/precise-security

« back to all changes in this revision

Viewing changes to cmd/smyrna/selection.c

  • Committer: Bazaar Package Importer
  • Author(s): David Claughton
  • Date: 2010-03-24 22:45:18 UTC
  • mfrom: (1.2.7 upstream) (6.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100324224518-do441tthbqjaqjzd
Tags: 2.26.3-4
Add patch to fix segfault in circo. Backported from upstream snapshot
release.  Thanks to Francis Russell for his work on this.
(Closes: #575255)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: selection.c,v 1.8 2008/04/23 19:08:53 arif Exp $ $Revision: 1.8 $ */
 
1
/* $Id: selection.c,v 1.27 2009/12/03 19:16:04 arif Exp $ $Revision: 1.27 $ */
2
2
/* vim:set shiftwidth=4 ts=8: */
3
3
 
4
4
/**********************************************************
19
19
#include "geomprocs.h"
20
20
#include "memory.h"
21
21
 
 
22
#ifdef UNUSED
22
23
static int rectintersects(float x, float y, float W, float H)
23
24
{
24
25
    //returns 1 if rect is completely in the clip rect
103
104
        return 1;
104
105
 
105
106
}
 
107
#endif
106
108
 
 
109
#ifdef UNUSED
107
110
static int spline_in_rect(xdot_op * op)
108
111
{
109
112
    //JUST SEND ALL CONTROL POINTS IN 3D ARRAYS
340
343
    int i, j, c = 0;
341
344
    int npol = op->u.polygon.cnt;
342
345
    float x, y;
343
 
#if 0
 
346
#ifdef UNUSED
344
347
// FIX
345
348
    op->u.polygon.pts[i].y;
346
349
    op->u.polygon.pts[i].x;
366
369
    }
367
370
    return c;
368
371
}
369
 
 
 
372
#endif
370
373
 
371
374
//select functions
372
 
static int select_graph(Agraph_t * g, Agraph_t * G)
373
 
{
374
 
    int ind = 0;
375
 
    //check if in the list
376
 
    for (ind = 0; ind < GD_selectedGraphsCount(g); ind++) {
377
 
        if (GD_selectedGraphs(g)[ind] == G)
378
 
            return 0;
379
 
    }
380
 
    //for single selections i think realloc is ok, for mass selections i ll figure out something else
381
 
    GD_selectedGraphs(g) = RALLOC(GD_selectedGraphsCount(g)+1,
382
 
                GD_selectedGraphs(g), Agraph_t*);
383
 
    GD_selectedGraphs(g)[GD_selectedGraphsCount(g)] = G;
384
 
    GD_selectedGraphsCount(g)++;
385
 
    OD_Selected(G) = 1;
386
 
    return 1;
387
 
}
388
 
 
389
 
int select_node(Agraph_t * g, Agnode_t * N)
390
 
{
391
 
    int ind = 0;
392
 
    //check if in the list
393
 
    for (ind = 0; ind < GD_selectedNodesCount(g); ind++) {
394
 
        if (GD_selectedNodes(g)[ind] == N)
395
 
            return 0;
396
 
    }
397
 
    //for single selections i think realloc is ok, for mass selections i ll figure out something else
398
 
    GD_selectedNodes(g) =
399
 
        RALLOC(GD_selectedNodesCount(g)+2,GD_selectedNodes(g), Agnode_t*);
400
 
    GD_selectedNodes(g)[GD_selectedNodesCount(g)] = N;
401
 
    GD_selectedNodesCount(g)++;
402
 
    OD_Selected(N) = 1;
403
 
    return 1;
404
 
}
405
 
 
406
 
int select_edge(Agraph_t * g, Agedge_t * E)
407
 
{
408
 
    int ind = 0;
409
 
    //check if in the list
410
 
    for (ind = 0; ind < GD_selectedEdgesCount(g); ind++) {
411
 
        if (GD_selectedEdges(g)[ind] == E)
412
 
            return 0;
413
 
    }
414
 
    //for single selections i think realloc is ok, for mass selections i ll figure out something else
415
 
    GD_selectedEdges(g) =
416
 
        RALLOC(GD_selectedEdgesCount(g)+1,GD_selectedEdges(g), Agedge_t*);
417
 
    GD_selectedEdges(g)[GD_selectedEdgesCount(g)] = E;
418
 
    GD_selectedEdgesCount(g)++;
419
 
    OD_Selected(E) = 1;
420
 
    return 1;
421
 
 
422
 
}
423
 
int select_object(Agraph_t * g, void *obj)
424
 
{
425
 
    switch (AGTYPE(obj)) {
426
 
    case AGNODE:
427
 
        select_node(g, obj);
428
 
        break;
429
 
    case AGEDGE:
430
 
        select_edge(g, obj);
431
 
        break;
432
 
    case AGRAPH:
433
 
        select_graph(g, obj);
434
 
        break;
435
 
    default:
436
 
        break;
437
 
    }
438
 
    return 1;
439
 
}
440
 
 
441
 
int deselect_node(Agraph_t * g, Agnode_t * N)
442
 
{
443
 
    int ind = 0;
444
 
    int valid = 0;
445
 
    //check if in the list
446
 
    for (ind = 0; ind < GD_selectedNodesCount(g); ind++) {
447
 
        if (valid)
448
 
            GD_selectedNodes(g)[ind - 1] = GD_selectedNodes(g)[ind];
449
 
        if (GD_selectedNodes(g)[ind] == N)
450
 
            valid = 1;
451
 
    }
452
 
    //for single selections i think realloc is ok, for mass selections i ll figure out something else
453
 
    if (valid) {
454
 
        GD_selectedNodes(g) =
455
 
            RALLOC(GD_selectedNodesCount(g)-1,GD_selectedNodes(g), Agnode_t*);
456
 
        GD_selectedNodesCount(g)--;
457
 
        OD_Selected(N) = 0;
458
 
        OD_SelFlag(N) = 0;
459
 
    }
460
 
    return 1;
461
 
 
462
 
}
463
 
 
464
 
int deselect_edge(Agraph_t * g, Agedge_t * E)
465
 
{
466
 
    int ind = 0;
467
 
    int valid = 0;
468
 
    //check if in the list
469
 
    for (ind = 0; ind < GD_selectedEdgesCount(g); ind++) {
470
 
        if (valid)
471
 
            GD_selectedEdges(g)[ind-1] = GD_selectedEdges(g)[ind];
472
 
        if (GD_selectedEdges(g)[ind] == E)
473
 
            valid = 1;
474
 
    }
475
 
    //for single selections i think realloc is ok, for mass selections i ll figure out something else
476
 
    if (valid) {
477
 
        GD_selectedEdges(g) =
478
 
            RALLOC(GD_selectedEdgesCount(g)-1,GD_selectedEdges(g), Agedge_t*);
479
 
        GD_selectedEdgesCount(g)--;
480
 
        OD_Selected(E) = 0;
481
 
        OD_SelFlag(E) = 0;
482
 
 
483
 
    }
484
 
    return 1;
485
 
}
486
 
static int deselect_graph(Agraph_t * g, Agraph_t * G)
487
 
{
488
 
    int ind = 0;
489
 
    int valid = 0;
490
 
    //check if in the list
491
 
    for (ind = 0; ind < GD_selectedGraphsCount(g); ind++) {
492
 
        if (valid)
493
 
            GD_selectedGraphs(g)[ind-1] = GD_selectedGraphs(g)[ind];
494
 
        if (GD_selectedGraphs(g)[ind] == G)
495
 
            valid = 1;
496
 
    }
497
 
    //for single selections i think realloc is ok, for mass selections i ll figure out something else
498
 
    if (valid) {
499
 
        GD_selectedGraphs(g) =
500
 
            RALLOC(GD_selectedGraphsCount(g)-1,GD_selectedGraphs(g), Agraph_t*);
501
 
        GD_selectedGraphsCount(g)--;
502
 
        OD_Selected(G) = 0;
503
 
        OD_SelFlag(G) = 0;
504
 
    }
505
 
    return 1;
506
 
}
507
 
int deselect_object(Agraph_t * g, void *obj)
508
 
{
509
 
    switch (AGTYPE(obj)) {
510
 
    case AGNODE:
511
 
        deselect_node(g, obj);
512
 
        break;
513
 
    case AGEDGE:
514
 
        deselect_edge(g, obj);
515
 
        break;
516
 
    case AGRAPH:
517
 
        deselect_graph(g, obj);
518
 
        break;
519
 
    default:
520
 
        break;
521
 
    }
522
 
    return 1;
523
 
}
 
375
 
 
376
static Agsym_t *getNodeSelectedAttr(Agraph_t * g)
 
377
{
 
378
    static Agraph_t *saveg;
 
379
    static Agsym_t *saveattr;
 
380
 
 
381
    if (saveg != g) {
 
382
        saveg = g;
 
383
        if (!(saveattr = agattr(saveg, AGNODE, "selected", 0))) {
 
384
            saveattr = agattr(saveg, AGNODE, "selected", "0");
 
385
        }
 
386
    }
 
387
    return saveattr;
 
388
}
 
389
 
 
390
static Agsym_t *getEdgeSelectedAttr(Agraph_t * g)
 
391
{
 
392
    static Agraph_t *saveg;
 
393
    static Agsym_t *saveattr;
 
394
 
 
395
    if (saveg != g) {
 
396
        saveg = g;
 
397
        if (!(saveattr = agattr(saveg, AGEDGE, "selected", 0))) {
 
398
            saveattr = agattr(saveg, AGEDGE, "selected", "0");
 
399
        }
 
400
    }
 
401
    return saveattr;
 
402
}
 
403
 
 
404
int select_node(topview_node * N)
 
405
{
 
406
    Agsym_t *a = getNodeSelectedAttr(N->Node->root);
 
407
 
 
408
    N->data.Selected = 1;
 
409
    return agxset(N->Node, a, "1");
 
410
}
 
411
 
 
412
int select_edge(topview_edge * E)
 
413
{
 
414
    Agsym_t *a = getEdgeSelectedAttr(aghead(E->Edge)->root);
 
415
 
 
416
    E->data.Selected = 1;
 
417
    return agxset(E->Edge, a, "1");
 
418
}
 
419
 
 
420
int deselect_node(topview_node * N)
 
421
{
 
422
    Agsym_t *a = getNodeSelectedAttr(N->Node->root);
 
423
 
 
424
    N->data.Selected = 0;
 
425
    return agxset(N->Node, a, "0");
 
426
}
 
427
 
 
428
int deselect_edge(topview_edge * E)
 
429
{
 
430
    Agsym_t *a = getEdgeSelectedAttr(aghead(E->Edge)->root);
 
431
 
 
432
    E->data.Selected = 0;
 
433
    return agxset(E->Edge, a, "0");
 
434
}
 
435
 
 
436
#if UNUSED
 
437
static void update_cgraph_pos(topview_node * N)
 
438
{
 
439
    char buf[512];
 
440
    Agsym_t *pos;
 
441
    Agnode_t *obj = N->Node;
 
442
 
 
443
    if ((pos = agattrsym(obj, "pos"))) {
 
444
        sprintf(buf, "%lf,%lf", N->distorted_x, N->distorted_y);
 
445
        agxset(obj, pos, buf);
 
446
    }
 
447
}
 
448
#endif
524
449
 
525
450
int select_all_nodes(Agraph_t * g)
526
451
{
527
 
    Agnode_t *n;
528
 
    for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
529
 
        select_node(g, n);
 
452
    int ind = 0;
 
453
    //check if in the list
 
454
    for (ind = 0; ind < view->Topview->Nodecount; ind++) {
 
455
        select_node(&view->Topview->Nodes[ind]);
530
456
    }
531
457
    return 1;
532
 
 
533
458
}
534
459
 
535
460
int select_all_edges(Agraph_t * g)
536
461
{
537
 
    Agnode_t *n;
538
 
    Agedge_t *e;
539
 
 
540
 
 
541
 
    n = agfstnode(g);
542
 
 
543
 
    for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
544
 
        for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
545
 
            select_edge(g, e);
546
 
        }
 
462
    int ind = 0;
 
463
    //check if in the list
 
464
    for (ind = 0; ind < view->Topview->Edgecount; ind++) {
 
465
        select_edge(&view->Topview->Edges[ind]);
547
466
    }
548
467
    return 1;
549
468
 
550
469
}
551
470
 
552
 
int select_all_graphs(Agraph_t * g)
553
 
{
554
 
    Agraph_t *s;
555
 
    for (s = agfstsubg(g); s; s = agnxtsubg(s))
556
 
        select_graph(g, s);
557
 
    return 1;
558
 
}
559
471
 
560
472
int deselect_all_nodes(Agraph_t * g)
561
473
{
562
 
    Agnode_t *n;
563
 
 
564
 
 
565
 
    n = agfstnode(g);
566
 
 
567
 
 
568
 
    for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
569
 
        deselect_node(g, n);
 
474
    int ind = 0;
 
475
    //check if in the list
 
476
    for (ind = 0; ind < view->Topview->Nodecount; ind++) {
 
477
        deselect_node(&view->Topview->Nodes[ind]);
570
478
    }
571
479
    return 1;
572
480
 
573
 
 
574
481
}
575
482
 
576
483
int deselect_all_edges(Agraph_t * g)
577
484
{
578
 
    {
579
 
        Agnode_t *n;
580
 
        Agedge_t *e;
581
 
        n = agfstnode(g);
582
 
        for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
583
 
            for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
584
 
                deselect_edge(g, e);
585
 
            }
586
 
        }
587
 
        return 1;
 
485
    int ind = 0;
 
486
    //check if in the list
 
487
    for (ind = 0; ind < view->Topview->Edgecount; ind++) {
 
488
        deselect_edge(&view->Topview->Edges[ind]);
588
489
    }
589
 
 
590
 
 
591
 
}
592
 
int deselect_all_graphs(Agraph_t * g)
593
 
{
594
 
    Agraph_t *s;
595
 
    for (s = agfstsubg(g); s; s = agnxtsubg(s))
596
 
        deselect_graph(g, s);
597
490
    return 1;
 
491
 
598
492
}
599
493
 
600
494
int select_all(Agraph_t * g)
601
495
{
602
496
    select_all_nodes(g);
603
497
    select_all_edges(g);
604
 
    select_all_graphs(g);
605
498
    return 1;
606
499
 
607
500
}
610
503
{
611
504
    deselect_all_nodes(g);
612
505
    deselect_all_edges(g);
613
 
    deselect_all_graphs(g);
614
506
    return 1;
 
507
 
615
508
}
616
509
 
 
510
 
617
511
int lineintersects(float X1, float X2, float Y1, float Y2)
618
512
{
619
513
    //line segment
631
525
    int intersects, in;
632
526
    RX = view->Selection.X;
633
527
    RY = view->Selection.Y;
634
 
    RW = view->Selection.W;
635
 
    RH = view->Selection.H;
 
528
    RH=view->Selection.H;
 
529
    RW=view->Selection.W;
636
530
    if ((is_point_in_rectangle(X1, Y1, RX, RY, RW, RH))
637
531
        && (is_point_in_rectangle(X2, Y2, RX, RY, RW, RH)))
638
532
        return 1;
681
575
 
682
576
}
683
577
 
684
 
int SelectBeziers(sdot_op * op)
685
 
{
686
 
    if (!view->Selection.Active)
687
 
        return 0;
688
 
    switch (view->Selection.Type) {
689
 
    case 0:
690
 
        if (view->Selection.AlreadySelected)
691
 
            return 0;
692
 
        if (spline_x_rect((xdot_op *) op)) {
693
 
            if (OD_Selected(op->obj) == 0) {
694
 
                OD_Selected(op->obj) = 1;
695
 
                select_object(view->g[view->activeGraph], op->obj);
696
 
                view->Selection.AlreadySelected = 1;
697
 
            } else {
698
 
                OD_Selected(op->obj) = 0;
699
 
                deselect_object(view->g[view->activeGraph], op->obj);
700
 
                view->Selection.AlreadySelected = 1;
701
 
            }
702
 
        }
703
 
        break;
704
 
    case 1:
705
 
#if 0
706
 
        if (OD_Selected(((xdot*)(op->parentxdot))->obj) == 1)
707
 
               return 0;
708
 
#endif
709
 
        if ((OD_SelFlag(op->obj) != -1) && spline_in_rect((xdot_op *) op)) {
710
 
            OD_Preselected(op->obj) = 1;
711
 
//          select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
712
 
            view->Selection.AlreadySelected = 1;
713
 
        } else {
714
 
            OD_Preselected(op->obj) = 0;
715
 
//                        deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
716
 
            view->Selection.AlreadySelected = 1;
717
 
            OD_SelFlag(op->obj) = -1;
718
 
        }
719
 
        break;
720
 
    case 2:
721
 
        if (OD_Selected(op->obj) == 1)
722
 
            return 0;
723
 
        if (spline_x_rect((xdot_op *) op)) {
724
 
            OD_Selected(op->obj) = 1;
725
 
            select_object(view->g[view->activeGraph], op->obj);
726
 
            view->Selection.AlreadySelected = 1;
727
 
        }
728
 
        break;
729
 
    default:
730
 
        return 0;
731
 
    }
732
 
    return 1;
733
 
 
734
 
}
735
 
 
736
 
int SelectPolygon(sdot_op * op)
737
 
{
738
 
 
739
 
    if (!view->Selection.Active)
740
 
        return 0;
741
 
    switch (view->Selection.Type) {
742
 
    case 0:
743
 
        if (view->Selection.AlreadySelected)
744
 
            return 0;
745
 
        if ((point_within_polygon((xdot_op *) op))
746
 
            || (polygon_x_rect((xdot_op *) op))) {
747
 
            if (OD_Selected(op->obj) == 0) {
748
 
                OD_Selected(op->obj) = 1;
749
 
                select_object(view->g[view->activeGraph], op->obj);
750
 
                view->Selection.AlreadySelected = 1;
751
 
            } else {
752
 
                OD_Selected(op->obj) = 0;
753
 
                deselect_object(view->g[view->activeGraph], op->obj);
754
 
                view->Selection.AlreadySelected = 1;
755
 
            }
756
 
        }
757
 
        break;
758
 
    case 1:
759
 
        if (OD_Selected(op->obj) == 1)
760
 
            return 0;
761
 
        if ((OD_SelFlag(op->obj) != -1) && polygon_in_rect((xdot_op *) op)) {
762
 
            OD_Preselected(op->obj) = 1;
763
 
//  select_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
764
 
//  view->Selection.AlreadySelected=1;
765
 
        } else {
766
 
            OD_Selected(op->obj) = 1;
767
 
            OD_Preselected(op->obj) = 0;
768
 
//  deselect_object (view->g[view->activeGraph],((xdot*)(op->parentxdot))->obj);
769
 
            view->Selection.AlreadySelected = 1;
770
 
            OD_SelFlag(op->obj) = -1;
771
 
        }
772
 
        break;
773
 
    case 2:
774
 
        if (OD_Selected(op->obj) == 1)
775
 
            return 0;
776
 
        if (polygon_x_rect((xdot_op *) op)) {
777
 
            OD_Selected(op->obj) = 1;
778
 
            select_object(view->g[view->activeGraph], op->obj);
779
 
            view->Selection.AlreadySelected = 1;
780
 
        }
781
 
        break;
782
 
    default:
783
 
        return 0;
784
 
    }
785
 
    return 1;
786
 
}
787
 
 
788
 
int SelectPolyline(sdot_op * op)
789
 
{
790
 
    if (!view->Selection.Active)
791
 
        return 0;
792
 
    switch (view->Selection.Type) {
793
 
    case 0:
794
 
        if (view->Selection.AlreadySelected)
795
 
            return 0;
796
 
 
797
 
        if (polyline_x_rect((xdot_op *) op)) {
798
 
            if (OD_Selected(op->obj) == 0) {
799
 
                OD_Selected(op->obj) = 1;
800
 
                select_object(view->g[view->activeGraph], op->obj);
801
 
                view->Selection.AlreadySelected = 1;
802
 
            } else {
803
 
                OD_Selected(op->obj) = 0;
804
 
                deselect_object(view->g[view->activeGraph], op->obj);
805
 
                view->Selection.AlreadySelected = 1;
806
 
            }
807
 
        }
808
 
        break;
809
 
    case 1:
810
 
        if (OD_Selected(op->obj) == 1)
811
 
            return 0;
812
 
        if ((OD_SelFlag(op->obj) != -1)
813
 
            && polyline_in_rect((xdot_op *) op)) {
814
 
            OD_Preselected(op->obj) = 1;
815
 
        } else {
816
 
            OD_Preselected(op->obj) = 0;
817
 
            view->Selection.AlreadySelected = 1;
818
 
            OD_SelFlag(op->obj) = -1;
819
 
        }
820
 
 
821
 
 
822
 
        break;
823
 
    case 2:
824
 
        if (OD_Selected(op->obj) == 1)
825
 
            return 0;
826
 
        if (polyline_x_rect((xdot_op *) op)) {
827
 
            OD_Selected(op->obj) = 1;
828
 
            select_object(view->g[view->activeGraph], op->obj);
829
 
            view->Selection.AlreadySelected = 1;
830
 
        }
831
 
        break;
832
 
    default:
833
 
        return 0;
834
 
    }
835
 
 
836
 
    return 1;
837
 
 
838
 
}
839
 
 
840
 
int SelectEllipse(sdot_op * op)
841
 
{
842
 
    if (!view->Selection.Active)
843
 
        return 0;
844
 
    switch (view->Selection.Type) {
845
 
    case 0:
846
 
        if (view->Selection.AlreadySelected)
847
 
            return 0;
848
 
 
849
 
        if (point_within_ellipse((xdot_op *) op)) {
850
 
            if (OD_Selected(op->obj) == 0) {
851
 
                OD_Selected(op->obj) = 1;
852
 
                select_object(view->g[view->activeGraph], op->obj);
853
 
                view->Selection.AlreadySelected = 1;
854
 
 
855
 
            } else {
856
 
                OD_Selected(op->obj) = 0;
857
 
                deselect_object(view->g[view->activeGraph], op->obj);
858
 
                view->Selection.AlreadySelected = 1;
859
 
 
860
 
            }
861
 
        } else if (ellipse_x_rect((xdot_op *) op)) {
862
 
            if (OD_Selected(op->obj) == 0) {
863
 
                OD_Selected(op->obj) = 1;
864
 
                select_object(view->g[view->activeGraph], op->obj);
865
 
                view->Selection.AlreadySelected = 1;
866
 
            } else {
867
 
                OD_Selected(op->obj) = 0;
868
 
                deselect_object(view->g[view->activeGraph], op->obj);
869
 
                view->Selection.AlreadySelected = 1;
870
 
            }
871
 
        }
872
 
        break;
873
 
    case 1:
874
 
        if (OD_Selected(op->obj) == 1)
875
 
            return 0;
876
 
        if ((OD_SelFlag(op->obj) != -1) && ellipse_in_rect((xdot_op *) op)) {
877
 
            OD_Preselected(op->obj) = 1;
878
 
            view->Selection.AlreadySelected = 1;
879
 
        } else {
880
 
            OD_Preselected(op->obj) = 0;
881
 
            view->Selection.AlreadySelected = 1;
882
 
            OD_SelFlag(op->obj) = -1;
883
 
        }
884
 
        break;
885
 
    case 2:
886
 
        if (OD_Selected(op->obj) == 1)
887
 
            return 0;
888
 
        if (ellipse_x_rect((xdot_op *) op)) {
889
 
            OD_Selected(op->obj) = 1;
890
 
            select_object(view->g[view->activeGraph], op->obj);
891
 
            view->Selection.AlreadySelected = 1;
892
 
 
893
 
        }
894
 
        break;
895
 
    default:
896
 
        return 0;
897
 
    }
898
 
    return 1;
899
 
}
900
 
 
901
 
int SelectText(sdot_op * op)
902
 
{
903
 
    if (!view->Selection.Active)
904
 
        return 0;
905
 
    switch (view->Selection.Type) {
906
 
    case 0:
907
 
        if (view->Selection.AlreadySelected)
908
 
            return 0;
909
 
        if (text_x_rect(op)) {
910
 
            if (OD_Selected(op->obj) == 0)
911
 
                OD_Selected(op->obj) = 1;
912
 
            else
913
 
                OD_Selected(op->obj) = 0;
914
 
        }
915
 
        break;
916
 
    case 1:
917
 
        if (OD_Selected(op->obj) == 1)
918
 
            return 0;
919
 
        if (text_in_rect(op)) {
920
 
            OD_Selected(op->obj) = 1;
921
 
            select_object(view->g[view->activeGraph], op->obj);
922
 
            view->Selection.AlreadySelected = 1;
923
 
        }
924
 
        break;
925
 
    case 2:
926
 
        if (OD_Selected(op->obj) == 1)
927
 
            return 0;
928
 
        if (text_x_rect(op)) {
929
 
            OD_Selected(op->obj) = 1;
930
 
            select_object(view->g[view->activeGraph], op->obj);
931
 
            view->Selection.AlreadySelected = 1;
932
 
        }
933
 
        break;
934
 
    default:
935
 
        return 0;
936
 
    }
937
 
    return 1;
938
 
 
939
 
}
940
 
 
941
 
int SelectImage(sdot_op * op)
942
 
{
943
 
    if (!view->Selection.Active)
944
 
        return 0;
945
 
    switch (view->Selection.Type) {
946
 
    case 0:
947
 
        if (view->Selection.AlreadySelected)
948
 
            return 0;
949
 
        if (image_x_rect((xdot_op *) op)) {
950
 
            if (OD_Selected(op->obj) == 0) {
951
 
                OD_Selected(op->obj) = 1;
952
 
                select_object(view->g[view->activeGraph], op->obj);
953
 
                view->Selection.AlreadySelected = 1;
954
 
            } else
955
 
                OD_Selected(op->obj) = 0;
956
 
        }
957
 
        break;
958
 
    case 1:
959
 
        if (OD_Selected(op->obj) == 1)
960
 
            return 0;
961
 
        if (image_in_rect((xdot_op *) op))
962
 
            OD_Selected(op->obj) = 1;
963
 
        break;
964
 
    case 2:
965
 
        if (OD_Selected(op->obj) == 1)
966
 
            return 0;
967
 
        if (image_x_rect((xdot_op *) op))
968
 
            OD_Selected(op->obj) = 1;
969
 
        break;
970
 
    default:
971
 
        return 0;
972
 
    }
973
 
    return 1;
974
 
 
975
 
}
976
 
 
977
 
#if 0
 
578
 
 
579
 
 
580
static void select_topview_node(topview_node * n)
 
581
{
 
582
    static float x1,y1,x2,y2,x,y;
 
583
    x=n->distorted_x;
 
584
    y=n->distorted_y;
 
585
    if(view->mouse.GLfinalPos.x > view->mouse.GLinitPos.x)
 
586
    {
 
587
        x1=view->mouse.GLinitPos.x;
 
588
        x2=view->mouse.GLfinalPos.x;
 
589
    }
 
590
    else
 
591
    {
 
592
        x2=view->mouse.GLinitPos.x;
 
593
        x1=view->mouse.GLfinalPos.x;
 
594
 
 
595
    }
 
596
    if(view->mouse.GLfinalPos.y > view->mouse.GLinitPos.y)
 
597
    {
 
598
        y1=view->mouse.GLinitPos.y;
 
599
        y2=view->mouse.GLfinalPos.y;
 
600
    }
 
601
    else
 
602
    {
 
603
        y2=view->mouse.GLinitPos.y;
 
604
        y1=view->mouse.GLfinalPos.y;
 
605
    }
 
606
    if(is_point_in_rectangle(x,y,x1,y1,x2-x1,y2-y1))
 
607
    {
 
608
        agset(n->Node,"selected","1");
 
609
        n->data.Selected=1;
 
610
    }
 
611
}
 
612
 
 
613
static void select_topview_edge(topview_edge* e)
 
614
{
 
615
    static float x1,y1,x2,y2,n1x,n1y,n2x,n2y;
 
616
    n1x=e->Node1->distorted_x;
 
617
    n1y=e->Node1->distorted_y;
 
618
    n2x=e->Node2->distorted_x;
 
619
    n2y=e->Node2->distorted_y;
 
620
    if(view->mouse.GLfinalPos.x > view->mouse.GLinitPos.x)
 
621
    {
 
622
        x1=view->mouse.GLinitPos.x;
 
623
        x2=view->mouse.GLfinalPos.x;
 
624
    }
 
625
    else
 
626
    {
 
627
        x2=view->mouse.GLinitPos.x;
 
628
        x1=view->mouse.GLfinalPos.x;
 
629
 
 
630
    }
 
631
    if(view->mouse.GLfinalPos.y > view->mouse.GLinitPos.y)
 
632
    {
 
633
        y1=view->mouse.GLinitPos.y;
 
634
        y2=view->mouse.GLfinalPos.y;
 
635
    }
 
636
    else
 
637
    {
 
638
        y2=view->mouse.GLinitPos.y;
 
639
        y1=view->mouse.GLfinalPos.y;
 
640
    }
 
641
    if((is_point_in_rectangle(n1x,n1y,x1,y1,x2-x1,y2-y1))
 
642
        &&
 
643
        (is_point_in_rectangle(n2x,n2y,x1,y1,x2-x1,y2-y1)) )
 
644
    {
 
645
        agset(e->Edge,"selected","1");
 
646
        e->data.Selected=1;
 
647
    }
 
648
}
 
649
 
 
650
static void node_rectangle_select(ViewInfo* v)
 
651
{
 
652
    int ind;
 
653
    topview_node* n;
 
654
   
 
655
    for (ind = 0; ind < v->Topview->Nodecount; ind++) 
 
656
    {
 
657
        n = v->Topview->Nodes + ind;
 
658
        select_topview_node(n);
 
659
    }
 
660
 
 
661
}
 
662
static void edge_rectangle_select(ViewInfo* v)
 
663
{
 
664
    int ind;
 
665
    topview_edge* e;
 
666
   
 
667
    for (ind = 0; ind < v->Topview->Edgecount; ind++) 
 
668
    {
 
669
        e = v->Topview->Edges + ind;
 
670
        select_topview_edge(e);
 
671
    }
 
672
 
 
673
}
 
674
void rectangle_select(ViewInfo* v)
 
675
{
 
676
    int selnodes=atoi(agget(view->g[view->activeGraph],"nodesselectable"));
 
677
    int seledges=atoi(agget(view->g[view->activeGraph],"edgesselectable"));
 
678
    if(selnodes)
 
679
        node_rectangle_select(v);
 
680
    if(seledges)
 
681
        edge_rectangle_select(v);
 
682
}
 
683
int linesegmentsintersects(float X1, float X2, float Y1, float Y2)
 
684
{
 
685
    //line segment
 
686
    //X1,Y1 point 1
 
687
    //X2,Y3 point 2
 
688
    //rectangle
 
689
    //RX,RY lower left corner of rectangle
 
690
    //RW width of rectangle
 
691
    //RH height of ractangle
 
692
    //returns 1 if line segment is completely in the rect
 
693
    //0 if they intersect
 
694
    //-1 if completely out
 
695
    float x, y, m, iter;
 
696
    float RX, RY, RW, RH;
 
697
    int intersects, in;
 
698
    RX = view->Selection.X;
 
699
    RY = view->Selection.Y;
 
700
    RH=view->Selection.H;
 
701
    RW=view->Selection.W;
 
702
    if ((is_point_in_rectangle(X1, Y1, RX, RY, RW, RH))
 
703
        && (is_point_in_rectangle(X2, Y2, RX, RY, RW, RH)))
 
704
        return 1;
 
705
    if ((is_point_in_rectangle(X1, Y1, RX, RY, RW, RH))
 
706
        || (is_point_in_rectangle(X2, Y2, RX, RY, RW, RH)))
 
707
        return 0;
 
708
    //to be absolute or not to be one
 
709
    if (X1 > X2) {
 
710
        x = X2;
 
711
        y = Y2;
 
712
        X2 = X1;
 
713
        Y2 = Y1;
 
714
        X1 = x;
 
715
        Y1 = y;
 
716
    }
 
717
    x = X1;
 
718
    //iter
 
719
    iter = RW / (float) SELECTION_SEGMENT_DIVIDER;
 
720
    m = (Y2 - Y1) / (X2 - X1);
 
721
 
 
722
    in = 1;
 
723
    intersects = 0;
 
724
    while (x <= X2) {
 
725
        x = x + iter;
 
726
        y = Y1 + m * (x - X1);
 
727
        if (!is_point_in_rectangle(x, y, RX, RY, RW, RH))
 
728
            in = 0;
 
729
        else
 
730
            intersects = 1;
 
731
 
 
732
    }
 
733
    if (in == 1)
 
734
        return 1;
 
735
    if (intersects == 1)
 
736
        return 0;
 
737
    return -1;
 
738
}
 
739
 
 
740
 
 
741
 
 
742
 
 
743
 
 
744
 
 
745
 
 
746
 
 
747
 
 
748
 
 
749
 
 
750
 
 
751
 
 
752
 
 
753
 
 
754
 
 
755
 
 
756
 
 
757
 
 
758
 
 
759
 
 
760
#ifdef UNUSED
978
761
static int line_intersects(float *x, float *y, float *X, float *Y)
979
762
{
980
763
    //x,y are arrayf of float for two lines parameters theyt hold 4 points with x and y
1018
801
    return (a <= 1);
1019
802
}
1020
803
int point_within_sphere_with_coords(float x0, float y0, float z0, float r,
1021
 
                                    float x, float y,float z)
1022
 
{
1023
 
        float rr=(x-x0)*(x-x0)+(y-y0)*(y-y0)+(z-z0)*(z-z0);
1024
 
        rr=(float)pow(rr,0.5);
1025
 
        if (rr <= r)
1026
 
                return 1;
1027
 
        return 0;
 
804
                                    float x, float y, float z)
 
805
{
 
806
    float rr =
 
807
        (x - x0) * (x - x0) + (y - y0) * (y - y0) + (z - z0) * (z - z0);
 
808
    rr = (float) pow(rr, 0.5);
 
809
    if (rr <= r)
 
810
        return 1;
 
811
    return 0;
 
812
}
 
813
float distance_to_line(float ax, float ay, float bx, float by, float cx,
 
814
                       float cy)
 
815
{
 
816
    //this function returns the distance between a line(a-b) segment and a point(c) in 2D plane
 
817
    return (float)
 
818
        sqrt(pow(((by - ay) * (cx - ax) + (bx - ax) * (cy - ay)), 2)
 
819
             / (pow((bx - ax), 2) + pow((by - ay), 2))
 
820
        );
1028
821
}