xref: /webtrees/app/Services/ChartService.php (revision 3e4bf26f9f16d92152484c56e3629888fb6019d5)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Services;
19
20use Fisharebest\Webtrees\Family;
21use Fisharebest\Webtrees\Individual;
22use Illuminate\Support\Collection;
23
24/**
25 * Find ancestors, descendants, cousins, etc for drawing charts.
26 */
27class ChartService
28{
29    /**
30     * Find the ancestors of an individual, indexed by their Sosa-Stradonitz number.
31     *
32     * @param Individual $individual  Start with this individual
33     * @param int        $generations Fetch this number of generations
34     *
35     * @return Collection
36     * @return Individual[]
37     */
38    public function sosaStradonitzAncestors(Individual $individual, int $generations): Collection
39    {
40        $ancestors = [1 => $individual];
41
42        $queue = [1];
43
44        $max = 2 ** ($generations - 1);
45
46        while (!empty($queue)) {
47            $sosa_stradonitz_number = array_shift($queue);
48
49            if ($sosa_stradonitz_number >= $max) {
50                break;
51            }
52
53            $family = $ancestors[$sosa_stradonitz_number]->primaryChildFamily();
54
55            if ($family instanceof Family) {
56                if ($family->husband() instanceof Individual) {
57                    $ancestors[$sosa_stradonitz_number * 2] = $family->husband();
58                    $queue[] = $sosa_stradonitz_number * 2;
59                }
60
61                if ($family->wife() instanceof Individual) {
62                    $ancestors[$sosa_stradonitz_number * 2 + 1] = $family->wife();
63                    $queue[] = $sosa_stradonitz_number * 2 + 1;
64                }
65            }
66        }
67
68        return new Collection($ancestors);
69    }
70
71    /**
72     * Find the descendants of an individual.
73     *
74     * @param Individual $individual  Start with this individual
75     * @param int        $generations Fetch this number of generations
76     *
77     * @return Collection
78     * @return Individual[]
79     */
80    public function descendants(Individual $individual, int $generations): Collection
81    {
82        $descendants = new Collection([$individual]);
83
84        if ($generations > 0) {
85            foreach ($individual->spouseFamilies() as $family) {
86                foreach ($family->children() as $child) {
87                    $descendants = $descendants->merge($this->descendants($child, $generations - 1));
88                }
89            }
90        }
91
92        return $descendants;
93    }
94
95    /**
96     * Find the descendants of an individual.
97     *
98     * @param Individual $individual  Start with this individual
99     * @param int        $generations Fetch this number of generations
100     *
101     * @return Collection
102     * @return Family[]
103     */
104    public function descendantFamilies(Individual $individual, int $generations): Collection
105    {
106        $descendants = new Collection($individual->spouseFamilies());
107
108        if ($generations > 0) {
109            foreach ($descendants as $family) {
110                foreach ($family->children() as $child) {
111                    $descendants = $descendants->merge($this->descendantFamilies($child, $generations - 1));
112                }
113            }
114        }
115
116        return $descendants;
117    }
118}
119