Debian Codegraph @

texmf-dist/scripts/pedigree-perl/Pedigree/Node.pm

raw
1 =pod
2
3 =head1 NAME
4
5 Pedigree::Node - the base package for nodes in pedigree charts
6
7 =head1 SYNOPSIS
8
9 use Pedigree::Node;
10
11 $node = new Pedigree::Node(I<%params>);
12
13 $node->CheckAllParents();
14
15 $Id = $node->Id();
16
17 $node->SetSortOrder(-1|0|1);
18
19 $result = $node->SortOrder();
20
21 $is_numbered=$node->isNumbered();
22
23 $type = $node->Type();
24
25 $Kids = $node->Kids();
26
27 $node->GetAndSortKids();
28
29 ($root, $newgen) = $node->FindRoot(I<$generation>, [I<$sort_order>]);
30
31 $x = $node->GetRelX();
32
33 $y = $node->GetRelY();
34
35 $node->SetRelX(I<$x>);
36
37 $node->SetRelX(I<$y>);
38
39 $x = $node->GetAbsX();
40
41 $y = $node->GetAbsY();
42
43 $node->SetIndexX(I<$n>);
44
45 $node->SetAbsX(I<$x>);
46
47 $node->SetAbsY(I<$y>);
48
49 $n = $node->GetIndexX();
50
51 $area = $node->SetArea();
52
53 $delta = $node->CenterKids();
54
55
56 $node->CalcAbsCoord(I<$x>, I<$y>);
57
58 $node->AddConsanguinicMarriages();
59
60 $node->AddTwins($ydist);
61
62 $frame = $node->SetFrame(I<$xdist>, I<$ydist>);
63
64
65 $node->DrawAll(I<$xdist>, I<$ydist>, I<$belowtextfont>, I<$abovetextfont>,
66 I<@fieldsfornode>);
67
68 $node->DrawConnections();
69
70 $node->PrintAllLegends(I<$land>, I<@fields>);
71
72 $node->PrintLegend(I<$land>, I<@fields>);
73
74 =head1 DESCRIPTION
75
76 This is the basic package that defines nodes for pedigrees.
77 Pedigree::PersonNode(3) and Pedigree::MarriageNode(3) inherit from
78 this package.
79
80 =cut
81
82 ####################################################################
83 # Define the package #
84 ####################################################################
85
86 package Pedigree::Node;
87 use strict;
88 use Pedigree;
89
90 ####################################################################
91 # package variables #
92 ####################################################################
93
94 #
95 # The pool: %node_by_id keeps the relation between
96 # nodes and references
97 #
98
99 our %node_by_id;
100
101 #
102 # The hash %node_by_gen is a hash of hasehs. The keys
103 # are generation numbers (with zero being the root of pedigree),
104 # and the values are the hashes Id->node
105 #
106 our %node_by_gen;
107
108 #
109 # The hash %kids_by_parent_id is a hash of hashes. The keys are
110 # Ids of parents. The hashes are $kid->1, where $kid is the kid id
111 # (NOT the kid node due to limitations of Perl)
112 #
113 our %kids_by_parent_id;
114
115 #
116 # The array @twin_sets lists all twin nodes. Each twin node is
117 # a has with entries 'Type' and 'KidIds'. They store
118 # twins type (monozygotic, qzygotic or empty) and Ids of the
119 # kid nodes correspondingly (KidIds is actually a hash of
120 # node Ids).
121 #
122 our @twin_sets;
123
124 ####################################################################
125 # And package methods #
126 ####################################################################
127
128 ####################################################################
129 # new #
130 ####################################################################
131
132 =pod
133
134 =over 4
135
136
137 =item B<new>(I<%params>);
138
139 Construct a new node from the given parameters. If a node with
140 the give Id exists, add new information to the node.
141
142 =cut
143
144 sub new {
145 my ($class,%params)=@_;
146
147 if (!exists($params{'Id'})) {
148 print STDERR "Warning: cannot create node from %params\n";
149 return 0;
150 }
151
152 my $Id=$params{'Id'};
153 my $self;
154 if (exists($node_by_id{$Id})) {
155 $self=$node_by_id{$Id};
156 } else {
157 $self={};
158 bless ($self,$class);
159 $node_by_id{$Id}=$self;
160 }
161
162 foreach my $key (keys %params) {
163 $self->{$key} = $params{$key};
164 }
165
166 #
167 # Calculate age at death
168 #
169 if (exists($self->{'DoB'}) && exists($self->{'DoD'})) {
170 $self->{'AgeAtDeath'} = 'unknown';
171 if (($self->{'DoB'} ne 'unknown') &&
172 ($self->{'DoD'} ne 'unknown')) {
173 my ($y1, $m1, $d1) = split /\./, $self->{'DoB'};
174 my ($y2, $m2, $d2) = split /\./, $self->{'DoD'};
175 $self->{'AgeAtDeath'} = int(($y2-$y1) + ($m2-$m1)/12
176 + ($d2-$d1)/12/30);
177 }
178 }
179
180
181 #
182 # Only Person Nodes are numbered in pedigrees
183 #
184 $self->{'Numbered'}=0;
185
186 #
187 # The field 'Kids' is special. This is a reference
188 # to an array filled by GetAndSortKids()
189 #
190 if (!exists($self->{'Kids'})) {
191 $self->{'Kids'}=[];
192 }
193
194 #
195 # Hashes %kids_by_parent_id
196 #
197 if (exists($self->{'Mother'})) {
198 my $parent = $self->{'Mother'};
199 $kids_by_parent_id{$parent}->{$self->Id()}=1;
200 }
201 if (exists($self->{'Father'})) {
202 my $parent = $self->{'Father'};
203 $kids_by_parent_id{$parent}->{$self->Id()}=1;
204 }
205
206 #
207 # Add references to the hashes
208 #
209
210 $self->{'node_by_id'} = \%node_by_id;
211 $self->{'node_by_gen'} = \%node_by_gen;
212 $self->{'kids_by_parent_id'} = \%kids_by_parent_id;
213 $self->{'twin_sets'} = \@twin_sets;
214
215 #
216 # Initially the nodes are sorted by age only
217 #
218 if (!($self->{'SortOrder'})) {
219 $self->{'SortOrder'} = 0;
220 }
221
222
223 return $self;
224
225 }
226
227 ####################################################################
228 # CheckAllParents #
229 ####################################################################
230
231 =pod
232
233 =item B<CheckAllParents>();
234
235 Check whether mothers and fathers of all nodes exist
236
237 =cut
238
239 sub CheckAllParents {
240 my $self = shift;
241
242 foreach my $parentId (keys %kids_by_parent_id) {
243 if (!exists($node_by_id{$parentId}) ) {
244 print STDERR
245 "Node $parentId does not exist and is listed as parent for nodes";
246 foreach my $kidId (keys %{$kids_by_parent_id{$parentId}}) {
247 print STDERR " ", $kidId;
248 my $kid = $node_by_id{$kidId};
249 if ($kid->{'Mother'} eq $parentId) {
250 delete $kid->{'Mother'};
251 }
252 if ($kid->{'Father'} eq $parentId) {
253 delete $kid->{'Father'};
254 }
255 }
256 print STDERR ". Deleting\n";
257 delete $kids_by_parent_id{$parentId};
258 } elsif ($main::DEBUG) {
259 print STDERR "Node $parentId is OK\n";
260 }
261 }
262
263 return 0;
264 }
265
266
267 ####################################################################
268 # Id #
269 ####################################################################
270
271 =pod
272
273 =item B<Id>();
274
275 Get Id of a node. Note that there is no way to set an Id of a node
276 that was already created.
277
278 =cut
279
280 sub Id {
281 my $self = shift;
282 return $self->{'Id'};
283 }
284
285 ####################################################################
286 # SetSortOrder #
287 ####################################################################
288
289 =pod
290
291 =item B<SetSortOrder>(I<-1|0|1>);
292
293 Normally the sibs nodes are sorted by age. However, if the nodes or
294 their descendants are connected by a marriage line, we must sort them
295 in the special way: all way to the left or all way to the right. The
296 procedure B<SetSortOrder> sets this flag for the node or deletes it
297 depending on the argument.
298
299 =cut
300
301 sub SetSortOrder {
302 my $self = shift;
303 my $order = shift;
304 $self->{'SortOrder'}=$order;
305 return $order;
306 }
307
308 ####################################################################
309 # SortOrder #
310 ####################################################################
311
312 =pod
313
314 =item B<SortOrder>();
315
316 Normally the sibs nodes are sorted by age. However, if the nodes or
317 their descendants are connected by a marriage line, we must sort them
318 in the special way: all way to the left or all way to the right. The
319 procedure B<SortOrder> checks this flag.
320
321 =cut
322
323 sub SortOrder {
324 my $self = shift;
325 return $self->{'SortOrder'};
326 }
327
328 ####################################################################
329 # isNumbered #
330 ####################################################################
331
332 =pod
333
334 =item B<isNumbered>();
335
336 Check whether the node should be numbered in pedigree
337
338 =cut
339
340 sub isNumbered {
341 my $self = shift;
342 return $self->{'Numbered'};
343 }
344
345 ####################################################################
346 # Type #
347 ####################################################################
348
349 =pod
350
351 =item B<Type>()
352
353 Return node type.
354
355 =cut
356
357 sub Type {
358 my $self=shift;
359 return $self->{'Type'};
360 }
361
362
363
364 ####################################################################
365 # Kids #
366 ####################################################################
367
368 =pod
369
370 =item B<Kids>();
371
372 Get the reference to the array of kids
373
374 =cut
375
376 sub Kids {
377 my $self = shift;
378 return $self->{'Kids'};
379 }
380
381
382 ####################################################################
383 # GetAndSortKids #
384 ####################################################################
385
386 =pod
387
388 =item B<GetAndSortKids>();
389
390 Apply sort the array of kids for the given node
391
392 =cut
393
394 sub GetAndSortKids {
395 my $self=shift;
396 my @kids;
397 my $Id = $self->Id();
398 foreach my $kidId (keys %{$self->{'kids_by_parent_id'}->{$Id}}) {
399 push @kids, $self->{'node_by_id'}->{$kidId};
400 }
401 @kids = sort by_sibs_order @kids;
402 $self->{'Kids'}=\@kids;
403 if ($main::DEBUG) {
404 print STDERR "Node ",$self->Id(),", Kids: ";
405 foreach my $kid (@{$self->Kids()}) {
406 print STDERR $kid->Id(), " ";
407 }
408 print STDERR "\n";
409 }
410 return 0;
411 }
412
413 ####################################################################
414 # FindRoot #
415 ####################################################################
416
417 =pod
418
419 =item B<FindRoot>(I<$generation>, [I<$sort_order>]);
420
421 Finds the root of the tree to which the current node belongs.
422 Takes the current generation number and returns the root and its
423 generation number. Here generation numbers go "backwards": the older
424 generations have higher numbers. The found node is assigned sort order
425 I<$sort_order>.
426
427 =cut
428
429 sub FindRoot {
430 my ($self,$gen,$sort)=@_;
431
432 if (defined $sort && !($self->SortOrder())) {
433 $self->SetSortOrder($sort);
434 }
435
436 # If there are no parents, I am the root
437 if (!exists($self->{'Mother'}) && !exists($self->{'Father'})) {
438 my @result=($self,$gen);
439 return \@result;
440 }
441
442 # If there are both parents, their union is the root
443 if (exists($self->{'Mother'}) && exists($self->{'Father'})) {
444 my $motherId=$self->{'Mother'};
445 my $mother=$node_by_id{$motherId};
446 my $fatherId=$self->{'Father'};
447 my $father=$node_by_id{$fatherId};
448
449
450 my $marriageId = $fatherId."_m_".$motherId;
451 my $marriage =
452 new Pedigree::MarriageNode (
453 'Id'=>$marriageId,
454 'MSpouse'=>$father,
455 'FSpouse'=>$mother
456 );
457 if (defined $sort) {
458 $marriage->SetSortOrder($sort);
459 }
460 my @result = ($marriage,$gen+1);
461 return \@result;
462 }
463
464 # Ok, only one parent is there. The search goes further
465
466 my $parentId;
467 if (exists($self->{'Mother'})) {
468 $parentId=$self->{'Mother'};
469 } else {
470 $parentId=$self->{'Father'};
471 }
472 my $parent=$node_by_id{$parentId};
473 return $parent->FindRoot($gen+1,$sort);
474 }
475
476 ####################################################################
477 # GetRelX #
478 ####################################################################
479
480 =pod
481
482 =item B<GetRelX>();
483
484 Find the relative x coordinate of the node. The coordinate is
485 relative to the precedessor or to the marriage node, which connects
486 this node to the proband
487
488 =cut
489
490 sub GetRelX {
491 my $self = shift;
492 return $self->{'RelX'};
493 }
494
495 ####################################################################
496 # GetRelY #
497 ####################################################################
498
499 =pod
500
501 =item B<GetRelY>();
502
503 Find the relative Y coordinate of the node. The coordinate is
504 relative to the precedessor or to the marriage node, which connects
505 this node to the proband. Note that the Y axis is down.
506
507 =cut
508
509 sub GetRelY {
510 my $self = shift;
511 return $self->{'RelY'};
512 }
513
514
515 ####################################################################
516 # SetRelX #
517 ####################################################################
518
519 =pod
520
521 =item B<SetRelX>(I<$x>);
522
523 Set the relative x coordinate of the node. The coordinate is
524 relative to the precedessor or to the marriage node, which connects
525 this node to the proband.
526
527 =cut
528
529 sub SetRelX {
530 my ($self, $x) = @_;
531 $self->{'RelX'} = $x;
532 return 0;
533 }
534
535 ####################################################################
536 # SetRelY #
537 ####################################################################
538
539 =pod
540
541 =item B<SetRelY>(I<$y>);
542
543 Set the relative y coordinate of the node. The coordinate is
544 relative to the precedessor or to the marriage node, which connects
545 this node to the proband. Note that the Y axis is down.
546
547 =cut
548
549 sub SetRelY {
550 my ($self, $y) = @_;
551 $self->{'RelY'} = $y;
552 return 0;
553 }
554
555 ####################################################################
556 # GetAbsX #
557 ####################################################################
558
559 =pod
560
561 =item B<GetAbsX>();
562
563 Find the absolute x coordinate of the node.
564
565 =cut
566
567 sub GetAbsX {
568 my $self = shift;
569 return $self->{'AbsX'};
570 }
571
572 ####################################################################
573 # GetAbsY #
574 ####################################################################
575
576 =pod
577
578 =item B<GetAbsY>();
579
580 Find the absolute Y coordinate of the node.
581
582 =cut
583
584 sub GetAbsY {
585 my $self = shift;
586 return $self->{'AbsY'};
587 }
588
589 ####################################################################
590 # GetIndexX #
591 ####################################################################
592
593 =pod
594
595 =item B<GetIndexX>();
596
597 Find the number of the node in the given generation.
598
599 =cut
600
601 sub GetIndexX {
602 my $self = shift;
603 return $self->{'IndexX'};
604 }
605
606
607
608 ####################################################################
609 # SetAbsX #
610 ####################################################################
611
612 =pod
613
614 =item B<SetAbsX>(I<$x>);
615
616 Set the absolute x coordinate of the node.
617
618 =cut
619
620 sub SetAbsX {
621 my ($self, $x) = @_;
622 $self->{'AbsX'} = $x;
623 return 0;
624 }
625
626 ####################################################################
627 # SetAbsY #
628 ####################################################################
629
630 =pod
631
632 =item B<SetAbsY>(I<$y>);
633
634 Set the absolute y coordinate of the node.
635
636 =cut
637
638 sub SetAbsY {
639 my ($self, $y) = @_;
640 $self->{'AbsY'} = $y;
641 return 0;
642 }
643
644
645 ####################################################################
646 # SetIndexX #
647 ####################################################################
648
649 =pod
650
651 =item B<SetIndexX>(I<$n>);
652
653 Set the number of the node in the given generation.
654
655 =cut
656
657 sub SetIndexX {
658 my ($self, $n) = @_;
659 $self->{'IndexX'} = $n;
660 return 0;
661 }
662
663
664
665 ####################################################################
666 # SetArea #
667 ####################################################################
668
669 =pod
670
671 =item B<SetArea>();
672
673 Calculate relative coordinates for all nodes, that are descendants of
674 the given node. We create a Pedigree::Area(3) around the given node
675 and recursively apply the function to all descendants. The subroutine
676 returns the reference to the created area.
677
678 =cut
679
680 sub SetArea {
681 my $self = shift;
682 $self->GetAndSortKids();
683 my $area = new Pedigree::Area ($self);
684 foreach my $kid (@{$self->Kids()}) {
685 my $kidarea = $kid->SetArea();
686 $kid->SetRelY(-1);
687 $area->AddRight($kidarea);
688 }
689 #
690 # We want the node to be the center of siblings
691 # subtrees
692 #
693 my $deltaX=$self->CenterKids();
694 $area->MoveLowerLayers($deltaX);
695 if ($main::DEBUG) {
696 print STDERR "Setting area for ",$self->Id(),"\n";
697 for (my $y=$area->GetYmin(); $y<=$area->GetYmax(); $y++) {
698 print STDERR "\t$y: ", $area->GetXmin($y), ", ",
699 $area->GetXmax($y), "\n";
700 }
701 }
702 $self->{'Area'} = $area;
703 return $area;
704 }
705
706 ####################################################################
707 # CenterKids #
708 ####################################################################
709
710 =pod
711
712 =item B<CenterKids>();
713
714 Move the relative coordinates of all the kids of the given node
715 so the given node is centered in relation to the kids. Returns
716 the shift to be applied to the Pedigree::Area(3).
717
718 =cut
719
720 sub CenterKids {
721 my $self=shift;
722 my $nKids=scalar @{$self->Kids()};
723 if ($nKids < 2) { # One or no kids - no need to center
724 return 0;
725 }
726 my $x0 = $self->Kids()->[0]->GetRelX();
727 my $x1 = $self->Kids()->[$nKids-1]->GetRelX();
728 my $delta = -($x0+$x1)/2;
729 foreach my $kid (@{$self->Kids()}) {
730 $kid->SetRelX($kid->GetRelX()+$delta);
731 }
732 return $delta;
733 }
734
735
736 ####################################################################
737 # CalcAbsCoor #
738 ####################################################################
739
740 =pod
741
742 =item B<CalcAbsCoor>(I<$x>, $<y>);
743
744 Set the absolute coordinates of the given node, if the absolute
745 coordinates of the parent node are (I<$x>, I<$y>), and recursively
746 do this for all descendants of this node. Additionally set up
747 B<%node_by_gen>.
748
749 =cut
750
751 sub CalcAbsCoor {
752 my $self=shift;
753 my ($x,$y) = @_;
754 $x += $self->GetRelX();
755 $y += $self->GetRelY();
756 #
757 # Consanguinic kids may be already set
758 #
759 if (!exists($self->{'AbsY'}) || $self->{'AbsY'} > $y) {
760 $self->SetAbsX($x);
761 $self->SetAbsY($y);
762 foreach my $kid (@{$self->Kids()}) {
763 $kid->CalcAbsCoor($x,$y);
764 }
765 $node_by_gen{$y}->{$self->Id()}= $self;
766 if ($main::DEBUG) {
767 print STDERR "Abs Coords for ", $self->Id(), ": $x, $y\n";
768 }
769 } else {
770 if ($main::DEBUG) {
771 print STDERR "Not setting abs coords for ",$self->Id(),"\n";
772 }
773 }
774
775 return 0;
776 }
777
778
779 ####################################################################
780 # AddConsanguinicMarriages #
781 ####################################################################
782
783 =pod
784
785 =item B<AddConsanguinicMarriages>();
786
787 Check the pedigree and add consanguinic marriages to it. Note
788 that this procedure must be called B<after> L<SetAbsCoor>, so
789 the coordinates of all nodes are set.
790
791 =cut
792
793 #
794 # This is rather a hack. Basically we think that a union is
795 # consanguinic if the spouses are already set in the pedigree.
796 # We check all kids which are in the pedigree and add those
797 # who have both mother and father that list them as kids.
798 #
799
800 sub AddConsanguinicMarriages {
801 my $self = shift;
802 foreach my $gen (keys %node_by_gen) {
803 foreach my $kid (values %{$node_by_gen{$gen}}) {
804 if (!exists($kid->{'Mother'}) ||
805 !exists($kid->{'Father'})) {
806 next; # kid
807 }
808 my $motherId=$kid->MotherId();
809 my $fatherId=$kid->FatherId();
810 my $mother=$node_by_id{$motherId};
811 my $father=$node_by_id{$fatherId};
812 if (!exists($mother->{'AbsX'}) ||
813 !exists($father->{'AbsX'})) {
814 next; # kid
815 }
816 if (exists($node_by_id{$fatherId."_m_".$motherId})) {
817 next; # kid
818 }
819 #
820 # If we are here, we found a consangunic marriage!
821 #
822 if ($main::DEBUG) {
823 print STDERR "Found a consanguinic marriage between ",
824 "$fatherId and $motherId. The kid is ",
825 $kid->Id(), "\n";
826 }
827 my $marriageId=$fatherId."_m_".$motherId;
828 my $marriage=
829 new Pedigree::MarriageNode (
830 Id=>$marriageId,
831 'Consanguinic'=>1,
832 'FSpouse'=>$mother,
833 'MSpouse'=>$father
834 );
835 #
836 # We set up this node in the middle between father
837 # and mother
838 #
839 $marriage->SetAbsX(($father->GetAbsX()+$mother->GetAbsX())/2);
840 $marriage->SetAbsY(($father->GetAbsY()+$mother->GetAbsY())/2);
841 $node_by_gen{$marriage->GetAbsY()}->{$marriageId}= $marriage;
842
843 #
844 # Repopulate parents' kids
845 #
846 $mother->GetAndSortKids();
847 $father->GetAndSortKids();
848
849 #
850 # We would like to make the kids to belong to this marriage,
851 # but it might be wrong: it might be in the wrong generation!
852 # Let is check it
853 if (($marriage->GetAbsY() - $kid->GetAbsY()) == 1) {
854 $marriage->GetAndSortKids();
855 } else {
856 #
857 # Ok, we need an additional node. It has the same
858 # abscissa as $marriage, but is one generation above kids
859 #
860 my $marriage1Id=$fatherId."_m1_".$motherId;
861 my $marriage1 =
862 new Pedigree::MarriageNode (
863 Id=>$marriage1Id,
864 'Consanguinic'=>1,
865 );
866 $marriage1->SetAbsX($marriage->GetAbsX());
867 $marriage1->SetAbsY(1+$kid->GetAbsY());
868 $node_by_gen{$marriage1->GetAbsY()}->{$marriage1Id}=
869 $marriage1;
870 #
871 # Now we transfer kids
872 #
873 $kids_by_parent_id{$marriage1Id} =
874 $kids_by_parent_id{$marriageId};
875 delete $kids_by_parent_id{$marriageId};
876 $kids_by_parent_id{$marriageId}->{$marriage1Id}=1;
877 $marriage->GetAndSortKids();
878 $marriage1->GetAndSortKids();
879 }
880 }
881 }
882 }
883
884
885 ####################################################################
886 # AddTwins #
887 ####################################################################
888
889 =pod
890
891 =item B<AddTwins>(I<$ydist>);
892
893 Check the pedigree and add twin nodes. Note
894 that this procedure must be called B<after> L<SetAbsCoor> and
895 L<AddConsanguinicMarriages>.
896
897 =cut
898
899 sub AddTwins {
900 my $self = shift;
901 my $ydist= shift;
902 #
903 # First, delete all kids from $twin_sets, for which there
904 # are no nodes
905 #
906 foreach my $set (@twin_sets) {
907 foreach my $kidId (keys %{$set->{'KidIds'}}) {
908 if (!exists($node_by_id{$kidId})) {
909 delete $set->{'KidIds'}->{$kidId};
910 if ($main::DEBUG) {
911 print STDERR "Bad node \"$kidId\" in twin sets\n";
912 }
913 }
914 }
915 }
916
917 #
918 # Now we are ready to check for twins
919 #
920 foreach my $gen (keys %node_by_gen) {
921 foreach my $parentId (keys %{$node_by_gen{$gen}}) {
922 foreach my $kidId (keys %{$kids_by_parent_id{$parentId}}) {
923 for (my $i=0; $i<scalar @twin_sets; $i++) {
924 if (exists $twin_sets[$i]->{'KidIds'}->{$kidId}) {
925 my @kidIds = keys %{$twin_sets[$i]->{'KidIds'}};
926 my $type = $twin_sets[$i]->{'Type'};
927 my $twinsId = 't_'.join('_',@kidIds);
928 my $twinsNode =
929 Pedigree::TwinsNode->new (
930 'Id'=>$twinsId,
931 'Type'=>$type,
932 'ParentId'=>$parentId,
933 'KidIds'=>
934 $twin_sets[$i]->{'KidIds'}
935 );
936 #
937 # Change kids
938 #
939 my $parent = $node_by_id{$parentId};
940 $parent->GetAndSortKids();
941 $twinsNode->GetAndSortKids();
942
943 #
944 # Now the coordinates of the node.
945 # It is centered over kids nodes and 0.24 $ydist above
946 #
947 my @kids = sort {$a->GetAbsX() <=>
948 $b->GetAbsX()}
949 @{$twinsNode->Kids()};
950 my $leftKid=$kids[0];
951 my $rightKid=$kids[scalar(@kids)-1];
952 $twinsNode->SetAbsX(($leftKid->GetAbsX() +
953 $rightKid->GetAbsX())/2.0);
954 $twinsNode->SetAbsY($leftKid->GetAbsY() +
955 0.2*$ydist);
956 $node_by_gen{$twinsNode->GetAbsY()}->
957 {$twinsId}= $twinsNode;
958
959 #
960 # There is no need to keep this in the twins set
961 #
962 splice @twin_sets, $i,1;
963 last; # twin_sets
964 }
965 }
966 }
967 }
968 }
969 return 0;
970 }
971
972
973
974
975
976 ####################################################################
977 # SetFrame #
978 ####################################################################
979
980 =pod
981
982 =item B<SetFrame>(I<$xidst>, I<$ydist>);
983
984 Calculate the frame: coordinates of the lower left and upper right
985 corners of the picture (in ps units). As a side effect, add generation
986 numbers to each person node and calculate the X index of each node.
987
988 =cut
989
990 sub SetFrame {
991 my $self=shift;
992 my ($xdist, $ydist) = @_;
993
994 my $xmin=0;
995 my $xmax=0;
996
997 my @sorted_gens = sort {$b <=> $a} keys %node_by_gen;
998 my $ymin=$sorted_gens[(scalar @sorted_gens) -1];
999 my $ymax=$sorted_gens[0];
1000 #
1001 # The names of the nodes look like I:1, V:5. Let the
1002 # first number be $i, and the second one be $j.
1003 # IndexX is different from $j by the fact that marriage nodes
1004 # are not skipped.
1005 #
1006 my $i=1;
1007 foreach my $gen (@sorted_gens) {
1008 my $roman=roman_num($i);
1009 my @sorted_nodes =
1010 sort {$a->GetAbsX() <=> $b->GetAbsX()} values %{$node_by_gen{$gen}};
1011 my $num_nodes= scalar @sorted_nodes;
1012 if ($sorted_nodes[0]->GetAbsX()<$xmin) {
1013 $xmin=$sorted_nodes[0]->GetAbsX();
1014 }
1015 if ($sorted_nodes[$num_nodes-1]->GetAbsX()>$xmax) {
1016 $xmax=$sorted_nodes[$num_nodes-1]->GetAbsX();
1017 }
1018 my $j=1;
1019 my $indexX=1;
1020 foreach my $node (@sorted_nodes) {
1021 $node->SetIndexX($indexX);
1022 if ($main::DEBUG) {
1023 print STDERR "Node ", $node->Id(), ", index ",
1024 $node->GetIndexX(), "\n";
1025 }
1026 $indexX++;
1027 if ($node->isNumbered()) {
1028 $node->SetGenName("$roman:$j");
1029 if ($main::DEBUG) {
1030 print STDERR $node->Id(), ": ", $node->GetGenName(),
1031 "\n";
1032 }
1033 $j++;
1034 }
1035 }
1036 #
1037 # The fractional "generations" are for twin nodes
1038 # and consanguinic marriage nodes.
1039 #
1040 if ($gen == int($gen)) {
1041 $i++;
1042 }
1043 }
1044 my @result = ($xdist*($xmin-1), $ydist*($ymin-1),
1045 $xdist*($xmax+1), $ydist*($ymax+1));
1046 return \@result;
1047 }
1048
1049
1050
1051 ####################################################################
1052 # DrawConnections #
1053 ####################################################################
1054
1055 =pod
1056
1057 =item B<DrawConnections>();
1058
1059 Draw the connections from the given node to its descendants
1060
1061 =cut
1062
1063 sub DrawConnections {
1064 my $self = shift;
1065 my $xdist = shift;
1066 my $ydist = shift;
1067 my $result;
1068 my $Id=$self->Id;
1069 foreach my $kid (@{$self->Kids()}) {
1070 my $kidId = $kid->Id();
1071 $result .= '\pstDescent{'.$Id.'}{'.$kidId.'}'."\n";
1072 }
1073 return $result;
1074 }
1075
1076
1077
1078
1079
1080 ####################################################################
1081 # DrawAll #
1082 ####################################################################
1083
1084 =pod
1085
1086 =item B<DrawAll>(I<$xdist>, I<$ydist>, I<$belowtextfont>,
1087 I<$abovetextfont>, I<@fieldsfornode>);
1088
1089 Draw all nodes and connections in the form suitable for
1090 pspicture
1091
1092 =cut
1093
1094 sub DrawAll {
1095 my ($self, $xdist, $ydist, $belowtextfont,
1096 $abovetextfont, @fieldsfornode) = @_;
1097
1098 #
1099 # Commands to draw nodes
1100 #
1101 my $nodes;
1102
1103 #
1104 # Commands to draw connections
1105 #
1106 my $connections;
1107
1108
1109 foreach my $gen (keys %node_by_gen) {
1110 foreach my $node (values %{$node_by_gen{$gen}}) {
1111 #
1112 # We draw only the nodes, who belong to the right
1113 # generation (consanguinity may lead to duplicate nodes
1114 #
1115 #
1116 if ($node->GetAbsY() <=> $gen) {
1117 delete $node_by_gen{$gen}->{$node->Id()};
1118 next;
1119 }
1120
1121 $nodes .= $node->DrawNode($xdist, $ydist,
1122 $belowtextfont, $abovetextfont,
1123 @fieldsfornode);
1124 $connections .=$node->DrawConnections($xdist, $ydist);
1125 }
1126 }
1127 return $nodes.$connections;
1128 }
1129
1130 ####################################################################
1131 # PrintAllLegends #
1132 ####################################################################
1133
1134 =pod
1135
1136 =item B<PrintAllLegends>(I<$lang>, I<@fields>);
1137
1138 Print legend for all the nodes. The first parameter is the
1139 language, the other is the fields to be included in the legend.
1140
1141 =cut
1142
1143 sub PrintAllLegends {
1144 my ($self, $lang, @fields) = @_;
1145
1146 my $result="\n\\begin{description}\n";
1147
1148 foreach my $gen (sort {$b <=> $a} keys(%node_by_gen)) {
1149 foreach my $node
1150 (sort {$a->GetIndexX() <=> $b->GetIndexX()}
1151 values(%{$node_by_gen{$gen}})) {
1152 $result .= $node->PrintLegend($lang,@fields);
1153 }
1154 }
1155
1156 $result .= "\\end{description}\n";
1157
1158 return $result;
1159 }
1160
1161
1162 ####################################################################
1163 # PrintLegend #
1164 ####################################################################
1165
1166 =pod
1167
1168 =item B<PrintLegend>(I<$lang>, I<@fields>);
1169
1170 This subroutine does nothing: a generic node has no legend. It
1171 is overriden by Pedigree::PersonNode(3) and Pedigree::AbortionNode(3).
1172
1173 =cut
1174
1175 sub PrintLegend {
1176
1177 return;
1178 }
1179
1180
1181
1182
1183 ####################################################################
1184 # by_sibs_order #
1185 ####################################################################
1186
1187 #
1188 # Internal procedure for sorting kids
1189 #
1190
1191 sub by_sibs_order {
1192 #
1193 # We compare sort order, and if it is the same, DoB
1194 #
1195 return ($a->SortOrder() <=> $b->SortOrder()) ||
1196 ($a->DoB() cmp $b->DoB());
1197 }
1198
1199 ####################################################################
1200 # roman_num #
1201 ####################################################################
1202
1203 #
1204 # Internal procedure for roman numerals
1205 #
1206
1207 sub roman_num {
1208 my $i=shift;
1209 my @nums=qw(0 I II III IV V VI VII VIII IX X XI XII XIII XIV XV
1210 XVI XVII XVIII XIX XX XXI XXII XXIII XXIV);
1211 return $nums[$i];
1212 }
1213
1214
1215 ####################################################################
1216 # THE END #
1217 ####################################################################
1218
1219
1220 =pod
1221
1222 =back
1223
1224 =head1 ENVIRONMENT
1225
1226 The calling program should define B<$main::DEBUG> and set it to 0
1227 or 1.
1228
1229 =head1 SEE ALSO
1230
1231 pedigree(1), Pedigree(3)
1232
1233 =head1 AUTHOR
1234
1235 Boris Veytsman, Leila Akhmadeeva, 2006, 2007
1236
1237
1238
1239 =cut
1240
1241 1;