gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Fix the date field comment of graphToEps() output (#336)
0 1 0
default
1 file changed with 1 insertions and 1 deletions:
↑ Collapse diff ↑
Ignore white space 384 line context
... ...
@@ -498,387 +498,387 @@
498 498
    _autoNodeScale=b;return *this;
499 499
  }
500 500

	
501 501
  ///Turns on/off the absolutematic node size scaling.
502 502

	
503 503
  ///Turns on/off the absolutematic node size scaling.
504 504
  ///
505 505
  ///\sa nodeScale()
506 506
  ///
507 507
  GraphToEps<T> &absoluteNodeSizes(bool b=true) {
508 508
    _absoluteNodeSizes=b;return *this;
509 509
  }
510 510

	
511 511
  ///Negates the Y coordinates.
512 512
  GraphToEps<T> &negateY(bool b=true) {
513 513
    _negY=b;return *this;
514 514
  }
515 515

	
516 516
  ///Turn on/off pre-scaling
517 517

	
518 518
  ///By default graphToEps() rescales the whole image in order to avoid
519 519
  ///very big or very small bounding boxes.
520 520
  ///
521 521
  ///This (p)rescaling can be turned off with this function.
522 522
  ///
523 523
  GraphToEps<T> &preScale(bool b=true) {
524 524
    _preScale=b;return *this;
525 525
  }
526 526

	
527 527
  ///Sets a global scale factor for arc widths
528 528

	
529 529
  /// Sets a global scale factor for arc widths.
530 530
  ///
531 531
  /// If arcWidths() is not given, this function simply sets the arc
532 532
  /// widths to \c d.  If arcWidths() is given, but
533 533
  /// autoArcWidthScale() is not, then the arc withs given by
534 534
  /// arcWidths() will be multiplied by the value \c d.
535 535
  /// If both arcWidths() and autoArcWidthScale() are used, then the
536 536
  /// arc withs will be scaled in such a way that the greatest width will be
537 537
  /// equal to \c d.
538 538
  GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;}
539 539
  ///Turns on/off the automatic arc width scaling.
540 540

	
541 541
  ///Turns on/off the automatic arc width scaling.
542 542
  ///
543 543
  ///\sa arcWidthScale()
544 544
  ///
545 545
  GraphToEps<T> &autoArcWidthScale(bool b=true) {
546 546
    _autoArcWidthScale=b;return *this;
547 547
  }
548 548
  ///Turns on/off the absolutematic arc width scaling.
549 549

	
550 550
  ///Turns on/off the absolutematic arc width scaling.
551 551
  ///
552 552
  ///\sa arcWidthScale()
553 553
  ///
554 554
  GraphToEps<T> &absoluteArcWidths(bool b=true) {
555 555
    _absoluteArcWidths=b;return *this;
556 556
  }
557 557
  ///Sets a global scale factor for the whole picture
558 558
  GraphToEps<T> &scale(double d) {_scale=d;return *this;}
559 559
  ///Sets the width of the border around the picture
560 560
  GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;}
561 561
  ///Sets the width of the border around the picture
562 562
  GraphToEps<T> &border(double x, double y) {
563 563
    _xBorder=x;_yBorder=y;return *this;
564 564
  }
565 565
  ///Sets whether to draw arrows
566 566
  GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;}
567 567
  ///Sets the length of the arrowheads
568 568
  GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;}
569 569
  ///Sets the width of the arrowheads
570 570
  GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;}
571 571

	
572 572
  ///Scales the drawing to fit to A4 page
573 573
  GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;}
574 574

	
575 575
  ///Enables parallel arcs
576 576
  GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;}
577 577

	
578 578
  ///Sets the distance between parallel arcs
579 579
  GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;}
580 580

	
581 581
  ///Hides the arcs
582 582
  GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;}
583 583
  ///Hides the nodes
584 584
  GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;}
585 585

	
586 586
  ///Sets the size of the node texts
587 587
  GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;}
588 588

	
589 589
  ///Sets the color of the node texts to be different from the node color
590 590

	
591 591
  ///Sets the color of the node texts to be as different from the node color
592 592
  ///as it is possible.
593 593
  GraphToEps<T> &distantColorNodeTexts()
594 594
  {_nodeTextColorType=DIST_COL;return *this;}
595 595
  ///Sets the color of the node texts to be black or white and always visible.
596 596

	
597 597
  ///Sets the color of the node texts to be black or white according to
598 598
  ///which is more different from the node color.
599 599
  GraphToEps<T> &distantBWNodeTexts()
600 600
  {_nodeTextColorType=DIST_BW;return *this;}
601 601

	
602 602
  ///Gives a preamble block for node Postscript block.
603 603

	
604 604
  ///Gives a preamble block for node Postscript block.
605 605
  ///
606 606
  ///\sa nodePsTexts()
607 607
  GraphToEps<T> & nodePsTextsPreamble(const char *str) {
608 608
    _nodePsTextsPreamble=str ;return *this;
609 609
  }
610 610
  ///Sets whether the graph is undirected
611 611

	
612 612
  ///Sets whether the graph is undirected.
613 613
  ///
614 614
  ///This setting is the default for undirected graphs.
615 615
  ///
616 616
  ///\sa directed()
617 617
   GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;}
618 618

	
619 619
  ///Sets whether the graph is directed
620 620

	
621 621
  ///Sets whether the graph is directed.
622 622
  ///Use it to show the edges as a pair of directed ones.
623 623
  ///
624 624
  ///This setting is the default for digraphs.
625 625
  ///
626 626
  ///\sa undirected()
627 627
  GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;}
628 628

	
629 629
  ///Sets the title.
630 630

	
631 631
  ///Sets the title of the generated image,
632 632
  ///namely it inserts a <tt>%%Title:</tt> DSC field to the header of
633 633
  ///the EPS file.
634 634
  GraphToEps<T> &title(const std::string &t) {_title=t;return *this;}
635 635
  ///Sets the copyright statement.
636 636

	
637 637
  ///Sets the copyright statement of the generated image,
638 638
  ///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of
639 639
  ///the EPS file.
640 640
  GraphToEps<T> &copyright(const std::string &t) {_copyright=t;return *this;}
641 641

	
642 642
protected:
643 643
  bool isInsideNode(dim2::Point<double> p, double r,int t)
644 644
  {
645 645
    switch(t) {
646 646
    case CIRCLE:
647 647
    case MALE:
648 648
    case FEMALE:
649 649
      return p.normSquare()<=r*r;
650 650
    case SQUARE:
651 651
      return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r;
652 652
    case DIAMOND:
653 653
      return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r;
654 654
    }
655 655
    return false;
656 656
  }
657 657

	
658 658
public:
659 659
  ~GraphToEps() { }
660 660

	
661 661
  ///Draws the graph.
662 662

	
663 663
  ///Like other functions using
664 664
  ///\ref named-templ-func-param "named template parameters",
665 665
  ///this function calls the algorithm itself, i.e. in this case
666 666
  ///it draws the graph.
667 667
  void run() {
668 668
    const double EPSILON=1e-9;
669 669
    if(dontPrint) return;
670 670

	
671 671
    _graph_to_eps_bits::_NegY<typename T::CoordsMapType>
672 672
      mycoords(_coords,_negY);
673 673

	
674 674
    os << "%!PS-Adobe-2.0 EPSF-2.0\n";
675 675
    if(_title.size()>0) os << "%%Title: " << _title << '\n';
676 676
     if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n';
677 677
    os << "%%Creator: LEMON, graphToEps()\n";
678 678

	
679 679
    {
680 680
      os << "%%CreationDate: ";
681 681
#ifndef WIN32
682 682
      timeval tv;
683 683
      gettimeofday(&tv, 0);
684 684

	
685 685
      char cbuf[26];
686 686
      ctime_r(&tv.tv_sec,cbuf);
687 687
      os << cbuf;
688 688
#else
689 689
      os << bits::getWinFormattedDate();
690
      os << std::endl;
690 691
#endif
691 692
    }
692
    os << std::endl;
693 693

	
694 694
    if (_autoArcWidthScale) {
695 695
      double max_w=0;
696 696
      for(ArcIt e(g);e!=INVALID;++e)
697 697
        max_w=std::max(double(_arcWidths[e]),max_w);
698 698
      if(max_w>EPSILON) {
699 699
        _arcWidthScale/=max_w;
700 700
      }
701 701
    }
702 702

	
703 703
    if (_autoNodeScale) {
704 704
      double max_s=0;
705 705
      for(NodeIt n(g);n!=INVALID;++n)
706 706
        max_s=std::max(double(_nodeSizes[n]),max_s);
707 707
      if(max_s>EPSILON) {
708 708
        _nodeScale/=max_s;
709 709
      }
710 710
    }
711 711

	
712 712
    double diag_len = 1;
713 713
    if(!(_absoluteNodeSizes&&_absoluteArcWidths)) {
714 714
      dim2::Box<double> bb;
715 715
      for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]);
716 716
      if (bb.empty()) {
717 717
        bb = dim2::Box<double>(dim2::Point<double>(0,0));
718 718
      }
719 719
      diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare());
720 720
      if(diag_len<EPSILON) diag_len = 1;
721 721
      if(!_absoluteNodeSizes) _nodeScale*=diag_len;
722 722
      if(!_absoluteArcWidths) _arcWidthScale*=diag_len;
723 723
    }
724 724

	
725 725
    dim2::Box<double> bb;
726 726
    for(NodeIt n(g);n!=INVALID;++n) {
727 727
      double ns=_nodeSizes[n]*_nodeScale;
728 728
      dim2::Point<double> p(ns,ns);
729 729
      switch(_nodeShapes[n]) {
730 730
      case CIRCLE:
731 731
      case SQUARE:
732 732
      case DIAMOND:
733 733
        bb.add(p+mycoords[n]);
734 734
        bb.add(-p+mycoords[n]);
735 735
        break;
736 736
      case MALE:
737 737
        bb.add(-p+mycoords[n]);
738 738
        bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]);
739 739
        break;
740 740
      case FEMALE:
741 741
        bb.add(p+mycoords[n]);
742 742
        bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]);
743 743
        break;
744 744
      }
745 745
    }
746 746
    if (bb.empty()) {
747 747
      bb = dim2::Box<double>(dim2::Point<double>(0,0));
748 748
    }
749 749

	
750 750
    if(_scaleToA4)
751 751
      os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n";
752 752
    else {
753 753
      if(_preScale) {
754 754
        //Rescale so that BoundingBox won't be neither to big nor too small.
755 755
        while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10;
756 756
        while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10;
757 757
      }
758 758

	
759 759
      os << "%%BoundingBox: "
760 760
         << int(floor(bb.left()   * _scale - _xBorder)) << ' '
761 761
         << int(floor(bb.bottom() * _scale - _yBorder)) << ' '
762 762
         << int(ceil(bb.right()  * _scale + _xBorder)) << ' '
763 763
         << int(ceil(bb.top()    * _scale + _yBorder)) << '\n';
764 764
    }
765 765

	
766 766
    os << "%%EndComments\n";
767 767

	
768 768
    //x1 y1 x2 y2 x3 y3 cr cg cb w
769 769
    os << "/lb { setlinewidth setrgbcolor newpath moveto\n"
770 770
       << "      4 2 roll 1 index 1 index curveto stroke } bind def\n";
771 771
    os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }"
772 772
       << " bind def\n";
773 773
    //x y r
774 774
    os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }"
775 775
       << " bind def\n";
776 776
    //x y r
777 777
    os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n"
778 778
       << "      2 index 1 index sub 2 index 2 index add lineto\n"
779 779
       << "      2 index 1 index sub 2 index 2 index sub lineto\n"
780 780
       << "      2 index 1 index add 2 index 2 index sub lineto\n"
781 781
       << "      closepath pop pop pop} bind def\n";
782 782
    //x y r
783 783
    os << "/di { newpath 2 index 1 index add 2 index moveto\n"
784 784
       << "      2 index             2 index 2 index add lineto\n"
785 785
       << "      2 index 1 index sub 2 index             lineto\n"
786 786
       << "      2 index             2 index 2 index sub lineto\n"
787 787
       << "      closepath pop pop pop} bind def\n";
788 788
    // x y r cr cg cb
789 789
    os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n"
790 790
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
791 791
       << "   } bind def\n";
792 792
    os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n"
793 793
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n"
794 794
       << "   } bind def\n";
795 795
    os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n"
796 796
       << "     setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n"
797 797
       << "   } bind def\n";
798 798
    os << "/nfemale { 0 0 0 setrgbcolor 3 index "
799 799
       << _nodeBorderQuotient/(1+_nodeBorderQuotient)
800 800
       << " 1.5 mul mul setlinewidth\n"
801 801
       << "  newpath 5 index 5 index moveto "
802 802
       << "5 index 5 index 5 index 3.01 mul sub\n"
803 803
       << "  lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub"
804 804
       << " moveto\n"
805 805
       << "  5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto "
806 806
       << "stroke\n"
807 807
       << "  5 index 5 index 5 index c fill\n"
808 808
       << "  setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
809 809
       << "  } bind def\n";
810 810
    os << "/nmale {\n"
811 811
       << "  0 0 0 setrgbcolor 3 index "
812 812
       << _nodeBorderQuotient/(1+_nodeBorderQuotient)
813 813
       <<" 1.5 mul mul setlinewidth\n"
814 814
       << "  newpath 5 index 5 index moveto\n"
815 815
       << "  5 index 4 index 1 mul 1.5 mul add\n"
816 816
       << "  5 index 5 index 3 sqrt 1.5 mul mul add\n"
817 817
       << "  1 index 1 index lineto\n"
818 818
       << "  1 index 1 index 7 index sub moveto\n"
819 819
       << "  1 index 1 index lineto\n"
820 820
       << "  exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub"
821 821
       << " lineto\n"
822 822
       << "  stroke\n"
823 823
       << "  5 index 5 index 5 index c fill\n"
824 824
       << "  setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n"
825 825
       << "  } bind def\n";
826 826

	
827 827

	
828 828
    os << "/arrl " << _arrowLength << " def\n";
829 829
    os << "/arrw " << _arrowWidth << " def\n";
830 830
    // l dx_norm dy_norm
831 831
    os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n";
832 832
    //len w dx_norm dy_norm x1 y1 cr cg cb
833 833
    os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx "
834 834
       << "exch def\n"
835 835
       << "       /w exch def /len exch def\n"
836 836
      //<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke"
837 837
       << "       newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n"
838 838
       << "       len w sub arrl sub dx dy lrl\n"
839 839
       << "       arrw dy dx neg lrl\n"
840 840
       << "       dx arrl w add mul dy w 2 div arrw add mul sub\n"
841 841
       << "       dy arrl w add mul dx w 2 div arrw add mul add rlineto\n"
842 842
       << "       dx arrl w add mul neg dy w 2 div arrw add mul sub\n"
843 843
       << "       dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n"
844 844
       << "       arrw dy dx neg lrl\n"
845 845
       << "       len w sub arrl sub neg dx dy lrl\n"
846 846
       << "       closepath fill } bind def\n";
847 847
    os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n"
848 848
       << "         neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n";
849 849

	
850 850
    os << "\ngsave\n";
851 851
    if(_scaleToA4)
852 852
      if(bb.height()>bb.width()) {
853 853
        double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(),
854 854
                  (A4WIDTH-2*A4BORDER)/bb.width());
855 855
        os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' '
856 856
           << ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER
857 857
           << " translate\n"
858 858
           << sc << " dup scale\n"
859 859
           << -bb.left() << ' ' << -bb.bottom() << " translate\n";
860 860
      }
861 861
      else {
862 862
        double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(),
863 863
                  (A4WIDTH-2*A4BORDER)/bb.height());
864 864
        os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' '
865 865
           << ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER
866 866
           << " translate\n"
867 867
           << sc << " dup scale\n90 rotate\n"
868 868
           << -bb.left() << ' ' << -bb.top() << " translate\n";
869 869
        }
870 870
    else if(_scale!=1.0) os << _scale << " dup scale\n";
871 871

	
872 872
    if(_showArcs) {
873 873
      os << "%Arcs:\ngsave\n";
874 874
      if(_enableParallel) {
875 875
        std::vector<Arc> el;
876 876
        for(ArcIt e(g);e!=INVALID;++e)
877 877
          if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0
878 878
             &&g.source(e)!=g.target(e))
879 879
            el.push_back(e);
880 880
        std::sort(el.begin(),el.end(),arcLess(g));
881 881

	
882 882
        typename std::vector<Arc>::iterator j;
883 883
        for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) {
884 884
          for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ;
0 comments (0 inline)