summaryrefslogtreecommitdiff
path: root/graphics/pstricks/contrib/pedigree/pedigree-perl/Pedigree/MarriageNode.pm
blob: 9061fd949a9ed7be4c5b8de936db0e76956e6c59 (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
=pod

=head1 NAME

Pedigree::MarriageNode - a marriage in a pedigree

=head1 SYNOPSIS

use Pedigree::MarriageNode;

$node = new Pedigree::MarriageNode(I<%params>);

$FSpouse = $node->FSpouse();

$MSpouse = $node->MSpouse();

$consang = $self->isConsanguinic();

$area = $node->SetArea();

$node->CalcAbsCoord(I<$x>, I<$y>);

$node->DrawNode(I<$xidst>, I<$ydist>, I<$belowtextfont>, I<$abovetextfont>,
I<@fieldsfornode>);

$node->DrawConnections();


=head1 DESCRIPTION

This package contains data about a marriage.


=cut



####################################################################
# Define the package                                               #
####################################################################

package Pedigree::MarriageNode;
use Pedigree;
use strict;
our @ISA=('Pedigree::Node');


####################################################################
#    new                                                           #
####################################################################

=pod

=over 4

=item B<new>(I<%params>);

Construct a new node from the given parameters.

=cut

sub new {
    my ($class,%params)=@_;
    my $self=$class->SUPER::new(%params);
    if (!ref($self)) {  # Bad node
        return 0;
    }

    bless ($self,$class);
    
    #
    # Normally marriage nodes are not consanguinic
    #
    if (!exists($self->{'Consanguinic'})) {
	$self->{'Consanguinic'} = 0;
    }
    
    # After we constructed the node, we want to move the kids
    # from the parent nodes to the marriage node
    
    my $selfId = $self->{'Id'};
    if (exists($self->{'FSpouse'}) && exists($self->{'MSpouse'})) {
	my $fspouse = $self->{'FSpouse'};
	my $fspouseId = $fspouse->Id();
	my $mspouse = $self->{'MSpouse'};
	my $mspouseId = $mspouse->Id();

    
	foreach my $kidId (keys %{$self->{'kids_by_parent_id'}->{$fspouseId}}) {
	    if ($main::DEBUG) {
		print STDERR "Checking kid $kidId for $selfId\n";
	    }
	    if (exists ($self->{'kids_by_parent_id'}->{$mspouseId}->{$kidId})) {
		if ($main::DEBUG) {
		    print STDERR 
			"Moving $kidId from $fspouseId and $mspouseId to ".
			"$selfId\n";
		}
		delete $self->{'kids_by_parent_id'}->{$mspouseId}->{$kidId};
		delete $self->{'kids_by_parent_id'}->{$fspouseId}->{$kidId};
		$self->{'kids_by_parent_id'}->{$selfId}->{$kidId}=1;
	    }
	}
    }

    return $self;
}


####################################################################
#    FSpouse                                                       #
####################################################################

=pod

=item B<FSpouse>();

Get female spouse of a node.

=cut

sub FSpouse {
    my $self = shift;
    return $self->{'FSpouse'};
}


####################################################################
#    MSpouse                                                       #
####################################################################

=pod

=item B<MSpouse>();

Get female spouse of a node.

=cut

sub MSpouse {
    my $self = shift;
    return $self->{'MSpouse'};
}

####################################################################
#    isConsanguinic                                                #
####################################################################

=pod

=item B<isConsanguinic>();

Check whether the node is consanguinic

=cut

sub isConsanguinic {
    my $self = shift;
    return $self->{'Consanguinic'};
}


####################################################################
#    SetArea                                                       #
####################################################################

=pod

=item B<SetArea>();

Calculate relative coordinates for all nodes, that are descendants of
the given node I<and> the spouses that form the marriage.  We create a
Pedigree::Area(3) around the given node and recursively apply the
function to all descendants. The subroutine 
returns the reference to the created area.

=cut

sub SetArea {
    my $self = shift;
    my $area = $self->SUPER::SetArea();

    #
    # Female is to the right, male is to the left unless we have
    # Sort Order set for anybody.  If it is set, the order
    # is OPPOSITE to the SortOrder
    #
    my ($left,$right) = ($self->MSpouse(), $self->FSpouse());
    if ($left->SortOrder() <=> $right->SortOrder()) {
	($left,$right) = 
	    sort {$a->SortOrder() <=> $b->SortOrder()} 	($left,$right);
    }
    my ($rightRoot,$gen) = @{$right->FindRoot(0,-1)};
    $rightRoot->SetRelY($gen);
    my $rightArea=$rightRoot->SetArea();
    $area->AddRight($rightArea);

    my ($leftRoot,$gen) = @{$left->FindRoot(0,1)};
    $leftRoot->SetRelY($gen);
    my $leftArea=$leftRoot->SetArea();
    $area->AddLeft($leftArea);
    
    $self->{'Area'}=$area;

    if ($main::DEBUG) {
	print STDERR "Setting area for marriage node ",$self->Id(),"\n";
	for (my $y=$area->GetYmin(); $y<=$area->GetYmax(); $y++) {
	    print STDERR "\t$y: ", $area->GetXmin($y), ", ",
	    $area->GetXmax($y), "\n";
	}
    }


    return $area;
}


####################################################################
#    CalcAbsCoor                                                   #
####################################################################

=pod

=item B<CalcAbsCoor>(I<$x>, $<y>);

Set the absolute coordinates of the given node, if the absolute
coordinates of the parent node are (I<$x>, I<$y>), and recursively
do this for all descendants of this node, and right and left clumps.

=cut

sub CalcAbsCoor {
    my $self=shift;
    my ($x,$y) = @_;
    $self->SUPER::CalcAbsCoor($x, $y);
    $x += $self->GetRelX();
    $y += $self->GetRelY();

    my ($FRoot,undef) = @{$self->FSpouse()->FindRoot(0)};
    $FRoot->CalcAbsCoor($x, $y);

    my ($MRoot,undef) = @{$self->MSpouse()->FindRoot(0)};
    $MRoot->CalcAbsCoor($x, $y);

    return 0;
}


####################################################################
#    DrawNode                                                      #
####################################################################

=pod

=item B<DrawNode>(I<$xdist>, I<$ydist>, I<$belowtextfont>, I<$abovetextfont>,
I<@fieldsfornode>);

Output the command to draw this node.  The parameters are
distances between the nodes (in cm).

=cut

sub DrawNode {
    my $self=shift;
    my ($xdist, $ydist, $belowtextfont, $abovetextfont, @fieldsfornode) = @_;
    my $result = '\rput('.($xdist*$self->GetAbsX()).", ".
	($ydist*$self->GetAbsY()).'){\pnode{'.
	$self->Id()."}}\n";
    return $result;
}

####################################################################
#    DrawConnections                                               #
####################################################################

=pod

=item B<DrawConnections>();

Draw the connections from the given node to its descendants and
the spouses

=cut

sub DrawConnections {
    my $self = shift;
    my $xdist = shift;
    my $ydist = shift;
    my $result = $self->SUPER::DrawConnections($xdist, $ydist);
    my $Id=$self->Id();
    my $style="";
    if ($self->isConsanguinic()) {
	$style='doubleline=true, ';
    }
    foreach my $spouse ($self->FSpouse(), $self->MSpouse()) {
	if (!ref($spouse)) {
	    next;
	}
	my $sId=$spouse->Id();
	# Check whether spouse nodes are adjacent to the marriage node.
	# We do this check only for non-consanguinic unions
	if (!($self->isConsanguinic()) &&
	    (abs($self->GetIndexX() - $spouse->GetIndexX()) > 1)) {
	    my ($nodeA,$nodeB) = sort 
	    {$a->GetIndexX() <=> $b->GetIndexX()} ($spouse, $self);
	    my $IdA=$nodeA->Id();
	    my $IdB=$nodeB->Id();
	    $result .= 
		"\\ncloop[$style angleA=0, angleB=180, loopsize=".
		0.4*$ydist . ', arm=' . 0.4*$xdist . 
		']{'.$IdA.'}{'.$IdB.'}'."\n";
	} else {
	    $result .= "\\ncline[$style]{".$Id.'}{'.$sId.'}'."\n";
	}
    }
    return $result;
}




####################################################################
#    THE END                                                       #
####################################################################

=pod

=back

=head1 ENVIRONMENT

The calling program should define B<$main::DEBUG> and set it to 0
or 1.

=head1 SEE ALSO

pedigree(1), Pedigree(3)

=head1  AUTHOR

Boris Veytsman, Leila Akhmadeeva, 2006, 2007



=cut

1;